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.
50 #include "jsutil.h" /* Added by JSIFY */
63 /* Forward declarations for js_ErrorClass's initializer. */
65 Exception(JSContext
*cx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
);
68 exn_finalize(JSContext
*cx
, JSObject
*obj
);
71 exn_trace(JSTracer
*trc
, JSObject
*obj
);
74 exn_finalize(JSContext
*cx
, JSObject
*obj
);
77 exn_enumerate(JSContext
*cx
, JSObject
*obj
);
80 exn_resolve(JSContext
*cx
, JSObject
*obj
, jsval id
, uintN flags
,
83 JSClass js_ErrorClass
= {
85 JSCLASS_HAS_PRIVATE
| JSCLASS_NEW_RESOLVE
| JSCLASS_MARK_IS_TRACE
|
86 JSCLASS_HAS_CACHED_PROTO(JSProto_Error
),
87 JS_PropertyStub
, JS_PropertyStub
, JS_PropertyStub
, JS_PropertyStub
,
88 exn_enumerate
, (JSResolveOp
)exn_resolve
, JS_ConvertStub
, exn_finalize
,
89 NULL
, NULL
, NULL
, Exception
,
90 NULL
, NULL
, JS_CLASS_TRACE(exn_trace
), NULL
93 typedef struct JSStackTraceElem
{
100 typedef struct JSExnPrivate
{
101 /* A copy of the JSErrorReport originally generated. */
102 JSErrorReport
*errorReport
;
107 JSStackTraceElem stackElems
[1];
111 StackTraceToString(JSContext
*cx
, JSExnPrivate
*priv
);
113 static JSErrorReport
*
114 CopyErrorReport(JSContext
*cx
, JSErrorReport
*report
)
117 * We use a single malloc block to make a deep copy of JSErrorReport with
118 * the following layout:
120 * array of copies of report->messageArgs
121 * jschar array with characters for all messageArgs
122 * jschar array with characters for ucmessage
123 * jschar array with characters for uclinebuf and uctokenptr
124 * char array with characters for linebuf and tokenptr
125 * char array with characters for filename
126 * Such layout together with the properties enforced by the following
127 * asserts does not need any extra alignment padding.
129 JS_STATIC_ASSERT(sizeof(JSErrorReport
) % sizeof(const char *) == 0);
130 JS_STATIC_ASSERT(sizeof(const char *) % sizeof(jschar
) == 0);
134 size_t uclinebufSize
;
135 size_t ucmessageSize
;
136 size_t i
, argsArraySize
, argsCopySize
, argSize
;
141 #define JS_CHARS_SIZE(jschars) ((js_strlen(jschars) + 1) * sizeof(jschar))
143 filenameSize
= report
->filename
? strlen(report
->filename
) + 1 : 0;
144 linebufSize
= report
->linebuf
? strlen(report
->linebuf
) + 1 : 0;
145 uclinebufSize
= report
->uclinebuf
? JS_CHARS_SIZE(report
->uclinebuf
) : 0;
149 if (report
->ucmessage
) {
150 ucmessageSize
= JS_CHARS_SIZE(report
->ucmessage
);
151 if (report
->messageArgs
) {
152 for (i
= 0; report
->messageArgs
[i
]; ++i
)
153 argsCopySize
+= JS_CHARS_SIZE(report
->messageArgs
[i
]);
155 /* Non-null messageArgs should have at least one non-null arg. */
157 argsArraySize
= (i
+ 1) * sizeof(const jschar
*);
162 * The mallocSize can not overflow since it represents the sum of the
163 * sizes of already allocated objects.
165 mallocSize
= sizeof(JSErrorReport
) + argsArraySize
+ argsCopySize
+
166 ucmessageSize
+ uclinebufSize
+ linebufSize
+ filenameSize
;
167 cursor
= (uint8
*)JS_malloc(cx
, mallocSize
);
171 copy
= (JSErrorReport
*)cursor
;
172 memset(cursor
, 0, sizeof(JSErrorReport
));
173 cursor
+= sizeof(JSErrorReport
);
175 if (argsArraySize
!= 0) {
176 copy
->messageArgs
= (const jschar
**)cursor
;
177 cursor
+= argsArraySize
;
178 for (i
= 0; report
->messageArgs
[i
]; ++i
) {
179 copy
->messageArgs
[i
] = (const jschar
*)cursor
;
180 argSize
= JS_CHARS_SIZE(report
->messageArgs
[i
]);
181 memcpy(cursor
, report
->messageArgs
[i
], argSize
);
184 copy
->messageArgs
[i
] = NULL
;
185 JS_ASSERT(cursor
== (uint8
*)copy
->messageArgs
[0] + argsCopySize
);
188 if (report
->ucmessage
) {
189 copy
->ucmessage
= (const jschar
*)cursor
;
190 memcpy(cursor
, report
->ucmessage
, ucmessageSize
);
191 cursor
+= ucmessageSize
;
194 if (report
->uclinebuf
) {
195 copy
->uclinebuf
= (const jschar
*)cursor
;
196 memcpy(cursor
, report
->uclinebuf
, uclinebufSize
);
197 cursor
+= uclinebufSize
;
198 if (report
->uctokenptr
) {
199 copy
->uctokenptr
= copy
->uclinebuf
+ (report
->uctokenptr
-
204 if (report
->linebuf
) {
205 copy
->linebuf
= (const char *)cursor
;
206 memcpy(cursor
, report
->linebuf
, linebufSize
);
207 cursor
+= linebufSize
;
208 if (report
->tokenptr
) {
209 copy
->tokenptr
= copy
->linebuf
+ (report
->tokenptr
-
214 if (report
->filename
) {
215 copy
->filename
= (const char *)cursor
;
216 memcpy(cursor
, report
->filename
, filenameSize
);
218 JS_ASSERT(cursor
+ filenameSize
== (uint8
*)copy
+ mallocSize
);
220 /* Copy non-pointer members. */
221 copy
->lineno
= report
->lineno
;
222 copy
->errorNumber
= report
->errorNumber
;
224 /* Note that this is before it gets flagged with JSREPORT_EXCEPTION */
225 copy
->flags
= report
->flags
;
232 GetStackTraceValueBuffer(JSExnPrivate
*priv
)
235 * We use extra memory after JSExnPrivateInfo.stackElems to store jsvals
236 * that helps to produce more informative stack traces. The following
237 * assert allows us to assume that no gap after stackElems is necessary to
238 * align the buffer properly.
240 JS_STATIC_ASSERT(sizeof(JSStackTraceElem
) % sizeof(jsval
) == 0);
242 return (jsval
*)(priv
->stackElems
+ priv
->stackDepth
);
246 InitExnPrivate(JSContext
*cx
, JSObject
*exnObject
, JSString
*message
,
247 JSString
*filename
, uintN lineno
, JSErrorReport
*report
)
249 JSCheckAccessOp checkAccess
;
250 JSErrorReporter older
;
251 JSExceptionState
*state
;
253 JSStackFrame
*fp
, *fpstop
;
254 size_t stackDepth
, valueCount
, size
;
257 JSStackTraceElem
*elem
;
260 JS_ASSERT(OBJ_GET_CLASS(cx
, exnObject
) == &js_ErrorClass
);
263 * Prepare stack trace data.
265 * Set aside any error reporter for cx and save its exception state
266 * so we can suppress any checkAccess failures. Such failures should stop
267 * the backtrace procedure, not result in a failure of this constructor.
269 checkAccess
= cx
->runtime
->checkObjectAccess
;
270 older
= JS_SetErrorReporter(cx
, NULL
);
271 state
= JS_SaveExceptionState(cx
);
273 callerid
= ATOM_KEY(cx
->runtime
->atomState
.callerAtom
);
276 for (fp
= cx
->fp
; fp
; fp
= fp
->down
) {
277 if (fp
->fun
&& fp
->argv
) {
280 !checkAccess(cx
, fp
->callee
, callerid
, JSACC_READ
, &v
)) {
283 valueCount
+= fp
->argc
;
287 JS_RestoreExceptionState(cx
, state
);
288 JS_SetErrorReporter(cx
, older
);
291 size
= offsetof(JSExnPrivate
, stackElems
);
292 overflow
= (stackDepth
> ((size_t)-1 - size
) / sizeof(JSStackTraceElem
));
293 size
+= stackDepth
* sizeof(JSStackTraceElem
);
294 overflow
|= (valueCount
> ((size_t)-1 - size
) / sizeof(jsval
));
295 size
+= valueCount
* sizeof(jsval
);
297 JS_ReportOutOfMemory(cx
);
300 priv
= (JSExnPrivate
*)JS_malloc(cx
, size
);
305 * We initialize errorReport with a copy of report after setting the
306 * private slot, to prevent GC accessing a junk value we clear the field
309 priv
->errorReport
= NULL
;
310 priv
->message
= message
;
311 priv
->filename
= filename
;
312 priv
->lineno
= lineno
;
313 priv
->stackDepth
= stackDepth
;
315 values
= GetStackTraceValueBuffer(priv
);
316 elem
= priv
->stackElems
;
317 for (fp
= cx
->fp
; fp
!= fpstop
; fp
= fp
->down
) {
319 elem
->funName
= NULL
;
322 elem
->funName
= fp
->fun
->atom
323 ? ATOM_TO_STRING(fp
->fun
->atom
)
324 : cx
->runtime
->emptyString
;
325 elem
->argc
= fp
->argc
;
326 memcpy(values
, fp
->argv
, fp
->argc
* sizeof(jsval
));
330 elem
->filename
= NULL
;
332 elem
->filename
= fp
->script
->filename
;
334 elem
->ulineno
= js_PCToLineNumber(cx
, fp
->script
, fp
->pc
);
338 JS_ASSERT(priv
->stackElems
+ stackDepth
== elem
);
339 JS_ASSERT(GetStackTraceValueBuffer(priv
) + valueCount
== values
);
341 OBJ_SET_SLOT(cx
, exnObject
, JSSLOT_PRIVATE
, PRIVATE_TO_JSVAL(priv
));
345 * Construct a new copy of the error report struct. We can't use the
346 * error report struct that was passed in, because it's allocated on
347 * the stack, and also because it may point to transient data in the
350 priv
->errorReport
= CopyErrorReport(cx
, report
);
351 if (!priv
->errorReport
) {
352 /* The finalizer realeases priv since it is in the private slot. */
360 static JSExnPrivate
*
361 GetExnPrivate(JSContext
*cx
, JSObject
*obj
)
366 JS_ASSERT(OBJ_GET_CLASS(cx
, obj
) == &js_ErrorClass
);
367 privateValue
= OBJ_GET_SLOT(cx
, obj
, JSSLOT_PRIVATE
);
368 if (JSVAL_IS_VOID(privateValue
))
370 priv
= (JSExnPrivate
*)JSVAL_TO_PRIVATE(privateValue
);
376 exn_trace(JSTracer
*trc
, JSObject
*obj
)
379 JSStackTraceElem
*elem
;
383 priv
= GetExnPrivate(trc
->context
, obj
);
386 JS_CALL_STRING_TRACER(trc
, priv
->message
, "exception message");
388 JS_CALL_STRING_TRACER(trc
, priv
->filename
, "exception filename");
390 elem
= priv
->stackElems
;
391 for (vcount
= i
= 0; i
!= priv
->stackDepth
; ++i
, ++elem
) {
393 JS_CALL_STRING_TRACER(trc
, elem
->funName
,
394 "stack trace function name");
396 if (IS_GC_MARKING_TRACER(trc
) && elem
->filename
)
397 js_MarkScriptFilename(elem
->filename
);
398 vcount
+= elem
->argc
;
400 vp
= GetStackTraceValueBuffer(priv
);
401 for (i
= 0; i
!= vcount
; ++i
, ++vp
) {
403 JS_CALL_VALUE_TRACER(trc
, v
, "stack trace argument");
409 exn_finalize(JSContext
*cx
, JSObject
*obj
)
413 priv
= GetExnPrivate(cx
, obj
);
415 if (priv
->errorReport
)
416 JS_free(cx
, priv
->errorReport
);
422 exn_enumerate(JSContext
*cx
, JSObject
*obj
)
424 JSAtomState
*atomState
;
430 JS_STATIC_ASSERT(sizeof(JSAtomState
) <= (size_t)(uint16
)-1);
431 static const uint16 offsets
[] = {
432 (uint16
)offsetof(JSAtomState
, messageAtom
),
433 (uint16
)offsetof(JSAtomState
, fileNameAtom
),
434 (uint16
)offsetof(JSAtomState
, lineNumberAtom
),
435 (uint16
)offsetof(JSAtomState
, stackAtom
),
438 atomState
= &cx
->runtime
->atomState
;
439 for (i
= 0; i
!= JS_ARRAY_LENGTH(offsets
); ++i
) {
440 atom
= *(JSAtom
**)((uint8
*)atomState
+ offsets
[i
]);
441 if (!js_LookupProperty(cx
, obj
, ATOM_TO_JSID(atom
), &pobj
, &prop
))
444 OBJ_DROP_PROPERTY(cx
, pobj
, prop
);
450 exn_resolve(JSContext
*cx
, JSObject
*obj
, jsval id
, uintN flags
,
461 priv
= GetExnPrivate(cx
, obj
);
462 if (priv
&& JSVAL_IS_STRING(id
)) {
463 str
= JSVAL_TO_STRING(id
);
465 atom
= cx
->runtime
->atomState
.messageAtom
;
466 if (str
== ATOM_TO_STRING(atom
)) {
467 prop
= js_message_str
;
468 v
= STRING_TO_JSVAL(priv
->message
);
472 atom
= cx
->runtime
->atomState
.fileNameAtom
;
473 if (str
== ATOM_TO_STRING(atom
)) {
474 prop
= js_fileName_str
;
475 v
= STRING_TO_JSVAL(priv
->filename
);
479 atom
= cx
->runtime
->atomState
.lineNumberAtom
;
480 if (str
== ATOM_TO_STRING(atom
)) {
481 prop
= js_lineNumber_str
;
482 v
= INT_TO_JSVAL(priv
->lineno
);
486 atom
= cx
->runtime
->atomState
.stackAtom
;
487 if (str
== ATOM_TO_STRING(atom
)) {
488 stack
= StackTraceToString(cx
, priv
);
492 /* Allow to GC all things that were used to build stack trace. */
493 priv
->stackDepth
= 0;
495 v
= STRING_TO_JSVAL(stack
);
502 if (!JS_DefineProperty(cx
, obj
, prop
, v
, NULL
, NULL
, JSPROP_ENUMERATE
))
509 js_ErrorFromException(JSContext
*cx
, jsval exn
)
514 if (JSVAL_IS_PRIMITIVE(exn
))
516 obj
= JSVAL_TO_OBJECT(exn
);
517 if (OBJ_GET_CLASS(cx
, obj
) != &js_ErrorClass
)
519 priv
= GetExnPrivate(cx
, obj
);
522 return priv
->errorReport
;
533 * All *Error constructors share the same JSClass, js_ErrorClass. But each
534 * constructor function for an *Error class must have a distinct native 'call'
535 * function pointer, in order for instanceof to work properly across multiple
536 * standard class sets. See jsfun.c:fun_hasInstance.
538 #define MAKE_EXCEPTION_CTOR(name) \
540 name(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) \
542 return Exception(cx, obj, argc, argv, rval); \
545 MAKE_EXCEPTION_CTOR(Error
)
546 MAKE_EXCEPTION_CTOR(InternalError
)
547 MAKE_EXCEPTION_CTOR(EvalError
)
548 MAKE_EXCEPTION_CTOR(RangeError
)
549 MAKE_EXCEPTION_CTOR(ReferenceError
)
550 MAKE_EXCEPTION_CTOR(SyntaxError
)
551 MAKE_EXCEPTION_CTOR(TypeError
)
552 MAKE_EXCEPTION_CTOR(URIError
)
554 #undef MAKE_EXCEPTION_CTOR
556 static struct JSExnSpec exceptions
[] = {
557 {JSEXN_NONE
, js_Error_str
, JSProto_Error
, Error
},
558 {JSEXN_ERR
, js_InternalError_str
, JSProto_InternalError
, InternalError
},
559 {JSEXN_ERR
, js_EvalError_str
, JSProto_EvalError
, EvalError
},
560 {JSEXN_ERR
, js_RangeError_str
, JSProto_RangeError
, RangeError
},
561 {JSEXN_ERR
, js_ReferenceError_str
, JSProto_ReferenceError
, ReferenceError
},
562 {JSEXN_ERR
, js_SyntaxError_str
, JSProto_SyntaxError
, SyntaxError
},
563 {JSEXN_ERR
, js_TypeError_str
, JSProto_TypeError
, TypeError
},
564 {JSEXN_ERR
, js_URIError_str
, JSProto_URIError
, URIError
},
565 {0, NULL
, JSProto_Null
, NULL
}
569 ValueToShortSource(JSContext
*cx
, jsval v
)
573 /* Avoid toSource bloat and fallibility for object types. */
574 if (JSVAL_IS_PRIMITIVE(v
)) {
575 str
= js_ValueToSource(cx
, v
);
576 } else if (VALUE_IS_FUNCTION(cx
, v
)) {
578 * XXX Avoid function decompilation bloat for now.
580 str
= JS_GetFunctionId(JS_ValueToFunction(cx
, v
));
581 if (!str
&& !(str
= js_ValueToSource(cx
, v
))) {
583 * Continue to soldier on if the function couldn't be
584 * converted into a string.
586 JS_ClearPendingException(cx
);
587 str
= JS_NewStringCopyZ(cx
, "[unknown function]");
591 * XXX Avoid toString on objects, it takes too long and uses too much
592 * memory, for too many classes (see Mozilla bug 166743).
595 JS_snprintf(buf
, sizeof buf
, "[object %s]",
596 OBJ_GET_CLASS(cx
, JSVAL_TO_OBJECT(v
))->name
);
597 str
= JS_NewStringCopyZ(cx
, buf
);
603 StackTraceToString(JSContext
*cx
, JSExnPrivate
*priv
)
606 size_t stacklen
, stackmax
;
607 JSStackTraceElem
*elem
, *endElem
;
614 /* After this point, failing control flow must goto bad. */
616 stacklen
= stackmax
= 0;
618 /* Limit the stackbuf length to a reasonable value to avoid overflow checks. */
619 #define STACK_LENGTH_LIMIT JS_BIT(20)
621 #define APPEND_CHAR_TO_STACK(c) \
623 if (stacklen == stackmax) { \
625 if (stackmax >= STACK_LENGTH_LIMIT) \
627 stackmax = stackmax ? 2 * stackmax : 64; \
628 ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \
631 stackbuf = (jschar *) ptr_; \
633 stackbuf[stacklen++] = (c); \
636 #define APPEND_STRING_TO_STACK(str) \
638 JSString *str_ = str; \
642 JSSTRING_CHARS_AND_LENGTH(str_, chars_, length_); \
643 if (length_ > stackmax - stacklen) { \
645 if (stackmax >= STACK_LENGTH_LIMIT || \
646 length_ >= STACK_LENGTH_LIMIT - stacklen) { \
649 stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \
650 ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \
653 stackbuf = (jschar *) ptr_; \
655 js_strncpy(stackbuf + stacklen, chars_, length_); \
656 stacklen += length_; \
659 values
= GetStackTraceValueBuffer(priv
);
660 elem
= priv
->stackElems
;
661 for (endElem
= elem
+ priv
->stackDepth
; elem
!= endElem
; elem
++) {
663 APPEND_STRING_TO_STACK(elem
->funName
);
664 APPEND_CHAR_TO_STACK('(');
665 for (i
= 0; i
!= elem
->argc
; i
++, values
++) {
667 APPEND_CHAR_TO_STACK(',');
668 str
= ValueToShortSource(cx
, *values
);
671 APPEND_STRING_TO_STACK(str
);
673 APPEND_CHAR_TO_STACK(')');
675 APPEND_CHAR_TO_STACK('@');
676 if (elem
->filename
) {
677 for (cp
= elem
->filename
; *cp
; cp
++)
678 APPEND_CHAR_TO_STACK(*cp
);
680 APPEND_CHAR_TO_STACK(':');
681 JS_snprintf(ulnbuf
, sizeof ulnbuf
, "%u", elem
->ulineno
);
682 for (cp
= ulnbuf
; *cp
; cp
++)
683 APPEND_CHAR_TO_STACK(*cp
);
684 APPEND_CHAR_TO_STACK('\n');
686 #undef APPEND_CHAR_TO_STACK
687 #undef APPEND_STRING_TO_STACK
688 #undef STACK_LENGTH_LIMIT
692 JS_ASSERT(!stackbuf
);
693 return cx
->runtime
->emptyString
;
695 if (stacklen
< stackmax
) {
697 * Realloc can fail when shrinking on some FreeBSD versions, so
698 * don't use JS_realloc here; simply let the oversized allocation
699 * be owned by the string in that rare case.
701 void *shrunk
= JS_realloc(cx
, stackbuf
, (stacklen
+1) * sizeof(jschar
));
703 stackbuf
= (jschar
*) shrunk
;
706 stackbuf
[stacklen
] = 0;
707 str
= js_NewString(cx
, stackbuf
, stacklen
);
713 JS_free(cx
, stackbuf
);
717 /* XXXbe Consolidate the ugly truth that we don't treat filename as UTF-8
718 with these two functions. */
720 FilenameToString(JSContext
*cx
, const char *filename
)
722 return JS_NewStringCopyZ(cx
, filename
);
726 StringToFilename(JSContext
*cx
, JSString
*str
)
728 return js_GetStringBytes(cx
, str
);
732 Exception(JSContext
*cx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
)
735 JSString
*message
, *filename
;
738 if (!(cx
->fp
->flags
& JSFRAME_CONSTRUCTING
)) {
740 * ECMA ed. 3, 15.11.1 requires Error, etc., to construct even when
741 * called as functions, without operator new. But as we do not give
742 * each constructor a distinct JSClass, whose .name member is used by
743 * js_NewObject to find the class prototype, we must get the class
744 * prototype ourselves.
746 if (!OBJ_GET_PROPERTY(cx
, JSVAL_TO_OBJECT(argv
[-2]),
747 ATOM_TO_JSID(cx
->runtime
->atomState
748 .classPrototypeAtom
),
751 obj
= js_NewObject(cx
, &js_ErrorClass
, JSVAL_TO_OBJECT(*rval
), NULL
);
754 *rval
= OBJECT_TO_JSVAL(obj
);
758 * If it's a new object of class Exception, then null out the private
759 * data so that the finalizer doesn't attempt to free it.
761 if (OBJ_GET_CLASS(cx
, obj
) == &js_ErrorClass
)
762 OBJ_SET_SLOT(cx
, obj
, JSSLOT_PRIVATE
, JSVAL_VOID
);
764 /* Set the 'message' property. */
766 message
= js_ValueToString(cx
, argv
[0]);
769 argv
[0] = STRING_TO_JSVAL(message
);
771 message
= cx
->runtime
->emptyString
;
774 /* Set the 'fileName' property. */
776 filename
= js_ValueToString(cx
, argv
[1]);
779 argv
[1] = STRING_TO_JSVAL(filename
);
782 fp
= JS_GetScriptedCaller(cx
, NULL
);
784 filename
= FilenameToString(cx
, fp
->script
->filename
);
788 filename
= cx
->runtime
->emptyString
;
792 /* Set the 'lineNumber' property. */
794 if (!js_ValueToECMAUint32(cx
, argv
[2], &lineno
))
798 fp
= JS_GetScriptedCaller(cx
, NULL
);
799 lineno
= (fp
&& fp
->pc
) ? js_PCToLineNumber(cx
, fp
->script
, fp
->pc
) : 0;
802 return (OBJ_GET_CLASS(cx
, obj
) != &js_ErrorClass
) ||
803 InitExnPrivate(cx
, obj
, message
, filename
, lineno
, NULL
);
809 * This method only uses JavaScript-modifiable properties name, message. It
810 * is left to the host to check for private data and report filename and line
811 * number information along with this message.
814 exn_toString(JSContext
*cx
, uintN argc
, jsval
*vp
)
818 JSString
*name
, *message
, *result
;
820 size_t name_length
, message_length
, length
;
822 obj
= JSVAL_TO_OBJECT(vp
[1]);
823 if (!OBJ_GET_PROPERTY(cx
, obj
,
824 ATOM_TO_JSID(cx
->runtime
->atomState
.nameAtom
),
828 name
= JSVAL_IS_STRING(v
) ? JSVAL_TO_STRING(v
) : cx
->runtime
->emptyString
;
829 *vp
= STRING_TO_JSVAL(name
);
831 if (!JS_GetProperty(cx
, obj
, js_message_str
, &v
))
833 message
= JSVAL_IS_STRING(v
) ? JSVAL_TO_STRING(v
)
834 : cx
->runtime
->emptyString
;
836 if (JSSTRING_LENGTH(message
) != 0) {
837 name_length
= JSSTRING_LENGTH(name
);
838 message_length
= JSSTRING_LENGTH(message
);
839 length
= (name_length
? name_length
+ 2 : 0) + message_length
;
840 cp
= chars
= (jschar
*) JS_malloc(cx
, (length
+ 1) * sizeof(jschar
));
845 js_strncpy(cp
, JSSTRING_CHARS(name
), name_length
);
847 *cp
++ = ':'; *cp
++ = ' ';
849 js_strncpy(cp
, JSSTRING_CHARS(message
), message_length
);
850 cp
+= message_length
;
853 result
= js_NewString(cx
, chars
, length
);
862 *vp
= STRING_TO_JSVAL(result
);
868 * Return a string that may eval to something similar to the original object.
871 exn_toSource(JSContext
*cx
, uintN argc
, jsval
*vp
)
875 JSString
*name
, *message
, *filename
, *lineno_as_str
, *result
;
877 size_t lineno_length
, name_length
, message_length
, filename_length
, length
;
880 localroots
= JS_ARGV(cx
, vp
) + argc
;
882 obj
= JS_THIS_OBJECT(cx
, vp
);
883 if (!OBJ_GET_PROPERTY(cx
, obj
,
884 ATOM_TO_JSID(cx
->runtime
->atomState
.nameAtom
),
888 name
= js_ValueToString(cx
, *vp
);
891 *vp
= STRING_TO_JSVAL(name
);
893 if (!JS_GetProperty(cx
, obj
, js_message_str
, &localroots
[0]) ||
894 !(message
= js_ValueToSource(cx
, localroots
[0]))) {
897 localroots
[0] = STRING_TO_JSVAL(message
);
899 if (!JS_GetProperty(cx
, obj
, js_fileName_str
, &localroots
[1]) ||
900 !(filename
= js_ValueToSource(cx
, localroots
[1]))) {
903 localroots
[1] = STRING_TO_JSVAL(filename
);
905 if (!JS_GetProperty(cx
, obj
, js_lineNumber_str
, &localroots
[2]) ||
906 !js_ValueToECMAUint32 (cx
, localroots
[2], &lineno
)) {
911 lineno_as_str
= js_ValueToString(cx
, localroots
[2]);
914 lineno_length
= JSSTRING_LENGTH(lineno_as_str
);
916 lineno_as_str
= NULL
;
920 /* Magic 8, for the characters in ``(new ())''. */
921 name_length
= JSSTRING_LENGTH(name
);
922 message_length
= JSSTRING_LENGTH(message
);
923 length
= 8 + name_length
+ message_length
;
925 filename_length
= JSSTRING_LENGTH(filename
);
926 if (filename_length
!= 0) {
927 /* append filename as ``, {filename}'' */
928 length
+= 2 + filename_length
;
930 /* append lineno as ``, {lineno_as_str}'' */
931 length
+= 2 + lineno_length
;
936 * no filename, but have line number,
937 * need to append ``, "", {lineno_as_str}''
939 length
+= 6 + lineno_length
;
943 cp
= chars
= (jschar
*) JS_malloc(cx
, (length
+ 1) * sizeof(jschar
));
947 *cp
++ = '('; *cp
++ = 'n'; *cp
++ = 'e'; *cp
++ = 'w'; *cp
++ = ' ';
948 js_strncpy(cp
, JSSTRING_CHARS(name
), name_length
);
951 if (message_length
!= 0) {
952 js_strncpy(cp
, JSSTRING_CHARS(message
), message_length
);
953 cp
+= message_length
;
956 if (filename_length
!= 0) {
957 /* append filename as ``, {filename}'' */
958 *cp
++ = ','; *cp
++ = ' ';
959 js_strncpy(cp
, JSSTRING_CHARS(filename
), filename_length
);
960 cp
+= filename_length
;
964 * no filename, but have line number,
965 * need to append ``, "", {lineno_as_str}''
967 *cp
++ = ','; *cp
++ = ' '; *cp
++ = '"'; *cp
++ = '"';
971 /* append lineno as ``, {lineno_as_str}'' */
972 *cp
++ = ','; *cp
++ = ' ';
973 js_strncpy(cp
, JSSTRING_CHARS(lineno_as_str
), lineno_length
);
977 *cp
++ = ')'; *cp
++ = ')'; *cp
= 0;
979 result
= js_NewString(cx
, chars
, length
);
984 *vp
= STRING_TO_JSVAL(result
);
989 static JSFunctionSpec exception_methods
[] = {
991 JS_FN(js_toSource_str
, exn_toSource
, 0,0,0,3),
993 JS_FN(js_toString_str
, exn_toString
, 0,0,0,0),
998 js_InitExceptionClasses(JSContext
*cx
, JSObject
*obj
)
1000 JSObject
*obj_proto
, *protos
[JSEXN_LIMIT
];
1004 * If lazy class initialization occurs for any Error subclass, then all
1005 * classes are initialized, starting with Error. To avoid reentry and
1006 * redundant initialization, we must not pass a null proto parameter to
1007 * js_NewObject below, when called for the Error superclass. We need to
1008 * ensure that Object.prototype is the proto of Error.prototype.
1010 * See the equivalent code to ensure that parent_proto is non-null when
1011 * JS_InitClass calls js_NewObject, in jsapi.c.
1013 if (!js_GetClassPrototype(cx
, obj
, INT_TO_JSID(JSProto_Object
),
1018 if (!js_EnterLocalRootScope(cx
))
1021 /* Initialize the prototypes first. */
1022 for (i
= 0; exceptions
[i
].name
!= 0; i
++) {
1026 JSString
*nameString
;
1027 int protoIndex
= exceptions
[i
].protoIndex
;
1029 /* Make the prototype for the current constructor name. */
1030 protos
[i
] = js_NewObject(cx
, &js_ErrorClass
,
1031 (protoIndex
!= JSEXN_NONE
)
1032 ? protos
[protoIndex
]
1038 /* So exn_finalize knows whether to destroy private data. */
1039 OBJ_SET_SLOT(cx
, protos
[i
], JSSLOT_PRIVATE
, JSVAL_VOID
);
1041 /* Make a constructor function for the current name. */
1042 atom
= cx
->runtime
->atomState
.classAtoms
[exceptions
[i
].key
];
1043 fun
= js_DefineFunction(cx
, obj
, atom
, exceptions
[i
].native
, 3, 0);
1047 /* Make this constructor make objects of class Exception. */
1048 fun
->clasp
= &js_ErrorClass
;
1050 /* Extract the constructor object. */
1051 funobj
= fun
->object
;
1053 /* Make the prototype and constructor links. */
1054 if (!js_SetClassPrototype(cx
, funobj
, protos
[i
],
1055 JSPROP_READONLY
| JSPROP_PERMANENT
)) {
1059 /* proto bootstrap bit from JS_InitClass omitted. */
1060 nameString
= JS_NewStringCopyZ(cx
, exceptions
[i
].name
);
1064 /* Add the name property to the prototype. */
1065 if (!JS_DefineProperty(cx
, protos
[i
], js_name_str
,
1066 STRING_TO_JSVAL(nameString
),
1068 JSPROP_ENUMERATE
)) {
1072 /* Finally, stash the constructor for later uses. */
1073 if (!js_SetClassObject(cx
, obj
, exceptions
[i
].key
, funobj
))
1077 js_LeaveLocalRootScope(cx
);
1078 if (exceptions
[i
].name
)
1082 * Add an empty message property. (To Exception.prototype only,
1083 * because this property will be the same for all the exception
1086 if (!JS_DefineProperty(cx
, protos
[0], js_message_str
,
1087 STRING_TO_JSVAL(cx
->runtime
->emptyString
),
1088 NULL
, NULL
, JSPROP_ENUMERATE
)) {
1091 if (!JS_DefineProperty(cx
, protos
[0], js_fileName_str
,
1092 STRING_TO_JSVAL(cx
->runtime
->emptyString
),
1093 NULL
, NULL
, JSPROP_ENUMERATE
)) {
1096 if (!JS_DefineProperty(cx
, protos
[0], js_lineNumber_str
,
1098 NULL
, NULL
, JSPROP_ENUMERATE
)) {
1103 * Add methods only to Exception.prototype, because ostensibly all
1104 * exception types delegate to that.
1106 if (!JS_DefineFunctions(cx
, protos
[0], exception_methods
))
1112 const JSErrorFormatString
*
1113 js_GetLocalizedErrorMessage(JSContext
* cx
, void *userRef
, const char *locale
,
1114 const uintN errorNumber
)
1116 const JSErrorFormatString
*errorString
= NULL
;
1118 if (cx
->localeCallbacks
&& cx
->localeCallbacks
->localeGetErrorMessage
) {
1119 errorString
= cx
->localeCallbacks
1120 ->localeGetErrorMessage(userRef
, locale
, errorNumber
);
1123 errorString
= js_GetErrorMessage(userRef
, locale
, errorNumber
);
1127 #if defined ( DEBUG_mccabe ) && defined ( PRINTNAMES )
1128 /* For use below... get character strings for error name and exception name */
1129 static struct exnname
{ char *name
; char *exception
; } errortoexnname
[] = {
1130 #define MSG_DEF(name, number, count, exception, format) \
1131 {#name, #exception},
1138 js_ErrorToException(JSContext
*cx
, const char *message
, JSErrorReport
*reportp
)
1140 JSErrNum errorNumber
;
1141 const JSErrorFormatString
*errorString
;
1144 JSTempValueRooter tvr
;
1146 JSObject
*errProto
, *errObject
;
1147 JSString
*messageStr
, *filenameStr
;
1150 * Tell our caller to report immediately if cx has no active frames, or if
1151 * this report is just a warning.
1154 if (!cx
->fp
|| JSREPORT_IS_WARNING(reportp
->flags
))
1157 /* Find the exception index associated with this error. */
1158 errorNumber
= (JSErrNum
) reportp
->errorNumber
;
1159 errorString
= js_GetLocalizedErrorMessage(cx
, NULL
, NULL
, errorNumber
);
1160 exn
= errorString
? (JSExnType
) errorString
->exnType
: JSEXN_NONE
;
1161 JS_ASSERT(exn
< JSEXN_LIMIT
);
1163 #if defined( DEBUG_mccabe ) && defined ( PRINTNAMES )
1164 /* Print the error name and the associated exception name to stderr */
1165 fprintf(stderr
, "%s\t%s\n",
1166 errortoexnname
[errorNumber
].name
,
1167 errortoexnname
[errorNumber
].exception
);
1171 * Return false (no exception raised) if no exception is associated
1172 * with the given error number.
1174 if (exn
== JSEXN_NONE
)
1178 * Prevent runaway recursion, via cx->generatingError. If an out-of-memory
1179 * error occurs, no exception object will be created, but we don't assume
1180 * that OOM is the only kind of error that subroutines of this function
1181 * called below might raise.
1183 if (cx
->generatingError
)
1186 /* After this point the control must flow through the label out. */
1187 cx
->generatingError
= JS_TRUE
;
1189 /* Protect the newly-created strings below from nesting GCs. */
1190 memset(tv
, 0, sizeof tv
);
1191 JS_PUSH_TEMP_ROOT(cx
, sizeof tv
/ sizeof tv
[0], tv
, &tvr
);
1194 * Try to get an appropriate prototype by looking up the corresponding
1195 * exception constructor name in the scope chain of the current context's
1196 * top stack frame, or in the global object if no frame is active.
1198 ok
= js_GetClassPrototype(cx
, NULL
, INT_TO_JSID(exceptions
[exn
].key
),
1202 tv
[0] = OBJECT_TO_JSVAL(errProto
);
1204 errObject
= js_NewObject(cx
, &js_ErrorClass
, errProto
, NULL
);
1209 tv
[1] = OBJECT_TO_JSVAL(errObject
);
1211 messageStr
= JS_NewStringCopyZ(cx
, message
);
1216 tv
[2] = STRING_TO_JSVAL(messageStr
);
1218 filenameStr
= JS_NewStringCopyZ(cx
, reportp
->filename
);
1223 tv
[3] = STRING_TO_JSVAL(filenameStr
);
1225 ok
= InitExnPrivate(cx
, errObject
, messageStr
, filenameStr
,
1226 reportp
->lineno
, reportp
);
1230 JS_SetPendingException(cx
, OBJECT_TO_JSVAL(errObject
));
1232 /* Flag the error report passed in to indicate an exception was raised. */
1233 reportp
->flags
|= JSREPORT_EXCEPTION
;
1236 JS_POP_TEMP_ROOT(cx
, &tvr
);
1237 cx
->generatingError
= JS_FALSE
;
1242 js_ReportUncaughtException(JSContext
*cx
)
1245 JSObject
*exnObject
;
1247 JSTempValueRooter tvr
;
1248 JSErrorReport
*reportp
, report
;
1253 if (!JS_IsExceptionPending(cx
))
1256 if (!JS_GetPendingException(cx
, &exn
))
1259 memset(roots
, 0, sizeof roots
);
1260 JS_PUSH_TEMP_ROOT(cx
, JS_ARRAY_LENGTH(roots
), roots
, &tvr
);
1263 * Because js_ValueToString below could error and an exception object
1264 * could become unrooted, we must root exnObject. Later, if exnObject is
1265 * non-null, we need to root other intermediates, so allocate an operand
1266 * stack segment to protect all of these values.
1268 if (JSVAL_IS_PRIMITIVE(exn
)) {
1271 exnObject
= JSVAL_TO_OBJECT(exn
);
1275 JS_ClearPendingException(cx
);
1276 reportp
= js_ErrorFromException(cx
, exn
);
1278 /* XXX L10N angels cry once again (see also jsemit.c, /L10N gaffes/) */
1279 str
= js_ValueToString(cx
, exn
);
1281 bytes
= "unknown (can't convert to string)";
1283 roots
[1] = STRING_TO_JSVAL(str
);
1284 bytes
= js_GetStringBytes(cx
, str
);
1294 OBJ_GET_CLASS(cx
, exnObject
) == &js_ErrorClass
) {
1295 const char *filename
;
1298 ok
= JS_GetProperty(cx
, exnObject
, js_message_str
, &roots
[2]);
1301 if (JSVAL_IS_STRING(roots
[2])) {
1302 bytes
= js_GetStringBytes(cx
, JSVAL_TO_STRING(roots
[2]));
1309 ok
= JS_GetProperty(cx
, exnObject
, js_fileName_str
, &roots
[3]);
1312 str
= js_ValueToString(cx
, roots
[3]);
1317 filename
= StringToFilename(cx
, str
);
1323 ok
= JS_GetProperty(cx
, exnObject
, js_lineNumber_str
, &roots
[4]);
1326 ok
= js_ValueToECMAUint32 (cx
, roots
[4], &lineno
);
1331 memset(&report
, 0, sizeof report
);
1332 report
.filename
= filename
;
1333 report
.lineno
= (uintN
) lineno
;
1337 JS_ReportErrorNumber(cx
, js_GetErrorMessage
, NULL
,
1338 JSMSG_UNCAUGHT_EXCEPTION
, bytes
);
1340 /* Flag the error as an exception. */
1341 reportp
->flags
|= JSREPORT_EXCEPTION
;
1342 js_ReportErrorAgain(cx
, bytes
, reportp
);
1346 JS_POP_TEMP_ROOT(cx
, &tvr
);