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 <stdlib.h> |
46 |
#include <string.h> |
47 |
#include "jstypes.h" |
48 |
#include "jsstdint.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 "jsversion.h" |
55 |
#include "jsdbgapi.h" |
56 |
#include "jsexn.h" |
57 |
#include "jsfun.h" |
58 |
#include "jsinterp.h" |
59 |
#include "jsnum.h" |
60 |
#include "jsobj.h" |
61 |
#include "jsopcode.h" |
62 |
#include "jsscope.h" |
63 |
#include "jsscript.h" |
64 |
#include "jsstaticcheck.h" |
65 |
|
66 |
/* Forward declarations for js_ErrorClass's initializer. */ |
67 |
static JSBool |
68 |
Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); |
69 |
|
70 |
static void |
71 |
exn_trace(JSTracer *trc, JSObject *obj); |
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 | 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 |
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 * |
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 *)cx->malloc(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 * |
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 |
246 |
InitExnPrivate(JSContext *cx, JSObject *exnObject, JSString *message, |
247 |
JSString *filename, uintN lineno, JSErrorReport *report) |
248 |
{ |
249 |
JSSecurityCallbacks *callbacks; |
250 |
JSCheckAccessOp checkAccess; |
251 |
JSErrorReporter older; |
252 |
JSExceptionState *state; |
253 |
jsval callerid, v; |
254 |
JSStackFrame *fp, *fpstop; |
255 |
size_t stackDepth, valueCount, size; |
256 |
JSBool overflow; |
257 |
JSExnPrivate *priv; |
258 |
JSStackTraceElem *elem; |
259 |
jsval *values; |
260 |
|
261 |
JS_ASSERT(OBJ_GET_CLASS(cx, exnObject) == &js_ErrorClass); |
262 |
|
263 |
/* |
264 |
* Prepare stack trace data. |
265 |
* |
266 |
* Set aside any error reporter for cx and save its exception state |
267 |
* so we can suppress any checkAccess failures. Such failures should stop |
268 |
* the backtrace procedure, not result in a failure of this constructor. |
269 |
*/ |
270 |
callbacks = JS_GetSecurityCallbacks(cx); |
271 |
checkAccess = callbacks |
272 |
? callbacks->checkObjectAccess |
273 |
: NULL; |
274 |
older = JS_SetErrorReporter(cx, NULL); |
275 |
state = JS_SaveExceptionState(cx); |
276 |
|
277 |
callerid = ATOM_KEY(cx->runtime->atomState.callerAtom); |
278 |
stackDepth = 0; |
279 |
valueCount = 0; |
280 |
for (fp = js_GetTopStackFrame(cx); fp; fp = fp->down) { |
281 |
if (fp->fun && fp->argv) { |
282 |
v = JSVAL_NULL; |
283 |
if (checkAccess && |
284 |
!checkAccess(cx, fp->callee(), callerid, JSACC_READ, &v)) { |
285 |
break; |
286 |
} |
287 |
valueCount += fp->argc; |
288 |
} |
289 |
++stackDepth; |
290 |
} |
291 |
JS_RestoreExceptionState(cx, state); |
292 |
JS_SetErrorReporter(cx, older); |
293 |
fpstop = fp; |
294 |
|
295 |
size = offsetof(JSExnPrivate, stackElems); |
296 |
overflow = (stackDepth > ((size_t)-1 - size) / sizeof(JSStackTraceElem)); |
297 |
size += stackDepth * sizeof(JSStackTraceElem); |
298 |
overflow |= (valueCount > ((size_t)-1 - size) / sizeof(jsval)); |
299 |
size += valueCount * sizeof(jsval); |
300 |
if (overflow) { |
301 |
js_ReportAllocationOverflow(cx); |
302 |
return JS_FALSE; |
303 |
} |
304 |
priv = (JSExnPrivate *)cx->malloc(size); |
305 |
if (!priv) |
306 |
return JS_FALSE; |
307 |
|
308 |
/* |
309 |
* We initialize errorReport with a copy of report after setting the |
310 |
* private slot, to prevent GC accessing a junk value we clear the field |
311 |
* here. |
312 |
*/ |
313 |
priv->errorReport = NULL; |
314 |
priv->message = message; |
315 |
priv->filename = filename; |
316 |
priv->lineno = lineno; |
317 |
priv->stackDepth = stackDepth; |
318 |
|
319 |
values = GetStackTraceValueBuffer(priv); |
320 |
elem = priv->stackElems; |
321 |
for (fp = js_GetTopStackFrame(cx); fp != fpstop; fp = fp->down) { |
322 |
if (!fp->fun) { |
323 |
elem->funName = NULL; |
324 |
elem->argc = 0; |
325 |
} else { |
326 |
elem->funName = fp->fun->atom |
327 |
? ATOM_TO_STRING(fp->fun->atom) |
328 |
: cx->runtime->emptyString; |
329 |
elem->argc = fp->argc; |
330 |
memcpy(values, fp->argv, fp->argc * sizeof(jsval)); |
331 |
values += fp->argc; |
332 |
} |
333 |
elem->ulineno = 0; |
334 |
elem->filename = NULL; |
335 |
if (fp->script) { |
336 |
elem->filename = fp->script->filename; |
337 |
if (fp->regs) |
338 |
elem->ulineno = js_FramePCToLineNumber(cx, fp); |
339 |
} |
340 |
++elem; |
341 |
} |
342 |
JS_ASSERT(priv->stackElems + stackDepth == elem); |
343 |
JS_ASSERT(GetStackTraceValueBuffer(priv) + valueCount == values); |
344 |
|
345 |
exnObject->setPrivate(priv); |
346 |
|
347 |
if (report) { |
348 |
/* |
349 |
* Construct a new copy of the error report struct. We can't use the |
350 |
* error report struct that was passed in, because it's allocated on |
351 |
* the stack, and also because it may point to transient data in the |
352 |
* JSTokenStream. |
353 |
*/ |
354 |
priv->errorReport = CopyErrorReport(cx, report); |
355 |
if (!priv->errorReport) { |
356 |
/* The finalizer realeases priv since it is in the private slot. */ |
357 |
return JS_FALSE; |
358 |
} |
359 |
} |
360 |
|
361 |
return JS_TRUE; |
362 |
} |
363 |
|
364 |
static inline JSExnPrivate * |
365 |
GetExnPrivate(JSContext *cx, JSObject *obj) |
366 |
{ |
367 |
return (JSExnPrivate *) obj->getPrivate(); |
368 |
} |
369 |
|
370 |
static void |
371 |
exn_trace(JSTracer *trc, JSObject *obj) |
372 |
{ |
373 |
JSExnPrivate *priv; |
374 |
JSStackTraceElem *elem; |
375 |
size_t vcount, i; |
376 |
jsval *vp, v; |
377 |
|
378 |
priv = GetExnPrivate(trc->context, obj); |
379 |
if (priv) { |
380 |
if (priv->message) |
381 |
JS_CALL_STRING_TRACER(trc, priv->message, "exception message"); |
382 |
if (priv->filename) |
383 |
JS_CALL_STRING_TRACER(trc, priv->filename, "exception filename"); |
384 |
|
385 |
elem = priv->stackElems; |
386 |
for (vcount = i = 0; i != priv->stackDepth; ++i, ++elem) { |
387 |
if (elem->funName) { |
388 |
JS_CALL_STRING_TRACER(trc, elem->funName, |
389 |
"stack trace function name"); |
390 |
} |
391 |
if (IS_GC_MARKING_TRACER(trc) && elem->filename) |
392 |
js_MarkScriptFilename(elem->filename); |
393 |
vcount += elem->argc; |
394 |
} |
395 |
vp = GetStackTraceValueBuffer(priv); |
396 |
for (i = 0; i != vcount; ++i, ++vp) { |
397 |
v = *vp; |
398 |
JS_CALL_VALUE_TRACER(trc, v, "stack trace argument"); |
399 |
} |
400 |
} |
401 |
} |
402 |
|
403 |
static void |
404 |
exn_finalize(JSContext *cx, JSObject *obj) |
405 |
{ |
406 |
JSExnPrivate *priv; |
407 |
|
408 |
priv = GetExnPrivate(cx, obj); |
409 |
if (priv) { |
410 |
if (priv->errorReport) |
411 |
cx->free(priv->errorReport); |
412 |
cx->free(priv); |
413 |
} |
414 |
} |
415 |
|
416 |
static JSBool |
417 |
exn_enumerate(JSContext *cx, JSObject *obj) |
418 |
{ |
419 |
JSAtomState *atomState; |
420 |
uintN i; |
421 |
JSAtom *atom; |
422 |
JSObject *pobj; |
423 |
JSProperty *prop; |
424 |
|
425 |
JS_STATIC_ASSERT(sizeof(JSAtomState) <= (size_t)(uint16)-1); |
426 |
static const uint16 offsets[] = { |
427 |
(uint16)offsetof(JSAtomState, messageAtom), |
428 |
(uint16)offsetof(JSAtomState, fileNameAtom), |
429 |
(uint16)offsetof(JSAtomState, lineNumberAtom), |
430 |
(uint16)offsetof(JSAtomState, stackAtom), |
431 |
}; |
432 |
|
433 |
atomState = &cx->runtime->atomState; |
434 |
for (i = 0; i != JS_ARRAY_LENGTH(offsets); ++i) { |
435 |
atom = *(JSAtom **)((uint8 *)atomState + offsets[i]); |
436 |
if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop)) |
437 |
return JS_FALSE; |
438 |
if (prop) |
439 |
pobj->dropProperty(cx, prop); |
440 |
} |
441 |
return JS_TRUE; |
442 |
} |
443 |
|
444 |
static JSBool |
445 |
exn_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, |
446 |
JSObject **objp) |
447 |
{ |
448 |
JSExnPrivate *priv; |
449 |
JSString *str; |
450 |
JSAtom *atom; |
451 |
JSString *stack; |
452 |
const char *prop; |
453 |
jsval v; |
454 |
|
455 |
*objp = NULL; |
456 |
priv = GetExnPrivate(cx, obj); |
457 |
if (priv && JSVAL_IS_STRING(id)) { |
458 |
str = JSVAL_TO_STRING(id); |
459 |
|
460 |
atom = cx->runtime->atomState.messageAtom; |
461 |
if (str == ATOM_TO_STRING(atom)) { |
462 |
prop = js_message_str; |
463 |
v = STRING_TO_JSVAL(priv->message); |
464 |
goto define; |
465 |
} |
466 |
|
467 |
atom = cx->runtime->atomState.fileNameAtom; |
468 |
if (str == ATOM_TO_STRING(atom)) { |
469 |
prop = js_fileName_str; |
470 |
v = STRING_TO_JSVAL(priv->filename); |
471 |
goto define; |
472 |
} |
473 |
|
474 |
atom = cx->runtime->atomState.lineNumberAtom; |
475 |
if (str == ATOM_TO_STRING(atom)) { |
476 |
prop = js_lineNumber_str; |
477 |
v = INT_TO_JSVAL(priv->lineno); |
478 |
goto define; |
479 |
} |
480 |
|
481 |
atom = cx->runtime->atomState.stackAtom; |
482 |
if (str == ATOM_TO_STRING(atom)) { |
483 |
stack = StackTraceToString(cx, priv); |
484 |
if (!stack) |
485 |
return JS_FALSE; |
486 |
|
487 |
/* Allow to GC all things that were used to build stack trace. */ |
488 |
priv->stackDepth = 0; |
489 |
prop = js_stack_str; |
490 |
v = STRING_TO_JSVAL(stack); |
491 |
goto define; |
492 |
} |
493 |
} |
494 |
return JS_TRUE; |
495 |
|
496 |
define: |
497 |
if (!JS_DefineProperty(cx, obj, prop, v, NULL, NULL, JSPROP_ENUMERATE)) |
498 |
return JS_FALSE; |
499 |
*objp = obj; |
500 |
return JS_TRUE; |
501 |
} |
502 |
|
503 |
JSErrorReport * |
504 |
js_ErrorFromException(JSContext *cx, jsval exn) |
505 |
{ |
506 |
JSObject *obj; |
507 |
JSExnPrivate *priv; |
508 |
|
509 |
if (JSVAL_IS_PRIMITIVE(exn)) |
510 |
return NULL; |
511 |
obj = JSVAL_TO_OBJECT(exn); |
512 |
if (OBJ_GET_CLASS(cx, obj) != &js_ErrorClass) |
513 |
return NULL; |
514 |
priv = GetExnPrivate(cx, obj); |
515 |
if (!priv) |
516 |
return NULL; |
517 |
return priv->errorReport; |
518 |
} |
519 |
|
520 |
static JSString * |
521 |
ValueToShortSource(JSContext *cx, jsval v) |
522 |
{ |
523 |
JSString *str; |
524 |
|
525 |
/* Avoid toSource bloat and fallibility for object types. */ |
526 |
if (JSVAL_IS_PRIMITIVE(v)) { |
527 |
str = js_ValueToSource(cx, v); |
528 |
} else if (VALUE_IS_FUNCTION(cx, v)) { |
529 |
/* |
530 |
* XXX Avoid function decompilation bloat for now. |
531 |
*/ |
532 |
str = JS_GetFunctionId(JS_ValueToFunction(cx, v)); |
533 |
if (!str && !(str = js_ValueToSource(cx, v))) { |
534 |
/* |
535 |
* Continue to soldier on if the function couldn't be |
536 |
* converted into a string. |
537 |
*/ |
538 |
JS_ClearPendingException(cx); |
539 |
str = JS_NewStringCopyZ(cx, "[unknown function]"); |
540 |
} |
541 |
} else { |
542 |
/* |
543 |
* XXX Avoid toString on objects, it takes too long and uses too much |
544 |
* memory, for too many classes (see Mozilla bug 166743). |
545 |
*/ |
546 |
char buf[100]; |
547 |
JS_snprintf(buf, sizeof buf, "[object %s]", |
548 |
OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(v))->name); |
549 |
str = JS_NewStringCopyZ(cx, buf); |
550 |
} |
551 |
return str; |
552 |
} |
553 |
|
554 |
static JSString * |
555 |
StackTraceToString(JSContext *cx, JSExnPrivate *priv) |
556 |
{ |
557 |
jschar *stackbuf; |
558 |
size_t stacklen, stackmax; |
559 |
JSStackTraceElem *elem, *endElem; |
560 |
jsval *values; |
561 |
size_t i; |
562 |
JSString *str; |
563 |
const char *cp; |
564 |
char ulnbuf[11]; |
565 |
|
566 |
/* After this point, failing control flow must goto bad. */ |
567 |
stackbuf = NULL; |
568 |
stacklen = stackmax = 0; |
569 |
|
570 |
/* Limit the stackbuf length to a reasonable value to avoid overflow checks. */ |
571 |
#define STACK_LENGTH_LIMIT JS_BIT(20) |
572 |
|
573 |
#define APPEND_CHAR_TO_STACK(c) \ |
574 |
JS_BEGIN_MACRO \ |
575 |
if (stacklen == stackmax) { \ |
576 |
void *ptr_; \ |
577 |
if (stackmax >= STACK_LENGTH_LIMIT) \ |
578 |
goto done; \ |
579 |
stackmax = stackmax ? 2 * stackmax : 64; \ |
580 |
ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \ |
581 |
if (!ptr_) \ |
582 |
goto bad; \ |
583 |
stackbuf = (jschar *) ptr_; \ |
584 |
} \ |
585 |
stackbuf[stacklen++] = (c); \ |
586 |
JS_END_MACRO |
587 |
|
588 |
#define APPEND_STRING_TO_STACK(str) \ |
589 |
JS_BEGIN_MACRO \ |
590 |
JSString *str_ = str; \ |
591 |
const jschar *chars_; \ |
592 |
size_t length_; \ |
593 |
\ |
594 |
str_->getCharsAndLength(chars_, length_); \ |
595 |
if (length_ > stackmax - stacklen) { \ |
596 |
void *ptr_; \ |
597 |
if (stackmax >= STACK_LENGTH_LIMIT || \ |
598 |
length_ >= STACK_LENGTH_LIMIT - stacklen) { \ |
599 |
goto done; \ |
600 |
} \ |
601 |
stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \ |
602 |
ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \ |
603 |
if (!ptr_) \ |
604 |
goto bad; \ |
605 |
stackbuf = (jschar *) ptr_; \ |
606 |
} \ |
607 |
js_strncpy(stackbuf + stacklen, chars_, length_); \ |
608 |
stacklen += length_; \ |
609 |
JS_END_MACRO |
610 |
|
611 |
values = GetStackTraceValueBuffer(priv); |
612 |
elem = priv->stackElems; |
613 |
for (endElem = elem + priv->stackDepth; elem != endElem; elem++) { |
614 |
if (elem->funName) { |
615 |
APPEND_STRING_TO_STACK(elem->funName); |
616 |
APPEND_CHAR_TO_STACK('('); |
617 |
for (i = 0; i != elem->argc; i++, values++) { |
618 |
if (i > 0) |
619 |
APPEND_CHAR_TO_STACK(','); |
620 |
str = ValueToShortSource(cx, *values); |
621 |
if (!str) |
622 |
goto bad; |
623 |
APPEND_STRING_TO_STACK(str); |
624 |
} |
625 |
APPEND_CHAR_TO_STACK(')'); |
626 |
} |
627 |
APPEND_CHAR_TO_STACK('@'); |
628 |
if (elem->filename) { |
629 |
for (cp = elem->filename; *cp; cp++) |
630 |
APPEND_CHAR_TO_STACK(*cp); |
631 |
} |
632 |
APPEND_CHAR_TO_STACK(':'); |
633 |
JS_snprintf(ulnbuf, sizeof ulnbuf, "%u", elem->ulineno); |
634 |
for (cp = ulnbuf; *cp; cp++) |
635 |
APPEND_CHAR_TO_STACK(*cp); |
636 |
APPEND_CHAR_TO_STACK('\n'); |
637 |
} |
638 |
#undef APPEND_CHAR_TO_STACK |
639 |
#undef APPEND_STRING_TO_STACK |
640 |
#undef STACK_LENGTH_LIMIT |
641 |
|
642 |
done: |
643 |
if (stacklen == 0) { |
644 |
JS_ASSERT(!stackbuf); |
645 |
return cx->runtime->emptyString; |
646 |
} |
647 |
if (stacklen < stackmax) { |
648 |
/* |
649 |
* Realloc can fail when shrinking on some FreeBSD versions, so |
650 |
* don't use JS_realloc here; simply let the oversized allocation |
651 |
* be owned by the string in that rare case. |
652 |
*/ |
653 |
void *shrunk = cx->realloc(stackbuf, (stacklen+1) * sizeof(jschar)); |
654 |
if (shrunk) |
655 |
stackbuf = (jschar *) shrunk; |
656 |
} |
657 |
|
658 |
stackbuf[stacklen] = 0; |
659 |
str = js_NewString(cx, stackbuf, stacklen); |
660 |
if (str) |
661 |
return str; |
662 |
|
663 |
bad: |
664 |
if (stackbuf) |
665 |
cx->free(stackbuf); |
666 |
return NULL; |
667 |
} |
668 |
|
669 |
/* XXXbe Consolidate the ugly truth that we don't treat filename as UTF-8 |
670 |
with these two functions. */ |
671 |
static JSString * |
672 |
FilenameToString(JSContext *cx, const char *filename) |
673 |
{ |
674 |
return JS_NewStringCopyZ(cx, filename); |
675 |
} |
676 |
|
677 |
static const char * |
678 |
StringToFilename(JSContext *cx, JSString *str) |
679 |
{ |
680 |
return js_GetStringBytes(cx, str); |
681 |
} |
682 |
|
683 |
static JSBool |
684 |
Exception(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) |
685 |
{ |
686 |
uint32 lineno; |
687 |
JSString *message, *filename; |
688 |
JSStackFrame *fp; |
689 |
|
690 |
if (!JS_IsConstructing(cx)) { |
691 |
/* |
692 |
* ECMA ed. 3, 15.11.1 requires Error, etc., to construct even when |
693 |
* called as functions, without operator new. But as we do not give |
694 |
* each constructor a distinct JSClass, whose .name member is used by |
695 |
* js_NewObject to find the class prototype, we must get the class |
696 |
* prototype ourselves. |
697 |
*/ |
698 |
if (!JSVAL_TO_OBJECT(argv[-2])->getProperty(cx, |
699 |
ATOM_TO_JSID(cx->runtime->atomState |
700 |
.classPrototypeAtom), |
701 |
rval)) { |
702 |
return JS_FALSE; |
703 |
} |
704 |
obj = js_NewObject(cx, &js_ErrorClass, JSVAL_TO_OBJECT(*rval), NULL); |
705 |
if (!obj) |
706 |
return JS_FALSE; |
707 |
*rval = OBJECT_TO_JSVAL(obj); |
708 |
} |
709 |
|
710 |
/* |
711 |
* If it's a new object of class Exception, then null out the private |
712 |
* data so that the finalizer doesn't attempt to free it. |
713 |
*/ |
714 |
if (OBJ_GET_CLASS(cx, obj) == &js_ErrorClass) |
715 |
obj->setPrivate(NULL); |
716 |
|
717 |
/* Set the 'message' property. */ |
718 |
if (argc != 0) { |
719 |
message = js_ValueToString(cx, argv[0]); |
720 |
if (!message) |
721 |
return JS_FALSE; |
722 |
argv[0] = STRING_TO_JSVAL(message); |
723 |
} else { |
724 |
message = cx->runtime->emptyString; |
725 |
} |
726 |
|
727 |
/* Set the 'fileName' property. */ |
728 |
if (argc > 1) { |
729 |
filename = js_ValueToString(cx, argv[1]); |
730 |
if (!filename) |
731 |
return JS_FALSE; |
732 |
argv[1] = STRING_TO_JSVAL(filename); |
733 |
fp = NULL; |
734 |
} else { |
735 |
fp = js_GetScriptedCaller(cx, NULL); |
736 |
if (fp) { |
737 |
filename = FilenameToString(cx, fp->script->filename); |
738 |
if (!filename) |
739 |
return JS_FALSE; |
740 |
} else { |
741 |
filename = cx->runtime->emptyString; |
742 |
} |
743 |
} |
744 |
|
745 |
/* Set the 'lineNumber' property. */ |
746 |
if (argc > 2) { |
747 |
lineno = js_ValueToECMAUint32(cx, &argv[2]); |
748 |
if (JSVAL_IS_NULL(argv[2])) |
749 |
return JS_FALSE; |
750 |
} else { |
751 |
if (!fp) |
752 |
fp = js_GetScriptedCaller(cx, NULL); |
753 |
lineno = (fp && fp->regs) ? js_FramePCToLineNumber(cx, fp) : 0; |
754 |
} |
755 |
|
756 |
return (OBJ_GET_CLASS(cx, obj) != &js_ErrorClass) || |
757 |
InitExnPrivate(cx, obj, message, filename, lineno, NULL); |
758 |
} |
759 |
|
760 |
/* |
761 |
* Convert to string. |
762 |
* |
763 |
* This method only uses JavaScript-modifiable properties name, message. It |
764 |
* is left to the host to check for private data and report filename and line |
765 |
* number information along with this message. |
766 |
*/ |
767 |
static JSBool |
768 |
exn_toString(JSContext *cx, uintN argc, jsval *vp) |
769 |
{ |
770 |
JSObject *obj; |
771 |
jsval v; |
772 |
JSString *name, *message, *result; |
773 |
jschar *chars, *cp; |
774 |
size_t name_length, message_length, length; |
775 |
|
776 |
obj = JS_THIS_OBJECT(cx, vp); |
777 |
if (!obj || !obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), &v)) |
778 |
return JS_FALSE; |
779 |
name = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) : cx->runtime->emptyString; |
780 |
*vp = STRING_TO_JSVAL(name); |
781 |
|
782 |
if (!JS_GetProperty(cx, obj, js_message_str, &v)) |
783 |
return JS_FALSE; |
784 |
message = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) |
785 |
: cx->runtime->emptyString; |
786 |
|
787 |
if (message->length() != 0) { |
788 |
name_length = name->length(); |
789 |
message_length = message->length(); |
790 |
length = (name_length ? name_length + 2 : 0) + message_length; |
791 |
cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar)); |
792 |
if (!chars) |
793 |
return JS_FALSE; |
794 |
|
795 |
if (name_length) { |
796 |
js_strncpy(cp, name->chars(), name_length); |
797 |
cp += name_length; |
798 |
*cp++ = ':'; *cp++ = ' '; |
799 |
} |
800 |
js_strncpy(cp, message->chars(), message_length); |
801 |
cp += message_length; |
802 |
*cp = 0; |
803 |
|
804 |
result = js_NewString(cx, chars, length); |
805 |
if (!result) { |
806 |
cx->free(chars); |
807 |
return JS_FALSE; |
808 |
} |
809 |
} else { |
810 |
result = name; |
811 |
} |
812 |
|
813 |
*vp = STRING_TO_JSVAL(result); |
814 |
return JS_TRUE; |
815 |
} |
816 |
|
817 |
#if JS_HAS_TOSOURCE |
818 |
/* |
819 |
* Return a string that may eval to something similar to the original object. |
820 |
*/ |
821 |
static JSBool |
822 |
exn_toSource(JSContext *cx, uintN argc, jsval *vp) |
823 |
{ |
824 |
JSObject *obj; |
825 |
JSString *name, *message, *filename, *lineno_as_str, *result; |
826 |
jsval localroots[3] = {JSVAL_NULL, JSVAL_NULL, JSVAL_NULL}; |
827 |
JSTempValueRooter tvr; |
828 |
JSBool ok; |
829 |
uint32 lineno; |
830 |
size_t lineno_length, name_length, message_length, filename_length, length; |
831 |
jschar *chars, *cp; |
832 |
|
833 |
obj = JS_THIS_OBJECT(cx, vp); |
834 |
if (!obj || !obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), vp)) |
835 |
return JS_FALSE; |
836 |
name = js_ValueToString(cx, *vp); |
837 |
if (!name) |
838 |
return JS_FALSE; |
839 |
*vp = STRING_TO_JSVAL(name); |
840 |
|
841 |
MUST_FLOW_THROUGH("out"); |
842 |
JS_PUSH_TEMP_ROOT(cx, 3, localroots, &tvr); |
843 |
|
844 |
#ifdef __GNUC__ |
845 |
message = filename = NULL; |
846 |
#endif |
847 |
ok = JS_GetProperty(cx, obj, js_message_str, &localroots[0]) && |
848 |
(message = js_ValueToSource(cx, localroots[0])); |
849 |
if (!ok) |
850 |
goto out; |
851 |
localroots[0] = STRING_TO_JSVAL(message); |
852 |
|
853 |
ok = JS_GetProperty(cx, obj, js_fileName_str, &localroots[1]) && |
854 |
(filename = js_ValueToSource(cx, localroots[1])); |
855 |
if (!ok) |
856 |
goto out; |
857 |
localroots[1] = STRING_TO_JSVAL(filename); |
858 |
|
859 |
ok = JS_GetProperty(cx, obj, js_lineNumber_str, &localroots[2]); |
860 |
if (!ok) |
861 |
goto out; |
862 |
lineno = js_ValueToECMAUint32 (cx, &localroots[2]); |
863 |
ok = !JSVAL_IS_NULL(localroots[2]); |
864 |
if (!ok) |
865 |
goto out; |
866 |
|
867 |
if (lineno != 0) { |
868 |
lineno_as_str = js_ValueToString(cx, localroots[2]); |
869 |
if (!lineno_as_str) { |
870 |
ok = JS_FALSE; |
871 |
goto out; |
872 |
} |
873 |
lineno_length = lineno_as_str->length(); |
874 |
} else { |
875 |
lineno_as_str = NULL; |
876 |
lineno_length = 0; |
877 |
} |
878 |
|
879 |
/* Magic 8, for the characters in ``(new ())''. */ |
880 |
name_length = name->length(); |
881 |
message_length = message->length(); |
882 |
length = 8 + name_length + message_length; |
883 |
|
884 |
filename_length = filename->length(); |
885 |
if (filename_length != 0) { |
886 |
/* append filename as ``, {filename}'' */ |
887 |
length += 2 + filename_length; |
888 |
if (lineno_as_str) { |
889 |
/* append lineno as ``, {lineno_as_str}'' */ |
890 |
length += 2 + lineno_length; |
891 |
} |
892 |
} else { |
893 |
if (lineno_as_str) { |
894 |
/* |
895 |
* no filename, but have line number, |
896 |
* need to append ``, "", {lineno_as_str}'' |
897 |
*/ |
898 |
length += 6 + lineno_length; |
899 |
} |
900 |
} |
901 |
|
902 |
cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar)); |
903 |
if (!chars) { |
904 |
ok = JS_FALSE; |
905 |
goto out; |
906 |
} |
907 |
|
908 |
*cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' '; |
909 |
js_strncpy(cp, name->chars(), name_length); |
910 |
cp += name_length; |
911 |
*cp++ = '('; |
912 |
if (message_length != 0) { |
913 |
js_strncpy(cp, message->chars(), message_length); |
914 |
cp += message_length; |
915 |
} |
916 |
|
917 |
if (filename_length != 0) { |
918 |
/* append filename as ``, {filename}'' */ |
919 |
*cp++ = ','; *cp++ = ' '; |
920 |
js_strncpy(cp, filename->chars(), filename_length); |
921 |
cp += filename_length; |
922 |
} else { |
923 |
if (lineno_as_str) { |
924 |
/* |
925 |
* no filename, but have line number, |
926 |
* need to append ``, "", {lineno_as_str}'' |
927 |
*/ |
928 |
*cp++ = ','; *cp++ = ' '; *cp++ = '"'; *cp++ = '"'; |
929 |
} |
930 |
} |
931 |
if (lineno_as_str) { |
932 |
/* append lineno as ``, {lineno_as_str}'' */ |
933 |
*cp++ = ','; *cp++ = ' '; |
934 |
js_strncpy(cp, lineno_as_str->chars(), lineno_length); |
935 |
cp += lineno_length; |
936 |
} |
937 |
|
938 |
*cp++ = ')'; *cp++ = ')'; *cp = 0; |
939 |
|
940 |
result = js_NewString(cx, chars, length); |
941 |
if (!result) { |
942 |
cx->free(chars); |
943 |
ok = JS_FALSE; |
944 |
goto out; |
945 |
} |
946 |
*vp = STRING_TO_JSVAL(result); |
947 |
ok = JS_TRUE; |
948 |
|
949 |
out: |
950 |
JS_POP_TEMP_ROOT(cx, &tvr); |
951 |
return ok; |
952 |
} |
953 |
#endif |
954 |
|
955 |
static JSFunctionSpec exception_methods[] = { |
956 |
#if JS_HAS_TOSOURCE |
957 |
JS_FN(js_toSource_str, exn_toSource, 0,0), |
958 |
#endif |
959 |
JS_FN(js_toString_str, exn_toString, 0,0), |
960 |
JS_FS_END |
961 |
}; |
962 |
|
963 |
/* JSProto_ ordering for exceptions shall match JSEXN_ constants. */ |
964 |
JS_STATIC_ASSERT(JSEXN_ERR == 0); |
965 |
JS_STATIC_ASSERT(JSProto_Error + JSEXN_INTERNALERR == JSProto_InternalError); |
966 |
JS_STATIC_ASSERT(JSProto_Error + JSEXN_EVALERR == JSProto_EvalError); |
967 |
JS_STATIC_ASSERT(JSProto_Error + JSEXN_RANGEERR == JSProto_RangeError); |
968 |
JS_STATIC_ASSERT(JSProto_Error + JSEXN_REFERENCEERR == JSProto_ReferenceError); |
969 |
JS_STATIC_ASSERT(JSProto_Error + JSEXN_SYNTAXERR == JSProto_SyntaxError); |
970 |
JS_STATIC_ASSERT(JSProto_Error + JSEXN_TYPEERR == JSProto_TypeError); |
971 |
JS_STATIC_ASSERT(JSProto_Error + JSEXN_URIERR == JSProto_URIError); |
972 |
|
973 |
static JS_INLINE JSProtoKey |
974 |
GetExceptionProtoKey(intN exn) |
975 |
{ |
976 |
JS_ASSERT(JSEXN_ERR <= exn); |
977 |
JS_ASSERT(exn < JSEXN_LIMIT); |
978 |
return (JSProtoKey) (JSProto_Error + exn); |
979 |
} |
980 |
|
981 |
JSObject * |
982 |
js_InitExceptionClasses(JSContext *cx, JSObject *obj) |
983 |
{ |
984 |
jsval roots[3]; |
985 |
JSObject *obj_proto, *error_proto; |
986 |
jsval empty; |
987 |
|
988 |
/* |
989 |
* If lazy class initialization occurs for any Error subclass, then all |
990 |
* classes are initialized, starting with Error. To avoid reentry and |
991 |
* redundant initialization, we must not pass a null proto parameter to |
992 |
* js_NewObject below, when called for the Error superclass. We need to |
993 |
* ensure that Object.prototype is the proto of Error.prototype. |
994 |
* |
995 |
* See the equivalent code to ensure that parent_proto is non-null when |
996 |
* JS_InitClass calls js_NewObject, in jsapi.c. |
997 |
*/ |
998 |
if (!js_GetClassPrototype(cx, obj, INT_TO_JSID(JSProto_Object), |
999 |
&obj_proto)) { |
1000 |
return NULL; |
1001 |
} |
1002 |
|
1003 |
memset(roots, 0, sizeof(roots)); |
1004 |
JSAutoTempValueRooter tvr(cx, JS_ARRAY_LENGTH(roots), roots); |
1005 |
|
1006 |
#ifdef __GNUC__ |
1007 |
error_proto = NULL; /* quell GCC overwarning */ |
1008 |
#endif |
1009 |
|
1010 |
/* Initialize the prototypes first. */ |
1011 |
for (intN i = JSEXN_ERR; i != JSEXN_LIMIT; i++) { |
1012 |
JSObject *proto; |
1013 |
JSProtoKey protoKey; |
1014 |
JSAtom *atom; |
1015 |
JSFunction *fun; |
1016 |
|
1017 |
/* Make the prototype for the current constructor name. */ |
1018 |
proto = js_NewObject(cx, &js_ErrorClass, |
1019 |
(i != JSEXN_ERR) ? error_proto : obj_proto, |
1020 |
obj); |
1021 |
if (!proto) |
1022 |
return NULL; |
1023 |
if (i == JSEXN_ERR) { |
1024 |
error_proto = proto; |
1025 |
roots[0] = OBJECT_TO_JSVAL(proto); |
1026 |
} else { |
1027 |
// We cannot share the root for error_proto and other prototypes |
1028 |
// as error_proto must be rooted until the function returns. |
1029 |
roots[1] = OBJECT_TO_JSVAL(proto); |
1030 |
} |
1031 |
|
1032 |
/* So exn_finalize knows whether to destroy private data. */ |
1033 |
proto->setPrivate(NULL); |
1034 |
|
1035 |
/* Make a constructor function for the current name. */ |
1036 |
protoKey = GetExceptionProtoKey(i); |
1037 |
atom = cx->runtime->atomState.classAtoms[protoKey]; |
1038 |
fun = js_DefineFunction(cx, obj, atom, Exception, 3, 0); |
1039 |
if (!fun) |
1040 |
return NULL; |
1041 |
roots[2] = OBJECT_TO_JSVAL(FUN_OBJECT(fun)); |
1042 |
|
1043 |
/* Make this constructor make objects of class Exception. */ |
1044 |
FUN_CLASP(fun) = &js_ErrorClass; |
1045 |
|
1046 |
/* Make the prototype and constructor links. */ |
1047 |
if (!js_SetClassPrototype(cx, FUN_OBJECT(fun), proto, |
1048 |
JSPROP_READONLY | JSPROP_PERMANENT)) { |
1049 |
return NULL; |
1050 |
} |
1051 |
|
1052 |
/* Add the name property to the prototype. */ |
1053 |
if (!JS_DefineProperty(cx, proto, js_name_str, ATOM_KEY(atom), |
1054 |
NULL, NULL, JSPROP_ENUMERATE)) { |
1055 |
return NULL; |
1056 |
} |
1057 |
|
1058 |
/* Finally, stash the constructor for later uses. */ |
1059 |
if (!js_SetClassObject(cx, obj, protoKey, FUN_OBJECT(fun))) |
1060 |
return NULL; |
1061 |
} |
1062 |
|
1063 |
/* |
1064 |
* Set default values and add methods. We do it only for Error.prototype |
1065 |
* as the rest of exceptions delegate to it. |
1066 |
*/ |
1067 |
empty = STRING_TO_JSVAL(cx->runtime->emptyString); |
1068 |
if (!JS_DefineProperty(cx, error_proto, js_message_str, empty, |
1069 |
NULL, NULL, JSPROP_ENUMERATE) || |
1070 |
!JS_DefineProperty(cx, error_proto, js_fileName_str, empty, |
1071 |
NULL, NULL, JSPROP_ENUMERATE) || |
1072 |
!JS_DefineProperty(cx, error_proto, js_lineNumber_str, JSVAL_ZERO, |
1073 |
NULL, NULL, JSPROP_ENUMERATE) || |
1074 |
!JS_DefineFunctions(cx, error_proto, exception_methods)) { |
1075 |
return NULL; |
1076 |
} |
1077 |
|
1078 |
return error_proto; |
1079 |
} |
1080 |
|
1081 |
const JSErrorFormatString* |
1082 |
js_GetLocalizedErrorMessage(JSContext* cx, void *userRef, const char *locale, |
1083 |
const uintN errorNumber) |
1084 |
{ |
1085 |
const JSErrorFormatString *errorString = NULL; |
1086 |
|
1087 |
if (cx->localeCallbacks && cx->localeCallbacks->localeGetErrorMessage) { |
1088 |
errorString = cx->localeCallbacks |
1089 |
->localeGetErrorMessage(userRef, locale, errorNumber); |
1090 |
} |
1091 |
if (!errorString) |
1092 |
errorString = js_GetErrorMessage(userRef, locale, errorNumber); |
1093 |
return errorString; |
1094 |
} |
1095 |
|
1096 |
#if defined ( DEBUG_mccabe ) && defined ( PRINTNAMES ) |
1097 |
/* For use below... get character strings for error name and exception name */ |
1098 |
static struct exnname { char *name; char *exception; } errortoexnname[] = { |
1099 |
#define MSG_DEF(name, number, count, exception, format) \ |
1100 |
{#name, #exception}, |
1101 |
#include "js.msg" |
1102 |
#undef MSG_DEF |
1103 |
}; |
1104 |
#endif /* DEBUG */ |
1105 |
|
1106 |
JSBool |
1107 |
js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp) |
1108 |
{ |
1109 |
JSErrNum errorNumber; |
1110 |
const JSErrorFormatString *errorString; |
1111 |
JSExnType exn; |
1112 |
jsval tv[4]; |
1113 |
JSTempValueRooter tvr; |
1114 |
JSBool ok; |
1115 |
JSObject *errProto, *errObject; |
1116 |
JSString *messageStr, *filenameStr; |
1117 |
|
1118 |
/* |
1119 |
* Tell our caller to report immediately if this report is just a warning. |
1120 |
*/ |
1121 |
JS_ASSERT(reportp); |
1122 |
if (JSREPORT_IS_WARNING(reportp->flags)) |
1123 |
return JS_FALSE; |
1124 |
|
1125 |
/* Find the exception index associated with this error. */ |
1126 |
errorNumber = (JSErrNum) reportp->errorNumber; |
1127 |
errorString = js_GetLocalizedErrorMessage(cx, NULL, NULL, errorNumber); |
1128 |
exn = errorString ? (JSExnType) errorString->exnType : JSEXN_NONE; |
1129 |
JS_ASSERT(exn < JSEXN_LIMIT); |
1130 |
|
1131 |
#if defined( DEBUG_mccabe ) && defined ( PRINTNAMES ) |
1132 |
/* Print the error name and the associated exception name to stderr */ |
1133 |
fprintf(stderr, "%s\t%s\n", |
1134 |
errortoexnname[errorNumber].name, |
1135 |
errortoexnname[errorNumber].exception); |
1136 |
#endif |
1137 |
|
1138 |
/* |
1139 |
* Return false (no exception raised) if no exception is associated |
1140 |
* with the given error number. |
1141 |
*/ |
1142 |
if (exn == JSEXN_NONE) |
1143 |
return JS_FALSE; |
1144 |
|
1145 |
/* |
1146 |
* Prevent runaway recursion, via cx->generatingError. If an out-of-memory |
1147 |
* error occurs, no exception object will be created, but we don't assume |
1148 |
* that OOM is the only kind of error that subroutines of this function |
1149 |
* called below might raise. |
1150 |
*/ |
1151 |
if (cx->generatingError) |
1152 |
return JS_FALSE; |
1153 |
|
1154 |
MUST_FLOW_THROUGH("out"); |
1155 |
cx->generatingError = JS_TRUE; |
1156 |
|
1157 |
/* Protect the newly-created strings below from nesting GCs. */ |
1158 |
memset(tv, 0, sizeof tv); |
1159 |
JS_PUSH_TEMP_ROOT(cx, JS_ARRAY_LENGTH(tv), tv, &tvr); |
1160 |
|
1161 |
/* |
1162 |
* Try to get an appropriate prototype by looking up the corresponding |
1163 |
* exception constructor name in the scope chain of the current context's |
1164 |
* top stack frame, or in the global object if no frame is active. |
1165 |
*/ |
1166 |
ok = js_GetClassPrototype(cx, NULL, INT_TO_JSID(GetExceptionProtoKey(exn)), |
1167 |
&errProto); |
1168 |
if (!ok) |
1169 |
goto out; |
1170 |
tv[0] = OBJECT_TO_JSVAL(errProto); |
1171 |
|
1172 |
errObject = js_NewObject(cx, &js_ErrorClass, errProto, NULL); |
1173 |
if (!errObject) { |
1174 |
ok = JS_FALSE; |
1175 |
goto out; |
1176 |
} |
1177 |
tv[1] = OBJECT_TO_JSVAL(errObject); |
1178 |
|
1179 |
messageStr = JS_NewStringCopyZ(cx, message); |
1180 |
if (!messageStr) { |
1181 |
ok = JS_FALSE; |
1182 |
goto out; |
1183 |
} |
1184 |
tv[2] = STRING_TO_JSVAL(messageStr); |
1185 |
|
1186 |
filenameStr = JS_NewStringCopyZ(cx, reportp->filename); |
1187 |
if (!filenameStr) { |
1188 |
ok = JS_FALSE; |
1189 |
goto out; |
1190 |
} |
1191 |
tv[3] = STRING_TO_JSVAL(filenameStr); |
1192 |
|
1193 |
ok = InitExnPrivate(cx, errObject, messageStr, filenameStr, |
1194 |
reportp->lineno, reportp); |
1195 |
if (!ok) |
1196 |
goto out; |
1197 |
|
1198 |
JS_SetPendingException(cx, OBJECT_TO_JSVAL(errObject)); |
1199 |
|
1200 |
/* Flag the error report passed in to indicate an exception was raised. */ |
1201 |
reportp->flags |= JSREPORT_EXCEPTION; |
1202 |
|
1203 |
out: |
1204 |
JS_POP_TEMP_ROOT(cx, &tvr); |
1205 |
cx->generatingError = JS_FALSE; |
1206 |
return ok; |
1207 |
} |
1208 |
|
1209 |
JSBool |
1210 |
js_ReportUncaughtException(JSContext *cx) |
1211 |
{ |
1212 |
jsval exn; |
1213 |
JSObject *exnObject; |
1214 |
jsval roots[5]; |
1215 |
JSTempValueRooter tvr; |
1216 |
JSErrorReport *reportp, report; |
1217 |
JSString *str; |
1218 |
const char *bytes; |
1219 |
JSBool ok; |
1220 |
|
1221 |
if (!JS_IsExceptionPending(cx)) |
1222 |
return JS_TRUE; |
1223 |
|
1224 |
if (!JS_GetPendingException(cx, &exn)) |
1225 |
return JS_FALSE; |
1226 |
|
1227 |
memset(roots, 0, sizeof roots); |
1228 |
JS_PUSH_TEMP_ROOT(cx, JS_ARRAY_LENGTH(roots), roots, &tvr); |
1229 |
|
1230 |
/* |
1231 |
* Because js_ValueToString below could error and an exception object |
1232 |
* could become unrooted, we must root exnObject. Later, if exnObject is |
1233 |
* non-null, we need to root other intermediates, so allocate an operand |
1234 |
* stack segment to protect all of these values. |
1235 |
*/ |
1236 |
if (JSVAL_IS_PRIMITIVE(exn)) { |
1237 |
exnObject = NULL; |
1238 |
} else { |
1239 |
exnObject = JSVAL_TO_OBJECT(exn); |
1240 |
roots[0] = exn; |
1241 |
} |
1242 |
|
1243 |
JS_ClearPendingException(cx); |
1244 |
reportp = js_ErrorFromException(cx, exn); |
1245 |
|
1246 |
/* XXX L10N angels cry once again (see also jsemit.c, /L10N gaffes/) */ |
1247 |
str = js_ValueToString(cx, exn); |
1248 |
if (!str) { |
1249 |
bytes = "unknown (can't convert to string)"; |
1250 |
} else { |
1251 |
roots[1] = STRING_TO_JSVAL(str); |
1252 |
bytes = js_GetStringBytes(cx, str); |
1253 |
if (!bytes) { |
1254 |
ok = JS_FALSE; |
1255 |
goto out; |
1256 |
} |
1257 |
} |
1258 |
ok = JS_TRUE; |
1259 |
|
1260 |
if (!reportp && |
1261 |
exnObject && |
1262 |
OBJ_GET_CLASS(cx, exnObject) == &js_ErrorClass) { |
1263 |
const char *filename; |
1264 |
uint32 lineno; |
1265 |
|
1266 |
ok = JS_GetProperty(cx, exnObject, js_message_str, &roots[2]); |
1267 |
if (!ok) |
1268 |
goto out; |
1269 |
if (JSVAL_IS_STRING(roots[2])) { |
1270 |
bytes = js_GetStringBytes(cx, JSVAL_TO_STRING(roots[2])); |
1271 |
if (!bytes) { |
1272 |
ok = JS_FALSE; |
1273 |
goto out; |
1274 |
} |
1275 |
} |
1276 |
|
1277 |
ok = JS_GetProperty(cx, exnObject, js_fileName_str, &roots[3]); |
1278 |
if (!ok) |
1279 |
goto out; |
1280 |
str = js_ValueToString(cx, roots[3]); |
1281 |
if (!str) { |
1282 |
ok = JS_FALSE; |
1283 |
goto out; |
1284 |
} |
1285 |
filename = StringToFilename(cx, str); |
1286 |
if (!filename) { |
1287 |
ok = JS_FALSE; |
1288 |
goto out; |
1289 |
} |
1290 |
|
1291 |
ok = JS_GetProperty(cx, exnObject, js_lineNumber_str, &roots[4]); |
1292 |
if (!ok) |
1293 |
goto out; |
1294 |
lineno = js_ValueToECMAUint32 (cx, &roots[4]); |
1295 |
ok = !JSVAL_IS_NULL(roots[4]); |
1296 |
if (!ok) |
1297 |
goto out; |
1298 |
|
1299 |
reportp = &report; |
1300 |
memset(&report, 0, sizeof report); |
1301 |
report.filename = filename; |
1302 |
report.lineno = (uintN) lineno; |
1303 |
} |
1304 |
|
1305 |
if (!reportp) { |
1306 |
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, |
1307 |
JSMSG_UNCAUGHT_EXCEPTION, bytes); |
1308 |
} else { |
1309 |
/* Flag the error as an exception. */ |
1310 |
reportp->flags |= JSREPORT_EXCEPTION; |
1311 |
|
1312 |
/* Pass the exception object. */ |
1313 |
JS_SetPendingException(cx, exn); |
1314 |
js_ReportErrorAgain(cx, bytes, reportp); |
1315 |
JS_ClearPendingException(cx); |
1316 |
} |
1317 |
|
1318 |
out: |
1319 |
JS_POP_TEMP_ROOT(cx, &tvr); |
1320 |
return ok; |
1321 |
} |