1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Communicator client code, released
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * JS standard exception implementation.
53 #include "jsversion.h"
62 #include "jsstaticcheck.h"
63 #include "jswrapper.h"
65 #include "jscntxtinlines.h"
66 #include "jsinterpinlines.h"
67 #include "jsobjinlines.h"
70 using namespace js::gc;
72 /* Forward declarations for js_ErrorClass's initializer. */
74 Exception(JSContext *cx, uintN argc, Value *vp);
77 exn_trace(JSTracer *trc, JSObject *obj);
80 exn_finalize(JSContext *cx, JSObject *obj);
83 exn_enumerate(JSContext *cx, JSObject *obj);
86 exn_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
89 Class js_ErrorClass = {
91 JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_MARK_IS_TRACE |
92 JSCLASS_HAS_CACHED_PROTO(JSProto_Error),
93 PropertyStub, /* addProperty */
94 PropertyStub, /* delProperty */
95 PropertyStub, /* getProperty */
96 StrictPropertyStub, /* setProperty */
98 (JSResolveOp)exn_resolve,
101 NULL, /* reserved0 */
102 NULL, /* checkAccess */
104 NULL, /* construct */
105 NULL, /* xdrObject */
106 NULL, /* hasInstance */
107 JS_CLASS_TRACE(exn_trace)
110 typedef struct JSStackTraceElem {
113 const char *filename;
117 typedef struct JSExnPrivate {
118 /* A copy of the JSErrorReport originally generated. */
119 JSErrorReport *errorReport;
124 JSStackTraceElem stackElems[1];
128 StackTraceToString(JSContext *cx, JSExnPrivate *priv);
130 static JSErrorReport *
131 CopyErrorReport(JSContext *cx, JSErrorReport *report)
134 * We use a single malloc block to make a deep copy of JSErrorReport with
135 * the following layout:
137 * array of copies of report->messageArgs
138 * jschar array with characters for all messageArgs
139 * jschar array with characters for ucmessage
140 * jschar array with characters for uclinebuf and uctokenptr
141 * char array with characters for linebuf and tokenptr
142 * char array with characters for filename
143 * Such layout together with the properties enforced by the following
144 * asserts does not need any extra alignment padding.
146 JS_STATIC_ASSERT(sizeof(JSErrorReport) % sizeof(const char *) == 0);
147 JS_STATIC_ASSERT(sizeof(const char *) % sizeof(jschar) == 0);
151 size_t uclinebufSize;
152 size_t ucmessageSize;
153 size_t i, argsArraySize, argsCopySize, argSize;
158 #define JS_CHARS_SIZE(jschars) ((js_strlen(jschars) + 1) * sizeof(jschar))
160 filenameSize = report->filename ? strlen(report->filename) + 1 : 0;
161 linebufSize = report->linebuf ? strlen(report->linebuf) + 1 : 0;
162 uclinebufSize = report->uclinebuf ? JS_CHARS_SIZE(report->uclinebuf) : 0;
166 if (report->ucmessage) {
167 ucmessageSize = JS_CHARS_SIZE(report->ucmessage);
168 if (report->messageArgs) {
169 for (i = 0; report->messageArgs[i]; ++i)
170 argsCopySize += JS_CHARS_SIZE(report->messageArgs[i]);
172 /* Non-null messageArgs should have at least one non-null arg. */
174 argsArraySize = (i + 1) * sizeof(const jschar *);
179 * The mallocSize can not overflow since it represents the sum of the
180 * sizes of already allocated objects.
182 mallocSize = sizeof(JSErrorReport) + argsArraySize + argsCopySize +
183 ucmessageSize + uclinebufSize + linebufSize + filenameSize;
184 cursor = (uint8 *)cx->malloc(mallocSize);
188 copy = (JSErrorReport *)cursor;
189 memset(cursor, 0, sizeof(JSErrorReport));
190 cursor += sizeof(JSErrorReport);
192 if (argsArraySize != 0) {
193 copy->messageArgs = (const jschar **)cursor;
194 cursor += argsArraySize;
195 for (i = 0; report->messageArgs[i]; ++i) {
196 copy->messageArgs[i] = (const jschar *)cursor;
197 argSize = JS_CHARS_SIZE(report->messageArgs[i]);
198 memcpy(cursor, report->messageArgs[i], argSize);
201 copy->messageArgs[i] = NULL;
202 JS_ASSERT(cursor == (uint8 *)copy->messageArgs[0] + argsCopySize);
205 if (report->ucmessage) {
206 copy->ucmessage = (const jschar *)cursor;
207 memcpy(cursor, report->ucmessage, ucmessageSize);
208 cursor += ucmessageSize;
211 if (report->uclinebuf) {
212 copy->uclinebuf = (const jschar *)cursor;
213 memcpy(cursor, report->uclinebuf, uclinebufSize);
214 cursor += uclinebufSize;
215 if (report->uctokenptr) {
216 copy->uctokenptr = copy->uclinebuf + (report->uctokenptr -
221 if (report->linebuf) {
222 copy->linebuf = (const char *)cursor;
223 memcpy(cursor, report->linebuf, linebufSize);
224 cursor += linebufSize;
225 if (report->tokenptr) {
226 copy->tokenptr = copy->linebuf + (report->tokenptr -
231 if (report->filename) {
232 copy->filename = (const char *)cursor;
233 memcpy(cursor, report->filename, filenameSize);
235 JS_ASSERT(cursor + filenameSize == (uint8 *)copy + mallocSize);
237 /* Copy non-pointer members. */
238 copy->lineno = report->lineno;
239 copy->errorNumber = report->errorNumber;
241 /* Note that this is before it gets flagged with JSREPORT_EXCEPTION */
242 copy->flags = report->flags;
249 GetStackTraceValueBuffer(JSExnPrivate *priv)
252 * We use extra memory after JSExnPrivateInfo.stackElems to store jsvals
253 * that helps to produce more informative stack traces. The following
254 * assert allows us to assume that no gap after stackElems is necessary to
255 * align the buffer properly.
257 JS_STATIC_ASSERT(sizeof(JSStackTraceElem) % sizeof(jsval) == 0);
259 return (jsval *)(priv->stackElems + priv->stackDepth);
263 InitExnPrivate(JSContext *cx, JSObject *exnObject, JSString *message,
264 JSString *filename, uintN lineno, JSErrorReport *report)
266 JSSecurityCallbacks *callbacks;
267 CheckAccessOp checkAccess;
268 JSErrorReporter older;
269 JSExceptionState *state;
271 JSStackFrame *fp, *fpstop;
272 size_t stackDepth, valueCount, size;
275 JSStackTraceElem *elem;
278 JS_ASSERT(exnObject->getClass() == &js_ErrorClass);
281 * Prepare stack trace data.
283 * Set aside any error reporter for cx and save its exception state
284 * so we can suppress any checkAccess failures. Such failures should stop
285 * the backtrace procedure, not result in a failure of this constructor.
287 callbacks = JS_GetSecurityCallbacks(cx);
288 checkAccess = callbacks
289 ? Valueify(callbacks->checkObjectAccess)
291 older = JS_SetErrorReporter(cx, NULL);
292 state = JS_SaveExceptionState(cx);
294 callerid = ATOM_TO_JSID(cx->runtime->atomState.callerAtom);
297 for (fp = js_GetTopStackFrame(cx); fp; fp = fp->prev()) {
298 if (fp->isFunctionFrame() && !fp->isEvalFrame()) {
299 Value v = NullValue();
301 !checkAccess(cx, &fp->callee(), callerid, JSACC_READ, &v)) {
304 valueCount += fp->numActualArgs();
308 JS_RestoreExceptionState(cx, state);
309 JS_SetErrorReporter(cx, older);
312 size = offsetof(JSExnPrivate, stackElems);
313 overflow = (stackDepth > ((size_t)-1 - size) / sizeof(JSStackTraceElem));
314 size += stackDepth * sizeof(JSStackTraceElem);
315 overflow |= (valueCount > ((size_t)-1 - size) / sizeof(jsval));
316 size += valueCount * sizeof(jsval);
318 js_ReportAllocationOverflow(cx);
321 priv = (JSExnPrivate *)cx->malloc(size);
326 * We initialize errorReport with a copy of report after setting the
327 * private slot, to prevent GC accessing a junk value we clear the field
330 priv->errorReport = NULL;
331 priv->message = message;
332 priv->filename = filename;
333 priv->lineno = lineno;
334 priv->stackDepth = stackDepth;
336 values = GetStackTraceValueBuffer(priv);
337 elem = priv->stackElems;
338 for (fp = js_GetTopStackFrame(cx); fp != fpstop; fp = fp->prev()) {
339 if (!fp->isFunctionFrame() || fp->isEvalFrame()) {
340 elem->funName = NULL;
343 elem->funName = fp->fun()->atom
344 ? ATOM_TO_STRING(fp->fun()->atom)
345 : cx->runtime->emptyString;
346 elem->argc = fp->numActualArgs();
347 fp->forEachCanonicalActualArg(CopyTo(Valueify(values)));
348 values += elem->argc;
351 elem->filename = NULL;
352 if (fp->isScriptFrame()) {
353 elem->filename = fp->script()->filename;
355 elem->ulineno = js_FramePCToLineNumber(cx, fp);
359 JS_ASSERT(priv->stackElems + stackDepth == elem);
360 JS_ASSERT(GetStackTraceValueBuffer(priv) + valueCount == values);
362 exnObject->setPrivate(priv);
366 * Construct a new copy of the error report struct. We can't use the
367 * error report struct that was passed in, because it's allocated on
368 * the stack, and also because it may point to transient data in the
371 priv->errorReport = CopyErrorReport(cx, report);
372 if (!priv->errorReport) {
373 /* The finalizer realeases priv since it is in the private slot. */
381 static inline JSExnPrivate *
382 GetExnPrivate(JSContext *cx, JSObject *obj)
384 return (JSExnPrivate *) obj->getPrivate();
388 exn_trace(JSTracer *trc, JSObject *obj)
391 JSStackTraceElem *elem;
395 priv = GetExnPrivate(trc->context, obj);
398 MarkString(trc, priv->message, "exception message");
400 MarkString(trc, priv->filename, "exception filename");
402 elem = priv->stackElems;
403 for (vcount = i = 0; i != priv->stackDepth; ++i, ++elem) {
405 MarkString(trc, elem->funName, "stack trace function name");
406 if (IS_GC_MARKING_TRACER(trc) && elem->filename)
407 js_MarkScriptFilename(elem->filename);
408 vcount += elem->argc;
410 vp = GetStackTraceValueBuffer(priv);
411 for (i = 0; i != vcount; ++i, ++vp) {
413 JS_CALL_VALUE_TRACER(trc, v, "stack trace argument");
419 exn_finalize(JSContext *cx, JSObject *obj)
423 priv = GetExnPrivate(cx, obj);
425 if (priv->errorReport)
426 cx->free(priv->errorReport);
432 exn_enumerate(JSContext *cx, JSObject *obj)
434 JSAtomState *atomState;
440 JS_STATIC_ASSERT(sizeof(JSAtomState) <= (size_t)(uint16)-1);
441 static const uint16 offsets[] = {
442 (uint16)offsetof(JSAtomState, messageAtom),
443 (uint16)offsetof(JSAtomState, fileNameAtom),
444 (uint16)offsetof(JSAtomState, lineNumberAtom),
445 (uint16)offsetof(JSAtomState, stackAtom),
448 atomState = &cx->runtime->atomState;
449 for (i = 0; i != JS_ARRAY_LENGTH(offsets); ++i) {
450 atom = *(JSAtom **)((uint8 *)atomState + offsets[i]);
451 if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop))
458 exn_resolve(JSContext *cx, JSObject *obj, jsid id, uintN flags,
469 priv = GetExnPrivate(cx, obj);
470 if (priv && JSID_IS_ATOM(id)) {
471 str = JSID_TO_STRING(id);
473 atom = cx->runtime->atomState.messageAtom;
474 if (str == ATOM_TO_STRING(atom)) {
475 prop = js_message_str;
476 v = STRING_TO_JSVAL(priv->message);
480 atom = cx->runtime->atomState.fileNameAtom;
481 if (str == ATOM_TO_STRING(atom)) {
482 prop = js_fileName_str;
483 v = STRING_TO_JSVAL(priv->filename);
487 atom = cx->runtime->atomState.lineNumberAtom;
488 if (str == ATOM_TO_STRING(atom)) {
489 prop = js_lineNumber_str;
490 v = INT_TO_JSVAL(priv->lineno);
494 atom = cx->runtime->atomState.stackAtom;
495 if (str == ATOM_TO_STRING(atom)) {
496 stack = StackTraceToString(cx, priv);
500 /* Allow to GC all things that were used to build stack trace. */
501 priv->stackDepth = 0;
503 v = STRING_TO_JSVAL(stack);
510 if (!JS_DefineProperty(cx, obj, prop, v, NULL, NULL, JSPROP_ENUMERATE))
517 js_ErrorFromException(JSContext *cx, jsval exn)
522 if (JSVAL_IS_PRIMITIVE(exn))
524 obj = JSVAL_TO_OBJECT(exn);
525 if (obj->getClass() != &js_ErrorClass)
527 priv = GetExnPrivate(cx, obj);
530 return priv->errorReport;
534 ValueToShortSource(JSContext *cx, jsval v)
538 /* Avoid toSource bloat and fallibility for object types. */
539 if (JSVAL_IS_PRIMITIVE(v))
540 return js_ValueToSource(cx, Valueify(v));
542 AutoCompartment ac(cx, JSVAL_TO_OBJECT(v));
546 if (VALUE_IS_FUNCTION(cx, v)) {
548 * XXX Avoid function decompilation bloat for now.
550 str = JS_GetFunctionId(JS_ValueToFunction(cx, v));
551 if (!str && !(str = js_ValueToSource(cx, Valueify(v)))) {
553 * Continue to soldier on if the function couldn't be
554 * converted into a string.
556 JS_ClearPendingException(cx);
557 str = JS_NewStringCopyZ(cx, "[unknown function]");
561 * XXX Avoid toString on objects, it takes too long and uses too much
562 * memory, for too many classes (see Mozilla bug 166743).
565 JS_snprintf(buf, sizeof buf, "[object %s]",
566 JSVAL_TO_OBJECT(v)->getClass()->name);
567 str = JS_NewStringCopyZ(cx, buf);
572 if (!str || !cx->compartment->wrap(cx, &str))
578 StackTraceToString(JSContext *cx, JSExnPrivate *priv)
581 size_t stacklen, stackmax;
582 JSStackTraceElem *elem, *endElem;
589 /* After this point, failing control flow must goto bad. */
591 stacklen = stackmax = 0;
593 /* Limit the stackbuf length to a reasonable value to avoid overflow checks. */
594 #define STACK_LENGTH_LIMIT JS_BIT(20)
596 #define APPEND_CHAR_TO_STACK(c) \
598 if (stacklen == stackmax) { \
600 if (stackmax >= STACK_LENGTH_LIMIT) \
602 stackmax = stackmax ? 2 * stackmax : 64; \
603 ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \
606 stackbuf = (jschar *) ptr_; \
608 stackbuf[stacklen++] = (c); \
611 #define APPEND_STRING_TO_STACK(str) \
613 JSString *str_ = str; \
614 size_t length_ = str_->length(); \
615 const jschar *chars_ = str_->getChars(cx); \
619 if (length_ > stackmax - stacklen) { \
621 if (stackmax >= STACK_LENGTH_LIMIT || \
622 length_ >= STACK_LENGTH_LIMIT - stacklen) { \
625 stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \
626 ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \
629 stackbuf = (jschar *) ptr_; \
631 js_strncpy(stackbuf + stacklen, chars_, length_); \
632 stacklen += length_; \
635 values = GetStackTraceValueBuffer(priv);
636 elem = priv->stackElems;
637 for (endElem = elem + priv->stackDepth; elem != endElem; elem++) {
639 APPEND_STRING_TO_STACK(elem->funName);
640 APPEND_CHAR_TO_STACK('(');
641 for (i = 0; i != elem->argc; i++, values++) {
643 APPEND_CHAR_TO_STACK(',');
644 str = ValueToShortSource(cx, *values);
647 APPEND_STRING_TO_STACK(str);
649 APPEND_CHAR_TO_STACK(')');
651 APPEND_CHAR_TO_STACK('@');
652 if (elem->filename) {
653 for (cp = elem->filename; *cp; cp++)
654 APPEND_CHAR_TO_STACK(*cp);
656 APPEND_CHAR_TO_STACK(':');
657 JS_snprintf(ulnbuf, sizeof ulnbuf, "%u", elem->ulineno);
658 for (cp = ulnbuf; *cp; cp++)
659 APPEND_CHAR_TO_STACK(*cp);
660 APPEND_CHAR_TO_STACK('\n');
662 #undef APPEND_CHAR_TO_STACK
663 #undef APPEND_STRING_TO_STACK
664 #undef STACK_LENGTH_LIMIT
668 JS_ASSERT(!stackbuf);
669 return cx->runtime->emptyString;
671 if (stacklen < stackmax) {
673 * Realloc can fail when shrinking on some FreeBSD versions, so
674 * don't use JS_realloc here; simply let the oversized allocation
675 * be owned by the string in that rare case.
677 void *shrunk = cx->realloc(stackbuf, (stacklen+1) * sizeof(jschar));
679 stackbuf = (jschar *) shrunk;
682 stackbuf[stacklen] = 0;
683 str = js_NewString(cx, stackbuf, stacklen);
693 /* XXXbe Consolidate the ugly truth that we don't treat filename as UTF-8
694 with these two functions. */
696 FilenameToString(JSContext *cx, const char *filename)
698 return JS_NewStringCopyZ(cx, filename);
702 Exception(JSContext *cx, uintN argc, Value *vp)
704 JSString *message, *filename;
708 * ECMA ed. 3, 15.11.1 requires Error, etc., to construct even when
709 * called as functions, without operator new. But as we do not give
710 * each constructor a distinct JSClass, whose .name member is used by
711 * NewNativeClassInstance to find the class prototype, we must get the
712 * class prototype ourselves.
714 JSObject &callee = vp[0].toObject();
716 if (!callee.getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom), &protov))
719 if (!protov.isObject()) {
720 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_PROTOTYPE, "Error");
724 JSObject *errProto = &protov.toObject();
725 JSObject *obj = NewNativeClassInstance(cx, &js_ErrorClass, errProto, errProto->getParent());
730 * If it's a new object of class Exception, then null out the private
731 * data so that the finalizer doesn't attempt to free it.
733 if (obj->getClass() == &js_ErrorClass)
734 obj->setPrivate(NULL);
736 /* Set the 'message' property. */
737 Value *argv = vp + 2;
739 message = js_ValueToString(cx, argv[0]);
742 argv[0].setString(message);
744 message = cx->runtime->emptyString;
747 /* Set the 'fileName' property. */
749 filename = js_ValueToString(cx, argv[1]);
752 argv[1].setString(filename);
755 fp = js_GetScriptedCaller(cx, NULL);
757 filename = FilenameToString(cx, fp->script()->filename);
761 filename = cx->runtime->emptyString;
765 /* Set the 'lineNumber' property. */
768 if (!ValueToECMAUint32(cx, argv[2], &lineno))
772 fp = js_GetScriptedCaller(cx, NULL);
773 lineno = (fp && fp->pc(cx)) ? js_FramePCToLineNumber(cx, fp) : 0;
776 if (obj->getClass() == &js_ErrorClass &&
777 !InitExnPrivate(cx, obj, message, filename, lineno, NULL)) {
788 * This method only uses JavaScript-modifiable properties name, message. It
789 * is left to the host to check for private data and report filename and line
790 * number information along with this message.
793 exn_toString(JSContext *cx, uintN argc, Value *vp)
796 JSString *name, *message, *result;
798 size_t name_length, message_length, length;
800 JSObject *obj = ToObject(cx, &vp[1]);
803 if (!obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), Valueify(&v)))
805 name = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) : cx->runtime->emptyString;
808 if (!JS_GetProperty(cx, obj, js_message_str, &v))
810 message = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v)
811 : cx->runtime->emptyString;
813 if (message->length() != 0) {
814 name_length = name->length();
815 message_length = message->length();
816 length = (name_length ? name_length + 2 : 0) + message_length;
817 cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar));
822 const jschar *name_chars = name->getChars(cx);
825 js_strncpy(cp, name_chars, name_length);
827 *cp++ = ':'; *cp++ = ' ';
829 const jschar *message_chars = message->getChars(cx);
832 js_strncpy(cp, message_chars, message_length);
833 cp += message_length;
836 result = js_NewString(cx, chars, length);
845 vp->setString(result);
851 * Return a string that may eval to something similar to the original object.
854 exn_toSource(JSContext *cx, uintN argc, Value *vp)
856 JSString *name, *message, *filename, *lineno_as_str, *result;
857 jsval localroots[3] = {JSVAL_NULL, JSVAL_NULL, JSVAL_NULL};
858 size_t lineno_length, name_length, message_length, filename_length, length;
861 JSObject *obj = ToObject(cx, &vp[1]);
864 if (!obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), vp))
866 name = js_ValueToString(cx, *vp);
872 AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(localroots), Valueify(localroots));
875 message = filename = NULL;
877 if (!JS_GetProperty(cx, obj, js_message_str, &localroots[0]) ||
878 !(message = js_ValueToSource(cx, Valueify(localroots[0])))) {
881 localroots[0] = STRING_TO_JSVAL(message);
883 if (!JS_GetProperty(cx, obj, js_fileName_str, &localroots[1]) ||
884 !(filename = js_ValueToSource(cx, Valueify(localroots[1])))) {
887 localroots[1] = STRING_TO_JSVAL(filename);
889 if (!JS_GetProperty(cx, obj, js_lineNumber_str, &localroots[2]))
892 if (!ValueToECMAUint32(cx, Valueify(localroots[2]), &lineno))
896 lineno_as_str = js_ValueToString(cx, Valueify(localroots[2]));
899 lineno_length = lineno_as_str->length();
901 lineno_as_str = NULL;
905 /* Magic 8, for the characters in ``(new ())''. */
906 name_length = name->length();
907 message_length = message->length();
908 length = 8 + name_length + message_length;
910 filename_length = filename->length();
911 if (filename_length != 0) {
912 /* append filename as ``, {filename}'' */
913 length += 2 + filename_length;
915 /* append lineno as ``, {lineno_as_str}'' */
916 length += 2 + lineno_length;
921 * no filename, but have line number,
922 * need to append ``, "", {lineno_as_str}''
924 length += 6 + lineno_length;
928 cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar));
932 *cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' ';
933 const jschar *name_chars = name->getChars(cx);
936 js_strncpy(cp, name_chars, name_length);
939 const jschar *message_chars = message->getChars(cx);
942 if (message_length != 0) {
943 js_strncpy(cp, message_chars, message_length);
944 cp += message_length;
947 if (filename_length != 0) {
948 /* append filename as ``, {filename}'' */
949 *cp++ = ','; *cp++ = ' ';
950 const jschar *filename_chars = filename->getChars(cx);
953 js_strncpy(cp, filename_chars, filename_length);
954 cp += filename_length;
958 * no filename, but have line number,
959 * need to append ``, "", {lineno_as_str}''
961 *cp++ = ','; *cp++ = ' '; *cp++ = '"'; *cp++ = '"';
965 /* append lineno as ``, {lineno_as_str}'' */
966 *cp++ = ','; *cp++ = ' ';
967 const jschar *lineno_chars = lineno_as_str->getChars(cx);
970 js_strncpy(cp, lineno_chars, lineno_length);
974 *cp++ = ')'; *cp++ = ')'; *cp = 0;
976 result = js_NewString(cx, chars, length);
981 vp->setString(result);
987 static JSFunctionSpec exception_methods[] = {
989 JS_FN(js_toSource_str, exn_toSource, 0,0),
991 JS_FN(js_toString_str, exn_toString, 0,0),
995 /* JSProto_ ordering for exceptions shall match JSEXN_ constants. */
996 JS_STATIC_ASSERT(JSEXN_ERR == 0);
997 JS_STATIC_ASSERT(JSProto_Error + JSEXN_INTERNALERR == JSProto_InternalError);
998 JS_STATIC_ASSERT(JSProto_Error + JSEXN_EVALERR == JSProto_EvalError);
999 JS_STATIC_ASSERT(JSProto_Error + JSEXN_RANGEERR == JSProto_RangeError);
1000 JS_STATIC_ASSERT(JSProto_Error + JSEXN_REFERENCEERR == JSProto_ReferenceError);
1001 JS_STATIC_ASSERT(JSProto_Error + JSEXN_SYNTAXERR == JSProto_SyntaxError);
1002 JS_STATIC_ASSERT(JSProto_Error + JSEXN_TYPEERR == JSProto_TypeError);
1003 JS_STATIC_ASSERT(JSProto_Error + JSEXN_URIERR == JSProto_URIError);
1005 static JS_INLINE JSProtoKey
1006 GetExceptionProtoKey(intN exn)
1008 JS_ASSERT(JSEXN_ERR <= exn);
1009 JS_ASSERT(exn < JSEXN_LIMIT);
1010 return (JSProtoKey) (JSProto_Error + exn);
1014 js_InitExceptionClasses(JSContext *cx, JSObject *obj)
1017 * If lazy class initialization occurs for any Error subclass, then all
1018 * classes are initialized, starting with Error. To avoid reentry and
1019 * redundant initialization, we must not pass a null proto parameter to
1020 * NewNonFunction below, when called for the Error superclass. We need to
1021 * ensure that Object.prototype is the proto of Error.prototype.
1023 * See the equivalent code to ensure that parent_proto is non-null when
1024 * js_InitClass calls NewObject, in jsobj.cpp.
1026 JSObject *obj_proto;
1027 if (!js_GetClassPrototype(cx, obj, JSProto_Object, &obj_proto))
1030 /* Define all error constructors. */
1031 Value empty = StringValue(cx->runtime->emptyString);
1032 jsid nameId = ATOM_TO_JSID(cx->runtime->atomState.nameAtom);
1033 jsid messageId = ATOM_TO_JSID(cx->runtime->atomState.messageAtom);
1034 jsid fileNameId = ATOM_TO_JSID(cx->runtime->atomState.fileNameAtom);
1035 jsid lineNumberId = ATOM_TO_JSID(cx->runtime->atomState.lineNumberAtom);
1036 JSObject *error_proto = NULL;
1037 for (intN i = JSEXN_ERR; i != JSEXN_LIMIT; i++) {
1038 JSProtoKey protoKey = GetExceptionProtoKey(i);
1039 JSAtom *atom = cx->runtime->atomState.classAtoms[protoKey];
1041 DefineConstructorAndPrototype(cx, obj, protoKey, atom,
1042 (i == JSEXN_ERR) ? obj_proto : error_proto,
1043 &js_ErrorClass, Exception, 1,
1044 NULL, (i == JSEXN_ERR) ? exception_methods : NULL,
1048 JS_ASSERT(proto->privateData == NULL);
1051 error_proto = proto;
1053 /* Add properties to the prototype. */
1054 JSAutoResolveFlags rf(cx, JSRESOLVE_QUALIFIED | JSRESOLVE_DECLARING);
1055 if (!js_DefineNativeProperty(cx, proto, nameId, StringValue(atom),
1056 PropertyStub, StrictPropertyStub,
1057 JSPROP_ENUMERATE, 0, 0, NULL) ||
1058 !js_DefineNativeProperty(cx, proto, messageId, empty,
1059 PropertyStub, StrictPropertyStub,
1060 JSPROP_ENUMERATE, 0, 0, NULL) ||
1061 !js_DefineNativeProperty(cx, proto, fileNameId, empty,
1062 PropertyStub, StrictPropertyStub,
1063 JSPROP_ENUMERATE, 0, 0, NULL) ||
1064 !js_DefineNativeProperty(cx, proto, lineNumberId, Valueify(JSVAL_ZERO),
1065 PropertyStub, StrictPropertyStub,
1066 JSPROP_ENUMERATE, 0, 0, NULL)) {
1074 const JSErrorFormatString*
1075 js_GetLocalizedErrorMessage(JSContext* cx, void *userRef, const char *locale,
1076 const uintN errorNumber)
1078 const JSErrorFormatString *errorString = NULL;
1080 if (cx->localeCallbacks && cx->localeCallbacks->localeGetErrorMessage) {
1081 errorString = cx->localeCallbacks
1082 ->localeGetErrorMessage(userRef, locale, errorNumber);
1085 errorString = js_GetErrorMessage(userRef, locale, errorNumber);
1089 #if defined ( DEBUG_mccabe ) && defined ( PRINTNAMES )
1090 /* For use below... get character strings for error name and exception name */
1091 static struct exnname { char *name; char *exception; } errortoexnname[] = {
1092 #define MSG_DEF(name, number, count, exception, format) \
1093 {#name, #exception},
1100 js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp,
1101 JSErrorCallback callback, void *userRef)
1103 JSErrNum errorNumber;
1104 const JSErrorFormatString *errorString;
1108 JSObject *errProto, *errObject;
1109 JSString *messageStr, *filenameStr;
1112 * Tell our caller to report immediately if this report is just a warning.
1115 if (JSREPORT_IS_WARNING(reportp->flags))
1118 /* Find the exception index associated with this error. */
1119 errorNumber = (JSErrNum) reportp->errorNumber;
1120 if (!callback || callback == js_GetErrorMessage)
1121 errorString = js_GetLocalizedErrorMessage(cx, NULL, NULL, errorNumber);
1123 errorString = callback(userRef, NULL, errorNumber);
1124 exn = errorString ? (JSExnType) errorString->exnType : JSEXN_NONE;
1125 JS_ASSERT(exn < JSEXN_LIMIT);
1127 #if defined( DEBUG_mccabe ) && defined ( PRINTNAMES )
1128 /* Print the error name and the associated exception name to stderr */
1129 fprintf(stderr, "%s\t%s\n",
1130 errortoexnname[errorNumber].name,
1131 errortoexnname[errorNumber].exception);
1135 * Return false (no exception raised) if no exception is associated
1136 * with the given error number.
1138 if (exn == JSEXN_NONE)
1142 * Prevent runaway recursion, via cx->generatingError. If an out-of-memory
1143 * error occurs, no exception object will be created, but we don't assume
1144 * that OOM is the only kind of error that subroutines of this function
1145 * called below might raise.
1147 if (cx->generatingError)
1150 MUST_FLOW_THROUGH("out");
1151 cx->generatingError = JS_TRUE;
1153 /* Protect the newly-created strings below from nesting GCs. */
1155 AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(tv), Valueify(tv));
1158 * Try to get an appropriate prototype by looking up the corresponding
1159 * exception constructor name in the scope chain of the current context's
1160 * top stack frame, or in the global object if no frame is active.
1162 ok = js_GetClassPrototype(cx, NULL, GetExceptionProtoKey(exn), &errProto);
1165 tv[0] = OBJECT_TO_JSVAL(errProto);
1167 errObject = NewNativeClassInstance(cx, &js_ErrorClass, errProto, errProto->getParent());
1172 tv[1] = OBJECT_TO_JSVAL(errObject);
1174 messageStr = JS_NewStringCopyZ(cx, message);
1179 tv[2] = STRING_TO_JSVAL(messageStr);
1181 filenameStr = JS_NewStringCopyZ(cx, reportp->filename);
1186 tv[3] = STRING_TO_JSVAL(filenameStr);
1188 ok = InitExnPrivate(cx, errObject, messageStr, filenameStr,
1189 reportp->lineno, reportp);
1193 JS_SetPendingException(cx, OBJECT_TO_JSVAL(errObject));
1195 /* Flag the error report passed in to indicate an exception was raised. */
1196 reportp->flags |= JSREPORT_EXCEPTION;
1199 cx->generatingError = JS_FALSE;
1204 js_ReportUncaughtException(JSContext *cx)
1207 JSObject *exnObject;
1209 JSErrorReport *reportp, report;
1213 if (!JS_IsExceptionPending(cx))
1216 if (!JS_GetPendingException(cx, &exn))
1219 PodArrayZero(roots);
1220 AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(roots), Valueify(roots));
1223 * Because js_ValueToString below could error and an exception object
1224 * could become unrooted, we must root exnObject. Later, if exnObject is
1225 * non-null, we need to root other intermediates, so allocate an operand
1226 * stack segment to protect all of these values.
1228 if (JSVAL_IS_PRIMITIVE(exn)) {
1231 exnObject = JSVAL_TO_OBJECT(exn);
1235 JS_ClearPendingException(cx);
1236 reportp = js_ErrorFromException(cx, exn);
1238 /* XXX L10N angels cry once again (see also jsemit.c, /L10N gaffes/) */
1239 str = js_ValueToString(cx, Valueify(exn));
1240 JSAutoByteString bytesStorage;
1242 bytes = "unknown (can't convert to string)";
1244 roots[1] = STRING_TO_JSVAL(str);
1245 if (!bytesStorage.encode(cx, str))
1247 bytes = bytesStorage.ptr();
1250 JSAutoByteString filename;
1251 if (!reportp && exnObject && exnObject->getClass() == &js_ErrorClass) {
1252 if (!JS_GetProperty(cx, exnObject, js_message_str, &roots[2]))
1254 if (JSVAL_IS_STRING(roots[2])) {
1255 bytesStorage.clear();
1256 if (!bytesStorage.encode(cx, str))
1258 bytes = bytesStorage.ptr();
1261 if (!JS_GetProperty(cx, exnObject, js_fileName_str, &roots[3]))
1263 str = js_ValueToString(cx, Valueify(roots[3]));
1264 if (!str || !filename.encode(cx, str))
1267 if (!JS_GetProperty(cx, exnObject, js_lineNumber_str, &roots[4]))
1270 if (!ValueToECMAUint32(cx, Valueify(roots[4]), &lineno))
1275 report.filename = filename.ptr();
1276 report.lineno = (uintN) lineno;
1277 if (JSVAL_IS_STRING(roots[2])) {
1278 report.ucmessage = js_GetStringChars(cx, JSVAL_TO_STRING(roots[2]));
1279 if (!report.ucmessage)
1285 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1286 JSMSG_UNCAUGHT_EXCEPTION, bytes);
1288 /* Flag the error as an exception. */
1289 reportp->flags |= JSREPORT_EXCEPTION;
1291 /* Pass the exception object. */
1292 JS_SetPendingException(cx, exn);
1293 js_ReportErrorAgain(cx, bytes, reportp);
1294 JS_ClearPendingException(cx);