2001-09-20 04:02:59 +04:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2006-06-22 03:54:04 +04:00
|
|
|
* vim: set ts=8 sw=4 et tw=78:
|
1998-10-14 14:22:38 +04:00
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-10-14 14:22:38 +04:00
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-10-14 14:22:38 +04:00
|
|
|
*
|
2001-09-20 04:02:59 +04:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
1999-09-29 03:12:09 +04:00
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-09-29 03:12:09 +04:00
|
|
|
*
|
2001-09-03 23:36:24 +04:00
|
|
|
* Contributor(s):
|
1999-09-29 03:12:09 +04:00
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-10-14 14:22:38 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JS standard exception implementation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "jsstddef.h"
|
2002-03-03 03:59:16 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-10-14 14:22:38 +04:00
|
|
|
#include "jstypes.h"
|
2002-03-03 03:59:16 +03:00
|
|
|
#include "jsbit.h"
|
1998-10-14 14:22:38 +04:00
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsconfig.h"
|
2004-12-09 05:19:53 +03:00
|
|
|
#include "jsdbgapi.h"
|
1998-10-14 14:22:38 +04:00
|
|
|
#include "jsexn.h"
|
1998-11-05 03:08:43 +03:00
|
|
|
#include "jsfun.h"
|
2002-02-09 09:28:58 +03:00
|
|
|
#include "jsinterp.h"
|
2000-09-09 04:41:15 +04:00
|
|
|
#include "jsnum.h"
|
2004-12-09 05:19:53 +03:00
|
|
|
#include "jsopcode.h"
|
2002-02-09 09:28:58 +03:00
|
|
|
#include "jsscript.h"
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2006-05-23 03:36:32 +04:00
|
|
|
/* Forward declarations for js_ErrorClass'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);
|
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
static uint32
|
|
|
|
exn_mark(JSContext *cx, JSObject *obj, void *arg);
|
|
|
|
|
|
|
|
static void
|
|
|
|
exn_finalize(JSContext *cx, JSObject *obj);
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
exn_enumerate(JSContext *cx, JSObject *obj);
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
exn_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
|
|
|
|
JSObject **objp);
|
|
|
|
|
2006-05-23 03:36:32 +04:00
|
|
|
JSClass js_ErrorClass = {
|
2006-04-19 04:22:43 +04:00
|
|
|
js_Error_str,
|
2006-09-03 16:34:18 +04:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
|
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Error),
|
1998-11-05 03:08:43 +03:00
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
2006-09-03 16:34:18 +04:00
|
|
|
exn_enumerate, (JSResolveOp)exn_resolve, JS_ConvertStub, exn_finalize,
|
1999-11-12 00:52:35 +03:00
|
|
|
NULL, NULL, NULL, Exception,
|
2006-09-03 16:34:18 +04:00
|
|
|
NULL, NULL, exn_mark, NULL
|
1998-11-05 03:08:43 +03:00
|
|
|
};
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
typedef struct JSStackTraceElem {
|
|
|
|
JSString *funName;
|
|
|
|
size_t argc;
|
|
|
|
const char *filename;
|
|
|
|
uintN ulineno;
|
|
|
|
} JSStackTraceElem;
|
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
typedef struct JSExnPrivate {
|
2006-09-03 16:34:18 +04:00
|
|
|
/* A copy of the JSErrorReport originally generated. */
|
|
|
|
JSErrorReport *errorReport;
|
|
|
|
JSString *message;
|
|
|
|
JSString *filename;
|
|
|
|
uintN lineno;
|
|
|
|
size_t stackDepth;
|
|
|
|
JSStackTraceElem stackElems[1];
|
1998-10-14 14:22:38 +04:00
|
|
|
} JSExnPrivate;
|
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
static JSString *
|
|
|
|
StackTraceToString(JSContext *cx, JSExnPrivate *priv);
|
|
|
|
|
2006-08-26 11:44:38 +04:00
|
|
|
static JSErrorReport *
|
|
|
|
CopyErrorReport(JSContext *cx, JSErrorReport *report)
|
2000-10-14 02:11:52 +04:00
|
|
|
{
|
2006-08-26 11:44:38 +04:00
|
|
|
/*
|
|
|
|
* We use a single malloc block to make a deep copy of JSErrorReport with
|
|
|
|
* the following layout:
|
|
|
|
* JSErrorReport
|
|
|
|
* array of copies of report->messageArgs
|
|
|
|
* jschar array with characters for all messageArgs
|
|
|
|
* jschar array with characters for ucmessage
|
|
|
|
* jschar array with characters for uclinebuf and uctokenptr
|
|
|
|
* char array with characters for linebuf and tokenptr
|
|
|
|
* char array with characters for filename
|
|
|
|
* Such layout together with the properties enforced by the following
|
|
|
|
* asserts does not need any extra alignment padding.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSErrorReport) % sizeof(const char *) == 0);
|
|
|
|
JS_STATIC_ASSERT(sizeof(const char *) % sizeof(jschar) == 0);
|
|
|
|
|
|
|
|
size_t filenameSize;
|
|
|
|
size_t linebufSize;
|
|
|
|
size_t uclinebufSize;
|
|
|
|
size_t ucmessageSize;
|
|
|
|
size_t i, argsArraySize, argsCopySize, argSize;
|
|
|
|
size_t mallocSize;
|
|
|
|
JSErrorReport *copy;
|
|
|
|
uint8 *cursor;
|
|
|
|
|
|
|
|
#define JS_CHARS_SIZE(jschars) ((js_strlen(jschars) + 1) * sizeof(jschar))
|
|
|
|
|
2006-08-29 20:15:42 +04:00
|
|
|
filenameSize = report->filename ? strlen(report->filename) + 1 : 0;
|
2006-08-26 11:44:38 +04:00
|
|
|
linebufSize = report->linebuf ? strlen(report->linebuf) + 1 : 0;
|
|
|
|
uclinebufSize = report->uclinebuf ? JS_CHARS_SIZE(report->uclinebuf) : 0;
|
|
|
|
ucmessageSize = 0;
|
|
|
|
argsArraySize = 0;
|
|
|
|
argsCopySize = 0;
|
|
|
|
if (report->ucmessage) {
|
|
|
|
ucmessageSize = JS_CHARS_SIZE(report->ucmessage);
|
2000-10-14 02:11:52 +04:00
|
|
|
if (report->messageArgs) {
|
2006-08-26 11:44:38 +04:00
|
|
|
for (i = 0; report->messageArgs[i]; ++i)
|
|
|
|
argsCopySize += JS_CHARS_SIZE(report->messageArgs[i]);
|
|
|
|
|
|
|
|
/* Non-null messageArgs should have at least one non-null arg. */
|
|
|
|
JS_ASSERT(i != 0);
|
|
|
|
argsArraySize = (i + 1) * sizeof(const jschar *);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The mallocSize can not overflow since it represents the sum of the
|
|
|
|
* sizes of already allocated objects.
|
|
|
|
*/
|
|
|
|
mallocSize = sizeof(JSErrorReport) + argsArraySize + argsCopySize +
|
|
|
|
ucmessageSize + uclinebufSize + linebufSize + filenameSize;
|
|
|
|
cursor = (uint8 *)JS_malloc(cx, mallocSize);
|
|
|
|
if (!cursor)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
copy = (JSErrorReport *)cursor;
|
|
|
|
memset(cursor, 0, sizeof(JSErrorReport));
|
|
|
|
cursor += sizeof(JSErrorReport);
|
|
|
|
|
|
|
|
if (argsArraySize != 0) {
|
|
|
|
copy->messageArgs = (const jschar **)cursor;
|
|
|
|
cursor += argsArraySize;
|
|
|
|
for (i = 0; report->messageArgs[i]; ++i) {
|
|
|
|
copy->messageArgs[i] = (const jschar *)cursor;
|
|
|
|
argSize = JS_CHARS_SIZE(report->messageArgs[i]);
|
|
|
|
memcpy(cursor, report->messageArgs[i], argSize);
|
|
|
|
cursor += argSize;
|
|
|
|
}
|
|
|
|
copy->messageArgs[i] = NULL;
|
2006-08-28 09:57:16 +04:00
|
|
|
JS_ASSERT(cursor == (uint8 *)copy->messageArgs[0] + argsCopySize);
|
2006-08-26 11:44:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (report->ucmessage) {
|
|
|
|
copy->ucmessage = (const jschar *)cursor;
|
|
|
|
memcpy(cursor, report->ucmessage, ucmessageSize);
|
|
|
|
cursor += ucmessageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (report->uclinebuf) {
|
|
|
|
copy->uclinebuf = (const jschar *)cursor;
|
|
|
|
memcpy(cursor, report->uclinebuf, uclinebufSize);
|
|
|
|
cursor += uclinebufSize;
|
|
|
|
if (report->uctokenptr) {
|
|
|
|
copy->uctokenptr = copy->uclinebuf + (report->uctokenptr -
|
|
|
|
report->uclinebuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (report->linebuf) {
|
|
|
|
copy->linebuf = (const char *)cursor;
|
|
|
|
memcpy(cursor, report->linebuf, linebufSize);
|
|
|
|
cursor += linebufSize;
|
|
|
|
if (report->tokenptr) {
|
|
|
|
copy->tokenptr = copy->linebuf + (report->tokenptr -
|
|
|
|
report->linebuf);
|
2000-10-14 02:11:52 +04:00
|
|
|
}
|
|
|
|
}
|
2006-08-26 11:44:38 +04:00
|
|
|
|
2006-08-29 20:15:42 +04:00
|
|
|
if (report->filename) {
|
|
|
|
copy->filename = (const char *)cursor;
|
|
|
|
memcpy(cursor, report->filename, filenameSize);
|
|
|
|
}
|
2006-08-26 11:44:38 +04:00
|
|
|
JS_ASSERT(cursor + filenameSize == (uint8 *)copy + mallocSize);
|
|
|
|
|
|
|
|
/* Copy non-pointer members. */
|
|
|
|
copy->lineno = report->lineno;
|
|
|
|
copy->errorNumber = report->errorNumber;
|
|
|
|
|
|
|
|
/* Note that this is before it gets flagged with JSREPORT_EXCEPTION */
|
|
|
|
copy->flags = report->flags;
|
|
|
|
|
|
|
|
#undef JS_CHARS_SIZE
|
|
|
|
return copy;
|
2000-10-14 02:11:52 +04:00
|
|
|
}
|
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
static jsval *
|
|
|
|
GetStackTraceValueBuffer(JSExnPrivate *priv)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We use extra memory after JSExnPrivateInfo.stackElems to store jsvals
|
|
|
|
* that helps to produce more informative stack traces. The following
|
|
|
|
* assert allows us to assume that no gap after stackElems is necessary to
|
|
|
|
* align the buffer properly.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSStackTraceElem) % sizeof(jsval) == 0);
|
|
|
|
|
|
|
|
return (jsval *)(priv->stackElems + priv->stackDepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
InitExnPrivate(JSContext *cx, JSObject *exnObject, JSString *message,
|
|
|
|
JSString *filename, uintN lineno, JSErrorReport *report)
|
|
|
|
{
|
|
|
|
JSCheckAccessOp checkAccess;
|
|
|
|
JSErrorReporter older;
|
|
|
|
JSExceptionState *state;
|
|
|
|
jsval callerid, v;
|
|
|
|
JSStackFrame *fp, *fpstop;
|
|
|
|
size_t stackDepth, valueCount, size;
|
|
|
|
JSBool overflow;
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
JSStackTraceElem *elem;
|
|
|
|
jsval *values;
|
|
|
|
|
|
|
|
JS_ASSERT(OBJ_GET_CLASS(cx, exnObject) == &js_ErrorClass);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare stack trace data.
|
|
|
|
*
|
|
|
|
* Set aside any error reporter for cx and save its exception state
|
|
|
|
* so we can suppress any checkAccess failures. Such failures should stop
|
|
|
|
* the backtrace procedure, not result in a failure of this constructor.
|
|
|
|
*/
|
|
|
|
checkAccess = cx->runtime->checkObjectAccess;
|
|
|
|
older = JS_SetErrorReporter(cx, NULL);
|
|
|
|
state = JS_SaveExceptionState(cx);
|
|
|
|
|
|
|
|
callerid = ATOM_KEY(cx->runtime->atomState.callerAtom);
|
|
|
|
stackDepth = 0;
|
|
|
|
valueCount = 0;
|
|
|
|
for (fp = cx->fp; fp; fp = fp->down) {
|
|
|
|
if (fp->fun && fp->argv) {
|
|
|
|
if (checkAccess) {
|
|
|
|
v = fp->argv[-2];
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(v) &&
|
|
|
|
!checkAccess(cx, JSVAL_TO_OBJECT(v), callerid,
|
|
|
|
JSACC_READ, &v /* ignored */)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valueCount += fp->argc;
|
|
|
|
}
|
|
|
|
++stackDepth;
|
|
|
|
}
|
|
|
|
JS_RestoreExceptionState(cx, state);
|
|
|
|
JS_SetErrorReporter(cx, older);
|
|
|
|
fpstop = fp;
|
|
|
|
|
|
|
|
size = offsetof(JSExnPrivate, stackElems);
|
|
|
|
overflow = (stackDepth > ((size_t)-1 - size) / sizeof(JSStackTraceElem));
|
|
|
|
size += stackDepth * sizeof(JSStackTraceElem);
|
|
|
|
overflow |= (valueCount > ((size_t)-1 - size) / sizeof(jsval));
|
|
|
|
size += valueCount * sizeof(jsval);
|
|
|
|
if (overflow) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
priv = (JSExnPrivate *)JS_malloc(cx, size);
|
|
|
|
if (!priv)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We initialize errorReport with a copy of report after setting the
|
|
|
|
* private slot, to prevent GC accessing a junk value we clear the field
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
priv->errorReport = NULL;
|
|
|
|
priv->message = message;
|
|
|
|
priv->filename = filename;
|
|
|
|
priv->lineno = lineno;
|
|
|
|
priv->stackDepth = stackDepth;
|
|
|
|
|
|
|
|
values = GetStackTraceValueBuffer(priv);
|
|
|
|
elem = priv->stackElems;
|
|
|
|
for (fp = cx->fp; fp != fpstop; fp = fp->down) {
|
|
|
|
if (!fp->fun) {
|
|
|
|
elem->funName = NULL;
|
|
|
|
elem->argc = 0;
|
|
|
|
} else {
|
|
|
|
elem->funName = fp->fun->atom
|
|
|
|
? ATOM_TO_STRING(fp->fun->atom)
|
|
|
|
: cx->runtime->emptyString;
|
|
|
|
elem->argc = fp->argc;
|
|
|
|
memcpy(values, fp->argv, fp->argc * sizeof(jsval));
|
|
|
|
values += fp->argc;
|
|
|
|
}
|
|
|
|
elem->ulineno = 0;
|
|
|
|
elem->filename = NULL;
|
|
|
|
if (fp->script) {
|
|
|
|
elem->filename = fp->script->filename;
|
|
|
|
if (fp->pc)
|
|
|
|
elem->ulineno = js_PCToLineNumber(cx, fp->script, fp->pc);
|
|
|
|
}
|
|
|
|
++elem;
|
|
|
|
}
|
|
|
|
JS_ASSERT(priv->stackElems + stackDepth == elem);
|
|
|
|
JS_ASSERT(GetStackTraceValueBuffer(priv) + valueCount == values);
|
|
|
|
|
|
|
|
OBJ_SET_SLOT(cx, exnObject, JSSLOT_PRIVATE, PRIVATE_TO_JSVAL(priv));
|
|
|
|
|
|
|
|
if (report) {
|
|
|
|
/*
|
|
|
|
* Construct a new copy of the error report struct. We can't use the
|
|
|
|
* error report struct that was passed in, because it's allocated on
|
|
|
|
* the stack, and also because it may point to transient data in the
|
|
|
|
* JSTokenStream.
|
|
|
|
*/
|
|
|
|
priv->errorReport = CopyErrorReport(cx, report);
|
|
|
|
if (!priv->errorReport) {
|
|
|
|
/* The finalizer realeases priv since it is in the private slot. */
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
static JSExnPrivate *
|
2006-09-03 16:34:18 +04:00
|
|
|
GetExnPrivate(JSContext *cx, JSObject *obj)
|
1998-10-14 14:22:38 +04:00
|
|
|
{
|
2006-09-03 16:34:18 +04:00
|
|
|
jsval privateValue;
|
|
|
|
JSExnPrivate *priv;
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
JS_ASSERT(OBJ_GET_CLASS(cx, obj) == &js_ErrorClass);
|
|
|
|
privateValue = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
|
|
|
|
if (JSVAL_IS_VOID(privateValue))
|
2006-08-26 11:44:38 +04:00
|
|
|
return NULL;
|
2006-09-03 16:34:18 +04:00
|
|
|
priv = (JSExnPrivate *)JSVAL_TO_PRIVATE(privateValue);
|
|
|
|
JS_ASSERT(priv);
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
exn_mark(JSContext *cx, JSObject *obj, void *arg)
|
|
|
|
{
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
JSStackTraceElem *elem;
|
|
|
|
size_t vcount, i;
|
|
|
|
jsval *vp, v;
|
|
|
|
|
|
|
|
priv = GetExnPrivate(cx, obj);
|
|
|
|
if (priv) {
|
|
|
|
GC_MARK(cx, priv->message, "exception message");
|
|
|
|
GC_MARK(cx, priv->filename, "exception filename");
|
|
|
|
elem = priv->stackElems;
|
|
|
|
for (vcount = i = 0; i != priv->stackDepth; ++i, ++elem) {
|
|
|
|
if (elem->funName)
|
|
|
|
GC_MARK(cx, elem->funName, "stack trace function name");
|
|
|
|
if (elem->filename)
|
|
|
|
js_MarkScriptFilename(elem->filename);
|
|
|
|
vcount += elem->argc;
|
|
|
|
}
|
|
|
|
vp = GetStackTraceValueBuffer(priv);
|
|
|
|
for (i = 0; i != vcount; ++i, ++vp) {
|
|
|
|
v = *vp;
|
|
|
|
if (JSVAL_IS_GCTHING(v))
|
|
|
|
GC_MARK(cx, JSVAL_TO_GCTHING(v), "stack trace argument");
|
|
|
|
}
|
1998-11-05 03:08:43 +03:00
|
|
|
}
|
2006-09-03 16:34:18 +04:00
|
|
|
return 0;
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
exn_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2006-09-03 16:34:18 +04:00
|
|
|
JSExnPrivate *priv;
|
2006-09-02 23:52:11 +04:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
priv = GetExnPrivate(cx, obj);
|
|
|
|
if (priv) {
|
|
|
|
if (priv->errorReport)
|
|
|
|
JS_free(cx, priv->errorReport);
|
|
|
|
JS_free(cx, priv);
|
|
|
|
}
|
|
|
|
}
|
2006-09-02 23:52:11 +04:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
static JSBool
|
|
|
|
exn_enumerate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSAtomState *atomState;
|
|
|
|
uintN i;
|
|
|
|
JSAtom *atom;
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
|
|
|
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSAtomState) <= (size_t)(uint16)-1);
|
|
|
|
static const uint16 offsets[] = {
|
|
|
|
(uint16)offsetof(JSAtomState, messageAtom),
|
|
|
|
(uint16)offsetof(JSAtomState, fileNameAtom),
|
|
|
|
(uint16)offsetof(JSAtomState, lineNumberAtom),
|
|
|
|
(uint16)offsetof(JSAtomState, stackAtom),
|
|
|
|
};
|
|
|
|
|
|
|
|
atomState = &cx->runtime->atomState;
|
|
|
|
for (i = 0; i != JS_ARRAY_LENGTH(offsets); ++i) {
|
|
|
|
atom = *(JSAtom **)((uint8 *)atomState + offsets[i]);
|
|
|
|
if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (prop)
|
|
|
|
OBJ_DROP_PROPERTY(cx, pobj, prop);
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
exn_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
|
|
|
|
JSObject **objp)
|
|
|
|
{
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
JSString *str;
|
|
|
|
JSAtom *atom;
|
|
|
|
JSString *stack;
|
|
|
|
const char *prop;
|
|
|
|
jsval v;
|
|
|
|
|
|
|
|
*objp = NULL;
|
|
|
|
priv = GetExnPrivate(cx, obj);
|
|
|
|
if (priv && JSVAL_IS_STRING(id)) {
|
|
|
|
str = JSVAL_TO_STRING(id);
|
|
|
|
|
|
|
|
atom = cx->runtime->atomState.messageAtom;
|
|
|
|
if (str == ATOM_TO_STRING(atom)) {
|
|
|
|
prop = js_message_str;
|
|
|
|
v = STRING_TO_JSVAL(priv->message);
|
|
|
|
goto define;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom = cx->runtime->atomState.fileNameAtom;
|
|
|
|
if (str == ATOM_TO_STRING(atom)) {
|
|
|
|
prop = js_fileName_str;
|
|
|
|
v = STRING_TO_JSVAL(priv->filename);
|
|
|
|
goto define;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom = cx->runtime->atomState.lineNumberAtom;
|
|
|
|
if (str == ATOM_TO_STRING(atom)) {
|
|
|
|
prop = js_lineNumber_str;
|
|
|
|
v = INT_TO_JSVAL(priv->lineno);
|
|
|
|
goto define;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom = cx->runtime->atomState.stackAtom;
|
|
|
|
if (str == ATOM_TO_STRING(atom)) {
|
|
|
|
stack = StackTraceToString(cx, priv);
|
|
|
|
if (!stack)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* Allow to GC all things that were used to build stack trace. */
|
|
|
|
priv->stackDepth = 0;
|
|
|
|
prop = js_stack_str;
|
|
|
|
v = STRING_TO_JSVAL(stack);
|
|
|
|
goto define;
|
2006-08-29 20:15:42 +04:00
|
|
|
}
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
2006-09-03 16:34:18 +04:00
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
define:
|
|
|
|
if (!JS_DefineProperty(cx, obj, prop, v, NULL, NULL, JSPROP_ENUMERATE))
|
|
|
|
return JS_FALSE;
|
|
|
|
*objp = obj;
|
|
|
|
return JS_TRUE;
|
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;
|
2006-09-03 16:34:18 +04:00
|
|
|
JSExnPrivate *priv;
|
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);
|
2006-05-23 03:36:32 +04:00
|
|
|
if (OBJ_GET_CLASS(cx, obj) != &js_ErrorClass)
|
1998-11-05 03:08:43 +03:00
|
|
|
return NULL;
|
2006-09-03 16:34:18 +04:00
|
|
|
priv = GetExnPrivate(cx, obj);
|
|
|
|
if (!priv)
|
1998-11-05 03:08:43 +03:00
|
|
|
return NULL;
|
2006-09-03 16:34:18 +04:00
|
|
|
return priv->errorReport;
|
1998-11-05 03:08:43 +03:00
|
|
|
}
|
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
struct JSExnSpec {
|
|
|
|
int protoIndex;
|
1998-11-05 03:08:43 +03:00
|
|
|
const char *name;
|
2006-04-19 04:22:43 +04:00
|
|
|
JSProtoKey key;
|
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
|
|
|
/*
|
2006-05-23 03:36:32 +04:00
|
|
|
* All *Error constructors share the same JSClass, js_ErrorClass. But each
|
2000-04-05 10:39:11 +04:00
|
|
|
* 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) \
|
|
|
|
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[] = {
|
2006-04-19 04:22:43 +04:00
|
|
|
{JSEXN_NONE, js_Error_str, JSProto_Error, Error},
|
|
|
|
{JSEXN_ERR, js_InternalError_str, JSProto_InternalError, InternalError},
|
|
|
|
{JSEXN_ERR, js_EvalError_str, JSProto_EvalError, EvalError},
|
|
|
|
{JSEXN_ERR, js_RangeError_str, JSProto_RangeError, RangeError},
|
|
|
|
{JSEXN_ERR, js_ReferenceError_str, JSProto_ReferenceError, ReferenceError},
|
|
|
|
{JSEXN_ERR, js_SyntaxError_str, JSProto_SyntaxError, SyntaxError},
|
|
|
|
{JSEXN_ERR, js_TypeError_str, JSProto_TypeError, TypeError},
|
|
|
|
{JSEXN_ERR, js_URIError_str, JSProto_URIError, URIError},
|
|
|
|
{0, NULL, JSProto_Null, NULL}
|
1998-10-14 14:22:38 +04:00
|
|
|
};
|
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
static JSString *
|
|
|
|
ValueToShortSource(JSContext *cx, jsval v)
|
|
|
|
{
|
|
|
|
JSString *str;
|
|
|
|
|
|
|
|
/* Avoid toSource bloat and fallibility for object types. */
|
|
|
|
if (JSVAL_IS_PRIMITIVE(v)) {
|
|
|
|
str = js_ValueToSource(cx, v);
|
|
|
|
} else if (VALUE_IS_FUNCTION(cx, v)) {
|
|
|
|
/*
|
|
|
|
* XXX Avoid function decompilation bloat for now.
|
|
|
|
*/
|
|
|
|
str = JS_GetFunctionId(JS_ValueToFunction(cx, v));
|
|
|
|
if (!str && !(str = js_ValueToSource(cx, v))) {
|
|
|
|
/*
|
|
|
|
* Continue to soldier on if the function couldn't be
|
|
|
|
* converted into a string.
|
|
|
|
*/
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
str = JS_NewStringCopyZ(cx, "[unknown function]");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* XXX Avoid toString on objects, it takes too long and uses too much
|
|
|
|
* memory, for too many classes (see Mozilla bug 166743).
|
|
|
|
*/
|
|
|
|
char buf[100];
|
|
|
|
JS_snprintf(buf, sizeof buf, "[object %s]",
|
|
|
|
OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(v))->name);
|
|
|
|
str = JS_NewStringCopyZ(cx, buf);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSString *
|
|
|
|
StackTraceToString(JSContext *cx, JSExnPrivate *priv)
|
1998-10-14 14:22:38 +04:00
|
|
|
{
|
2002-03-03 03:59:16 +03:00
|
|
|
jschar *stackbuf;
|
|
|
|
size_t stacklen, stackmax;
|
2006-09-03 16:34:18 +04:00
|
|
|
JSStackTraceElem *elem, *endElem;
|
|
|
|
jsval *values;
|
|
|
|
size_t i;
|
|
|
|
JSString *str;
|
2002-03-03 03:59:16 +03:00
|
|
|
const char *cp;
|
|
|
|
char ulnbuf[11];
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
/* After this point, failing control flow must goto bad. */
|
2002-03-03 03:59:16 +03:00
|
|
|
stackbuf = NULL;
|
|
|
|
stacklen = stackmax = 0;
|
|
|
|
|
2006-08-15 00:01:30 +04:00
|
|
|
/* Limit the stackbuf length to a reasonable value to avoid overflow checks. */
|
|
|
|
#define STACK_LENGTH_LIMIT JS_BIT(20)
|
|
|
|
|
2002-03-03 03:59:16 +03:00
|
|
|
#define APPEND_CHAR_TO_STACK(c) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
if (stacklen == stackmax) { \
|
|
|
|
void *ptr_; \
|
2006-08-15 00:01:30 +04:00
|
|
|
if (stackmax >= STACK_LENGTH_LIMIT) \
|
|
|
|
goto done; \
|
2002-03-03 03:59:16 +03:00
|
|
|
stackmax = stackmax ? 2 * stackmax : 64; \
|
|
|
|
ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \
|
2006-09-03 16:34:18 +04:00
|
|
|
if (!ptr_) \
|
|
|
|
goto bad; \
|
2002-03-03 03:59:16 +03:00
|
|
|
stackbuf = ptr_; \
|
|
|
|
} \
|
|
|
|
stackbuf[stacklen++] = (c); \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
|
|
|
#define APPEND_STRING_TO_STACK(str) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
JSString *str_ = str; \
|
|
|
|
size_t length_ = JSSTRING_LENGTH(str_); \
|
2006-08-15 00:01:30 +04:00
|
|
|
if (length_ > stackmax - stacklen) { \
|
2002-03-03 03:59:16 +03:00
|
|
|
void *ptr_; \
|
2006-08-15 00:01:30 +04:00
|
|
|
if (stackmax >= STACK_LENGTH_LIMIT || \
|
|
|
|
length_ >= STACK_LENGTH_LIMIT - stacklen) { \
|
|
|
|
goto done; \
|
|
|
|
} \
|
2002-03-03 03:59:16 +03:00
|
|
|
stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \
|
|
|
|
ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \
|
2006-09-03 16:34:18 +04:00
|
|
|
if (!ptr_) \
|
|
|
|
goto bad; \
|
2002-03-03 03:59:16 +03:00
|
|
|
stackbuf = ptr_; \
|
|
|
|
} \
|
|
|
|
js_strncpy(stackbuf + stacklen, JSSTRING_CHARS(str_), length_); \
|
|
|
|
stacklen += length_; \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
values = GetStackTraceValueBuffer(priv);
|
|
|
|
elem = priv->stackElems;
|
|
|
|
for (endElem = elem + priv->stackDepth; elem != endElem; elem++) {
|
|
|
|
if (elem->funName) {
|
|
|
|
APPEND_STRING_TO_STACK(elem->funName);
|
2002-03-03 03:59:16 +03:00
|
|
|
APPEND_CHAR_TO_STACK('(');
|
2006-09-03 16:34:18 +04:00
|
|
|
for (i = 0; i != elem->argc; i++, values++) {
|
2002-03-03 03:59:16 +03:00
|
|
|
if (i > 0)
|
|
|
|
APPEND_CHAR_TO_STACK(',');
|
2006-09-03 16:34:18 +04:00
|
|
|
str = ValueToShortSource(cx, *values);
|
|
|
|
if (!str)
|
|
|
|
goto bad;
|
|
|
|
APPEND_STRING_TO_STACK(str);
|
2002-02-09 09:28:58 +03:00
|
|
|
}
|
2002-03-03 03:59:16 +03:00
|
|
|
APPEND_CHAR_TO_STACK(')');
|
2002-02-09 09:28:58 +03:00
|
|
|
}
|
2002-03-03 03:59:16 +03:00
|
|
|
APPEND_CHAR_TO_STACK('@');
|
2006-09-03 16:34:18 +04:00
|
|
|
if (elem->filename) {
|
|
|
|
for (cp = elem->filename; *cp; cp++)
|
2002-03-03 03:59:16 +03:00
|
|
|
APPEND_CHAR_TO_STACK(*cp);
|
|
|
|
}
|
|
|
|
APPEND_CHAR_TO_STACK(':');
|
2006-09-03 16:34:18 +04:00
|
|
|
JS_snprintf(ulnbuf, sizeof ulnbuf, "%u", elem->ulineno);
|
|
|
|
for (cp = ulnbuf; *cp; cp++)
|
|
|
|
APPEND_CHAR_TO_STACK(*cp);
|
2002-03-03 03:59:16 +03:00
|
|
|
APPEND_CHAR_TO_STACK('\n');
|
2002-02-09 09:28:58 +03:00
|
|
|
}
|
2002-03-03 03:59:16 +03:00
|
|
|
#undef APPEND_CHAR_TO_STACK
|
|
|
|
#undef APPEND_STRING_TO_STACK
|
2006-08-15 00:01:30 +04:00
|
|
|
#undef STACK_LENGTH_LIMIT
|
2002-03-03 03:59:16 +03:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
done:
|
|
|
|
if (stacklen == 0) {
|
|
|
|
JS_ASSERT(!stackbuf);
|
|
|
|
return cx->runtime->emptyString;
|
2006-09-03 00:53:20 +04:00
|
|
|
}
|
2006-09-03 16:34:18 +04:00
|
|
|
if (stacklen < stackmax) {
|
|
|
|
/*
|
|
|
|
* Realloc can fail when shrinking on some FreeBSD versions, so
|
|
|
|
* don't use JS_realloc here; simply let the oversized allocation
|
|
|
|
* be owned by the string in that rare case.
|
|
|
|
*/
|
|
|
|
void *shrunk = JS_realloc(cx, stackbuf, (stacklen+1) * sizeof(jschar));
|
|
|
|
if (shrunk)
|
|
|
|
stackbuf = shrunk;
|
2006-09-03 00:53:20 +04:00
|
|
|
}
|
2006-09-03 16:34:18 +04:00
|
|
|
|
|
|
|
stackbuf[stacklen] = 0;
|
|
|
|
str = js_NewString(cx, stackbuf, stacklen, 0);
|
|
|
|
if (str)
|
|
|
|
return str;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
if (stackbuf)
|
|
|
|
JS_free(cx, stackbuf);
|
|
|
|
return NULL;
|
2002-03-03 03:59:16 +03:00
|
|
|
}
|
|
|
|
|
2005-01-07 06:35:36 +03:00
|
|
|
/* XXXbe Consolidate the ugly truth that we don't treat filename as UTF-8
|
|
|
|
with these two functions. */
|
|
|
|
static JSString *
|
|
|
|
FilenameToString(JSContext *cx, const char *filename)
|
|
|
|
{
|
|
|
|
return JS_NewStringCopyZ(cx, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
StringToFilename(JSContext *cx, JSString *str)
|
|
|
|
{
|
|
|
|
return JS_GetStringBytes(str);
|
|
|
|
}
|
|
|
|
|
2002-03-03 03:59:16 +03:00
|
|
|
static JSBool
|
|
|
|
Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
|
|
|
JSBool ok;
|
2005-01-07 06:35:36 +03:00
|
|
|
uint32 lineno;
|
2002-03-03 03:59:16 +03:00
|
|
|
JSString *message, *filename;
|
2004-12-09 05:19:53 +03:00
|
|
|
JSStackFrame *fp;
|
2002-03-03 03:59:16 +03:00
|
|
|
|
|
|
|
if (cx->creatingException)
|
|
|
|
return JS_FALSE;
|
|
|
|
cx->creatingException = JS_TRUE;
|
|
|
|
|
|
|
|
if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
ok = OBJ_GET_PROPERTY(cx, JSVAL_TO_OBJECT(argv[-2]),
|
2004-10-05 14:19:07 +04:00
|
|
|
ATOM_TO_JSID(cx->runtime->atomState
|
|
|
|
.classPrototypeAtom),
|
2006-05-23 11:54:17 +04:00
|
|
|
rval);
|
2002-03-03 03:59:16 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2006-05-23 11:54:17 +04:00
|
|
|
obj = js_NewObject(cx, &js_ErrorClass, JSVAL_TO_OBJECT(*rval), NULL);
|
2002-03-03 03:59:16 +03:00
|
|
|
if (!obj) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-05-23 03:36:32 +04:00
|
|
|
if (OBJ_GET_CLASS(cx, obj) == &js_ErrorClass)
|
2002-03-03 03:59:16 +03:00
|
|
|
OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, JSVAL_VOID);
|
|
|
|
|
|
|
|
/* Set the 'message' property. */
|
|
|
|
if (argc != 0) {
|
|
|
|
message = js_ValueToString(cx, argv[0]);
|
|
|
|
if (!message) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
2004-09-21 05:18:00 +04:00
|
|
|
argv[0] = STRING_TO_JSVAL(message);
|
2002-03-03 03:59:16 +03:00
|
|
|
} else {
|
|
|
|
message = cx->runtime->emptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the 'fileName' property. */
|
|
|
|
if (argc > 1) {
|
|
|
|
filename = js_ValueToString(cx, argv[1]);
|
|
|
|
if (!filename) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
2004-09-21 05:18:00 +04:00
|
|
|
argv[1] = STRING_TO_JSVAL(filename);
|
2004-12-09 05:19:53 +03:00
|
|
|
fp = NULL;
|
2002-03-03 03:59:16 +03:00
|
|
|
} else {
|
2004-12-09 05:19:53 +03:00
|
|
|
fp = JS_GetScriptedCaller(cx, NULL);
|
|
|
|
if (fp) {
|
2005-01-07 06:35:36 +03:00
|
|
|
filename = FilenameToString(cx, fp->script->filename);
|
2004-12-09 05:19:53 +03:00
|
|
|
if (!filename) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filename = cx->runtime->emptyString;
|
|
|
|
}
|
2002-03-03 03:59:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the 'lineNumber' property. */
|
|
|
|
if (argc > 2) {
|
2005-01-07 06:35:36 +03:00
|
|
|
ok = js_ValueToECMAUint32(cx, argv[2], &lineno);
|
2002-03-03 03:59:16 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
} else {
|
2004-12-09 05:19:53 +03:00
|
|
|
if (!fp)
|
|
|
|
fp = JS_GetScriptedCaller(cx, NULL);
|
|
|
|
lineno = (fp && fp->pc) ? js_PCToLineNumber(cx, fp->script, fp->pc) : 0;
|
2002-03-03 03:59:16 +03:00
|
|
|
}
|
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
ok = (OBJ_GET_CLASS(cx, obj) != &js_ErrorClass) ||
|
|
|
|
InitExnPrivate(cx, obj, message, filename, lineno, NULL);
|
2002-03-03 03:59:16 +03:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
out:
|
2002-03-03 03:59:16 +03:00
|
|
|
cx->creatingException = JS_FALSE;
|
2002-02-09 09:28:58 +03:00
|
|
|
return ok;
|
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;
|
2001-10-25 04:26:38 +04:00
|
|
|
size_t name_length, message_length, length;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2004-10-05 14:19:07 +04:00
|
|
|
if (!OBJ_GET_PROPERTY(cx, obj,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState.nameAtom),
|
|
|
|
&v)) {
|
2000-04-05 10:39:11 +04:00
|
|
|
return JS_FALSE;
|
2004-10-05 14:19:07 +04:00
|
|
|
}
|
2004-09-21 05:18:00 +04:00
|
|
|
name = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) : cx->runtime->emptyString;
|
2005-10-20 03:32:37 +04:00
|
|
|
*rval = STRING_TO_JSVAL(name);
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2004-09-21 05:18:00 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, js_message_str, &v))
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
2004-09-21 05:18:00 +04:00
|
|
|
message = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v)
|
|
|
|
: cx->runtime->emptyString;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2001-10-25 04:26:38 +04:00
|
|
|
if (JSSTRING_LENGTH(message) != 0) {
|
|
|
|
name_length = JSSTRING_LENGTH(name);
|
|
|
|
message_length = JSSTRING_LENGTH(message);
|
2004-09-21 05:18:00 +04:00
|
|
|
length = (name_length ? name_length + 2 : 0) + message_length;
|
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
|
|
|
|
2004-09-21 05:18:00 +04:00
|
|
|
if (name_length) {
|
|
|
|
js_strncpy(cp, JSSTRING_CHARS(name), name_length);
|
|
|
|
cp += name_length;
|
|
|
|
*cp++ = ':'; *cp++ = ' ';
|
|
|
|
}
|
2001-10-25 04:26:38 +04:00
|
|
|
js_strncpy(cp, JSSTRING_CHARS(message), message_length);
|
|
|
|
cp += message_length;
|
1998-11-05 03:08:43 +03:00
|
|
|
*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)
|
|
|
|
{
|
2005-10-23 08:59:50 +04:00
|
|
|
jsval *vp;
|
2000-09-09 04:41:15 +04:00
|
|
|
JSString *name, *message, *filename, *lineno_as_str, *result;
|
2005-01-07 06:35:36 +03:00
|
|
|
uint32 lineno;
|
2001-10-25 04:26:38 +04:00
|
|
|
size_t lineno_length, name_length, message_length, filename_length, length;
|
1998-11-05 03:08:43 +03:00
|
|
|
jschar *chars, *cp;
|
|
|
|
|
2005-10-23 08:59:50 +04:00
|
|
|
vp = argv + argc; /* beginning of explicit local roots */
|
|
|
|
|
2004-10-05 14:19:07 +04:00
|
|
|
if (!OBJ_GET_PROPERTY(cx, obj,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState.nameAtom),
|
2005-10-23 08:59:50 +04:00
|
|
|
rval)) {
|
2000-04-05 10:39:11 +04:00
|
|
|
return JS_FALSE;
|
2004-10-05 14:19:07 +04:00
|
|
|
}
|
2005-10-23 08:59:50 +04:00
|
|
|
name = js_ValueToString(cx, *rval);
|
2000-04-05 10:39:11 +04:00
|
|
|
if (!name)
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
2005-10-20 03:32:37 +04:00
|
|
|
*rval = STRING_TO_JSVAL(name);
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2005-10-23 08:59:50 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, js_message_str, &vp[0]) ||
|
|
|
|
!(message = js_ValueToSource(cx, vp[0]))) {
|
1998-11-05 03:08:43 +03:00
|
|
|
return JS_FALSE;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
2005-10-23 08:59:50 +04:00
|
|
|
vp[0] = STRING_TO_JSVAL(message);
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, js_fileName_str, &vp[1]) ||
|
2005-10-23 08:59:50 +04:00
|
|
|
!(filename = js_ValueToSource(cx, vp[1]))) {
|
2000-09-09 04:41:15 +04:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2005-10-23 08:59:50 +04:00
|
|
|
vp[1] = STRING_TO_JSVAL(filename);
|
2000-09-09 04:41:15 +04:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, js_lineNumber_str, &vp[2]) ||
|
2005-10-23 08:59:50 +04:00
|
|
|
!js_ValueToECMAUint32 (cx, vp[2], &lineno)) {
|
2000-09-09 04:41:15 +04:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineno != 0) {
|
2005-10-23 08:59:50 +04:00
|
|
|
lineno_as_str = js_ValueToString(cx, vp[2]);
|
|
|
|
if (!lineno_as_str)
|
2000-09-09 04:41:15 +04:00
|
|
|
return JS_FALSE;
|
2001-10-25 04:26:38 +04:00
|
|
|
lineno_length = JSSTRING_LENGTH(lineno_as_str);
|
2000-09-09 04:41:15 +04:00
|
|
|
} else {
|
|
|
|
lineno_as_str = NULL;
|
2001-10-25 04:26:38 +04:00
|
|
|
lineno_length = 0;
|
2001-09-03 23:36:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Magic 8, for the characters in ``(new ())''. */
|
2001-10-25 04:26:38 +04:00
|
|
|
name_length = JSSTRING_LENGTH(name);
|
|
|
|
message_length = JSSTRING_LENGTH(message);
|
|
|
|
length = 8 + name_length + message_length;
|
2001-09-03 23:36:24 +04:00
|
|
|
|
2001-10-25 04:26:38 +04:00
|
|
|
filename_length = JSSTRING_LENGTH(filename);
|
|
|
|
if (filename_length != 0) {
|
2001-09-03 23:36:24 +04:00
|
|
|
/* append filename as ``, {filename}'' */
|
2001-10-25 04:26:38 +04:00
|
|
|
length += 2 + filename_length;
|
2000-09-09 04:41:15 +04:00
|
|
|
if (lineno_as_str) {
|
|
|
|
/* append lineno as ``, {lineno_as_str}'' */
|
2001-10-25 04:26:38 +04:00
|
|
|
length += 2 + lineno_length;
|
2000-09-09 04:41:15 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (lineno_as_str) {
|
|
|
|
/*
|
|
|
|
* no filename, but have line number,
|
|
|
|
* need to append ``, "", {lineno_as_str}''
|
|
|
|
*/
|
2001-10-25 04:26:38 +04:00
|
|
|
length += 6 + lineno_length;
|
2000-09-09 04:41:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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++ = ' ';
|
2001-10-25 04:26:38 +04:00
|
|
|
js_strncpy(cp, JSSTRING_CHARS(name), name_length);
|
|
|
|
cp += name_length;
|
1998-11-05 03:08:43 +03:00
|
|
|
*cp++ = '(';
|
2001-10-25 04:26:38 +04:00
|
|
|
if (message_length != 0) {
|
|
|
|
js_strncpy(cp, JSSTRING_CHARS(message), message_length);
|
|
|
|
cp += message_length;
|
1998-11-05 03:08:43 +03:00
|
|
|
}
|
2000-09-09 04:41:15 +04:00
|
|
|
|
2001-10-25 04:26:38 +04:00
|
|
|
if (filename_length != 0) {
|
2001-09-03 23:36:24 +04:00
|
|
|
/* append filename as ``, {filename}'' */
|
|
|
|
*cp++ = ','; *cp++ = ' ';
|
2001-10-25 04:26:38 +04:00
|
|
|
js_strncpy(cp, JSSTRING_CHARS(filename), filename_length);
|
|
|
|
cp += filename_length;
|
2000-09-09 04:41:15 +04:00
|
|
|
} else {
|
|
|
|
if (lineno_as_str) {
|
|
|
|
/*
|
|
|
|
* no filename, but have line number,
|
|
|
|
* need to append ``, "", {lineno_as_str}''
|
|
|
|
*/
|
|
|
|
*cp++ = ','; *cp++ = ' '; *cp++ = '"'; *cp++ = '"';
|
|
|
|
}
|
|
|
|
}
|
2001-10-25 04:26:38 +04:00
|
|
|
if (lineno_as_str) {
|
|
|
|
/* append lineno as ``, {lineno_as_str}'' */
|
|
|
|
*cp++ = ','; *cp++ = ' ';
|
|
|
|
js_strncpy(cp, JSSTRING_CHARS(lineno_as_str), lineno_length);
|
|
|
|
cp += lineno_length;
|
|
|
|
}
|
2000-09-09 04:41:15 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
*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
|
2005-10-23 08:59:50 +04:00
|
|
|
{js_toSource_str, exn_toSource, 0,0,3},
|
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)
|
|
|
|
{
|
2006-04-21 15:08:18 +04:00
|
|
|
JSObject *obj_proto, *protos[JSEXN_LIMIT];
|
1998-10-14 14:22:38 +04:00
|
|
|
int i;
|
2006-04-21 15:08:18 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If lazy class initialization occurs for any Error subclass, then all
|
|
|
|
* classes are initialized, starting with Error. To avoid reentry and
|
|
|
|
* redundant initialization, we must not pass a null proto parameter to
|
|
|
|
* js_NewObject below, when called for the Error superclass. We need to
|
|
|
|
* ensure that Object.prototype is the proto of Error.prototype.
|
|
|
|
*
|
|
|
|
* See the equivalent code to ensure that parent_proto is non-null when
|
|
|
|
* JS_InitClass calls js_NewObject, in jsapi.c.
|
|
|
|
*/
|
|
|
|
if (!js_GetClassPrototype(cx, obj, INT_TO_JSID(JSProto_Object),
|
|
|
|
&obj_proto)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2005-05-05 09:40:22 +04:00
|
|
|
if (!js_EnterLocalRootScope(cx))
|
|
|
|
return NULL;
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Initialize the prototypes first. */
|
|
|
|
for (i = 0; exceptions[i].name != 0; i++) {
|
|
|
|
JSAtom *atom;
|
|
|
|
JSFunction *fun;
|
2006-06-22 03:54:04 +04:00
|
|
|
JSObject *funobj;
|
1998-11-05 03:08:43 +03:00
|
|
|
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. */
|
2006-05-23 03:36:32 +04:00
|
|
|
protos[i] = js_NewObject(cx, &js_ErrorClass,
|
2000-04-05 10:39:11 +04:00
|
|
|
(protoIndex != JSEXN_NONE)
|
|
|
|
? protos[protoIndex]
|
2006-04-21 15:08:18 +04:00
|
|
|
: obj_proto,
|
1998-11-05 03:08:43 +03:00
|
|
|
obj);
|
|
|
|
if (!protos[i])
|
2005-05-05 09:40:22 +04:00
|
|
|
break;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2000-04-05 10:39:11 +04:00
|
|
|
/* So exn_finalize knows whether to destroy private data. */
|
2002-02-09 09:28:58 +03:00
|
|
|
OBJ_SET_SLOT(cx, protos[i], JSSLOT_PRIVATE, JSVAL_VOID);
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
/* Make a constructor function for the current name. */
|
2006-04-21 15:08:18 +04:00
|
|
|
atom = cx->runtime->atomState.classAtoms[exceptions[i].key];
|
2002-02-09 09:28:58 +03:00
|
|
|
fun = js_DefineFunction(cx, obj, atom, exceptions[i].native, 3, 0);
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!fun)
|
2005-05-05 09:40:22 +04:00
|
|
|
break;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
|
|
|
/* Make this constructor make objects of class Exception. */
|
2006-05-23 03:36:32 +04:00
|
|
|
fun->clasp = &js_ErrorClass;
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2006-06-22 03:54:04 +04:00
|
|
|
/* Extract the constructor object. */
|
|
|
|
funobj = fun->object;
|
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
/* Make the prototype and constructor links. */
|
2006-06-22 03:54:04 +04:00
|
|
|
if (!js_SetClassPrototype(cx, funobj, protos[i],
|
2000-04-05 10:39:11 +04:00
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT)) {
|
2005-05-05 09:40:22 +04:00
|
|
|
break;
|
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);
|
2000-12-30 01:19:09 +03:00
|
|
|
if (!nameString)
|
2005-05-05 09:40:22 +04:00
|
|
|
break;
|
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)) {
|
2005-05-05 09:40:22 +04:00
|
|
|
break;
|
2000-04-05 10:39:11 +04:00
|
|
|
}
|
2006-06-22 03:54:04 +04:00
|
|
|
|
|
|
|
/* Finally, stash the constructor for later uses. */
|
|
|
|
if (!js_SetClassObject(cx, obj, exceptions[i].key, funobj))
|
|
|
|
break;
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
2000-04-05 10:39:11 +04:00
|
|
|
|
2005-05-05 09:40:22 +04:00
|
|
|
js_LeaveLocalRootScope(cx);
|
|
|
|
if (exceptions[i].name)
|
|
|
|
return NULL;
|
|
|
|
|
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
|
|
|
}
|
2006-09-03 16:34:18 +04:00
|
|
|
if (!JS_DefineProperty(cx, protos[0], js_fileName_str,
|
2000-09-09 04:41:15 +04:00
|
|
|
STRING_TO_JSVAL(cx->runtime->emptyString),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-09-03 16:34:18 +04:00
|
|
|
if (!JS_DefineProperty(cx, protos[0], js_lineNumber_str,
|
2000-09-09 04:41:15 +04:00
|
|
|
INT_TO_JSVAL(0),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2006-02-14 12:33:45 +03:00
|
|
|
const JSErrorFormatString*
|
2005-12-03 05:31:21 +03:00
|
|
|
js_GetLocalizedErrorMessage(JSContext* cx, void *userRef, const char *locale, const uintN errorNumber)
|
|
|
|
{
|
|
|
|
const JSErrorFormatString *errorString = NULL;
|
|
|
|
|
2005-12-03 10:32:07 +03:00
|
|
|
if (cx->localeCallbacks && cx->localeCallbacks->localeGetErrorMessage) {
|
2005-12-03 05:31:21 +03:00
|
|
|
errorString = cx->localeCallbacks
|
|
|
|
->localeGetErrorMessage(userRef, locale, errorNumber);
|
|
|
|
}
|
|
|
|
if (!errorString)
|
|
|
|
errorString = js_GetErrorMessage(userRef, locale, errorNumber);
|
|
|
|
return errorString;
|
|
|
|
}
|
1998-10-14 14:22:38 +04:00
|
|
|
|
|
|
|
#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;
|
2006-10-27 02:23:52 +04:00
|
|
|
const JSErrorFormatString *errorString;
|
1998-10-14 14:22:38 +04:00
|
|
|
JSExnType exn;
|
2006-10-27 02:23:52 +04:00
|
|
|
jsval tv[4];
|
|
|
|
JSTempValueRooter tvr;
|
2002-03-03 03:59:16 +03:00
|
|
|
JSBool ok;
|
|
|
|
JSObject *errProto, *errObject;
|
|
|
|
JSString *messageStr, *filenameStr;
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2002-09-06 02:25:48 +04:00
|
|
|
/*
|
|
|
|
* Tell our caller to report immediately if cx has no active frames, or if
|
|
|
|
* this report is just a warning.
|
|
|
|
*/
|
1998-10-14 14:22:38 +04:00
|
|
|
JS_ASSERT(reportp);
|
2002-09-06 02:25:48 +04:00
|
|
|
if (!cx->fp || JSREPORT_IS_WARNING(reportp->flags))
|
2000-01-18 14:06:05 +03:00
|
|
|
return JS_FALSE;
|
2002-09-06 02:25:48 +04:00
|
|
|
|
|
|
|
/* Find the exception index associated with this error. */
|
2000-02-02 04:10:31 +03:00
|
|
|
errorNumber = (JSErrNum) reportp->errorNumber;
|
2005-12-03 05:31:21 +03:00
|
|
|
errorString = js_GetLocalizedErrorMessage(cx, NULL, NULL, errorNumber);
|
|
|
|
exn = errorString ? errorString->exnType : JSEXN_NONE;
|
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",
|
2002-03-14 03:14:48 +03:00
|
|
|
errortoexnname[errorNumber].name,
|
|
|
|
errortoexnname[errorNumber].exception);
|
1998-10-14 14:22:38 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return false (no exception raised) if no exception is associated
|
|
|
|
* with the given error number.
|
|
|
|
*/
|
|
|
|
if (exn == JSEXN_NONE)
|
2002-03-14 03:14:48 +03:00
|
|
|
return JS_FALSE;
|
1998-10-14 14:22:38 +04:00
|
|
|
|
2002-03-03 03:59:16 +03:00
|
|
|
/*
|
|
|
|
* Prevent runaway recursion, just as the Exception native constructor
|
|
|
|
* must do, via cx->creatingException. If an out-of-memory error occurs,
|
|
|
|
* no exception object will be created, but we don't assume that OOM is
|
|
|
|
* the only kind of error that subroutines of this function called below
|
|
|
|
* might raise.
|
|
|
|
*/
|
|
|
|
if (cx->creatingException)
|
|
|
|
return JS_FALSE;
|
2006-10-27 02:23:52 +04:00
|
|
|
|
|
|
|
/* After this point the control must flow through the label out. */
|
2002-03-03 03:59:16 +03:00
|
|
|
cx->creatingException = JS_TRUE;
|
|
|
|
|
2005-10-21 04:26:56 +04:00
|
|
|
/* Protect the newly-created strings below from nesting GCs. */
|
2006-10-27 02:23:52 +04:00
|
|
|
memset(tv, 0, sizeof tv);
|
|
|
|
JS_PUSH_TEMP_ROOT(cx, sizeof tv / sizeof tv[0], tv, &tvr);
|
2005-10-21 04:26:56 +04:00
|
|
|
|
1998-10-14 14:22:38 +04:00
|
|
|
/*
|
1998-11-05 03:08:43 +03:00
|
|
|
* Try to get an appropriate prototype by looking up the corresponding
|
2001-08-22 08:59:28 +04:00
|
|
|
* exception constructor name in the scope chain of the current context's
|
|
|
|
* top stack frame, or in the global object if no frame is active.
|
1998-10-14 14:22:38 +04:00
|
|
|
*/
|
2006-04-19 04:22:43 +04:00
|
|
|
ok = js_GetClassPrototype(cx, NULL, INT_TO_JSID(exceptions[exn].key),
|
|
|
|
&errProto);
|
2004-11-17 10:43:01 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2006-10-27 02:23:52 +04:00
|
|
|
tv[0] = OBJECT_TO_JSVAL(errProto);
|
2002-03-03 03:59:16 +03:00
|
|
|
|
2006-05-23 03:36:32 +04:00
|
|
|
errObject = js_NewObject(cx, &js_ErrorClass, errProto, NULL);
|
2002-03-03 03:59:16 +03:00
|
|
|
if (!errObject) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-10-27 02:23:52 +04:00
|
|
|
tv[1] = OBJECT_TO_JSVAL(errObject);
|
2002-05-07 12:34:51 +04:00
|
|
|
|
2002-03-03 03:59:16 +03:00
|
|
|
messageStr = JS_NewStringCopyZ(cx, message);
|
|
|
|
if (!messageStr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
2001-08-22 08:59:28 +04:00
|
|
|
}
|
2006-10-27 02:23:52 +04:00
|
|
|
tv[2] = STRING_TO_JSVAL(messageStr);
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2006-03-10 02:58:35 +03:00
|
|
|
filenameStr = JS_NewStringCopyZ(cx, reportp->filename);
|
|
|
|
if (!filenameStr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
2000-09-09 04:41:15 +04:00
|
|
|
}
|
2006-10-27 02:23:52 +04:00
|
|
|
tv[3] = STRING_TO_JSVAL(filenameStr);
|
2006-03-10 02:58:35 +03:00
|
|
|
|
2006-10-27 02:23:52 +04:00
|
|
|
ok = InitExnPrivate(cx, errObject, messageStr, filenameStr,
|
|
|
|
reportp->lineno, reportp);
|
2002-03-03 03:59:16 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2001-09-03 23:36:24 +04:00
|
|
|
|
2006-10-27 02:23:52 +04:00
|
|
|
JS_SetPendingException(cx, OBJECT_TO_JSVAL(errObject));
|
|
|
|
|
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
|
|
|
|
2002-03-03 03:59:16 +03:00
|
|
|
out:
|
2006-10-27 02:23:52 +04:00
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
2002-03-03 03:59:16 +03:00
|
|
|
cx->creatingException = JS_FALSE;
|
|
|
|
return ok;
|
1998-10-14 14:22:38 +04:00
|
|
|
}
|
1998-11-05 03:08:43 +03:00
|
|
|
|
2002-03-14 03:14:48 +03:00
|
|
|
JSBool
|
1998-11-05 03:08:43 +03:00
|
|
|
js_ReportUncaughtException(JSContext *cx)
|
|
|
|
{
|
2006-10-27 23:38:33 +04:00
|
|
|
jsval exn;
|
1998-11-05 03:08:43 +03:00
|
|
|
JSObject *exnObject;
|
2006-10-27 23:38:33 +04:00
|
|
|
jsval vp[5];
|
|
|
|
JSTempValueRooter tvr;
|
2004-12-09 05:19:53 +03:00
|
|
|
JSErrorReport *reportp, report;
|
2005-10-23 08:59:50 +04:00
|
|
|
JSString *str;
|
1999-10-07 01:31:34 +04:00
|
|
|
const char *bytes;
|
2004-12-12 01:00:34 +03:00
|
|
|
JSBool ok;
|
1999-10-07 01:31:34 +04:00
|
|
|
|
1998-11-05 03:08:43 +03:00
|
|
|
if (!JS_IsExceptionPending(cx))
|
2004-07-17 06:39:55 +04:00
|
|
|
return JS_TRUE;
|
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
|
2005-10-23 08:59:50 +04:00
|
|
|
* could become unrooted, we must root exnObject. Later, if exnObject is
|
|
|
|
* non-null, we need to root other intermediates, so allocate an operand
|
|
|
|
* stack segment to protect all of these values.
|
1998-11-05 03:08:43 +03:00
|
|
|
*/
|
2004-07-17 06:39:55 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(exn)) {
|
|
|
|
exnObject = NULL;
|
|
|
|
} else {
|
1998-11-05 03:08:43 +03:00
|
|
|
exnObject = JSVAL_TO_OBJECT(exn);
|
2005-10-23 08:59:50 +04:00
|
|
|
vp[0] = exn;
|
2006-10-27 23:38:33 +04:00
|
|
|
memset(vp + 1, 0, sizeof vp - sizeof vp[0]);
|
|
|
|
JS_PUSH_TEMP_ROOT(cx, JS_ARRAY_LENGTH(vp), vp, &tvr);
|
1998-11-05 03:08:43 +03:00
|
|
|
}
|
|
|
|
|
2006-09-08 04:39:30 +04:00
|
|
|
JS_ClearPendingException(cx);
|
1998-11-05 03:08:43 +03:00
|
|
|
reportp = js_ErrorFromException(cx, exn);
|
|
|
|
|
2005-07-06 01:18:13 +04:00
|
|
|
/* XXX L10N angels cry once again (see also jsemit.c, /L10N gaffes/) */
|
2002-03-14 03:14:48 +03:00
|
|
|
str = js_ValueToString(cx, exn);
|
2005-10-23 08:59:50 +04:00
|
|
|
if (!str) {
|
|
|
|
bytes = "unknown (can't convert to string)";
|
|
|
|
} else {
|
2006-10-27 23:38:33 +04:00
|
|
|
if (exnObject)
|
2005-10-23 10:22:46 +04:00
|
|
|
vp[1] = STRING_TO_JSVAL(str);
|
2006-03-26 01:55:00 +03:00
|
|
|
bytes = js_GetStringBytes(cx->runtime, str);
|
2005-10-23 08:59:50 +04:00
|
|
|
}
|
2004-12-12 01:00:34 +03:00
|
|
|
ok = JS_TRUE;
|
1999-10-07 01:31:34 +04:00
|
|
|
|
2005-01-07 06:35:36 +03:00
|
|
|
if (!reportp &&
|
|
|
|
exnObject &&
|
2006-05-23 03:36:32 +04:00
|
|
|
OBJ_GET_CLASS(cx, exnObject) == &js_ErrorClass) {
|
2004-12-09 05:19:53 +03:00
|
|
|
const char *filename;
|
2005-01-07 06:35:36 +03:00
|
|
|
uint32 lineno;
|
2004-12-09 05:19:53 +03:00
|
|
|
|
2005-10-23 08:59:50 +04:00
|
|
|
ok = JS_GetProperty(cx, exnObject, js_message_str, &vp[2]);
|
2004-12-12 01:00:34 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2005-10-23 08:59:50 +04:00
|
|
|
if (JSVAL_IS_STRING(vp[2]))
|
|
|
|
bytes = JS_GetStringBytes(JSVAL_TO_STRING(vp[2]));
|
2004-12-09 05:19:53 +03:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
ok = JS_GetProperty(cx, exnObject, js_fileName_str, &vp[3]);
|
2004-12-12 01:00:34 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2005-10-23 08:59:50 +04:00
|
|
|
str = js_ValueToString(cx, vp[3]);
|
2005-01-07 06:35:36 +03:00
|
|
|
if (!str) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
filename = StringToFilename(cx, str);
|
2004-12-09 05:19:53 +03:00
|
|
|
|
2006-09-03 16:34:18 +04:00
|
|
|
ok = JS_GetProperty(cx, exnObject, js_lineNumber_str, &vp[4]);
|
2004-12-12 01:00:34 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2005-10-23 08:59:50 +04:00
|
|
|
ok = js_ValueToECMAUint32 (cx, vp[4], &lineno);
|
2004-12-12 01:00:34 +03:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
2004-12-09 05:19:53 +03:00
|
|
|
|
|
|
|
reportp = &report;
|
|
|
|
memset(&report, 0, sizeof report);
|
|
|
|
report.filename = filename;
|
2005-01-07 06:35:36 +03:00
|
|
|
report.lineno = (uintN) lineno;
|
2004-12-09 05:19:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!reportp) {
|
1998-11-05 03:08:43 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-12-12 01:00:34 +03:00
|
|
|
out:
|
|
|
|
if (exnObject)
|
2006-10-27 23:38:33 +04:00
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
2004-12-12 01:00:34 +03:00
|
|
|
return ok;
|
1998-11-05 03:08:43 +03:00
|
|
|
}
|