1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
3 *
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
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/
11 *
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.
16 *
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
19 *
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.
24 *
25 * Contributor(s):
26 *
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.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 /*
42 * JS standard exception implementation.
43 */
44
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"
62
63 /* Forward declarations for js_ErrorClass's initializer. */
64 static JSBool
65 Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
66
67 static void
68 exn_finalize(JSContext *cx, JSObject *obj);
69
70 static uint32
71 exn_mark(JSContext *cx, JSObject *obj, void *arg);
72
73 static void
74 exn_finalize(JSContext *cx, JSObject *obj);
75
76 static JSBool
77 exn_enumerate(JSContext *cx, JSObject *obj);
78
79 static JSBool
80 exn_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
81 JSObject **objp);
82
83 JSClass js_ErrorClass = {
84 js_Error_str,
85 JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE |
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, exn_mark, NULL
91 };
92
93 typedef struct JSStackTraceElem {
94 JSString *funName;
95 size_t argc;
96 const char *filename;
97 uintN ulineno;
98 } JSStackTraceElem;
99
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;
109
110 static JSString *
111 StackTraceToString(JSContext *cx, JSExnPrivate *priv);
112
113 static JSErrorReport *
CopyErrorReport(JSContext * cx,JSErrorReport * report)114 CopyErrorReport(JSContext *cx, JSErrorReport *report)
115 {
116 /*
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.
128 */
129 JS_STATIC_ASSERT(sizeof(JSErrorReport) % sizeof(const char *) == 0);
130 JS_STATIC_ASSERT(sizeof(const char *) % sizeof(jschar) == 0);
131
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;
140
141 #define JS_CHARS_SIZE(jschars) ((js_strlen(jschars) + 1) * sizeof(jschar))
142
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]);
154
155 /* Non-null messageArgs should have at least one non-null arg. */
156 JS_ASSERT(i != 0);
157 argsArraySize = (i + 1) * sizeof(const jschar *);
158 }
159 }
160
161 /*
162 * The mallocSize can not overflow since it represents the sum of the
163 * sizes of already allocated objects.
164 */
165 mallocSize = sizeof(JSErrorReport) + argsArraySize + argsCopySize +
166 ucmessageSize + uclinebufSize + linebufSize + filenameSize;
167 cursor = (uint8 *)JS_malloc(cx, mallocSize);
168 if (!cursor)
169 return NULL;
170
171 copy = (JSErrorReport *)cursor;
172 memset(cursor, 0, sizeof(JSErrorReport));
173 cursor += sizeof(JSErrorReport);
174
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;
183 }
184 copy->messageArgs[i] = NULL;
185 JS_ASSERT(cursor == (uint8 *)copy->messageArgs[0] + argsCopySize);
186 }
187
188 if (report->ucmessage) {
189 copy->ucmessage = (const jschar *)cursor;
190 memcpy(cursor, report->ucmessage, ucmessageSize);
191 cursor += ucmessageSize;
192 }
193
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);
201 }
202 }
203
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);
211 }
212 }
213
214 if (report->filename) {
215 copy->filename = (const char *)cursor;
216 memcpy(cursor, report->filename, filenameSize);
217 }
218 JS_ASSERT(cursor + filenameSize == (uint8 *)copy + mallocSize);
219
220 /* Copy non-pointer members. */
221 copy->lineno = report->lineno;
222 copy->errorNumber = report->errorNumber;
223
224 /* Note that this is before it gets flagged with JSREPORT_EXCEPTION */
225 copy->flags = report->flags;
226
227 #undef JS_CHARS_SIZE
228 return copy;
229 }
230
231 static jsval *
GetStackTraceValueBuffer(JSExnPrivate * priv)232 GetStackTraceValueBuffer(JSExnPrivate *priv)
233 {
234 /*
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.
239 */
240 JS_STATIC_ASSERT(sizeof(JSStackTraceElem) % sizeof(jsval) == 0);
241
242 return (jsval *)(priv->stackElems + priv->stackDepth);
243 }
244
245 static JSBool
InitExnPrivate(JSContext * cx,JSObject * exnObject,JSString * message,JSString * filename,uintN lineno,JSErrorReport * report)246 InitExnPrivate(JSContext *cx, JSObject *exnObject, JSString *message,
247 JSString *filename, uintN lineno, JSErrorReport *report)
248 {
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;
259
260 JS_ASSERT(OBJ_GET_CLASS(cx, exnObject) == &js_ErrorClass);
261
262 /*
263 * Prepare stack trace data.
264 *
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.
268 */
269 checkAccess = cx->runtime->checkObjectAccess;
270 older = JS_SetErrorReporter(cx, NULL);
271 state = JS_SaveExceptionState(cx);
272
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 if (checkAccess) {
279 v = fp->argv[-2];
280 if (!JSVAL_IS_PRIMITIVE(v) &&
281 !checkAccess(cx, JSVAL_TO_OBJECT(v), callerid,
282 JSACC_READ, &v /* ignored */)) {
283 break;
284 }
285 }
286 valueCount += fp->argc;
287 }
288 ++stackDepth;
289 }
290 JS_RestoreExceptionState(cx, state);
291 JS_SetErrorReporter(cx, older);
292 fpstop = fp;
293
294 size = offsetof(JSExnPrivate, stackElems);
295 overflow = (stackDepth > ((size_t)-1 - size) / sizeof(JSStackTraceElem));
296 size += stackDepth * sizeof(JSStackTraceElem);
297 overflow |= (valueCount > ((size_t)-1 - size) / sizeof(jsval));
298 size += valueCount * sizeof(jsval);
299 if (overflow) {
300 JS_ReportOutOfMemory(cx);
301 return JS_FALSE;
302 }
303 priv = (JSExnPrivate *)JS_malloc(cx, size);
304 if (!priv)
305 return JS_FALSE;
306
307 /*
308 * We initialize errorReport with a copy of report after setting the
309 * private slot, to prevent GC accessing a junk value we clear the field
310 * here.
311 */
312 priv->errorReport = NULL;
313 priv->message = message;
314 priv->filename = filename;
315 priv->lineno = lineno;
316 priv->stackDepth = stackDepth;
317
318 values = GetStackTraceValueBuffer(priv);
319 elem = priv->stackElems;
320 for (fp = cx->fp; fp != fpstop; fp = fp->down) {
321 if (!fp->fun) {
322 elem->funName = NULL;
323 elem->argc = 0;
324 } else {
325 elem->funName = fp->fun->atom
326 ? ATOM_TO_STRING(fp->fun->atom)
327 : cx->runtime->emptyString;
328 elem->argc = fp->argc;
329 memcpy(values, fp->argv, fp->argc * sizeof(jsval));
330 values += fp->argc;
331 }
332 elem->ulineno = 0;
333 elem->filename = NULL;
334 if (fp->script) {
335 elem->filename = fp->script->filename;
336 if (fp->pc)
337 elem->ulineno = js_PCToLineNumber(cx, fp->script, fp->pc);
338 }
339 ++elem;
340 }
341 JS_ASSERT(priv->stackElems + stackDepth == elem);
342 JS_ASSERT(GetStackTraceValueBuffer(priv) + valueCount == values);
343
344 OBJ_SET_SLOT(cx, exnObject, JSSLOT_PRIVATE, PRIVATE_TO_JSVAL(priv));
345
346 if (report) {
347 /*
348 * Construct a new copy of the error report struct. We can't use the
349 * error report struct that was passed in, because it's allocated on
350 * the stack, and also because it may point to transient data in the
351 * JSTokenStream.
352 */
353 priv->errorReport = CopyErrorReport(cx, report);
354 if (!priv->errorReport) {
355 /* The finalizer realeases priv since it is in the private slot. */
356 return JS_FALSE;
357 }
358 }
359
360 return JS_TRUE;
361 }
362
363 static JSExnPrivate *
GetExnPrivate(JSContext * cx,JSObject * obj)364 GetExnPrivate(JSContext *cx, JSObject *obj)
365 {
366 jsval privateValue;
367 JSExnPrivate *priv;
368
369 JS_ASSERT(OBJ_GET_CLASS(cx, obj) == &js_ErrorClass);
370 privateValue = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
371 if (JSVAL_IS_VOID(privateValue))
372 return NULL;
373 priv = (JSExnPrivate *)JSVAL_TO_PRIVATE(privateValue);
374 JS_ASSERT(priv);
375 return priv;
376 }
377
378 static uint32
exn_mark(JSContext * cx,JSObject * obj,void * arg)379 exn_mark(JSContext *cx, JSObject *obj, void *arg)
380 {
381 JSExnPrivate *priv;
382 JSStackTraceElem *elem;
383 size_t vcount, i;
384 jsval *vp, v;
385
386 priv = GetExnPrivate(cx, obj);
387 if (priv) {
388 GC_MARK(cx, priv->message, "exception message");
389 GC_MARK(cx, priv->filename, "exception filename");
390 elem = priv->stackElems;
391 for (vcount = i = 0; i != priv->stackDepth; ++i, ++elem) {
392 if (elem->funName)
393 GC_MARK(cx, elem->funName, "stack trace function name");
394 if (elem->filename)
395 js_MarkScriptFilename(elem->filename);
396 vcount += elem->argc;
397 }
398 vp = GetStackTraceValueBuffer(priv);
399 for (i = 0; i != vcount; ++i, ++vp) {
400 v = *vp;
401 if (JSVAL_IS_GCTHING(v))
402 GC_MARK(cx, JSVAL_TO_GCTHING(v), "stack trace argument");
403 }
404 }
405 return 0;
406 }
407
408 static void
exn_finalize(JSContext * cx,JSObject * obj)409 exn_finalize(JSContext *cx, JSObject *obj)
410 {
411 JSExnPrivate *priv;
412
413 priv = GetExnPrivate(cx, obj);
414 if (priv) {
415 if (priv->errorReport)
416 JS_free(cx, priv->errorReport);
417 JS_free(cx, priv);
418 }
419 }
420
421 static JSBool
exn_enumerate(JSContext * cx,JSObject * obj)422 exn_enumerate(JSContext *cx, JSObject *obj)
423 {
424 JSAtomState *atomState;
425 uintN i;
426 JSAtom *atom;
427 JSObject *pobj;
428 JSProperty *prop;
429
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),
436 };
437
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);
445 }
446 return JS_TRUE;
447 }
448
449 static JSBool
exn_resolve(JSContext * cx,JSObject * obj,jsval id,uintN flags,JSObject ** objp)450 exn_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
451 JSObject **objp)
452 {
453 JSExnPrivate *priv;
454 JSString *str;
455 JSAtom *atom;
456 JSString *stack;
457 const char *prop;
458 jsval v;
459
460 *objp = NULL;
461 priv = GetExnPrivate(cx, obj);
462 if (priv && JSVAL_IS_STRING(id)) {
463 str = JSVAL_TO_STRING(id);
464
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;
470 }
471
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;
477 }
478
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;
484 }
485
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;
491
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;
497 }
498 }
499 return JS_TRUE;
500
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;
506 }
507
508 JSErrorReport *
js_ErrorFromException(JSContext * cx,jsval exn)509 js_ErrorFromException(JSContext *cx, jsval exn)
510 {
511 JSObject *obj;
512 JSExnPrivate *priv;
513
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;
523 }
524
525 struct JSExnSpec {
526 int protoIndex;
527 const char *name;
528 JSProtoKey key;
529 JSNative native;
530 };
531
532 /*
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.
537 */
538 #define MAKE_EXCEPTION_CTOR(name) \
539 static JSBool \
540 name(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) \
541 { \
542 return Exception(cx, obj, argc, argv, rval); \
543 }
544
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)
553
554 #undef MAKE_EXCEPTION_CTOR
555
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}
566 };
567
568 static JSString *
ValueToShortSource(JSContext * cx,jsval v)569 ValueToShortSource(JSContext *cx, jsval v)
570 {
571 JSString *str;
572
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)) {
577 /*
578 * XXX Avoid function decompilation bloat for now.
579 */
580 str = JS_GetFunctionId(JS_ValueToFunction(cx, v));
581 if (!str && !(str = js_ValueToSource(cx, v))) {
582 /*
583 * Continue to soldier on if the function couldn't be
584 * converted into a string.
585 */
586 JS_ClearPendingException(cx);
587 str = JS_NewStringCopyZ(cx, "[unknown function]");
588 }
589 } else {
590 /*
591 * XXX Avoid toString on objects, it takes too long and uses too much
592 * memory, for too many classes (see Mozilla bug 166743).
593 */
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);
598 }
599 return str;
600 }
601
602 static JSString *
StackTraceToString(JSContext * cx,JSExnPrivate * priv)603 StackTraceToString(JSContext *cx, JSExnPrivate *priv)
604 {
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];
613
614 /* After this point, failing control flow must goto bad. */
615 stackbuf = NULL;
616 stacklen = stackmax = 0;
617
618 /* Limit the stackbuf length to a reasonable value to avoid overflow checks. */
619 #define STACK_LENGTH_LIMIT JS_BIT(20)
620
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 = ptr_; \
632 } \
633 stackbuf[stacklen++] = (c); \
634 JS_END_MACRO
635
636 #define APPEND_STRING_TO_STACK(str) \
637 JS_BEGIN_MACRO \
638 JSString *str_ = str; \
639 size_t length_ = JSSTRING_LENGTH(str_); \
640 if (length_ > stackmax - stacklen) { \
641 void *ptr_; \
642 if (stackmax >= STACK_LENGTH_LIMIT || \
643 length_ >= STACK_LENGTH_LIMIT - stacklen) { \
644 goto done; \
645 } \
646 stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \
647 ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \
648 if (!ptr_) \
649 goto bad; \
650 stackbuf = ptr_; \
651 } \
652 js_strncpy(stackbuf + stacklen, JSSTRING_CHARS(str_), length_); \
653 stacklen += length_; \
654 JS_END_MACRO
655
656 values = GetStackTraceValueBuffer(priv);
657 elem = priv->stackElems;
658 for (endElem = elem + priv->stackDepth; elem != endElem; elem++) {
659 if (elem->funName) {
660 APPEND_STRING_TO_STACK(elem->funName);
661 APPEND_CHAR_TO_STACK('(');
662 for (i = 0; i != elem->argc; i++, values++) {
663 if (i > 0)
664 APPEND_CHAR_TO_STACK(',');
665 str = ValueToShortSource(cx, *values);
666 if (!str)
667 goto bad;
668 APPEND_STRING_TO_STACK(str);
669 }
670 APPEND_CHAR_TO_STACK(')');
671 }
672 APPEND_CHAR_TO_STACK('@');
673 if (elem->filename) {
674 for (cp = elem->filename; *cp; cp++)
675 APPEND_CHAR_TO_STACK(*cp);
676 }
677 APPEND_CHAR_TO_STACK(':');
678 JS_snprintf(ulnbuf, sizeof ulnbuf, "%u", elem->ulineno);
679 for (cp = ulnbuf; *cp; cp++)
680 APPEND_CHAR_TO_STACK(*cp);
681 APPEND_CHAR_TO_STACK('\n');
682 }
683 #undef APPEND_CHAR_TO_STACK
684 #undef APPEND_STRING_TO_STACK
685 #undef STACK_LENGTH_LIMIT
686
687 done:
688 if (stacklen == 0) {
689 JS_ASSERT(!stackbuf);
690 return cx->runtime->emptyString;
691 }
692 if (stacklen < stackmax) {
693 /*
694 * Realloc can fail when shrinking on some FreeBSD versions, so
695 * don't use JS_realloc here; simply let the oversized allocation
696 * be owned by the string in that rare case.
697 */
698 void *shrunk = JS_realloc(cx, stackbuf, (stacklen+1) * sizeof(jschar));
699 if (shrunk)
700 stackbuf = shrunk;
701 }
702
703 stackbuf[stacklen] = 0;
704 str = js_NewString(cx, stackbuf, stacklen, 0);
705 if (str)
706 return str;
707
708 bad:
709 if (stackbuf)
710 JS_free(cx, stackbuf);
711 return NULL;
712 }
713
714 /* XXXbe Consolidate the ugly truth that we don't treat filename as UTF-8
715 with these two functions. */
716 static JSString *
FilenameToString(JSContext * cx,const char * filename)717 FilenameToString(JSContext *cx, const char *filename)
718 {
719 return JS_NewStringCopyZ(cx, filename);
720 }
721
722 static const char *
StringToFilename(JSContext * cx,JSString * str)723 StringToFilename(JSContext *cx, JSString *str)
724 {
725 return JS_GetStringBytes(str);
726 }
727
728 static JSBool
Exception(JSContext * cx,JSObject * obj,uintN argc,jsval * argv,jsval * rval)729 Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
730 {
731 JSBool ok;
732 uint32 lineno;
733 JSString *message, *filename;
734 JSStackFrame *fp;
735
736 if (cx->creatingException)
737 return JS_FALSE;
738 cx->creatingException = JS_TRUE;
739
740 if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
741 /*
742 * ECMA ed. 3, 15.11.1 requires Error, etc., to construct even when
743 * called as functions, without operator new. But as we do not give
744 * each constructor a distinct JSClass, whose .name member is used by
745 * js_NewObject to find the class prototype, we must get the class
746 * prototype ourselves.
747 */
748 ok = OBJ_GET_PROPERTY(cx, JSVAL_TO_OBJECT(argv[-2]),
749 ATOM_TO_JSID(cx->runtime->atomState
750 .classPrototypeAtom),
751 rval);
752 if (!ok)
753 goto out;
754 obj = js_NewObject(cx, &js_ErrorClass, JSVAL_TO_OBJECT(*rval), NULL);
755 if (!obj) {
756 ok = JS_FALSE;
757 goto out;
758 }
759 *rval = OBJECT_TO_JSVAL(obj);
760 }
761
762 /*
763 * If it's a new object of class Exception, then null out the private
764 * data so that the finalizer doesn't attempt to free it.
765 */
766 if (OBJ_GET_CLASS(cx, obj) == &js_ErrorClass)
767 OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, JSVAL_VOID);
768
769 /* Set the 'message' property. */
770 if (argc != 0) {
771 message = js_ValueToString(cx, argv[0]);
772 if (!message) {
773 ok = JS_FALSE;
774 goto out;
775 }
776 argv[0] = STRING_TO_JSVAL(message);
777 } else {
778 message = cx->runtime->emptyString;
779 }
780
781 /* Set the 'fileName' property. */
782 if (argc > 1) {
783 filename = js_ValueToString(cx, argv[1]);
784 if (!filename) {
785 ok = JS_FALSE;
786 goto out;
787 }
788 argv[1] = STRING_TO_JSVAL(filename);
789 fp = NULL;
790 } else {
791 fp = JS_GetScriptedCaller(cx, NULL);
792 if (fp) {
793 filename = FilenameToString(cx, fp->script->filename);
794 if (!filename) {
795 ok = JS_FALSE;
796 goto out;
797 }
798 } else {
799 filename = cx->runtime->emptyString;
800 }
801 }
802
803 /* Set the 'lineNumber' property. */
804 if (argc > 2) {
805 ok = js_ValueToECMAUint32(cx, argv[2], &lineno);
806 if (!ok)
807 goto out;
808 } else {
809 if (!fp)
810 fp = JS_GetScriptedCaller(cx, NULL);
811 lineno = (fp && fp->pc) ? js_PCToLineNumber(cx, fp->script, fp->pc) : 0;
812 }
813
814 ok = (OBJ_GET_CLASS(cx, obj) != &js_ErrorClass) ||
815 InitExnPrivate(cx, obj, message, filename, lineno, NULL);
816
817 out:
818 cx->creatingException = JS_FALSE;
819 return ok;
820 }
821
822 /*
823 * Convert to string.
824 *
825 * This method only uses JavaScript-modifiable properties name, message. It
826 * is left to the host to check for private data and report filename and line
827 * number information along with this message.
828 */
829 static JSBool
exn_toString(JSContext * cx,JSObject * obj,uintN argc,jsval * argv,jsval * rval)830 exn_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
831 {
832 jsval v;
833 JSString *name, *message, *result;
834 jschar *chars, *cp;
835 size_t name_length, message_length, length;
836
837 if (!OBJ_GET_PROPERTY(cx, obj,
838 ATOM_TO_JSID(cx->runtime->atomState.nameAtom),
839 &v)) {
840 return JS_FALSE;
841 }
842 name = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) : cx->runtime->emptyString;
843 *rval = STRING_TO_JSVAL(name);
844
845 if (!JS_GetProperty(cx, obj, js_message_str, &v))
846 return JS_FALSE;
847 message = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v)
848 : cx->runtime->emptyString;
849
850 if (JSSTRING_LENGTH(message) != 0) {
851 name_length = JSSTRING_LENGTH(name);
852 message_length = JSSTRING_LENGTH(message);
853 length = (name_length ? name_length + 2 : 0) + message_length;
854 cp = chars = (jschar*) JS_malloc(cx, (length + 1) * sizeof(jschar));
855 if (!chars)
856 return JS_FALSE;
857
858 if (name_length) {
859 js_strncpy(cp, JSSTRING_CHARS(name), name_length);
860 cp += name_length;
861 *cp++ = ':'; *cp++ = ' ';
862 }
863 js_strncpy(cp, JSSTRING_CHARS(message), message_length);
864 cp += message_length;
865 *cp = 0;
866
867 result = js_NewString(cx, chars, length, 0);
868 if (!result) {
869 JS_free(cx, chars);
870 return JS_FALSE;
871 }
872 } else {
873 result = name;
874 }
875
876 *rval = STRING_TO_JSVAL(result);
877 return JS_TRUE;
878 }
879
880 #if JS_HAS_TOSOURCE
881 /*
882 * Return a string that may eval to something similar to the original object.
883 */
884 static JSBool
exn_toSource(JSContext * cx,JSObject * obj,uintN argc,jsval * argv,jsval * rval)885 exn_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
886 {
887 jsval *vp;
888 JSString *name, *message, *filename, *lineno_as_str, *result;
889 uint32 lineno;
890 size_t lineno_length, name_length, message_length, filename_length, length;
891 jschar *chars, *cp;
892
893 vp = argv + argc; /* beginning of explicit local roots */
894
895 if (!OBJ_GET_PROPERTY(cx, obj,
896 ATOM_TO_JSID(cx->runtime->atomState.nameAtom),
897 rval)) {
898 return JS_FALSE;
899 }
900 name = js_ValueToString(cx, *rval);
901 if (!name)
902 return JS_FALSE;
903 *rval = STRING_TO_JSVAL(name);
904
905 if (!JS_GetProperty(cx, obj, js_message_str, &vp[0]) ||
906 !(message = js_ValueToSource(cx, vp[0]))) {
907 return JS_FALSE;
908 }
909 vp[0] = STRING_TO_JSVAL(message);
910
911 if (!JS_GetProperty(cx, obj, js_fileName_str, &vp[1]) ||
912 !(filename = js_ValueToSource(cx, vp[1]))) {
913 return JS_FALSE;
914 }
915 vp[1] = STRING_TO_JSVAL(filename);
916
917 if (!JS_GetProperty(cx, obj, js_lineNumber_str, &vp[2]) ||
918 !js_ValueToECMAUint32 (cx, vp[2], &lineno)) {
919 return JS_FALSE;
920 }
921
922 if (lineno != 0) {
923 lineno_as_str = js_ValueToString(cx, vp[2]);
924 if (!lineno_as_str)
925 return JS_FALSE;
926 lineno_length = JSSTRING_LENGTH(lineno_as_str);
927 } else {
928 lineno_as_str = NULL;
929 lineno_length = 0;
930 }
931
932 /* Magic 8, for the characters in ``(new ())''. */
933 name_length = JSSTRING_LENGTH(name);
934 message_length = JSSTRING_LENGTH(message);
935 length = 8 + name_length + message_length;
936
937 filename_length = JSSTRING_LENGTH(filename);
938 if (filename_length != 0) {
939 /* append filename as ``, {filename}'' */
940 length += 2 + filename_length;
941 if (lineno_as_str) {
942 /* append lineno as ``, {lineno_as_str}'' */
943 length += 2 + lineno_length;
944 }
945 } else {
946 if (lineno_as_str) {
947 /*
948 * no filename, but have line number,
949 * need to append ``, "", {lineno_as_str}''
950 */
951 length += 6 + lineno_length;
952 }
953 }
954
955 cp = chars = (jschar*) JS_malloc(cx, (length + 1) * sizeof(jschar));
956 if (!chars)
957 return JS_FALSE;
958
959 *cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' ';
960 js_strncpy(cp, JSSTRING_CHARS(name), name_length);
961 cp += name_length;
962 *cp++ = '(';
963 if (message_length != 0) {
964 js_strncpy(cp, JSSTRING_CHARS(message), message_length);
965 cp += message_length;
966 }
967
968 if (filename_length != 0) {
969 /* append filename as ``, {filename}'' */
970 *cp++ = ','; *cp++ = ' ';
971 js_strncpy(cp, JSSTRING_CHARS(filename), filename_length);
972 cp += filename_length;
973 } else {
974 if (lineno_as_str) {
975 /*
976 * no filename, but have line number,
977 * need to append ``, "", {lineno_as_str}''
978 */
979 *cp++ = ','; *cp++ = ' '; *cp++ = '"'; *cp++ = '"';
980 }
981 }
982 if (lineno_as_str) {
983 /* append lineno as ``, {lineno_as_str}'' */
984 *cp++ = ','; *cp++ = ' ';
985 js_strncpy(cp, JSSTRING_CHARS(lineno_as_str), lineno_length);
986 cp += lineno_length;
987 }
988
989 *cp++ = ')'; *cp++ = ')'; *cp = 0;
990
991 result = js_NewString(cx, chars, length, 0);
992 if (!result) {
993 JS_free(cx, chars);
994 return JS_FALSE;
995 }
996 *rval = STRING_TO_JSVAL(result);
997 return JS_TRUE;
998 }
999 #endif
1000
1001 static JSFunctionSpec exception_methods[] = {
1002 #if JS_HAS_TOSOURCE
1003 {js_toSource_str, exn_toSource, 0,0,3},
1004 #endif
1005 {js_toString_str, exn_toString, 0,0,0},
1006 {0,0,0,0,0}
1007 };
1008
1009 JSObject *
js_InitExceptionClasses(JSContext * cx,JSObject * obj)1010 js_InitExceptionClasses(JSContext *cx, JSObject *obj)
1011 {
1012 JSObject *obj_proto, *protos[JSEXN_LIMIT];
1013 int i;
1014
1015 /*
1016 * If lazy class initialization occurs for any Error subclass, then all
1017 * classes are initialized, starting with Error. To avoid reentry and
1018 * redundant initialization, we must not pass a null proto parameter to
1019 * js_NewObject below, when called for the Error superclass. We need to
1020 * ensure that Object.prototype is the proto of Error.prototype.
1021 *
1022 * See the equivalent code to ensure that parent_proto is non-null when
1023 * JS_InitClass calls js_NewObject, in jsapi.c.
1024 */
1025 if (!js_GetClassPrototype(cx, obj, INT_TO_JSID(JSProto_Object),
1026 &obj_proto)) {
1027 return NULL;
1028 }
1029
1030 if (!js_EnterLocalRootScope(cx))
1031 return NULL;
1032
1033 /* Initialize the prototypes first. */
1034 for (i = 0; exceptions[i].name != 0; i++) {
1035 JSAtom *atom;
1036 JSFunction *fun;
1037 JSObject *funobj;
1038 JSString *nameString;
1039 int protoIndex = exceptions[i].protoIndex;
1040
1041 /* Make the prototype for the current constructor name. */
1042 protos[i] = js_NewObject(cx, &js_ErrorClass,
1043 (protoIndex != JSEXN_NONE)
1044 ? protos[protoIndex]
1045 : obj_proto,
1046 obj);
1047 if (!protos[i])
1048 break;
1049
1050 /* So exn_finalize knows whether to destroy private data. */
1051 OBJ_SET_SLOT(cx, protos[i], JSSLOT_PRIVATE, JSVAL_VOID);
1052
1053 /* Make a constructor function for the current name. */
1054 atom = cx->runtime->atomState.classAtoms[exceptions[i].key];
1055 fun = js_DefineFunction(cx, obj, atom, exceptions[i].native, 3, 0);
1056 if (!fun)
1057 break;
1058
1059 /* Make this constructor make objects of class Exception. */
1060 fun->clasp = &js_ErrorClass;
1061
1062 /* Extract the constructor object. */
1063 funobj = fun->object;
1064
1065 /* Make the prototype and constructor links. */
1066 if (!js_SetClassPrototype(cx, funobj, protos[i],
1067 JSPROP_READONLY | JSPROP_PERMANENT)) {
1068 break;
1069 }
1070
1071 /* proto bootstrap bit from JS_InitClass omitted. */
1072 nameString = JS_NewStringCopyZ(cx, exceptions[i].name);
1073 if (!nameString)
1074 break;
1075
1076 /* Add the name property to the prototype. */
1077 if (!JS_DefineProperty(cx, protos[i], js_name_str,
1078 STRING_TO_JSVAL(nameString),
1079 NULL, NULL,
1080 JSPROP_ENUMERATE)) {
1081 break;
1082 }
1083
1084 /* Finally, stash the constructor for later uses. */
1085 if (!js_SetClassObject(cx, obj, exceptions[i].key, funobj))
1086 break;
1087 }
1088
1089 js_LeaveLocalRootScope(cx);
1090 if (exceptions[i].name)
1091 return NULL;
1092
1093 /*
1094 * Add an empty message property. (To Exception.prototype only,
1095 * because this property will be the same for all the exception
1096 * protos.)
1097 */
1098 if (!JS_DefineProperty(cx, protos[0], js_message_str,
1099 STRING_TO_JSVAL(cx->runtime->emptyString),
1100 NULL, NULL, JSPROP_ENUMERATE)) {
1101 return NULL;
1102 }
1103 if (!JS_DefineProperty(cx, protos[0], js_fileName_str,
1104 STRING_TO_JSVAL(cx->runtime->emptyString),
1105 NULL, NULL, JSPROP_ENUMERATE)) {
1106 return NULL;
1107 }
1108 if (!JS_DefineProperty(cx, protos[0], js_lineNumber_str,
1109 INT_TO_JSVAL(0),
1110 NULL, NULL, JSPROP_ENUMERATE)) {
1111 return NULL;
1112 }
1113
1114 /*
1115 * Add methods only to Exception.prototype, because ostensibly all
1116 * exception types delegate to that.
1117 */
1118 if (!JS_DefineFunctions(cx, protos[0], exception_methods))
1119 return NULL;
1120
1121 return protos[0];
1122 }
1123
1124 const JSErrorFormatString*
js_GetLocalizedErrorMessage(JSContext * cx,void * userRef,const char * locale,const uintN errorNumber)1125 js_GetLocalizedErrorMessage(JSContext* cx, void *userRef, const char *locale, const uintN errorNumber)
1126 {
1127 const JSErrorFormatString *errorString = NULL;
1128
1129 if (cx->localeCallbacks && cx->localeCallbacks->localeGetErrorMessage) {
1130 errorString = cx->localeCallbacks
1131 ->localeGetErrorMessage(userRef, locale, errorNumber);
1132 }
1133 if (!errorString)
1134 errorString = js_GetErrorMessage(userRef, locale, errorNumber);
1135 return errorString;
1136 }
1137
1138 #if defined ( DEBUG_mccabe ) && defined ( PRINTNAMES )
1139 /* For use below... get character strings for error name and exception name */
1140 static struct exnname { char *name; char *exception; } errortoexnname[] = {
1141 #define MSG_DEF(name, number, count, exception, format) \
1142 {#name, #exception},
1143 #include "js.msg"
1144 #undef MSG_DEF
1145 };
1146 #endif /* DEBUG */
1147
1148 JSBool
js_ErrorToException(JSContext * cx,const char * message,JSErrorReport * reportp)1149 js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp)
1150 {
1151 JSErrNum errorNumber;
1152 const JSErrorFormatString *errorString;
1153 JSExnType exn;
1154 jsval tv[4];
1155 JSTempValueRooter tvr;
1156 JSBool ok;
1157 JSObject *errProto, *errObject;
1158 JSString *messageStr, *filenameStr;
1159
1160 /*
1161 * Tell our caller to report immediately if cx has no active frames, or if
1162 * this report is just a warning.
1163 */
1164 JS_ASSERT(reportp);
1165 if (!cx->fp || JSREPORT_IS_WARNING(reportp->flags))
1166 return JS_FALSE;
1167
1168 /* Find the exception index associated with this error. */
1169 errorNumber = (JSErrNum) reportp->errorNumber;
1170 errorString = js_GetLocalizedErrorMessage(cx, NULL, NULL, errorNumber);
1171 exn = errorString ? errorString->exnType : JSEXN_NONE;
1172 JS_ASSERT(exn < JSEXN_LIMIT);
1173
1174 #if defined( DEBUG_mccabe ) && defined ( PRINTNAMES )
1175 /* Print the error name and the associated exception name to stderr */
1176 fprintf(stderr, "%s\t%s\n",
1177 errortoexnname[errorNumber].name,
1178 errortoexnname[errorNumber].exception);
1179 #endif
1180
1181 /*
1182 * Return false (no exception raised) if no exception is associated
1183 * with the given error number.
1184 */
1185 if (exn == JSEXN_NONE)
1186 return JS_FALSE;
1187
1188 /*
1189 * Prevent runaway recursion, just as the Exception native constructor
1190 * must do, via cx->creatingException. If an out-of-memory error occurs,
1191 * no exception object will be created, but we don't assume that OOM is
1192 * the only kind of error that subroutines of this function called below
1193 * might raise.
1194 */
1195 if (cx->creatingException)
1196 return JS_FALSE;
1197
1198 /* After this point the control must flow through the label out. */
1199 cx->creatingException = JS_TRUE;
1200
1201 /* Protect the newly-created strings below from nesting GCs. */
1202 memset(tv, 0, sizeof tv);
1203 JS_PUSH_TEMP_ROOT(cx, sizeof tv / sizeof tv[0], tv, &tvr);
1204
1205 /*
1206 * Try to get an appropriate prototype by looking up the corresponding
1207 * exception constructor name in the scope chain of the current context's
1208 * top stack frame, or in the global object if no frame is active.
1209 */
1210 ok = js_GetClassPrototype(cx, NULL, INT_TO_JSID(exceptions[exn].key),
1211 &errProto);
1212 if (!ok)
1213 goto out;
1214 tv[0] = OBJECT_TO_JSVAL(errProto);
1215
1216 errObject = js_NewObject(cx, &js_ErrorClass, errProto, NULL);
1217 if (!errObject) {
1218 ok = JS_FALSE;
1219 goto out;
1220 }
1221 tv[1] = OBJECT_TO_JSVAL(errObject);
1222
1223 messageStr = JS_NewStringCopyZ(cx, message);
1224 if (!messageStr) {
1225 ok = JS_FALSE;
1226 goto out;
1227 }
1228 tv[2] = STRING_TO_JSVAL(messageStr);
1229
1230 filenameStr = JS_NewStringCopyZ(cx, reportp->filename);
1231 if (!filenameStr) {
1232 ok = JS_FALSE;
1233 goto out;
1234 }
1235 tv[3] = STRING_TO_JSVAL(filenameStr);
1236
1237 ok = InitExnPrivate(cx, errObject, messageStr, filenameStr,
1238 reportp->lineno, reportp);
1239 if (!ok)
1240 goto out;
1241
1242 JS_SetPendingException(cx, OBJECT_TO_JSVAL(errObject));
1243
1244 /* Flag the error report passed in to indicate an exception was raised. */
1245 reportp->flags |= JSREPORT_EXCEPTION;
1246
1247 out:
1248 JS_POP_TEMP_ROOT(cx, &tvr);
1249 cx->creatingException = JS_FALSE;
1250 return ok;
1251 }
1252
1253 JSBool
js_ReportUncaughtException(JSContext * cx)1254 js_ReportUncaughtException(JSContext *cx)
1255 {
1256 jsval exn;
1257 JSObject *exnObject;
1258 jsval vp[5];
1259 JSTempValueRooter tvr;
1260 JSErrorReport *reportp, report;
1261 JSString *str;
1262 const char *bytes;
1263 JSBool ok;
1264
1265 if (!JS_IsExceptionPending(cx))
1266 return JS_TRUE;
1267
1268 if (!JS_GetPendingException(cx, &exn))
1269 return JS_FALSE;
1270
1271 /*
1272 * Because js_ValueToString below could error and an exception object
1273 * could become unrooted, we must root exnObject. Later, if exnObject is
1274 * non-null, we need to root other intermediates, so allocate an operand
1275 * stack segment to protect all of these values.
1276 */
1277 if (JSVAL_IS_PRIMITIVE(exn)) {
1278 exnObject = NULL;
1279 } else {
1280 exnObject = JSVAL_TO_OBJECT(exn);
1281 vp[0] = exn;
1282 memset(vp + 1, 0, sizeof vp - sizeof vp[0]);
1283 JS_PUSH_TEMP_ROOT(cx, JS_ARRAY_LENGTH(vp), vp, &tvr);
1284 }
1285
1286 JS_ClearPendingException(cx);
1287 reportp = js_ErrorFromException(cx, exn);
1288
1289 /* XXX L10N angels cry once again (see also jsemit.c, /L10N gaffes/) */
1290 str = js_ValueToString(cx, exn);
1291 if (!str) {
1292 bytes = "unknown (can't convert to string)";
1293 } else {
1294 if (exnObject)
1295 vp[1] = STRING_TO_JSVAL(str);
1296 bytes = js_GetStringBytes(cx->runtime, str);
1297 }
1298 ok = JS_TRUE;
1299
1300 if (!reportp &&
1301 exnObject &&
1302 OBJ_GET_CLASS(cx, exnObject) == &js_ErrorClass) {
1303 const char *filename;
1304 uint32 lineno;
1305
1306 ok = JS_GetProperty(cx, exnObject, js_message_str, &vp[2]);
1307 if (!ok)
1308 goto out;
1309 if (JSVAL_IS_STRING(vp[2]))
1310 bytes = JS_GetStringBytes(JSVAL_TO_STRING(vp[2]));
1311
1312 ok = JS_GetProperty(cx, exnObject, js_fileName_str, &vp[3]);
1313 if (!ok)
1314 goto out;
1315 str = js_ValueToString(cx, vp[3]);
1316 if (!str) {
1317 ok = JS_FALSE;
1318 goto out;
1319 }
1320 filename = StringToFilename(cx, str);
1321
1322 ok = JS_GetProperty(cx, exnObject, js_lineNumber_str, &vp[4]);
1323 if (!ok)
1324 goto out;
1325 ok = js_ValueToECMAUint32 (cx, vp[4], &lineno);
1326 if (!ok)
1327 goto out;
1328
1329 reportp = &report;
1330 memset(&report, 0, sizeof report);
1331 report.filename = filename;
1332 report.lineno = (uintN) lineno;
1333 }
1334
1335 if (!reportp) {
1336 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1337 JSMSG_UNCAUGHT_EXCEPTION, bytes);
1338 } else {
1339 /* Flag the error as an exception. */
1340 reportp->flags |= JSREPORT_EXCEPTION;
1341 js_ReportErrorAgain(cx, bytes, reportp);
1342 }
1343
1344 out:
1345 if (exnObject)
1346 JS_POP_TEMP_ROOT(cx, &tvr);
1347 return ok;
1348 }
1349