Save all modification
[mozilla-1.9/m8.git] / js / src / jsexn.c
blobeae0f7e3a24350c9c77e59fa1c0bb9b2948c921d
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
15 * License.
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
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.
25 * Contributor(s):
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.
45 #include "jsstddef.h"
46 #include <stdlib.h>
47 #include <string.h>
48 #include "jstypes.h"
49 #include "jsbit.h"
50 #include "jsutil.h" /* Added by JSIFY */
51 #include "jsprf.h"
52 #include "jsapi.h"
53 #include "jscntxt.h"
54 #include "jsconfig.h"
55 #include "jsdbgapi.h"
56 #include "jsexn.h"
57 #include "jsfun.h"
58 #include "jsinterp.h"
59 #include "jsnum.h"
60 #include "jsopcode.h"
61 #include "jsscript.h"
63 /* Forward declarations for js_ErrorClass's initializer. */
64 static JSBool
65 Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
67 static void
68 exn_finalize(JSContext *cx, JSObject *obj);
70 static void
71 exn_trace(JSTracer *trc, JSObject *obj);
73 static void
74 exn_finalize(JSContext *cx, JSObject *obj);
76 static JSBool
77 exn_enumerate(JSContext *cx, JSObject *obj);
79 static JSBool
80 exn_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
81 JSObject **objp);
83 JSClass js_ErrorClass = {
84 js_Error_str,
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 {
94 JSString *funName;
95 size_t argc;
96 const char *filename;
97 uintN ulineno;
98 } JSStackTraceElem;
100 typedef struct JSExnPrivate {
101 /* A copy of the JSErrorReport originally generated. */
102 JSErrorReport *errorReport;
103 JSString *message;
104 JSString *filename;
105 uintN lineno;
106 size_t stackDepth;
107 JSStackTraceElem stackElems[1];
108 } JSExnPrivate;
110 static JSString *
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:
119 * JSErrorReport
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);
132 size_t filenameSize;
133 size_t linebufSize;
134 size_t uclinebufSize;
135 size_t ucmessageSize;
136 size_t i, argsArraySize, argsCopySize, argSize;
137 size_t mallocSize;
138 JSErrorReport *copy;
139 uint8 *cursor;
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;
146 ucmessageSize = 0;
147 argsArraySize = 0;
148 argsCopySize = 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. */
156 JS_ASSERT(i != 0);
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);
168 if (!cursor)
169 return NULL;
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);
182 cursor += 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 -
200 report->uclinebuf);
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 -
210 report->linebuf);
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;
227 #undef JS_CHARS_SIZE
228 return copy;
231 static jsval *
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);
245 static JSBool
246 InitExnPrivate(JSContext *cx, JSObject *exnObject, JSString *message,
247 JSString *filename, uintN lineno, JSErrorReport *report)
249 JSCheckAccessOp checkAccess;
250 JSErrorReporter older;
251 JSExceptionState *state;
252 jsval callerid, v;
253 JSStackFrame *fp, *fpstop;
254 size_t stackDepth, valueCount, size;
255 JSBool overflow;
256 JSExnPrivate *priv;
257 JSStackTraceElem *elem;
258 jsval *values;
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);
274 stackDepth = 0;
275 valueCount = 0;
276 for (fp = cx->fp; fp; fp = fp->down) {
277 if (fp->fun && fp->argv) {
278 v = JSVAL_NULL;
279 if (checkAccess &&
280 !checkAccess(cx, fp->callee, callerid, JSACC_READ, &v)) {
281 break;
283 valueCount += fp->argc;
285 ++stackDepth;
287 JS_RestoreExceptionState(cx, state);
288 JS_SetErrorReporter(cx, older);
289 fpstop = fp;
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);
296 if (overflow) {
297 JS_ReportOutOfMemory(cx);
298 return JS_FALSE;
300 priv = (JSExnPrivate *)JS_malloc(cx, size);
301 if (!priv)
302 return JS_FALSE;
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
307 * here.
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) {
318 if (!fp->fun) {
319 elem->funName = NULL;
320 elem->argc = 0;
321 } else {
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));
327 values += fp->argc;
329 elem->ulineno = 0;
330 elem->filename = NULL;
331 if (fp->script) {
332 elem->filename = fp->script->filename;
333 if (fp->pc)
334 elem->ulineno = js_PCToLineNumber(cx, fp->script, fp->pc);
336 ++elem;
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));
343 if (report) {
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
348 * JSTokenStream.
350 priv->errorReport = CopyErrorReport(cx, report);
351 if (!priv->errorReport) {
352 /* The finalizer realeases priv since it is in the private slot. */
353 return JS_FALSE;
357 return JS_TRUE;
360 static JSExnPrivate *
361 GetExnPrivate(JSContext *cx, JSObject *obj)
363 jsval privateValue;
364 JSExnPrivate *priv;
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))
369 return NULL;
370 priv = (JSExnPrivate *)JSVAL_TO_PRIVATE(privateValue);
371 JS_ASSERT(priv);
372 return priv;
375 static void
376 exn_trace(JSTracer *trc, JSObject *obj)
378 JSExnPrivate *priv;
379 JSStackTraceElem *elem;
380 size_t vcount, i;
381 jsval *vp, v;
383 priv = GetExnPrivate(trc->context, obj);
384 if (priv) {
385 if (priv->message)
386 JS_CALL_STRING_TRACER(trc, priv->message, "exception message");
387 if (priv->filename)
388 JS_CALL_STRING_TRACER(trc, priv->filename, "exception filename");
390 elem = priv->stackElems;
391 for (vcount = i = 0; i != priv->stackDepth; ++i, ++elem) {
392 if (elem->funName) {
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) {
402 v = *vp;
403 JS_CALL_VALUE_TRACER(trc, v, "stack trace argument");
408 static void
409 exn_finalize(JSContext *cx, JSObject *obj)
411 JSExnPrivate *priv;
413 priv = GetExnPrivate(cx, obj);
414 if (priv) {
415 if (priv->errorReport)
416 JS_free(cx, priv->errorReport);
417 JS_free(cx, priv);
421 static JSBool
422 exn_enumerate(JSContext *cx, JSObject *obj)
424 JSAtomState *atomState;
425 uintN i;
426 JSAtom *atom;
427 JSObject *pobj;
428 JSProperty *prop;
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))
442 return JS_FALSE;
443 if (prop)
444 OBJ_DROP_PROPERTY(cx, pobj, prop);
446 return JS_TRUE;
449 static JSBool
450 exn_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
451 JSObject **objp)
453 JSExnPrivate *priv;
454 JSString *str;
455 JSAtom *atom;
456 JSString *stack;
457 const char *prop;
458 jsval v;
460 *objp = NULL;
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);
469 goto define;
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);
476 goto define;
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);
483 goto define;
486 atom = cx->runtime->atomState.stackAtom;
487 if (str == ATOM_TO_STRING(atom)) {
488 stack = StackTraceToString(cx, priv);
489 if (!stack)
490 return JS_FALSE;
492 /* Allow to GC all things that were used to build stack trace. */
493 priv->stackDepth = 0;
494 prop = js_stack_str;
495 v = STRING_TO_JSVAL(stack);
496 goto define;
499 return JS_TRUE;
501 define:
502 if (!JS_DefineProperty(cx, obj, prop, v, NULL, NULL, JSPROP_ENUMERATE))
503 return JS_FALSE;
504 *objp = obj;
505 return JS_TRUE;
508 JSErrorReport *
509 js_ErrorFromException(JSContext *cx, jsval exn)
511 JSObject *obj;
512 JSExnPrivate *priv;
514 if (JSVAL_IS_PRIMITIVE(exn))
515 return NULL;
516 obj = JSVAL_TO_OBJECT(exn);
517 if (OBJ_GET_CLASS(cx, obj) != &js_ErrorClass)
518 return NULL;
519 priv = GetExnPrivate(cx, obj);
520 if (!priv)
521 return NULL;
522 return priv->errorReport;
525 struct JSExnSpec {
526 int protoIndex;
527 const char *name;
528 JSProtoKey key;
529 JSNative native;
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) \
539 static JSBool \
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}
568 static JSString *
569 ValueToShortSource(JSContext *cx, jsval v)
571 JSString *str;
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]");
589 } else {
591 * XXX Avoid toString on objects, it takes too long and uses too much
592 * memory, for too many classes (see Mozilla bug 166743).
594 char buf[100];
595 JS_snprintf(buf, sizeof buf, "[object %s]",
596 OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(v))->name);
597 str = JS_NewStringCopyZ(cx, buf);
599 return str;
602 static JSString *
603 StackTraceToString(JSContext *cx, JSExnPrivate *priv)
605 jschar *stackbuf;
606 size_t stacklen, stackmax;
607 JSStackTraceElem *elem, *endElem;
608 jsval *values;
609 size_t i;
610 JSString *str;
611 const char *cp;
612 char ulnbuf[11];
614 /* After this point, failing control flow must goto bad. */
615 stackbuf = NULL;
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) \
622 JS_BEGIN_MACRO \
623 if (stacklen == stackmax) { \
624 void *ptr_; \
625 if (stackmax >= STACK_LENGTH_LIMIT) \
626 goto done; \
627 stackmax = stackmax ? 2 * stackmax : 64; \
628 ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \
629 if (!ptr_) \
630 goto bad; \
631 stackbuf = (jschar *) ptr_; \
633 stackbuf[stacklen++] = (c); \
634 JS_END_MACRO
636 #define APPEND_STRING_TO_STACK(str) \
637 JS_BEGIN_MACRO \
638 JSString *str_ = str; \
639 jschar *chars_; \
640 size_t length_; \
642 JSSTRING_CHARS_AND_LENGTH(str_, chars_, length_); \
643 if (length_ > stackmax - stacklen) { \
644 void *ptr_; \
645 if (stackmax >= STACK_LENGTH_LIMIT || \
646 length_ >= STACK_LENGTH_LIMIT - stacklen) { \
647 goto done; \
649 stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \
650 ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \
651 if (!ptr_) \
652 goto bad; \
653 stackbuf = (jschar *) ptr_; \
655 js_strncpy(stackbuf + stacklen, chars_, length_); \
656 stacklen += length_; \
657 JS_END_MACRO
659 values = GetStackTraceValueBuffer(priv);
660 elem = priv->stackElems;
661 for (endElem = elem + priv->stackDepth; elem != endElem; elem++) {
662 if (elem->funName) {
663 APPEND_STRING_TO_STACK(elem->funName);
664 APPEND_CHAR_TO_STACK('(');
665 for (i = 0; i != elem->argc; i++, values++) {
666 if (i > 0)
667 APPEND_CHAR_TO_STACK(',');
668 str = ValueToShortSource(cx, *values);
669 if (!str)
670 goto bad;
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
690 done:
691 if (stacklen == 0) {
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));
702 if (shrunk)
703 stackbuf = (jschar *) shrunk;
706 stackbuf[stacklen] = 0;
707 str = js_NewString(cx, stackbuf, stacklen);
708 if (str)
709 return str;
711 bad:
712 if (stackbuf)
713 JS_free(cx, stackbuf);
714 return NULL;
717 /* XXXbe Consolidate the ugly truth that we don't treat filename as UTF-8
718 with these two functions. */
719 static JSString *
720 FilenameToString(JSContext *cx, const char *filename)
722 return JS_NewStringCopyZ(cx, filename);
725 static const char *
726 StringToFilename(JSContext *cx, JSString *str)
728 return js_GetStringBytes(cx, str);
731 static JSBool
732 Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
734 uint32 lineno;
735 JSString *message, *filename;
736 JSStackFrame *fp;
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),
749 rval))
750 return JS_FALSE;
751 obj = js_NewObject(cx, &js_ErrorClass, JSVAL_TO_OBJECT(*rval), NULL);
752 if (!obj)
753 return JS_FALSE;
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. */
765 if (argc != 0) {
766 message = js_ValueToString(cx, argv[0]);
767 if (!message)
768 return JS_FALSE;
769 argv[0] = STRING_TO_JSVAL(message);
770 } else {
771 message = cx->runtime->emptyString;
774 /* Set the 'fileName' property. */
775 if (argc > 1) {
776 filename = js_ValueToString(cx, argv[1]);
777 if (!filename)
778 return JS_FALSE;
779 argv[1] = STRING_TO_JSVAL(filename);
780 fp = NULL;
781 } else {
782 fp = JS_GetScriptedCaller(cx, NULL);
783 if (fp) {
784 filename = FilenameToString(cx, fp->script->filename);
785 if (!filename)
786 return JS_FALSE;
787 } else {
788 filename = cx->runtime->emptyString;
792 /* Set the 'lineNumber' property. */
793 if (argc > 2) {
794 if (!js_ValueToECMAUint32(cx, argv[2], &lineno))
795 return JS_FALSE;
796 } else {
797 if (!fp)
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);
807 * Convert to string.
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.
813 static JSBool
814 exn_toString(JSContext *cx, uintN argc, jsval *vp)
816 JSObject *obj;
817 jsval v;
818 JSString *name, *message, *result;
819 jschar *chars, *cp;
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),
825 &v)) {
826 return JS_FALSE;
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))
832 return JS_FALSE;
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));
841 if (!chars)
842 return JS_FALSE;
844 if (name_length) {
845 js_strncpy(cp, JSSTRING_CHARS(name), name_length);
846 cp += name_length;
847 *cp++ = ':'; *cp++ = ' ';
849 js_strncpy(cp, JSSTRING_CHARS(message), message_length);
850 cp += message_length;
851 *cp = 0;
853 result = js_NewString(cx, chars, length);
854 if (!result) {
855 JS_free(cx, chars);
856 return JS_FALSE;
858 } else {
859 result = name;
862 *vp = STRING_TO_JSVAL(result);
863 return JS_TRUE;
866 #if JS_HAS_TOSOURCE
868 * Return a string that may eval to something similar to the original object.
870 static JSBool
871 exn_toSource(JSContext *cx, uintN argc, jsval *vp)
873 jsval *localroots;
874 JSObject *obj;
875 JSString *name, *message, *filename, *lineno_as_str, *result;
876 uint32 lineno;
877 size_t lineno_length, name_length, message_length, filename_length, length;
878 jschar *chars, *cp;
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),
885 vp)) {
886 return JS_FALSE;
888 name = js_ValueToString(cx, *vp);
889 if (!name)
890 return JS_FALSE;
891 *vp = STRING_TO_JSVAL(name);
893 if (!JS_GetProperty(cx, obj, js_message_str, &localroots[0]) ||
894 !(message = js_ValueToSource(cx, localroots[0]))) {
895 return JS_FALSE;
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]))) {
901 return JS_FALSE;
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)) {
907 return JS_FALSE;
910 if (lineno != 0) {
911 lineno_as_str = js_ValueToString(cx, localroots[2]);
912 if (!lineno_as_str)
913 return JS_FALSE;
914 lineno_length = JSSTRING_LENGTH(lineno_as_str);
915 } else {
916 lineno_as_str = NULL;
917 lineno_length = 0;
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;
929 if (lineno_as_str) {
930 /* append lineno as ``, {lineno_as_str}'' */
931 length += 2 + lineno_length;
933 } else {
934 if (lineno_as_str) {
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));
944 if (!chars)
945 return JS_FALSE;
947 *cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' ';
948 js_strncpy(cp, JSSTRING_CHARS(name), name_length);
949 cp += name_length;
950 *cp++ = '(';
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;
961 } else {
962 if (lineno_as_str) {
964 * no filename, but have line number,
965 * need to append ``, "", {lineno_as_str}''
967 *cp++ = ','; *cp++ = ' '; *cp++ = '"'; *cp++ = '"';
970 if (lineno_as_str) {
971 /* append lineno as ``, {lineno_as_str}'' */
972 *cp++ = ','; *cp++ = ' ';
973 js_strncpy(cp, JSSTRING_CHARS(lineno_as_str), lineno_length);
974 cp += lineno_length;
977 *cp++ = ')'; *cp++ = ')'; *cp = 0;
979 result = js_NewString(cx, chars, length);
980 if (!result) {
981 JS_free(cx, chars);
982 return JS_FALSE;
984 *vp = STRING_TO_JSVAL(result);
985 return JS_TRUE;
987 #endif
989 static JSFunctionSpec exception_methods[] = {
990 #if JS_HAS_TOSOURCE
991 JS_FN(js_toSource_str, exn_toSource, 0,0,0,3),
992 #endif
993 JS_FN(js_toString_str, exn_toString, 0,0,0,0),
994 JS_FS_END
997 JSObject *
998 js_InitExceptionClasses(JSContext *cx, JSObject *obj)
1000 JSObject *obj_proto, *protos[JSEXN_LIMIT];
1001 int i;
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),
1014 &obj_proto)) {
1015 return NULL;
1018 if (!js_EnterLocalRootScope(cx))
1019 return NULL;
1021 /* Initialize the prototypes first. */
1022 for (i = 0; exceptions[i].name != 0; i++) {
1023 JSAtom *atom;
1024 JSFunction *fun;
1025 JSObject *funobj;
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]
1033 : obj_proto,
1034 obj);
1035 if (!protos[i])
1036 break;
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);
1044 if (!fun)
1045 break;
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)) {
1056 break;
1059 /* proto bootstrap bit from JS_InitClass omitted. */
1060 nameString = JS_NewStringCopyZ(cx, exceptions[i].name);
1061 if (!nameString)
1062 break;
1064 /* Add the name property to the prototype. */
1065 if (!JS_DefineProperty(cx, protos[i], js_name_str,
1066 STRING_TO_JSVAL(nameString),
1067 NULL, NULL,
1068 JSPROP_ENUMERATE)) {
1069 break;
1072 /* Finally, stash the constructor for later uses. */
1073 if (!js_SetClassObject(cx, obj, exceptions[i].key, funobj))
1074 break;
1077 js_LeaveLocalRootScope(cx);
1078 if (exceptions[i].name)
1079 return NULL;
1082 * Add an empty message property. (To Exception.prototype only,
1083 * because this property will be the same for all the exception
1084 * protos.)
1086 if (!JS_DefineProperty(cx, protos[0], js_message_str,
1087 STRING_TO_JSVAL(cx->runtime->emptyString),
1088 NULL, NULL, JSPROP_ENUMERATE)) {
1089 return NULL;
1091 if (!JS_DefineProperty(cx, protos[0], js_fileName_str,
1092 STRING_TO_JSVAL(cx->runtime->emptyString),
1093 NULL, NULL, JSPROP_ENUMERATE)) {
1094 return NULL;
1096 if (!JS_DefineProperty(cx, protos[0], js_lineNumber_str,
1097 INT_TO_JSVAL(0),
1098 NULL, NULL, JSPROP_ENUMERATE)) {
1099 return NULL;
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))
1107 return NULL;
1109 return protos[0];
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);
1122 if (!errorString)
1123 errorString = js_GetErrorMessage(userRef, locale, errorNumber);
1124 return errorString;
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},
1132 #include "js.msg"
1133 #undef MSG_DEF
1135 #endif /* DEBUG */
1137 JSBool
1138 js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp)
1140 JSErrNum errorNumber;
1141 const JSErrorFormatString *errorString;
1142 JSExnType exn;
1143 jsval tv[4];
1144 JSTempValueRooter tvr;
1145 JSBool ok;
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.
1153 JS_ASSERT(reportp);
1154 if (!cx->fp || JSREPORT_IS_WARNING(reportp->flags))
1155 return JS_FALSE;
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);
1168 #endif
1171 * Return false (no exception raised) if no exception is associated
1172 * with the given error number.
1174 if (exn == JSEXN_NONE)
1175 return JS_FALSE;
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)
1184 return JS_FALSE;
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),
1199 &errProto);
1200 if (!ok)
1201 goto out;
1202 tv[0] = OBJECT_TO_JSVAL(errProto);
1204 errObject = js_NewObject(cx, &js_ErrorClass, errProto, NULL);
1205 if (!errObject) {
1206 ok = JS_FALSE;
1207 goto out;
1209 tv[1] = OBJECT_TO_JSVAL(errObject);
1211 messageStr = JS_NewStringCopyZ(cx, message);
1212 if (!messageStr) {
1213 ok = JS_FALSE;
1214 goto out;
1216 tv[2] = STRING_TO_JSVAL(messageStr);
1218 filenameStr = JS_NewStringCopyZ(cx, reportp->filename);
1219 if (!filenameStr) {
1220 ok = JS_FALSE;
1221 goto out;
1223 tv[3] = STRING_TO_JSVAL(filenameStr);
1225 ok = InitExnPrivate(cx, errObject, messageStr, filenameStr,
1226 reportp->lineno, reportp);
1227 if (!ok)
1228 goto out;
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;
1235 out:
1236 JS_POP_TEMP_ROOT(cx, &tvr);
1237 cx->generatingError = JS_FALSE;
1238 return ok;
1241 JSBool
1242 js_ReportUncaughtException(JSContext *cx)
1244 jsval exn;
1245 JSObject *exnObject;
1246 jsval roots[5];
1247 JSTempValueRooter tvr;
1248 JSErrorReport *reportp, report;
1249 JSString *str;
1250 const char *bytes;
1251 JSBool ok;
1253 if (!JS_IsExceptionPending(cx))
1254 return JS_TRUE;
1256 if (!JS_GetPendingException(cx, &exn))
1257 return JS_FALSE;
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)) {
1269 exnObject = NULL;
1270 } else {
1271 exnObject = JSVAL_TO_OBJECT(exn);
1272 roots[0] = 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);
1280 if (!str) {
1281 bytes = "unknown (can't convert to string)";
1282 } else {
1283 roots[1] = STRING_TO_JSVAL(str);
1284 bytes = js_GetStringBytes(cx, str);
1285 if (!bytes) {
1286 ok = JS_FALSE;
1287 goto out;
1290 ok = JS_TRUE;
1292 if (!reportp &&
1293 exnObject &&
1294 OBJ_GET_CLASS(cx, exnObject) == &js_ErrorClass) {
1295 const char *filename;
1296 uint32 lineno;
1298 ok = JS_GetProperty(cx, exnObject, js_message_str, &roots[2]);
1299 if (!ok)
1300 goto out;
1301 if (JSVAL_IS_STRING(roots[2])) {
1302 bytes = js_GetStringBytes(cx, JSVAL_TO_STRING(roots[2]));
1303 if (!bytes) {
1304 ok = JS_FALSE;
1305 goto out;
1309 ok = JS_GetProperty(cx, exnObject, js_fileName_str, &roots[3]);
1310 if (!ok)
1311 goto out;
1312 str = js_ValueToString(cx, roots[3]);
1313 if (!str) {
1314 ok = JS_FALSE;
1315 goto out;
1317 filename = StringToFilename(cx, str);
1318 if (!filename) {
1319 ok = JS_FALSE;
1320 goto out;
1323 ok = JS_GetProperty(cx, exnObject, js_lineNumber_str, &roots[4]);
1324 if (!ok)
1325 goto out;
1326 ok = js_ValueToECMAUint32 (cx, roots[4], &lineno);
1327 if (!ok)
1328 goto out;
1330 reportp = &report;
1331 memset(&report, 0, sizeof report);
1332 report.filename = filename;
1333 report.lineno = (uintN) lineno;
1336 if (!reportp) {
1337 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1338 JSMSG_UNCAUGHT_EXCEPTION, bytes);
1339 } else {
1340 /* Flag the error as an exception. */
1341 reportp->flags |= JSREPORT_EXCEPTION;
1342 js_ReportErrorAgain(cx, bytes, reportp);
1345 out:
1346 JS_POP_TEMP_ROOT(cx, &tvr);
1347 return ok;