1998-10-14 14:22:38 +04:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-09-29 03:12:09 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-10-14 14:22:38 +04:00
|
|
|
*
|
1999-09-29 03:12:09 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-10-14 14:22:38 +04:00
|
|
|
*
|
1999-09-29 03:12:09 +04:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-10-14 14:22:38 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-09-29 03:12:09 +04:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the
|
|
|
|
* terms of the GNU Public License (the "GPL"), in which case the
|
|
|
|
* provisions of the GPL are applicable instead of those above.
|
|
|
|
* If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of the GPL and not to allow others to use your
|
|
|
|
* version of this file under the NPL, indicate your decision by
|
|
|
|
* deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this
|
|
|
|
* file under either the NPL or the GPL.
|
1998-10-14 14:22:38 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS standard exception implementation.
|
|
|
|
*/
|
|
|
|
|
1999-07-16 03:37:05 +04:00
|
|
|
#include <string.h>
|
1998-10-14 14:22:38 +04:00
|
|
|
#include "jsstddef.h"
|
|
|
|
#include "jstypes.h"
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsconfig.h"
|
|
|
|
#include "jsexn.h"
|
1998-11-05 03:08:43 +03:00
|
|
|
#include "jsfun.h"
|
1998-10-14 14:22:38 +04:00
|
|
|
|
|
|
|
#if JS_HAS_ERROR_EXCEPTIONS
|
|
|
|
#if !JS_HAS_EXCEPTIONS
|
|
|
|
# error "JS_HAS_EXCEPTIONS must be defined to use JS_HAS_ERROR_EXCEPTIONS"
|
|
|
|
#endif
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
/* XXX consider adding rt->atomState.messageAtom */
|
|
|
|
static char js_message_str[] = "message";
|
|
|
|
|
|
|
|
/* Forward declarations for ExceptionClass's initializer. */
|
1998-11-05 03:08:43 +03:00
|
|
|
static JSBool
|
|
|
|
Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
|
|
|
|
|
|
|
|
static void
|
|
|
|
exn_finalize(JSContext *cx, JSObject *obj);
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
static JSClass ExceptionClass = {
|
1998-11-05 03:08:43 +03:00
|
|
|
"Exception",
|
|
|
|
JSCLASS_HAS_PRIVATE,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, exn_finalize,
|
1999-11-12 00:52:35 +03:00
|
|
|
NULL, NULL, NULL, Exception,
|
2000-08-26 06:30:22 +04:00
|
|
|
NULL, NULL, NULL, 0
|
1998-11-05 03:08:43 +03:00
|
|
|
};
|
1998-10-14 14:22:38 +04:00
|
|
|
|
|
|
|
/*
|
1998-11-05 03:08:43 +03:00
|
|
|
* A copy of the JSErrorReport originally generated.
|
1998-10-14 14:22:38 +04:00
|
|
|
*/
|
|
|
|
typedef struct JSExnPrivate {
|
|
|
|
JSErrorReport *errorReport;
|
|
|
|
} JSExnPrivate;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy everything interesting about an error into allocated memory.
|
|
|
|
*/
|
|
|
|
static JSExnPrivate *
|
1998-11-05 03:08:43 +03:00
|
|
|
exn_initPrivate(JSContext *cx, JSErrorReport *report)
|
1998-10-14 14:22:38 +04:00
|
|
|
{
|
|
|
|
JSExnPrivate *newPrivate;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSErrorReport *newReport;
|
1998-10-14 14:22:38 +04:00
|
|
|
|
|
|
|
newPrivate = (JSExnPrivate *)JS_malloc(cx, sizeof (JSExnPrivate));
|
|
|
|
|
|
|
|
/* Copy the error report */
|
|
|
|
newReport = (JSErrorReport *)JS_malloc(cx, sizeof (JSErrorReport));
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
if (report->filename != NULL) {
|
1998-10-14 14:22:38 +04:00
|
|
|
newReport->filename =
|
|
|
|
(const char *)JS_malloc(cx, strlen(report->filename)+1);
|
|
|
|
strcpy((char *)newReport->filename, report->filename);
|
|
|
|
} else {
|
|
|
|
newReport->filename = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newReport->lineno = report->lineno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need to copy linebuf and tokenptr, because they
|
|
|
|
* point into the deflated string cache. (currently?)
|
|
|
|
*/
|
|
|
|
newReport->linebuf = report->linebuf;
|
|
|
|
newReport->tokenptr = report->tokenptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* But we do need to copy uclinebuf, uctokenptr, because they're
|
|
|
|
* pointers into internal tokenstream structs, and may go away.
|
|
|
|
*
|
|
|
|
* NOTE nothing uses this and I'm not really maintaining it until
|
|
|
|
* I know it's the desired API.
|
|
|
|
*/
|
1998-11-05 03:08:43 +03:00
|
|
|
if (report->uclinebuf != NULL) {
|
1999-04-27 19:18:57 +04:00
|
|
|
jsint len = js_strlen(report->uclinebuf) + 1;
|
1998-10-14 14:22:38 +04:00
|
|
|
newReport->uclinebuf =
|
1998-11-05 03:08:43 +03:00
|
|
|
(const jschar *)JS_malloc(cx, len * sizeof(jschar));
|
1999-04-27 19:18:57 +04:00
|
|
|
js_strncpy((jschar *)newReport->uclinebuf, report->uclinebuf, len);
|
1998-10-14 14:22:38 +04:00
|
|
|
newReport->uctokenptr = newReport->uclinebuf + (report->uctokenptr -
|
|
|
|
report->uclinebuf);
|
2000-04-05 10:39:11 +04:00
|
|
|
} else {
|
1998-10-14 14:22:38 +04:00
|
|
|
newReport->uclinebuf = newReport->uctokenptr = NULL;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
if (report->ucmessage != NULL) {
|
1999-04-27 19:18:57 +04:00
|
|
|
jsint len = js_strlen(report->ucmessage) + 1;
|
1999-07-16 03:37:05 +04:00
|
|
|
newReport->ucmessage = (const jschar *)JS_malloc(cx, len * sizeof(jschar));
|
1998-11-05 03:08:43 +03:00
|
|
|
js_strncpy((jschar *)newReport->ucmessage, report->ucmessage, len);
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
if (report->messageArgs) {
|
1999-04-27 19:18:57 +04:00
|
|
|
intN i;
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
for (i = 0; report->messageArgs[i] != NULL; i++)
|
|
|
|
;
|
|
|
|
JS_ASSERT(i);
|
|
|
|
newReport->messageArgs =
|
|
|
|
(const jschar **)JS_malloc(cx, (i + 1) * sizeof(jschar *));
|
|
|
|
for (i = 0; report->messageArgs[i] != NULL; i++) {
|
|
|
|
len = js_strlen(report->messageArgs[i]) + 1;
|
|
|
|
newReport->messageArgs[i] =
|
|
|
|
(const jschar *)JS_malloc(cx, len * sizeof(jschar));
|
|
|
|
js_strncpy((jschar *)(newReport->messageArgs[i]),
|
|
|
|
report->messageArgs[i], len);
|
|
|
|
}
|
1999-07-16 03:37:05 +04:00
|
|
|
newReport->messageArgs[i] = NULL;
|
1998-11-05 03:08:43 +03:00
|
|
|
} else {
|
1999-07-16 03:37:05 +04:00
|
|
|
newReport->messageArgs = NULL;
|
1998-11-05 03:08:43 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newReport->ucmessage = NULL;
|
|
|
|
newReport->messageArgs = NULL;
|
|
|
|
}
|
1999-04-27 19:18:57 +04:00
|
|
|
newReport->errorNumber = report->errorNumber;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
/* Note that this is before it gets flagged with JSREPORT_EXCEPTION */
|
|
|
|
newReport->flags = report->flags;
|
|
|
|
|
|
|
|
newPrivate->errorReport = newReport;
|
|
|
|
return newPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undo all the damage done by exn_initPrivate.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
exn_destroyPrivate(JSContext *cx, JSExnPrivate *privateData)
|
|
|
|
{
|
1998-11-05 03:08:43 +03:00
|
|
|
JSErrorReport *report;
|
|
|
|
const jschar **args;
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
report = privateData->errorReport;
|
|
|
|
JS_ASSERT(report);
|
|
|
|
if (report->uclinebuf)
|
|
|
|
JS_free(cx, (void *)report->uclinebuf);
|
|
|
|
if (report->filename)
|
|
|
|
JS_free(cx, (void *)report->filename);
|
|
|
|
if (report->ucmessage)
|
|
|
|
JS_free(cx, (void *)report->ucmessage);
|
|
|
|
if (report->messageArgs) {
|
|
|
|
args = report->messageArgs;
|
2000-04-05 10:39:11 +04:00
|
|
|
while (*args != NULL)
|
1999-04-27 19:18:57 +04:00
|
|
|
JS_free(cx, (void *)*args++);
|
1998-11-05 03:08:43 +03:00
|
|
|
JS_free(cx, (void *)report->messageArgs);
|
|
|
|
}
|
|
|
|
JS_free(cx, report);
|
1998-10-14 14:22:38 +04:00
|
|
|
JS_free(cx, privateData);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
exn_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSExnPrivate *privateData;
|
2000-02-02 04:10:31 +03:00
|
|
|
jsval privateValue;
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2000-02-02 04:10:31 +03:00
|
|
|
privateValue = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2000-02-02 04:10:31 +03:00
|
|
|
if (privateValue != JSVAL_NULL) {
|
|
|
|
privateData = (JSExnPrivate*) JSVAL_TO_PRIVATE(privateValue);
|
1998-11-05 03:08:43 +03:00
|
|
|
if (privateData)
|
|
|
|
exn_destroyPrivate(cx, privateData);
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-06 01:07:23 +04:00
|
|
|
JSErrorReport *
|
1998-11-05 03:08:43 +03:00
|
|
|
js_ErrorFromException(JSContext *cx, jsval exn)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
JSExnPrivate *privateData;
|
2000-02-02 04:10:31 +03:00
|
|
|
jsval privateValue;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
1999-12-23 00:58:53 +03:00
|
|
|
if (JSVAL_IS_PRIMITIVE(exn))
|
1998-11-05 03:08:43 +03:00
|
|
|
return NULL;
|
|
|
|
obj = JSVAL_TO_OBJECT(exn);
|
2000-04-05 10:39:11 +04:00
|
|
|
if (OBJ_GET_CLASS(cx, obj) != &ExceptionClass)
|
1998-11-05 03:08:43 +03:00
|
|
|
return NULL;
|
2000-02-02 04:10:31 +03:00
|
|
|
privateValue = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
|
|
|
|
if (privateValue == JSVAL_NULL)
|
1999-10-06 01:07:23 +04:00
|
|
|
return NULL;
|
2000-02-02 04:10:31 +03:00
|
|
|
privateData = (JSExnPrivate*) JSVAL_TO_PRIVATE(privateValue);
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!privateData)
|
|
|
|
return NULL;
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
JS_ASSERT(privateData->errorReport);
|
|
|
|
return privateData->errorReport;
|
|
|
|
}
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
/*
|
|
|
|
* This must be kept in synch with the exceptions array below.
|
|
|
|
* XXX use a jsexn.tbl file a la jsopcode.tbl
|
|
|
|
*/
|
1998-10-14 14:22:38 +04:00
|
|
|
typedef enum JSExnType {
|
|
|
|
JSEXN_NONE = -1,
|
|
|
|
JSEXN_ERR,
|
|
|
|
JSEXN_INTERNALERR,
|
1999-09-21 22:58:51 +04:00
|
|
|
JSEXN_EVALERR,
|
1999-09-16 11:12:00 +04:00
|
|
|
JSEXN_RANGEERR,
|
1999-09-21 22:58:51 +04:00
|
|
|
JSEXN_REFERENCEERR,
|
|
|
|
JSEXN_SYNTAXERR,
|
|
|
|
JSEXN_TYPEERR,
|
|
|
|
JSEXN_URIERR,
|
1998-10-14 14:22:38 +04:00
|
|
|
JSEXN_LIMIT
|
|
|
|
} JSExnType;
|
|
|
|
|
|
|
|
struct JSExnSpec {
|
|
|
|
int protoIndex;
|
1998-11-05 03:08:43 +03:00
|
|
|
const char *name;
|
2000-04-05 10:39:11 +04:00
|
|
|
JSNative native;
|
1998-10-14 14:22:38 +04:00
|
|
|
};
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
/*
|
|
|
|
* All *Error constructors share the same JSClass, ExceptionClass. But each
|
|
|
|
* constructor function for an *Error class must have a distinct native 'call'
|
|
|
|
* function pointer, in order for instanceof to work properly across multiple
|
|
|
|
* standard class sets. See jsfun.c:fun_hasInstance.
|
|
|
|
*/
|
|
|
|
#define MAKE_EXCEPTION_CTOR(name) \
|
2000-08-19 12:37:07 +04:00
|
|
|
const char js_##name##_str[] = #name; \
|
2000-04-05 10:39:11 +04:00
|
|
|
static JSBool \
|
|
|
|
name(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) \
|
|
|
|
{ \
|
|
|
|
return Exception(cx, obj, argc, argv, rval); \
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_EXCEPTION_CTOR(Error)
|
|
|
|
MAKE_EXCEPTION_CTOR(InternalError)
|
|
|
|
MAKE_EXCEPTION_CTOR(EvalError)
|
|
|
|
MAKE_EXCEPTION_CTOR(RangeError)
|
|
|
|
MAKE_EXCEPTION_CTOR(ReferenceError)
|
|
|
|
MAKE_EXCEPTION_CTOR(SyntaxError)
|
|
|
|
MAKE_EXCEPTION_CTOR(TypeError)
|
|
|
|
MAKE_EXCEPTION_CTOR(URIError)
|
|
|
|
|
|
|
|
#undef MAKE_EXCEPTION_CTOR
|
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
static struct JSExnSpec exceptions[] = {
|
2000-08-19 12:37:07 +04:00
|
|
|
{ JSEXN_NONE, js_Error_str, Error },
|
|
|
|
{ JSEXN_ERR, js_InternalError_str, InternalError },
|
|
|
|
{ JSEXN_ERR, js_EvalError_str, EvalError },
|
|
|
|
{ JSEXN_ERR, js_RangeError_str, RangeError },
|
|
|
|
{ JSEXN_ERR, js_ReferenceError_str, ReferenceError },
|
|
|
|
{ JSEXN_ERR, js_SyntaxError_str, SyntaxError },
|
|
|
|
{ JSEXN_ERR, js_TypeError_str, TypeError },
|
|
|
|
{ JSEXN_ERR, js_URIError_str, URIError },
|
2000-04-05 10:39:11 +04:00
|
|
|
{0,0,NULL}
|
1998-10-14 14:22:38 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
2000-04-05 10:39:11 +04:00
|
|
|
jsval pval;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSString *message;
|
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
if (!cx->fp->constructing) {
|
2000-04-05 10:39:11 +04:00
|
|
|
/*
|
|
|
|
* ECMA ed. 3, 15.11.1 requires Error, etc., to construct even when
|
|
|
|
* called as functions, without operator new. But as we do not give
|
|
|
|
* each constructor a distinct JSClass, whose .name member is used by
|
|
|
|
* js_NewObject to find the class prototype, we must get the class
|
|
|
|
* prototype ourselves.
|
|
|
|
*/
|
|
|
|
if (!OBJ_GET_PROPERTY(cx, JSVAL_TO_OBJECT(argv[-2]),
|
|
|
|
(jsid)cx->runtime->atomState.classPrototypeAtom,
|
|
|
|
&pval)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
obj = js_NewObject(cx, &ExceptionClass, JSVAL_TO_OBJECT(pval), NULL);
|
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/*
|
|
|
|
* If it's a new object of class Exception, then null out the private
|
|
|
|
* data so that the finalizer doesn't attempt to free it.
|
|
|
|
*/
|
2000-04-05 10:39:11 +04:00
|
|
|
if (OBJ_GET_CLASS(cx, obj) == &ExceptionClass)
|
1998-11-05 03:08:43 +03:00
|
|
|
OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, JSVAL_NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the 'message' property.
|
|
|
|
*/
|
|
|
|
if (argc > 0) {
|
2000-04-05 10:39:11 +04:00
|
|
|
message = js_ValueToString(cx, argv[0]);
|
|
|
|
if (!message)
|
2000-02-04 05:01:49 +03:00
|
|
|
return JS_FALSE;
|
2000-04-05 10:39:11 +04:00
|
|
|
} else {
|
|
|
|
message = cx->runtime->emptyString;
|
2000-02-04 05:01:49 +03:00
|
|
|
}
|
2000-04-05 10:39:11 +04:00
|
|
|
return JS_DefineProperty(cx, obj, js_message_str, STRING_TO_JSVAL(message),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE);
|
1999-09-21 22:58:51 +04:00
|
|
|
}
|
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
/*
|
1998-11-05 03:08:43 +03:00
|
|
|
* Convert to string.
|
1998-10-14 14:22:38 +04:00
|
|
|
*
|
2000-04-05 10:39:11 +04:00
|
|
|
* This method only uses JavaScript-modifiable properties name, message. It
|
|
|
|
* is left to the host to check for private data and report filename and line
|
1998-11-05 03:08:43 +03:00
|
|
|
* number information along with this message.
|
1998-10-14 14:22:38 +04:00
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
exn_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
|
|
|
jsval v;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSString *name, *message, *result;
|
|
|
|
jschar *chars, *cp;
|
|
|
|
size_t length;
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!OBJ_GET_PROPERTY(cx, obj, (jsid)cx->runtime->atomState.nameAtom, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
name = js_ValueToString(cx, v);
|
|
|
|
if (!name)
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, js_message_str, &v) ||
|
|
|
|
!(message = js_ValueToString(cx, v))) {
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
if (message->length > 0) {
|
|
|
|
length = name->length + message->length + 2;
|
2000-02-02 04:10:31 +03:00
|
|
|
cp = chars = (jschar*) JS_malloc(cx, (length + 1) * sizeof(jschar));
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!chars)
|
|
|
|
return JS_FALSE;
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
js_strncpy(cp, name->chars, name->length);
|
|
|
|
cp += name->length;
|
|
|
|
*cp++ = ':'; *cp++ = ' ';
|
|
|
|
js_strncpy(cp, message->chars, message->length);
|
|
|
|
cp += message->length;
|
|
|
|
*cp = 0;
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
result = js_NewString(cx, chars, length, 0);
|
|
|
|
if (!result) {
|
|
|
|
JS_free(cx, chars);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
1998-10-14 14:22:38 +04:00
|
|
|
} else {
|
1998-11-05 03:08:43 +03:00
|
|
|
result = name;
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
*rval = STRING_TO_JSVAL(result);
|
1998-10-14 14:22:38 +04:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
/*
|
|
|
|
* Return a string that may eval to something similar to the original object.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
exn_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
2000-04-05 10:39:11 +04:00
|
|
|
jsval v;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSString *name, *message, *result;
|
|
|
|
jschar *chars, *cp;
|
|
|
|
size_t length;
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!OBJ_GET_PROPERTY(cx, obj, (jsid)cx->runtime->atomState.nameAtom, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
name = js_ValueToString(cx, v);
|
|
|
|
if (!name)
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, js_message_str, &v) ||
|
|
|
|
!(message = js_ValueToString(cx, v))) {
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
length = (message->length > 0) ? name->length + message->length + 10
|
|
|
|
: name->length + 8;
|
|
|
|
|
2000-02-02 04:10:31 +03:00
|
|
|
cp = chars = (jschar*) JS_malloc(cx, (length + 1) * sizeof(jschar));
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!chars)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
*cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' ';
|
|
|
|
js_strncpy(cp, name->chars, name->length);
|
|
|
|
cp += name->length;
|
|
|
|
*cp++ = '(';
|
|
|
|
if (message->length > 0) {
|
|
|
|
*cp++ = '"';
|
|
|
|
js_strncpy(cp, message->chars, message->length);
|
|
|
|
cp += message->length;
|
|
|
|
*cp++ = '"';
|
|
|
|
}
|
|
|
|
*cp++ = ')'; *cp++ = ')'; *cp = 0;
|
|
|
|
|
|
|
|
result = js_NewString(cx, chars, length, 0);
|
|
|
|
if (!result) {
|
|
|
|
JS_free(cx, chars);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rval = STRING_TO_JSVAL(result);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
static JSFunctionSpec exception_methods[] = {
|
1998-11-05 03:08:43 +03:00
|
|
|
#if JS_HAS_TOSOURCE
|
1999-11-12 00:52:35 +03:00
|
|
|
{js_toSource_str, exn_toSource, 0,0,0},
|
1998-11-05 03:08:43 +03:00
|
|
|
#endif
|
1999-11-12 00:52:35 +03:00
|
|
|
{js_toString_str, exn_toString, 0,0,0},
|
|
|
|
{0,0,0,0,0}
|
1998-10-14 14:22:38 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitExceptionClasses(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
int i;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSObject *protos[JSEXN_LIMIT];
|
1998-10-14 14:22:38 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Initialize the prototypes first. */
|
|
|
|
for (i = 0; exceptions[i].name != 0; i++) {
|
|
|
|
JSAtom *atom;
|
|
|
|
JSFunction *fun;
|
|
|
|
JSString *nameString;
|
2000-04-05 10:39:11 +04:00
|
|
|
int protoIndex = exceptions[i].protoIndex;
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Make the prototype for the current constructor name. */
|
2000-04-05 10:39:11 +04:00
|
|
|
protos[i] = js_NewObject(cx, &ExceptionClass,
|
|
|
|
(protoIndex != JSEXN_NONE)
|
|
|
|
? protos[protoIndex]
|
|
|
|
: NULL,
|
1998-11-05 03:08:43 +03:00
|
|
|
obj);
|
|
|
|
if (!protos[i])
|
|
|
|
return NULL;
|
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
/* So exn_finalize knows whether to destroy private data. */
|
|
|
|
OBJ_SET_SLOT(cx, protos[i], JSSLOT_PRIVATE, JSVAL_NULL);
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
atom = js_Atomize(cx, exceptions[i].name, strlen(exceptions[i].name), 0);
|
|
|
|
if (!atom)
|
1999-09-21 22:58:51 +04:00
|
|
|
return NULL;
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Make a constructor function for the current name. */
|
2000-04-05 10:39:11 +04:00
|
|
|
fun = js_DefineFunction(cx, obj, atom, exceptions[i].native, 1, 0);
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!fun)
|
2000-04-05 10:39:11 +04:00
|
|
|
return NULL;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
/* Make this constructor make objects of class Exception. */
|
2000-04-05 10:39:11 +04:00
|
|
|
fun->clasp = &ExceptionClass;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
/* Make the prototype and constructor links. */
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!js_SetClassPrototype(cx, fun->object, protos[i],
|
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT)) {
|
1998-11-05 03:08:43 +03:00
|
|
|
return NULL;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
/* proto bootstrap bit from JS_InitClass omitted. */
|
|
|
|
nameString = JS_NewStringCopyZ(cx, exceptions[i].name);
|
|
|
|
if (nameString == NULL)
|
|
|
|
return NULL;
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Add the name property to the prototype. */
|
|
|
|
if (!JS_DefineProperty(cx, protos[i], js_name_str,
|
|
|
|
STRING_TO_JSVAL(nameString),
|
|
|
|
NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE)) {
|
|
|
|
return NULL;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
/*
|
1998-11-05 03:08:43 +03:00
|
|
|
* Add an empty message property. (To Exception.prototype only,
|
|
|
|
* because this property will be the same for all the exception
|
|
|
|
* protos.)
|
1998-10-14 14:22:38 +04:00
|
|
|
*/
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!JS_DefineProperty(cx, protos[0], js_message_str,
|
1998-11-05 03:08:43 +03:00
|
|
|
STRING_TO_JSVAL(cx->runtime->emptyString),
|
2000-04-05 10:39:11 +04:00
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
1998-11-05 03:08:43 +03:00
|
|
|
return NULL;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/*
|
|
|
|
* Add methods only to Exception.prototype, because ostensibly all
|
|
|
|
* exception types delegate to that.
|
|
|
|
*/
|
|
|
|
if (!JS_DefineFunctions(cx, protos[0], exception_methods))
|
|
|
|
return NULL;
|
1998-10-14 14:22:38 +04:00
|
|
|
|
|
|
|
return protos[0];
|
|
|
|
}
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
static JSExnType errorToExceptionNum[] = {
|
1998-10-14 14:22:38 +04:00
|
|
|
#define MSG_DEF(name, number, count, exception, format) \
|
|
|
|
exception,
|
|
|
|
#include "js.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined ( DEBUG_mccabe ) && defined ( PRINTNAMES )
|
|
|
|
/* For use below... get character strings for error name and exception name */
|
|
|
|
static struct exnname { char *name; char *exception; } errortoexnname[] = {
|
|
|
|
#define MSG_DEF(name, number, count, exception, format) \
|
|
|
|
{#name, #exception},
|
|
|
|
#include "js.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
};
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
JSBool
|
1998-11-05 03:08:43 +03:00
|
|
|
js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp)
|
1998-10-14 14:22:38 +04:00
|
|
|
{
|
|
|
|
JSErrNum errorNumber;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSObject *errObject, *errProto;
|
1998-10-14 14:22:38 +04:00
|
|
|
JSExnType exn;
|
|
|
|
JSExnPrivate *privateData;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSString *msgstr;
|
1998-10-14 14:22:38 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Find the exception index associated with this error. */
|
1998-10-14 14:22:38 +04:00
|
|
|
JS_ASSERT(reportp);
|
2000-01-18 14:06:05 +03:00
|
|
|
if (JSREPORT_IS_WARNING(reportp->flags))
|
|
|
|
return JS_FALSE;
|
2000-02-02 04:10:31 +03:00
|
|
|
errorNumber = (JSErrNum) reportp->errorNumber;
|
1998-11-05 03:08:43 +03:00
|
|
|
exn = errorToExceptionNum[errorNumber];
|
1998-10-14 14:22:38 +04:00
|
|
|
JS_ASSERT(exn < JSEXN_LIMIT);
|
|
|
|
|
|
|
|
#if defined( DEBUG_mccabe ) && defined ( PRINTNAMES )
|
|
|
|
/* Print the error name and the associated exception name to stderr */
|
|
|
|
fprintf(stderr, "%s\t%s\n",
|
|
|
|
errortoexnname[errorNumber].name,
|
|
|
|
errortoexnname[errorNumber].exception);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return false (no exception raised) if no exception is associated
|
|
|
|
* with the given error number.
|
|
|
|
*/
|
|
|
|
if (exn == JSEXN_NONE)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
1998-11-05 03:08:43 +03:00
|
|
|
* Try to get an appropriate prototype by looking up the corresponding
|
|
|
|
* exception constructor name in the current context. If the constructor
|
|
|
|
* has been deleted or overwritten, this may fail or return NULL, and
|
|
|
|
* js_NewObject will fall back to using Object.prototype.
|
1998-10-14 14:22:38 +04:00
|
|
|
*/
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!js_GetClassPrototype(cx, exceptions[exn].name, &errProto))
|
|
|
|
errProto = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use js_NewObject instead of js_ConstructObject, because
|
|
|
|
* js_ConstructObject seems to require a frame.
|
|
|
|
*/
|
2000-04-05 10:39:11 +04:00
|
|
|
errObject = js_NewObject(cx, &ExceptionClass, errProto, NULL);
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
/* Store 'message' as a javascript-visible value. */
|
|
|
|
msgstr = JS_NewStringCopyZ(cx, message);
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!JS_DefineProperty(cx, errObject, js_message_str,
|
|
|
|
STRING_TO_JSVAL(msgstr), NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE)) {
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
1998-10-14 14:22:38 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a new copy of the error report, and store it in the
|
|
|
|
* exception objects' private data. We can't use the error report
|
|
|
|
* handed in, because it's stack-allocated, and may point to transient
|
|
|
|
* data in the JSTokenStream.
|
|
|
|
*/
|
1998-11-05 03:08:43 +03:00
|
|
|
privateData = exn_initPrivate(cx, reportp);
|
|
|
|
OBJ_SET_SLOT(cx, errObject, JSSLOT_PRIVATE, PRIVATE_TO_JSVAL(privateData));
|
1998-10-14 14:22:38 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Set the generated Exception object as the current exception. */
|
|
|
|
JS_SetPendingException(cx, OBJECT_TO_JSVAL(errObject));
|
1998-10-14 14:22:38 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Flag the error report passed in to indicate an exception was raised. */
|
1998-10-14 14:22:38 +04:00
|
|
|
reportp->flags |= JSREPORT_EXCEPTION;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif /* JS_HAS_ERROR_EXCEPTIONS */
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
#if JS_HAS_EXCEPTIONS
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
js_ReportUncaughtException(JSContext *cx)
|
|
|
|
{
|
|
|
|
JSObject *exnObject;
|
|
|
|
JSString *str;
|
|
|
|
jsval exn;
|
|
|
|
JSErrorReport *reportp;
|
1999-10-07 01:31:34 +04:00
|
|
|
const char *bytes;
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!JS_IsExceptionPending(cx))
|
|
|
|
return JS_FALSE;
|
2000-04-05 10:39:11 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!JS_GetPendingException(cx, &exn))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because js_ValueToString below could error and an exception object
|
|
|
|
* could become unrooted, we root it here.
|
|
|
|
*/
|
1999-04-27 19:18:57 +04:00
|
|
|
if (JSVAL_IS_OBJECT(exn) && exn != JSVAL_NULL) {
|
1998-11-05 03:08:43 +03:00
|
|
|
exnObject = JSVAL_TO_OBJECT(exn);
|
1999-04-27 19:18:57 +04:00
|
|
|
if (!js_AddRoot(cx, &exnObject, "exn.report.root"))
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
exnObject = NULL;
|
|
|
|
}
|
|
|
|
str = js_ValueToString(cx, exn);
|
|
|
|
|
|
|
|
#if JS_HAS_ERROR_EXCEPTIONS
|
|
|
|
reportp = js_ErrorFromException(cx, exn);
|
|
|
|
#else
|
|
|
|
reportp = NULL;
|
|
|
|
#endif
|
|
|
|
|
1999-10-07 01:31:34 +04:00
|
|
|
if (str != NULL) {
|
|
|
|
bytes = js_GetStringBytes(str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bytes = "null";
|
|
|
|
}
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
if (reportp == NULL) {
|
|
|
|
/*
|
|
|
|
* XXXmccabe todo: Instead of doing this, synthesize an error report
|
|
|
|
* struct that includes the filename, lineno where the exception was
|
|
|
|
* originally thrown.
|
|
|
|
*/
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
1999-10-07 01:31:34 +04:00
|
|
|
JSMSG_UNCAUGHT_EXCEPTION, bytes);
|
1998-11-05 03:08:43 +03:00
|
|
|
} else {
|
|
|
|
/* Flag the error as an exception. */
|
|
|
|
reportp->flags |= JSREPORT_EXCEPTION;
|
1999-10-07 01:31:34 +04:00
|
|
|
js_ReportErrorAgain(cx, bytes, reportp);
|
1998-11-05 03:08:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (exnObject != NULL)
|
1999-06-23 18:18:56 +04:00
|
|
|
js_RemoveRoot(cx->runtime, &exnObject);
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* JS_HAS_EXCEPTIONS */
|