2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2007-03-22 10:30:00 -07:00
|
|
|
* vim: set ts=8 sw=4 et tw=78:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS standard exception implementation.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsbit.h"
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "jsexn.h"
|
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jsnum.h"
|
2008-03-04 15:40:10 -08:00
|
|
|
#include "jsobj.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsopcode.h"
|
2008-03-04 15:40:10 -08:00
|
|
|
#include "jsscope.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsscript.h"
|
2008-09-08 10:51:10 -07:00
|
|
|
#include "jsstaticcheck.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
#include "jscntxtinlines.h"
|
2010-04-10 16:16:35 -07:00
|
|
|
#include "jsobjinlines.h"
|
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
using namespace js;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Forward declarations for js_ErrorClass's initializer. */
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
Exception(JSContext *cx, JSObject *obj, uintN argc, Value *argv, Value *rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
exn_trace(JSTracer *trc, JSObject *obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
exn_finalize(JSContext *cx, JSObject *obj);
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
exn_enumerate(JSContext *cx, JSObject *obj);
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
exn_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject **objp);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class js_ErrorClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_Error_str,
|
2007-04-16 23:53:37 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_MARK_IS_TRACE |
|
2007-03-22 10:30:00 -07:00
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Error),
|
2010-06-12 09:29:04 -07:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
PropertyStub, /* setProperty */
|
|
|
|
exn_enumerate,
|
|
|
|
(JSResolveOp)exn_resolve,
|
|
|
|
ConvertStub,
|
|
|
|
exn_finalize,
|
|
|
|
NULL, /* reserved0 */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
Exception, /* construct */
|
|
|
|
NULL, /* xdrObject */
|
|
|
|
NULL, /* hasInstance */
|
|
|
|
JS_CLASS_TRACE(exn_trace)
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct JSStackTraceElem {
|
|
|
|
JSString *funName;
|
|
|
|
size_t argc;
|
|
|
|
const char *filename;
|
|
|
|
uintN ulineno;
|
|
|
|
} JSStackTraceElem;
|
|
|
|
|
|
|
|
typedef struct JSExnPrivate {
|
|
|
|
/* A copy of the JSErrorReport originally generated. */
|
|
|
|
JSErrorReport *errorReport;
|
|
|
|
JSString *message;
|
|
|
|
JSString *filename;
|
|
|
|
uintN lineno;
|
|
|
|
size_t stackDepth;
|
|
|
|
JSStackTraceElem stackElems[1];
|
|
|
|
} JSExnPrivate;
|
|
|
|
|
|
|
|
static JSString *
|
|
|
|
StackTraceToString(JSContext *cx, JSExnPrivate *priv);
|
|
|
|
|
|
|
|
static JSErrorReport *
|
|
|
|
CopyErrorReport(JSContext *cx, JSErrorReport *report)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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))
|
|
|
|
|
|
|
|
filenameSize = report->filename ? strlen(report->filename) + 1 : 0;
|
|
|
|
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);
|
|
|
|
if (report->messageArgs) {
|
|
|
|
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;
|
2009-07-27 21:10:12 -07:00
|
|
|
cursor = (uint8 *)cx->malloc(mallocSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
JS_ASSERT(cursor == (uint8 *)copy->messageArgs[0] + argsCopySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (report->filename) {
|
|
|
|
copy->filename = (const char *)cursor;
|
|
|
|
memcpy(cursor, report->filename, filenameSize);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-09-05 16:24:53 -07:00
|
|
|
JSSecurityCallbacks *callbacks;
|
2010-07-14 23:19:36 -07:00
|
|
|
CheckAccessOp checkAccess;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSErrorReporter older;
|
|
|
|
JSExceptionState *state;
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid callerid;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSStackFrame *fp, *fpstop;
|
|
|
|
size_t stackDepth, valueCount, size;
|
|
|
|
JSBool overflow;
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
JSStackTraceElem *elem;
|
|
|
|
jsval *values;
|
|
|
|
|
2010-04-07 16:09:49 -07:00
|
|
|
JS_ASSERT(exnObject->getClass() == &js_ErrorClass);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2008-09-05 16:24:53 -07:00
|
|
|
callbacks = JS_GetSecurityCallbacks(cx);
|
|
|
|
checkAccess = callbacks
|
2010-07-14 23:19:36 -07:00
|
|
|
? Valueify(callbacks->checkObjectAccess)
|
2008-09-05 16:24:53 -07:00
|
|
|
: NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
older = JS_SetErrorReporter(cx, NULL);
|
|
|
|
state = JS_SaveExceptionState(cx);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
callerid = ATOM_TO_JSID(cx->runtime->atomState.callerAtom);
|
2007-03-22 10:30:00 -07:00
|
|
|
stackDepth = 0;
|
|
|
|
valueCount = 0;
|
2008-12-09 08:38:32 -08:00
|
|
|
for (fp = js_GetTopStackFrame(cx); fp; fp = fp->down) {
|
2010-08-15 05:44:51 -07:00
|
|
|
if (fp->hasFunction() && fp->argv) {
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v = NullValue();
|
2007-08-01 23:32:41 -07:00
|
|
|
if (checkAccess &&
|
2009-08-24 17:09:44 -07:00
|
|
|
!checkAccess(cx, fp->callee(), callerid, JSACC_READ, &v)) {
|
2007-08-01 23:32:41 -07:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-19 16:44:12 -07:00
|
|
|
valueCount += fp->numActualArgs();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
++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) {
|
2008-03-12 16:07:47 -07:00
|
|
|
js_ReportAllocationOverflow(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2009-07-27 21:10:12 -07:00
|
|
|
priv = (JSExnPrivate *)cx->malloc(size);
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2008-12-09 08:38:32 -08:00
|
|
|
for (fp = js_GetTopStackFrame(cx); fp != fpstop; fp = fp->down) {
|
2010-08-15 05:44:51 -07:00
|
|
|
if (!fp->hasFunction()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
elem->funName = NULL;
|
|
|
|
elem->argc = 0;
|
|
|
|
} else {
|
2010-08-15 05:44:51 -07:00
|
|
|
elem->funName = fp->getFunction()->atom
|
|
|
|
? ATOM_TO_STRING(fp->getFunction()->atom)
|
2007-03-22 10:30:00 -07:00
|
|
|
: cx->runtime->emptyString;
|
2010-08-19 16:44:12 -07:00
|
|
|
elem->argc = fp->numActualArgs();
|
|
|
|
memcpy(values, fp->argv, elem->argc * sizeof(jsval));
|
|
|
|
values += elem->argc;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
elem->ulineno = 0;
|
|
|
|
elem->filename = NULL;
|
2010-08-15 05:44:51 -07:00
|
|
|
if (fp->hasScript()) {
|
|
|
|
elem->filename = fp->getScript()->filename;
|
2010-03-03 18:10:13 -08:00
|
|
|
if (fp->pc(cx))
|
2008-11-13 00:30:20 -08:00
|
|
|
elem->ulineno = js_FramePCToLineNumber(cx, fp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
++elem;
|
|
|
|
}
|
|
|
|
JS_ASSERT(priv->stackElems + stackDepth == elem);
|
|
|
|
JS_ASSERT(GetStackTraceValueBuffer(priv) + valueCount == values);
|
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
exnObject->setPrivate(priv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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
|
2010-03-21 16:07:48 -07:00
|
|
|
* TokenStream.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
static inline JSExnPrivate *
|
2007-03-22 10:30:00 -07:00
|
|
|
GetExnPrivate(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2009-08-04 14:06:55 -07:00
|
|
|
return (JSExnPrivate *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
exn_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
JSStackTraceElem *elem;
|
|
|
|
size_t vcount, i;
|
|
|
|
jsval *vp, v;
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
priv = GetExnPrivate(trc->context, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (priv) {
|
2007-04-16 23:53:37 -07:00
|
|
|
if (priv->message)
|
|
|
|
JS_CALL_STRING_TRACER(trc, priv->message, "exception message");
|
|
|
|
if (priv->filename)
|
|
|
|
JS_CALL_STRING_TRACER(trc, priv->filename, "exception filename");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
elem = priv->stackElems;
|
|
|
|
for (vcount = i = 0; i != priv->stackDepth; ++i, ++elem) {
|
2007-04-16 23:53:37 -07:00
|
|
|
if (elem->funName) {
|
|
|
|
JS_CALL_STRING_TRACER(trc, elem->funName,
|
|
|
|
"stack trace function name");
|
|
|
|
}
|
|
|
|
if (IS_GC_MARKING_TRACER(trc) && elem->filename)
|
2007-03-22 10:30:00 -07:00
|
|
|
js_MarkScriptFilename(elem->filename);
|
|
|
|
vcount += elem->argc;
|
|
|
|
}
|
|
|
|
vp = GetStackTraceValueBuffer(priv);
|
|
|
|
for (i = 0; i != vcount; ++i, ++vp) {
|
|
|
|
v = *vp;
|
2007-04-16 23:53:37 -07:00
|
|
|
JS_CALL_VALUE_TRACER(trc, v, "stack trace argument");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
exn_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
|
|
|
|
priv = GetExnPrivate(cx, obj);
|
|
|
|
if (priv) {
|
|
|
|
if (priv->errorReport)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(priv->errorReport);
|
|
|
|
cx->free(priv);
|
2007-03-22 10:30:00 -07: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)
|
2009-08-11 13:05:44 -07:00
|
|
|
pobj->dropProperty(cx, prop);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
exn_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject **objp)
|
|
|
|
{
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
JSString *str;
|
|
|
|
JSAtom *atom;
|
|
|
|
JSString *stack;
|
|
|
|
const char *prop;
|
|
|
|
jsval v;
|
|
|
|
|
|
|
|
*objp = NULL;
|
|
|
|
priv = GetExnPrivate(cx, obj);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (priv && JSID_IS_ATOM(id)) {
|
|
|
|
str = JSID_TO_STRING(id);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
define:
|
|
|
|
if (!JS_DefineProperty(cx, obj, prop, v, NULL, NULL, JSPROP_ENUMERATE))
|
|
|
|
return JS_FALSE;
|
|
|
|
*objp = obj;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSErrorReport *
|
|
|
|
js_ErrorFromException(JSContext *cx, jsval exn)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
JSExnPrivate *priv;
|
|
|
|
|
|
|
|
if (JSVAL_IS_PRIMITIVE(exn))
|
|
|
|
return NULL;
|
|
|
|
obj = JSVAL_TO_OBJECT(exn);
|
2010-04-07 16:09:49 -07:00
|
|
|
if (obj->getClass() != &js_ErrorClass)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
priv = GetExnPrivate(cx, obj);
|
|
|
|
if (!priv)
|
|
|
|
return NULL;
|
|
|
|
return priv->errorReport;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSString *
|
|
|
|
ValueToShortSource(JSContext *cx, jsval v)
|
|
|
|
{
|
|
|
|
JSString *str;
|
|
|
|
|
|
|
|
/* Avoid toSource bloat and fallibility for object types. */
|
|
|
|
if (JSVAL_IS_PRIMITIVE(v)) {
|
2010-07-14 23:19:36 -07:00
|
|
|
str = js_ValueToSource(cx, Valueify(v));
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (VALUE_IS_FUNCTION(cx, v)) {
|
|
|
|
/*
|
|
|
|
* XXX Avoid function decompilation bloat for now.
|
|
|
|
*/
|
|
|
|
str = JS_GetFunctionId(JS_ValueToFunction(cx, v));
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!str && !(str = js_ValueToSource(cx, Valueify(v)))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* 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]",
|
2010-04-07 16:09:49 -07:00
|
|
|
JSVAL_TO_OBJECT(v)->getClass()->name);
|
2007-03-22 10:30:00 -07:00
|
|
|
str = JS_NewStringCopyZ(cx, buf);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSString *
|
|
|
|
StackTraceToString(JSContext *cx, JSExnPrivate *priv)
|
|
|
|
{
|
|
|
|
jschar *stackbuf;
|
|
|
|
size_t stacklen, stackmax;
|
|
|
|
JSStackTraceElem *elem, *endElem;
|
|
|
|
jsval *values;
|
|
|
|
size_t i;
|
|
|
|
JSString *str;
|
|
|
|
const char *cp;
|
|
|
|
char ulnbuf[11];
|
|
|
|
|
|
|
|
/* After this point, failing control flow must goto bad. */
|
|
|
|
stackbuf = NULL;
|
|
|
|
stacklen = stackmax = 0;
|
|
|
|
|
|
|
|
/* Limit the stackbuf length to a reasonable value to avoid overflow checks. */
|
|
|
|
#define STACK_LENGTH_LIMIT JS_BIT(20)
|
|
|
|
|
|
|
|
#define APPEND_CHAR_TO_STACK(c) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
if (stacklen == stackmax) { \
|
|
|
|
void *ptr_; \
|
|
|
|
if (stackmax >= STACK_LENGTH_LIMIT) \
|
|
|
|
goto done; \
|
|
|
|
stackmax = stackmax ? 2 * stackmax : 64; \
|
2009-07-27 21:10:12 -07:00
|
|
|
ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ptr_) \
|
|
|
|
goto bad; \
|
2007-07-05 13:37:47 -07:00
|
|
|
stackbuf = (jschar *) ptr_; \
|
2007-03-22 10:30:00 -07:00
|
|
|
} \
|
|
|
|
stackbuf[stacklen++] = (c); \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
|
|
|
#define APPEND_STRING_TO_STACK(str) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
JSString *str_ = str; \
|
2009-06-10 18:29:44 -07:00
|
|
|
const jschar *chars_; \
|
2007-08-15 23:23:06 -07:00
|
|
|
size_t length_; \
|
|
|
|
\
|
2009-06-10 18:29:44 -07:00
|
|
|
str_->getCharsAndLength(chars_, length_); \
|
2007-03-22 10:30:00 -07:00
|
|
|
if (length_ > stackmax - stacklen) { \
|
|
|
|
void *ptr_; \
|
|
|
|
if (stackmax >= STACK_LENGTH_LIMIT || \
|
|
|
|
length_ >= STACK_LENGTH_LIMIT - stacklen) { \
|
|
|
|
goto done; \
|
|
|
|
} \
|
|
|
|
stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \
|
2009-07-27 21:10:12 -07:00
|
|
|
ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ptr_) \
|
|
|
|
goto bad; \
|
2007-07-05 13:37:47 -07:00
|
|
|
stackbuf = (jschar *) ptr_; \
|
2007-03-22 10:30:00 -07:00
|
|
|
} \
|
2007-08-15 23:23:06 -07:00
|
|
|
js_strncpy(stackbuf + stacklen, chars_, length_); \
|
2007-03-22 10:30:00 -07:00
|
|
|
stacklen += length_; \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
|
|
|
values = GetStackTraceValueBuffer(priv);
|
|
|
|
elem = priv->stackElems;
|
|
|
|
for (endElem = elem + priv->stackDepth; elem != endElem; elem++) {
|
|
|
|
if (elem->funName) {
|
|
|
|
APPEND_STRING_TO_STACK(elem->funName);
|
|
|
|
APPEND_CHAR_TO_STACK('(');
|
|
|
|
for (i = 0; i != elem->argc; i++, values++) {
|
|
|
|
if (i > 0)
|
|
|
|
APPEND_CHAR_TO_STACK(',');
|
|
|
|
str = ValueToShortSource(cx, *values);
|
|
|
|
if (!str)
|
|
|
|
goto bad;
|
|
|
|
APPEND_STRING_TO_STACK(str);
|
|
|
|
}
|
|
|
|
APPEND_CHAR_TO_STACK(')');
|
|
|
|
}
|
|
|
|
APPEND_CHAR_TO_STACK('@');
|
|
|
|
if (elem->filename) {
|
|
|
|
for (cp = elem->filename; *cp; cp++)
|
|
|
|
APPEND_CHAR_TO_STACK(*cp);
|
|
|
|
}
|
|
|
|
APPEND_CHAR_TO_STACK(':');
|
|
|
|
JS_snprintf(ulnbuf, sizeof ulnbuf, "%u", elem->ulineno);
|
|
|
|
for (cp = ulnbuf; *cp; cp++)
|
|
|
|
APPEND_CHAR_TO_STACK(*cp);
|
|
|
|
APPEND_CHAR_TO_STACK('\n');
|
|
|
|
}
|
|
|
|
#undef APPEND_CHAR_TO_STACK
|
|
|
|
#undef APPEND_STRING_TO_STACK
|
|
|
|
#undef STACK_LENGTH_LIMIT
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (stacklen == 0) {
|
|
|
|
JS_ASSERT(!stackbuf);
|
|
|
|
return cx->runtime->emptyString;
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
2009-07-27 21:10:12 -07:00
|
|
|
void *shrunk = cx->realloc(stackbuf, (stacklen+1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (shrunk)
|
2007-07-05 13:37:47 -07:00
|
|
|
stackbuf = (jschar *) shrunk;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
stackbuf[stacklen] = 0;
|
2007-08-15 23:23:06 -07:00
|
|
|
str = js_NewString(cx, stackbuf, stacklen);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (str)
|
|
|
|
return str;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
if (stackbuf)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(stackbuf);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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(cx, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
Exception(JSContext *cx, JSObject *obj, uintN argc, Value *argv, Value *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSString *message, *filename;
|
|
|
|
JSStackFrame *fp;
|
|
|
|
|
2008-12-09 08:38:32 -08:00
|
|
|
if (!JS_IsConstructing(cx)) {
|
2007-03-22 10:30:00 -07: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
|
2010-06-18 17:43:02 -07:00
|
|
|
* NewNativeClassInstance to find the class prototype, we must get the
|
|
|
|
* class prototype ourselves.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!argv[-2].toObject().getProperty(cx,
|
|
|
|
ATOM_TO_JSID(cx->runtime->atomState
|
|
|
|
.classPrototypeAtom),
|
|
|
|
rval)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2009-08-11 13:05:44 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *errProto = &rval->toObject();
|
2010-06-18 17:43:02 -07:00
|
|
|
obj = NewNativeClassInstance(cx, &js_ErrorClass, errProto, errProto->getParent());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
rval->setObject(*obj);
|
2007-03-22 10:30:00 -07: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.
|
|
|
|
*/
|
2010-04-07 16:09:49 -07:00
|
|
|
if (obj->getClass() == &js_ErrorClass)
|
2009-08-04 14:06:55 -07:00
|
|
|
obj->setPrivate(NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Set the 'message' property. */
|
|
|
|
if (argc != 0) {
|
|
|
|
message = js_ValueToString(cx, argv[0]);
|
|
|
|
if (!message)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
argv[0].setString(message);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
message = cx->runtime->emptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the 'fileName' property. */
|
|
|
|
if (argc > 1) {
|
|
|
|
filename = js_ValueToString(cx, argv[1]);
|
|
|
|
if (!filename)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
argv[1].setString(filename);
|
2007-03-22 10:30:00 -07:00
|
|
|
fp = NULL;
|
|
|
|
} else {
|
2008-12-09 08:38:32 -08:00
|
|
|
fp = js_GetScriptedCaller(cx, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (fp) {
|
2010-08-15 05:44:51 -07:00
|
|
|
filename = FilenameToString(cx, fp->getScript()->filename);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!filename)
|
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
filename = cx->runtime->emptyString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the 'lineNumber' property. */
|
2010-04-07 13:18:50 -07:00
|
|
|
uint32_t lineno;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (argc > 2) {
|
2010-04-07 13:18:50 -07:00
|
|
|
if (!ValueToECMAUint32(cx, argv[2], &lineno))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
if (!fp)
|
2008-12-09 08:38:32 -08:00
|
|
|
fp = js_GetScriptedCaller(cx, NULL);
|
2010-03-03 18:10:13 -08:00
|
|
|
lineno = (fp && fp->pc(cx)) ? js_FramePCToLineNumber(cx, fp) : 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-04-07 16:09:49 -07:00
|
|
|
return (obj->getClass() != &js_ErrorClass) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
InitExnPrivate(cx, obj, message, filename, lineno, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert to string.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* number information along with this message.
|
|
|
|
*/
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
exn_toString(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
jsval v;
|
|
|
|
JSString *name, *message, *result;
|
|
|
|
jschar *chars, *cp;
|
|
|
|
size_t name_length, message_length, length;
|
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!obj || !obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), Valueify(&v)))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
name = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) : cx->runtime->emptyString;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(name);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!JS_GetProperty(cx, obj, js_message_str, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
message = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v)
|
|
|
|
: cx->runtime->emptyString;
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
if (message->length() != 0) {
|
|
|
|
name_length = name->length();
|
|
|
|
message_length = message->length();
|
2007-03-22 10:30:00 -07:00
|
|
|
length = (name_length ? name_length + 2 : 0) + message_length;
|
2009-07-27 21:10:12 -07:00
|
|
|
cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!chars)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (name_length) {
|
2009-06-10 18:29:44 -07:00
|
|
|
js_strncpy(cp, name->chars(), name_length);
|
2007-03-22 10:30:00 -07:00
|
|
|
cp += name_length;
|
|
|
|
*cp++ = ':'; *cp++ = ' ';
|
|
|
|
}
|
2009-06-10 18:29:44 -07:00
|
|
|
js_strncpy(cp, message->chars(), message_length);
|
2007-03-22 10:30:00 -07:00
|
|
|
cp += message_length;
|
|
|
|
*cp = 0;
|
|
|
|
|
2007-08-15 23:23:06 -07:00
|
|
|
result = js_NewString(cx, chars, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!result) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(chars);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = name;
|
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(result);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
/*
|
|
|
|
* Return a string that may eval to something similar to the original object.
|
|
|
|
*/
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
exn_toSource(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *name, *message, *filename, *lineno_as_str, *result;
|
2007-10-13 13:09:48 -07:00
|
|
|
jsval localroots[3] = {JSVAL_NULL, JSVAL_NULL, JSVAL_NULL};
|
2007-03-22 10:30:00 -07:00
|
|
|
size_t lineno_length, name_length, message_length, filename_length, length;
|
|
|
|
jschar *chars, *cp;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!obj || !obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), Valueify(vp)))
|
2010-01-11 09:52:21 -08:00
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
name = js_ValueToString(cx, Valueify(*vp));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!name)
|
2010-01-11 09:52:21 -08:00
|
|
|
return false;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(name);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(localroots), Valueify(localroots));
|
2007-10-13 13:09:48 -07:00
|
|
|
|
2008-08-12 14:50:42 -07:00
|
|
|
#ifdef __GNUC__
|
2010-01-11 09:52:21 -08:00
|
|
|
message = filename = NULL;
|
2008-08-12 14:50:42 -07:00
|
|
|
#endif
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, js_message_str, &localroots[0]) ||
|
2010-07-14 23:19:36 -07:00
|
|
|
!(message = js_ValueToSource(cx, Valueify(localroots[0])))) {
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-01-11 09:52:21 -08:00
|
|
|
localroots[0] = STRING_TO_JSVAL(message);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, js_fileName_str, &localroots[1]) ||
|
2010-07-14 23:19:36 -07:00
|
|
|
!(filename = js_ValueToSource(cx, Valueify(localroots[1])))) {
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-01-11 09:52:21 -08:00
|
|
|
localroots[1] = STRING_TO_JSVAL(filename);
|
|
|
|
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, js_lineNumber_str, &localroots[2]))
|
|
|
|
return false;
|
2010-04-07 13:18:50 -07:00
|
|
|
uint32_t lineno;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToECMAUint32(cx, Valueify(localroots[2]), &lineno))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2010-01-11 09:52:21 -08:00
|
|
|
|
|
|
|
if (lineno != 0) {
|
2010-07-14 23:19:36 -07:00
|
|
|
lineno_as_str = js_ValueToString(cx, Valueify(localroots[2]));
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!lineno_as_str)
|
|
|
|
return false;
|
2010-01-11 09:52:21 -08:00
|
|
|
lineno_length = lineno_as_str->length();
|
|
|
|
} else {
|
|
|
|
lineno_as_str = NULL;
|
|
|
|
lineno_length = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
/* Magic 8, for the characters in ``(new ())''. */
|
|
|
|
name_length = name->length();
|
|
|
|
message_length = message->length();
|
|
|
|
length = 8 + name_length + message_length;
|
|
|
|
|
|
|
|
filename_length = filename->length();
|
|
|
|
if (filename_length != 0) {
|
|
|
|
/* append filename as ``, {filename}'' */
|
|
|
|
length += 2 + filename_length;
|
|
|
|
if (lineno_as_str) {
|
|
|
|
/* append lineno as ``, {lineno_as_str}'' */
|
|
|
|
length += 2 + lineno_length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (lineno_as_str) {
|
|
|
|
/*
|
|
|
|
* no filename, but have line number,
|
|
|
|
* need to append ``, "", {lineno_as_str}''
|
|
|
|
*/
|
|
|
|
length += 6 + lineno_length;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar));
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!chars)
|
|
|
|
return false;
|
2010-01-11 09:52:21 -08:00
|
|
|
|
|
|
|
*cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' ';
|
|
|
|
js_strncpy(cp, name->chars(), name_length);
|
|
|
|
cp += name_length;
|
|
|
|
*cp++ = '(';
|
|
|
|
if (message_length != 0) {
|
|
|
|
js_strncpy(cp, message->chars(), message_length);
|
|
|
|
cp += message_length;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
if (filename_length != 0) {
|
|
|
|
/* append filename as ``, {filename}'' */
|
|
|
|
*cp++ = ','; *cp++ = ' ';
|
|
|
|
js_strncpy(cp, filename->chars(), filename_length);
|
|
|
|
cp += filename_length;
|
|
|
|
} else {
|
|
|
|
if (lineno_as_str) {
|
|
|
|
/*
|
|
|
|
* no filename, but have line number,
|
|
|
|
* need to append ``, "", {lineno_as_str}''
|
|
|
|
*/
|
|
|
|
*cp++ = ','; *cp++ = ' '; *cp++ = '"'; *cp++ = '"';
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (lineno_as_str) {
|
2010-01-11 09:52:21 -08:00
|
|
|
/* append lineno as ``, {lineno_as_str}'' */
|
|
|
|
*cp++ = ','; *cp++ = ' ';
|
|
|
|
js_strncpy(cp, lineno_as_str->chars(), lineno_length);
|
|
|
|
cp += lineno_length;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
*cp++ = ')'; *cp++ = ')'; *cp = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
result = js_NewString(cx, chars, length);
|
|
|
|
if (!result) {
|
|
|
|
cx->free(chars);
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2010-01-11 09:52:21 -08:00
|
|
|
}
|
|
|
|
*vp = STRING_TO_JSVAL(result);
|
2010-03-28 22:05:50 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static JSFunctionSpec exception_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toSource_str, exn_toSource, 0,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toString_str, exn_toString, 0,0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2008-12-17 11:31:24 -08:00
|
|
|
/* JSProto_ ordering for exceptions shall match JSEXN_ constants. */
|
|
|
|
JS_STATIC_ASSERT(JSEXN_ERR == 0);
|
|
|
|
JS_STATIC_ASSERT(JSProto_Error + JSEXN_INTERNALERR == JSProto_InternalError);
|
|
|
|
JS_STATIC_ASSERT(JSProto_Error + JSEXN_EVALERR == JSProto_EvalError);
|
|
|
|
JS_STATIC_ASSERT(JSProto_Error + JSEXN_RANGEERR == JSProto_RangeError);
|
|
|
|
JS_STATIC_ASSERT(JSProto_Error + JSEXN_REFERENCEERR == JSProto_ReferenceError);
|
|
|
|
JS_STATIC_ASSERT(JSProto_Error + JSEXN_SYNTAXERR == JSProto_SyntaxError);
|
|
|
|
JS_STATIC_ASSERT(JSProto_Error + JSEXN_TYPEERR == JSProto_TypeError);
|
|
|
|
JS_STATIC_ASSERT(JSProto_Error + JSEXN_URIERR == JSProto_URIError);
|
|
|
|
|
|
|
|
static JS_INLINE JSProtoKey
|
|
|
|
GetExceptionProtoKey(intN exn)
|
|
|
|
{
|
|
|
|
JS_ASSERT(JSEXN_ERR <= exn);
|
|
|
|
JS_ASSERT(exn < JSEXN_LIMIT);
|
|
|
|
return (JSProtoKey) (JSProto_Error + exn);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *
|
|
|
|
js_InitExceptionClasses(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2008-12-17 11:31:24 -08:00
|
|
|
jsval roots[3];
|
|
|
|
JSObject *obj_proto, *error_proto;
|
2007-03-22 10:30:00 -07: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
|
2010-08-09 09:11:22 -07:00
|
|
|
* NewNonFunction below, when called for the Error superclass. We need to
|
2007-03-22 10:30:00 -07:00
|
|
|
* ensure that Object.prototype is the proto of Error.prototype.
|
|
|
|
*
|
|
|
|
* See the equivalent code to ensure that parent_proto is non-null when
|
2010-08-09 09:11:22 -07:00
|
|
|
* js_InitClass calls NewObject, in jsobj.cpp.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-02-19 09:44:23 -08:00
|
|
|
if (!js_GetClassPrototype(cx, obj, JSProto_Object, &obj_proto))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-03-10 15:34:12 -08:00
|
|
|
PodArrayZero(roots);
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(roots), Valueify(roots));
|
2008-12-17 11:31:24 -08:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
error_proto = NULL; /* quell GCC overwarning */
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-26 14:08:58 -07:00
|
|
|
jsval empty = STRING_TO_JSVAL(cx->runtime->emptyString);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Initialize the prototypes first. */
|
2008-12-17 11:31:24 -08:00
|
|
|
for (intN i = JSEXN_ERR; i != JSEXN_LIMIT; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Make the prototype for the current constructor name. */
|
2010-06-26 14:08:58 -07:00
|
|
|
JSObject *proto =
|
2010-08-09 09:11:22 -07:00
|
|
|
NewNonFunction<WithProto::Class>(cx, &js_ErrorClass, (i != JSEXN_ERR) ? error_proto : obj_proto, obj);
|
2008-12-17 11:31:24 -08:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
if (i == JSEXN_ERR) {
|
|
|
|
error_proto = proto;
|
|
|
|
roots[0] = OBJECT_TO_JSVAL(proto);
|
|
|
|
} else {
|
|
|
|
// We cannot share the root for error_proto and other prototypes
|
|
|
|
// as error_proto must be rooted until the function returns.
|
|
|
|
roots[1] = OBJECT_TO_JSVAL(proto);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* So exn_finalize knows whether to destroy private data. */
|
2009-08-04 14:06:55 -07:00
|
|
|
proto->setPrivate(NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Make a constructor function for the current name. */
|
2010-06-26 14:08:58 -07:00
|
|
|
JSProtoKey protoKey = GetExceptionProtoKey(i);
|
|
|
|
JSAtom *atom = cx->runtime->atomState.classAtoms[protoKey];
|
|
|
|
JSFunction *fun = js_DefineFunction(cx, obj, atom, Exception, 3, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fun)
|
2008-12-17 11:31:24 -08:00
|
|
|
return NULL;
|
|
|
|
roots[2] = OBJECT_TO_JSVAL(FUN_OBJECT(fun));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Make this constructor make objects of class Exception. */
|
2008-10-08 15:08:33 -07:00
|
|
|
FUN_CLASP(fun) = &js_ErrorClass;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Make the prototype and constructor links. */
|
2008-12-17 11:31:24 -08:00
|
|
|
if (!js_SetClassPrototype(cx, FUN_OBJECT(fun), proto,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT)) {
|
2008-12-17 11:31:24 -08:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the name property to the prototype. */
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JS_DefineProperty(cx, proto, js_name_str,
|
|
|
|
STRING_TO_JSVAL(ATOM_TO_STRING(atom)),
|
2008-12-17 11:31:24 -08:00
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, stash the constructor for later uses. */
|
2010-06-02 20:30:45 -07:00
|
|
|
if (!js_SetClassObject(cx, obj, protoKey, FUN_OBJECT(fun), proto))
|
2008-12-17 11:31:24 -08:00
|
|
|
return NULL;
|
2010-06-26 14:08:58 -07:00
|
|
|
|
|
|
|
/* Set default values. */
|
|
|
|
if (!JS_DefineProperty(cx, proto, js_message_str, empty, NULL, NULL, JSPROP_ENUMERATE) ||
|
|
|
|
!JS_DefineProperty(cx, proto, js_fileName_str, empty, NULL, NULL, JSPROP_ENUMERATE) ||
|
|
|
|
!JS_DefineProperty(cx, proto, js_lineNumber_str, JSVAL_ZERO, NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-26 14:08:58 -07:00
|
|
|
if (!JS_DefineFunctions(cx, error_proto, exception_methods))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
|
2008-12-17 11:31:24 -08:00
|
|
|
return error_proto;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const JSErrorFormatString*
|
2007-05-09 13:19:26 -07:00
|
|
|
js_GetLocalizedErrorMessage(JSContext* cx, void *userRef, const char *locale,
|
|
|
|
const uintN errorNumber)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
const JSErrorFormatString *errorString = NULL;
|
|
|
|
|
|
|
|
if (cx->localeCallbacks && cx->localeCallbacks->localeGetErrorMessage) {
|
|
|
|
errorString = cx->localeCallbacks
|
|
|
|
->localeGetErrorMessage(userRef, locale, errorNumber);
|
|
|
|
}
|
|
|
|
if (!errorString)
|
|
|
|
errorString = js_GetErrorMessage(userRef, locale, errorNumber);
|
|
|
|
return errorString;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
2009-12-15 14:20:48 -08:00
|
|
|
js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp,
|
|
|
|
JSErrorCallback callback, void *userRef)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSErrNum errorNumber;
|
|
|
|
const JSErrorFormatString *errorString;
|
|
|
|
JSExnType exn;
|
|
|
|
jsval tv[4];
|
|
|
|
JSBool ok;
|
|
|
|
JSObject *errProto, *errObject;
|
|
|
|
JSString *messageStr, *filenameStr;
|
|
|
|
|
|
|
|
/*
|
2008-09-08 02:47:12 -07:00
|
|
|
* Tell our caller to report immediately if this report is just a warning.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
JS_ASSERT(reportp);
|
2008-09-08 02:47:12 -07:00
|
|
|
if (JSREPORT_IS_WARNING(reportp->flags))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* Find the exception index associated with this error. */
|
|
|
|
errorNumber = (JSErrNum) reportp->errorNumber;
|
2009-12-15 14:20:48 -08:00
|
|
|
if (!callback || callback == js_GetErrorMessage)
|
|
|
|
errorString = js_GetLocalizedErrorMessage(cx, NULL, NULL, errorNumber);
|
|
|
|
else
|
|
|
|
errorString = callback(userRef, NULL, errorNumber);
|
2007-07-05 13:37:47 -07:00
|
|
|
exn = errorString ? (JSExnType) errorString->exnType : JSEXN_NONE;
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prevent runaway recursion, via cx->generatingError. 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->generatingError)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2008-09-08 10:51:10 -07:00
|
|
|
MUST_FLOW_THROUGH("out");
|
2007-03-22 10:30:00 -07:00
|
|
|
cx->generatingError = JS_TRUE;
|
|
|
|
|
|
|
|
/* Protect the newly-created strings below from nesting GCs. */
|
2010-03-10 15:34:12 -08:00
|
|
|
PodArrayZero(tv);
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(tv), Valueify(tv));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to get an appropriate prototype by looking up the corresponding
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-02-19 09:44:23 -08:00
|
|
|
ok = js_GetClassPrototype(cx, NULL, GetExceptionProtoKey(exn), &errProto);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
tv[0] = OBJECT_TO_JSVAL(errProto);
|
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
errObject = NewNativeClassInstance(cx, &js_ErrorClass, errProto, errProto->getParent());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!errObject) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
tv[1] = OBJECT_TO_JSVAL(errObject);
|
|
|
|
|
|
|
|
messageStr = JS_NewStringCopyZ(cx, message);
|
|
|
|
if (!messageStr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
tv[2] = STRING_TO_JSVAL(messageStr);
|
|
|
|
|
|
|
|
filenameStr = JS_NewStringCopyZ(cx, reportp->filename);
|
|
|
|
if (!filenameStr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
tv[3] = STRING_TO_JSVAL(filenameStr);
|
|
|
|
|
|
|
|
ok = InitExnPrivate(cx, errObject, messageStr, filenameStr,
|
|
|
|
reportp->lineno, reportp);
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
JS_SetPendingException(cx, OBJECT_TO_JSVAL(errObject));
|
|
|
|
|
|
|
|
/* Flag the error report passed in to indicate an exception was raised. */
|
|
|
|
reportp->flags |= JSREPORT_EXCEPTION;
|
|
|
|
|
|
|
|
out:
|
|
|
|
cx->generatingError = JS_FALSE;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_ReportUncaughtException(JSContext *cx)
|
|
|
|
{
|
|
|
|
jsval exn;
|
|
|
|
JSObject *exnObject;
|
2007-08-01 21:33:52 -07:00
|
|
|
jsval roots[5];
|
2007-03-22 10:30:00 -07:00
|
|
|
JSErrorReport *reportp, report;
|
|
|
|
JSString *str;
|
|
|
|
const char *bytes;
|
|
|
|
|
|
|
|
if (!JS_IsExceptionPending(cx))
|
2010-01-11 09:52:21 -08:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!JS_GetPendingException(cx, &exn))
|
2010-01-11 09:52:21 -08:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-10 15:34:12 -08:00
|
|
|
PodArrayZero(roots);
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(roots), Valueify(roots));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Because js_ValueToString below could error and an exception object
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (JSVAL_IS_PRIMITIVE(exn)) {
|
|
|
|
exnObject = NULL;
|
|
|
|
} else {
|
|
|
|
exnObject = JSVAL_TO_OBJECT(exn);
|
2007-08-01 21:33:52 -07:00
|
|
|
roots[0] = exn;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
reportp = js_ErrorFromException(cx, exn);
|
|
|
|
|
|
|
|
/* XXX L10N angels cry once again (see also jsemit.c, /L10N gaffes/) */
|
2010-07-14 23:19:36 -07:00
|
|
|
str = js_ValueToString(cx, Valueify(exn));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str) {
|
|
|
|
bytes = "unknown (can't convert to string)";
|
|
|
|
} else {
|
2007-08-01 21:33:52 -07:00
|
|
|
roots[1] = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
bytes = js_GetStringBytes(cx, str);
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!bytes)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-26 21:53:40 -07:00
|
|
|
if (!reportp && exnObject && exnObject->getClass() == &js_ErrorClass) {
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *filename;
|
|
|
|
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!JS_GetProperty(cx, exnObject, js_message_str, &roots[2]))
|
|
|
|
return false;
|
2007-08-01 21:33:52 -07:00
|
|
|
if (JSVAL_IS_STRING(roots[2])) {
|
|
|
|
bytes = js_GetStringBytes(cx, JSVAL_TO_STRING(roots[2]));
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!bytes)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!JS_GetProperty(cx, exnObject, js_fileName_str, &roots[3]))
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
str = js_ValueToString(cx, Valueify(roots[3]));
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!str)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
filename = StringToFilename(cx, str);
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!filename)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-28 22:05:50 -07:00
|
|
|
if (!JS_GetProperty(cx, exnObject, js_lineNumber_str, &roots[4]))
|
|
|
|
return false;
|
2010-04-07 13:18:50 -07:00
|
|
|
uint32_t lineno;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToECMAUint32 (cx, Valueify(roots[4]), &lineno))
|
2010-03-28 22:05:50 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
reportp = &report;
|
2010-03-10 15:34:12 -08:00
|
|
|
PodZero(&report);
|
2007-03-22 10:30:00 -07:00
|
|
|
report.filename = filename;
|
|
|
|
report.lineno = (uintN) lineno;
|
2010-04-08 15:57:19 -07:00
|
|
|
if (JSVAL_IS_STRING(roots[2])) {
|
|
|
|
report.ucmessage = js_GetStringChars(cx, JSVAL_TO_STRING(roots[2]));
|
|
|
|
if (!report.ucmessage)
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!reportp) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_UNCAUGHT_EXCEPTION, bytes);
|
|
|
|
} else {
|
|
|
|
/* Flag the error as an exception. */
|
|
|
|
reportp->flags |= JSREPORT_EXCEPTION;
|
2008-03-01 14:11:27 -08:00
|
|
|
|
|
|
|
/* Pass the exception object. */
|
|
|
|
JS_SetPendingException(cx, exn);
|
2007-03-22 10:30:00 -07:00
|
|
|
js_ReportErrorAgain(cx, bytes, reportp);
|
2008-03-01 14:11:27 -08:00
|
|
|
JS_ClearPendingException(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-28 22:05:50 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|