forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsexn.cpp
938 lines (813 loc) · 29.8 KB
/
jsexn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* JS standard exception implementation.
*/
#include "jsexn.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/PodOperations.h"
#include <string.h>
#include "jsapi.h"
#include "jscntxt.h"
#include "jsfun.h"
#include "jsnum.h"
#include "jsobj.h"
#include "jsscript.h"
#include "jstypes.h"
#include "jsutil.h"
#include "jswrapper.h"
#include "gc/Marking.h"
#include "vm/ErrorObject.h"
#include "vm/GlobalObject.h"
#include "vm/StringBuffer.h"
#include "jsobjinlines.h"
#include "vm/ErrorObject-inl.h"
using namespace js;
using namespace js::gc;
using namespace js::types;
using mozilla::ArrayLength;
using mozilla::PodArrayZero;
using mozilla::PodZero;
static void
exn_finalize(FreeOp *fop, JSObject *obj);
bool
Error(JSContext *cx, unsigned argc, Value *vp);
static bool
exn_toSource(JSContext *cx, unsigned argc, Value *vp);
static const JSFunctionSpec exception_methods[] = {
#if JS_HAS_TOSOURCE
JS_FN(js_toSource_str, exn_toSource, 0, 0),
#endif
JS_SELF_HOSTED_FN(js_toString_str, "ErrorToString", 0,0),
JS_FS_END
};
#define IMPLEMENT_ERROR_SUBCLASS(name) \
{ \
js_Error_str, /* yes, really */ \
JSCLASS_IMPLEMENTS_BARRIERS | \
JSCLASS_HAS_CACHED_PROTO(JSProto_##name) | \
JSCLASS_HAS_RESERVED_SLOTS(ErrorObject::RESERVED_SLOTS), \
JS_PropertyStub, /* addProperty */ \
JS_DeletePropertyStub, /* delProperty */ \
JS_PropertyStub, /* getProperty */ \
JS_StrictPropertyStub, /* setProperty */ \
JS_EnumerateStub, \
JS_ResolveStub, \
JS_ConvertStub, \
exn_finalize, \
nullptr, /* call */ \
nullptr, /* hasInstance */ \
nullptr, /* construct */ \
nullptr, /* trace */ \
{ \
ErrorObject::createConstructor, \
ErrorObject::createProto, \
nullptr, \
exception_methods, \
nullptr, \
nullptr, \
JSProto_Error \
} \
}
const Class
ErrorObject::classes[JSEXN_LIMIT] = {
{
js_Error_str,
JSCLASS_IMPLEMENTS_BARRIERS |
JSCLASS_HAS_CACHED_PROTO(JSProto_Error) |
JSCLASS_HAS_RESERVED_SLOTS(ErrorObject::RESERVED_SLOTS),
JS_PropertyStub, /* addProperty */
JS_DeletePropertyStub, /* delProperty */
JS_PropertyStub, /* getProperty */
JS_StrictPropertyStub, /* setProperty */
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
exn_finalize,
nullptr, /* call */
nullptr, /* hasInstance */
nullptr, /* construct */
nullptr, /* trace */
{
ErrorObject::createConstructor,
ErrorObject::createProto,
nullptr,
exception_methods,
0
}
},
IMPLEMENT_ERROR_SUBCLASS(InternalError),
IMPLEMENT_ERROR_SUBCLASS(EvalError),
IMPLEMENT_ERROR_SUBCLASS(RangeError),
IMPLEMENT_ERROR_SUBCLASS(ReferenceError),
IMPLEMENT_ERROR_SUBCLASS(SyntaxError),
IMPLEMENT_ERROR_SUBCLASS(TypeError),
IMPLEMENT_ERROR_SUBCLASS(URIError)
};
JSErrorReport *
js::CopyErrorReport(JSContext *cx, JSErrorReport *report)
{
/*
* We use a single malloc block to make a deep copy of JSErrorReport with
* the following layout:
* JSErrorReport
* array of copies of report->messageArgs
* char16_t array with characters for all messageArgs
* char16_t array with characters for ucmessage
* char16_t array with characters for uclinebuf and uctokenptr
* char array with characters for linebuf and tokenptr
* char array with characters for filename
* Such layout together with the properties enforced by the following
* asserts does not need any extra alignment padding.
*/
JS_STATIC_ASSERT(sizeof(JSErrorReport) % sizeof(const char *) == 0);
JS_STATIC_ASSERT(sizeof(const char *) % sizeof(char16_t) == 0);
size_t filenameSize;
size_t linebufSize;
size_t uclinebufSize;
size_t ucmessageSize;
size_t i, argsArraySize, argsCopySize, argSize;
size_t mallocSize;
JSErrorReport *copy;
uint8_t *cursor;
#define JS_CHARS_SIZE(chars) ((js_strlen(chars) + 1) * sizeof(char16_t))
filenameSize = report->filename ? strlen(report->filename) + 1 : 0;
linebufSize = report->linebuf ? strlen(report->linebuf) + 1 : 0;
uclinebufSize = report->uclinebuf ? JS_CHARS_SIZE(report->uclinebuf) : 0;
ucmessageSize = 0;
argsArraySize = 0;
argsCopySize = 0;
if (report->ucmessage) {
ucmessageSize = JS_CHARS_SIZE(report->ucmessage);
if (report->messageArgs) {
for (i = 0; report->messageArgs[i]; ++i)
argsCopySize += JS_CHARS_SIZE(report->messageArgs[i]);
/* Non-null messageArgs should have at least one non-null arg. */
JS_ASSERT(i != 0);
argsArraySize = (i + 1) * sizeof(const char16_t *);
}
}
/*
* The mallocSize can not overflow since it represents the sum of the
* sizes of already allocated objects.
*/
mallocSize = sizeof(JSErrorReport) + argsArraySize + argsCopySize +
ucmessageSize + uclinebufSize + linebufSize + filenameSize;
cursor = cx->pod_malloc<uint8_t>(mallocSize);
if (!cursor)
return nullptr;
copy = (JSErrorReport *)cursor;
memset(cursor, 0, sizeof(JSErrorReport));
cursor += sizeof(JSErrorReport);
if (argsArraySize != 0) {
copy->messageArgs = (const char16_t **)cursor;
cursor += argsArraySize;
for (i = 0; report->messageArgs[i]; ++i) {
copy->messageArgs[i] = (const char16_t *)cursor;
argSize = JS_CHARS_SIZE(report->messageArgs[i]);
js_memcpy(cursor, report->messageArgs[i], argSize);
cursor += argSize;
}
copy->messageArgs[i] = nullptr;
JS_ASSERT(cursor == (uint8_t *)copy->messageArgs[0] + argsCopySize);
}
if (report->ucmessage) {
copy->ucmessage = (const char16_t *)cursor;
js_memcpy(cursor, report->ucmessage, ucmessageSize);
cursor += ucmessageSize;
}
if (report->uclinebuf) {
copy->uclinebuf = (const char16_t *)cursor;
js_memcpy(cursor, report->uclinebuf, uclinebufSize);
cursor += uclinebufSize;
if (report->uctokenptr) {
copy->uctokenptr = copy->uclinebuf + (report->uctokenptr -
report->uclinebuf);
}
}
if (report->linebuf) {
copy->linebuf = (const char *)cursor;
js_memcpy(cursor, report->linebuf, linebufSize);
cursor += linebufSize;
if (report->tokenptr) {
copy->tokenptr = copy->linebuf + (report->tokenptr -
report->linebuf);
}
}
if (report->filename) {
copy->filename = (const char *)cursor;
js_memcpy(cursor, report->filename, filenameSize);
}
JS_ASSERT(cursor + filenameSize == (uint8_t *)copy + mallocSize);
/* HOLD called by the destination error object. */
copy->originPrincipals = report->originPrincipals;
/* Copy non-pointer members. */
copy->lineno = report->lineno;
copy->column = report->column;
copy->errorNumber = report->errorNumber;
copy->exnType = report->exnType;
/* Note that this is before it gets flagged with JSREPORT_EXCEPTION */
copy->flags = report->flags;
#undef JS_CHARS_SIZE
return copy;
}
struct SuppressErrorsGuard
{
JSContext *cx;
JSErrorReporter prevReporter;
JS::AutoSaveExceptionState prevState;
explicit SuppressErrorsGuard(JSContext *cx)
: cx(cx),
prevReporter(JS_SetErrorReporter(cx->runtime(), nullptr)),
prevState(cx)
{}
~SuppressErrorsGuard()
{
JS_SetErrorReporter(cx->runtime(), prevReporter);
}
};
JSString *
js::ComputeStackString(JSContext *cx)
{
StringBuffer sb(cx);
{
RootedAtom atom(cx);
SuppressErrorsGuard seg(cx);
for (NonBuiltinFrameIter i(cx, FrameIter::ALL_CONTEXTS, FrameIter::GO_THROUGH_SAVED,
cx->compartment()->principals);
!i.done();
++i)
{
/* First append the function name, if any. */
if (i.isNonEvalFunctionFrame())
atom = i.functionDisplayAtom();
else
atom = nullptr;
if (atom && !sb.append(atom))
return nullptr;
/* Next a @ separating function name from source location. */
if (!sb.append('@'))
return nullptr;
/* Now the filename. */
const char *cfilename = i.scriptFilename();
if (!cfilename)
cfilename = "";
if (!sb.append(cfilename, strlen(cfilename)))
return nullptr;
uint32_t column = 0;
uint32_t line = i.computeLine(&column);
// Now the line number
if (!sb.append(':') || !NumberValueToStringBuffer(cx, NumberValue(line), sb))
return nullptr;
// Finally, : followed by the column number (1-based, as in other browsers)
// and a newline.
if (!sb.append(':') || !NumberValueToStringBuffer(cx, NumberValue(column + 1), sb) ||
!sb.append('\n'))
{
return nullptr;
}
/*
* Cut off the stack if it gets too deep (most commonly for
* infinite recursion errors).
*/
const size_t MaxReportedStackDepth = 1u << 20;
if (sb.length() > MaxReportedStackDepth)
break;
}
}
return sb.finishString();
}
static void
exn_finalize(FreeOp *fop, JSObject *obj)
{
if (JSErrorReport *report = obj->as<ErrorObject>().getErrorReport()) {
/* These were held by ErrorObject::init. */
if (JSPrincipals *prin = report->originPrincipals)
JS_DropPrincipals(fop->runtime(), prin);
fop->free_(report);
}
}
JSErrorReport *
js_ErrorFromException(JSContext *cx, HandleObject objArg)
{
// It's ok to UncheckedUnwrap here, since all we do is get the
// JSErrorReport, and consumers are careful with the information they get
// from that anyway. Anyone doing things that would expose anything in the
// JSErrorReport to page script either does a security check on the
// JSErrorReport's principal or also tries to do toString on our object and
// will fail if they can't unwrap it.
RootedObject obj(cx, UncheckedUnwrap(objArg));
if (!obj->is<ErrorObject>())
return nullptr;
return obj->as<ErrorObject>().getOrCreateErrorReport(cx);
}
bool
Error(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
/* Compute the error message, if any. */
RootedString message(cx, nullptr);
if (args.hasDefined(0)) {
message = ToString<CanGC>(cx, args[0]);
if (!message)
return false;
}
/* Find the scripted caller. */
NonBuiltinFrameIter iter(cx);
/* Set the 'fileName' property. */
RootedString fileName(cx);
if (args.length() > 1) {
fileName = ToString<CanGC>(cx, args[1]);
} else {
fileName = cx->runtime()->emptyString;
if (!iter.done()) {
if (const char *cfilename = iter.scriptFilename())
fileName = JS_NewStringCopyZ(cx, cfilename);
}
}
if (!fileName)
return false;
/* Set the 'lineNumber' property. */
uint32_t lineNumber, columnNumber = 0;
if (args.length() > 2) {
if (!ToUint32(cx, args[2], &lineNumber))
return false;
} else {
lineNumber = iter.done() ? 0 : iter.computeLine(&columnNumber);
}
Rooted<JSString*> stack(cx, ComputeStackString(cx));
if (!stack)
return false;
/*
* ECMA ed. 3, 15.11.1 requires Error, etc., to construct even when
* called as functions, without operator new. But as we do not give
* each constructor a distinct JSClass, we must get the exception type
* ourselves.
*/
JSExnType exnType = JSExnType(args.callee().as<JSFunction>().getExtendedSlot(0).toInt32());
RootedObject obj(cx, ErrorObject::create(cx, exnType, stack, fileName,
lineNumber, columnNumber, nullptr, message));
if (!obj)
return false;
args.rval().setObject(*obj);
return true;
}
#if JS_HAS_TOSOURCE
/*
* Return a string that may eval to something similar to the original object.
*/
static bool
exn_toSource(JSContext *cx, unsigned argc, Value *vp)
{
JS_CHECK_RECURSION(cx, return false);
CallArgs args = CallArgsFromVp(argc, vp);
RootedObject obj(cx, ToObject(cx, args.thisv()));
if (!obj)
return false;
RootedValue nameVal(cx);
RootedString name(cx);
if (!JSObject::getProperty(cx, obj, obj, cx->names().name, &nameVal) ||
!(name = ToString<CanGC>(cx, nameVal)))
{
return false;
}
RootedValue messageVal(cx);
RootedString message(cx);
if (!JSObject::getProperty(cx, obj, obj, cx->names().message, &messageVal) ||
!(message = ValueToSource(cx, messageVal)))
{
return false;
}
RootedValue filenameVal(cx);
RootedString filename(cx);
if (!JSObject::getProperty(cx, obj, obj, cx->names().fileName, &filenameVal) ||
!(filename = ValueToSource(cx, filenameVal)))
{
return false;
}
RootedValue linenoVal(cx);
uint32_t lineno;
if (!JSObject::getProperty(cx, obj, obj, cx->names().lineNumber, &linenoVal) ||
!ToUint32(cx, linenoVal, &lineno))
{
return false;
}
StringBuffer sb(cx);
if (!sb.append("(new ") || !sb.append(name) || !sb.append("("))
return false;
if (!sb.append(message))
return false;
if (!filename->empty()) {
if (!sb.append(", ") || !sb.append(filename))
return false;
}
if (lineno != 0) {
/* We have a line, but no filename, add empty string */
if (filename->empty() && !sb.append(", \"\""))
return false;
JSString *linenumber = ToString<CanGC>(cx, linenoVal);
if (!linenumber)
return false;
if (!sb.append(", ") || !sb.append(linenumber))
return false;
}
if (!sb.append("))"))
return false;
JSString *str = sb.finishString();
if (!str)
return false;
args.rval().setString(str);
return true;
}
#endif
/* JSProto_ ordering for exceptions shall match JSEXN_ constants. */
JS_STATIC_ASSERT(JSEXN_ERR == 0);
JS_STATIC_ASSERT(JSProto_Error + JSEXN_INTERNALERR == JSProto_InternalError);
JS_STATIC_ASSERT(JSProto_Error + JSEXN_EVALERR == JSProto_EvalError);
JS_STATIC_ASSERT(JSProto_Error + JSEXN_RANGEERR == JSProto_RangeError);
JS_STATIC_ASSERT(JSProto_Error + JSEXN_REFERENCEERR == JSProto_ReferenceError);
JS_STATIC_ASSERT(JSProto_Error + JSEXN_SYNTAXERR == JSProto_SyntaxError);
JS_STATIC_ASSERT(JSProto_Error + JSEXN_TYPEERR == JSProto_TypeError);
JS_STATIC_ASSERT(JSProto_Error + JSEXN_URIERR == JSProto_URIError);
/* static */ JSObject *
ErrorObject::createProto(JSContext *cx, JSProtoKey key)
{
RootedObject errorProto(cx, GenericCreatePrototype(cx, key));
if (!errorProto)
return nullptr;
Rooted<ErrorObject*> err(cx, &errorProto->as<ErrorObject>());
RootedString emptyStr(cx, cx->names().empty);
JSExnType type = ExnTypeFromProtoKey(key);
if (!ErrorObject::init(cx, err, type, nullptr, emptyStr, emptyStr, 0, 0, emptyStr))
return nullptr;
// The various prototypes also have .name in addition to the normal error
// instance properties.
RootedPropertyName name(cx, ClassName(key, cx));
RootedValue nameValue(cx, StringValue(name));
if (!JSObject::defineProperty(cx, err, cx->names().name, nameValue,
JS_PropertyStub, JS_StrictPropertyStub, 0))
{
return nullptr;
}
return errorProto;
}
/* static */ JSObject *
ErrorObject::createConstructor(JSContext *cx, JSProtoKey key)
{
RootedObject ctor(cx);
ctor = GenericCreateConstructor<Error, 1, JSFunction::ExtendedFinalizeKind>(cx, key);
if (!ctor)
return nullptr;
ctor->as<JSFunction>().setExtendedSlot(0, Int32Value(ExnTypeFromProtoKey(key)));
return ctor;
}
JS_FRIEND_API(JSFlatString *)
js::GetErrorTypeName(JSRuntime *rt, int16_t exnType)
{
/*
* JSEXN_INTERNALERR returns null to prevent that "InternalError: "
* is prepended before "uncaught exception: "
*/
if (exnType <= JSEXN_NONE || exnType >= JSEXN_LIMIT ||
exnType == JSEXN_INTERNALERR)
{
return nullptr;
}
JSProtoKey key = GetExceptionProtoKey(JSExnType(exnType));
return ClassName(key, rt);
}
bool
js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp,
JSErrorCallback callback, void *userRef)
{
// Tell our caller to report immediately if this report is just a warning.
JS_ASSERT(reportp);
if (JSREPORT_IS_WARNING(reportp->flags))
return false;
// Find the exception index associated with this error.
JSErrNum errorNumber = static_cast<JSErrNum>(reportp->errorNumber);
if (!callback)
callback = js_GetErrorMessage;
const JSErrorFormatString *errorString = callback(userRef, errorNumber);
JSExnType exnType = errorString ? static_cast<JSExnType>(errorString->exnType) : JSEXN_NONE;
MOZ_ASSERT(exnType < JSEXN_LIMIT);
// Return false (no exception raised) if no exception is associated
// with the given error number.
if (exnType == JSEXN_NONE)
return false;
// Prevent infinite recursion.
if (cx->generatingError)
return false;
AutoScopedAssign<bool> asa(&cx->generatingError, true);
// Create an exception object.
RootedString messageStr(cx, reportp->ucmessage ? JS_NewUCStringCopyZ(cx, reportp->ucmessage)
: JS_NewStringCopyZ(cx, message));
if (!messageStr)
return cx->isExceptionPending();
RootedString fileName(cx, JS_NewStringCopyZ(cx, reportp->filename));
if (!fileName)
return cx->isExceptionPending();
uint32_t lineNumber = reportp->lineno;
uint32_t columnNumber = reportp->column;
RootedString stack(cx, ComputeStackString(cx));
if (!stack)
return cx->isExceptionPending();
js::ScopedJSFreePtr<JSErrorReport> report(CopyErrorReport(cx, reportp));
if (!report)
return cx->isExceptionPending();
RootedObject errObject(cx, ErrorObject::create(cx, exnType, stack, fileName,
lineNumber, columnNumber, &report, messageStr));
if (!errObject)
return cx->isExceptionPending();
// Throw it.
RootedValue errValue(cx, ObjectValue(*errObject));
JS_SetPendingException(cx, errValue);
// Flag the error report passed in to indicate an exception was raised.
reportp->flags |= JSREPORT_EXCEPTION;
return true;
}
static bool
IsDuckTypedErrorObject(JSContext *cx, HandleObject exnObject, const char **filename_strp)
{
bool found;
if (!JS_HasProperty(cx, exnObject, js_message_str, &found) || !found)
return false;
const char *filename_str = *filename_strp;
if (!JS_HasProperty(cx, exnObject, filename_str, &found) || !found) {
/* Now try "fileName", in case this quacks like an Error */
filename_str = js_fileName_str;
if (!JS_HasProperty(cx, exnObject, filename_str, &found) || !found)
return false;
}
if (!JS_HasProperty(cx, exnObject, js_lineNumber_str, &found) || !found)
return false;
*filename_strp = filename_str;
return true;
}
JS_FRIEND_API(JSString *)
js::ErrorReportToString(JSContext *cx, JSErrorReport *reportp)
{
JSExnType type = static_cast<JSExnType>(reportp->exnType);
RootedString str(cx, cx->runtime()->emptyString);
if (type != JSEXN_NONE)
str = ClassName(GetExceptionProtoKey(type), cx);
RootedString toAppend(cx, JS_NewUCStringCopyN(cx, MOZ_UTF16(": "), 2));
if (!str || !toAppend)
return nullptr;
str = ConcatStrings<CanGC>(cx, str, toAppend);
if (!str)
return nullptr;
toAppend = JS_NewUCStringCopyZ(cx, reportp->ucmessage);
if (toAppend)
str = ConcatStrings<CanGC>(cx, str, toAppend);
return str;
}
bool
js_ReportUncaughtException(JSContext *cx)
{
if (!cx->isExceptionPending())
return true;
RootedValue exn(cx);
if (!cx->getPendingException(&exn))
return false;
cx->clearPendingException();
ErrorReport err(cx);
if (!err.init(cx, exn)) {
cx->clearPendingException();
return false;
}
cx->setPendingException(exn);
CallErrorReporter(cx, err.message(), err.report());
cx->clearPendingException();
return true;
}
ErrorReport::ErrorReport(JSContext *cx)
: reportp(nullptr),
message_(nullptr),
ownedMessage(nullptr),
str(cx),
strChars(cx),
exnObject(cx)
{
}
ErrorReport::~ErrorReport()
{
if (!ownedMessage)
return;
js_free(ownedMessage);
if (ownedReport.messageArgs) {
/*
* js_ExpandErrorArguments owns its messageArgs only if it had to
* inflate the arguments (from regular |char *|s), which is always in
* our case.
*/
size_t i = 0;
while (ownedReport.messageArgs[i])
js_free(const_cast<char16_t*>(ownedReport.messageArgs[i++]));
js_free(ownedReport.messageArgs);
}
js_free(const_cast<char16_t*>(ownedReport.ucmessage));
}
bool
ErrorReport::init(JSContext *cx, HandleValue exn)
{
MOZ_ASSERT(!cx->isExceptionPending());
/*
* Because ToString below could error and an exception object could become
* unrooted, we must root our exception object, if any.
*/
if (exn.isObject()) {
exnObject = &exn.toObject();
reportp = js_ErrorFromException(cx, exnObject);
}
// Be careful not to invoke ToString if we've already successfully extracted
// an error report, since the exception might be wrapped in a security
// wrapper, and ToString-ing it might throw.
if (reportp)
str = ErrorReportToString(cx, reportp);
else
str = ToString<CanGC>(cx, exn);
if (!str)
cx->clearPendingException();
// If js_ErrorFromException didn't get us a JSErrorReport, then the object
// was not an ErrorObject, security-wrapped or otherwise. However, it might
// still quack like one. Give duck-typing a chance. We start by looking for
// "filename" (all lowercase), since that's where DOMExceptions store their
// filename. Then we check "fileName", which is where Errors store it. We
// have to do it in that order, because DOMExceptions have Error.prototype
// on their proto chain, and hence also have a "fileName" property, but its
// value is "".
const char *filename_str = "filename";
if (!reportp && exnObject && IsDuckTypedErrorObject(cx, exnObject, &filename_str))
{
// Temporary value for pulling properties off of duck-typed objects.
RootedValue val(cx);
RootedString name(cx);
if (JS_GetProperty(cx, exnObject, js_name_str, &val) && val.isString())
name = val.toString();
else
cx->clearPendingException();
RootedString msg(cx);
if (JS_GetProperty(cx, exnObject, js_message_str, &val) && val.isString())
msg = val.toString();
else
cx->clearPendingException();
// If we have the right fields, override the ToString we performed on
// the exception object above with something built out of its quacks
// (i.e. as much of |NameQuack: MessageQuack| as we can make).
//
// It would be nice to use ErrorReportToString here, but we can't quite
// do it - mostly because we'd need to figure out what JSExnType |name|
// corresponds to, which may not be any JSExnType at all.
if (name && msg) {
RootedString colon(cx, JS_NewStringCopyZ(cx, ": "));
if (!colon)
return false;
RootedString nameColon(cx, ConcatStrings<CanGC>(cx, name, colon));
if (!nameColon)
return false;
str = ConcatStrings<CanGC>(cx, nameColon, msg);
if (!str)
return false;
} else if (name) {
str = name;
} else if (msg) {
str = msg;
}
if (JS_GetProperty(cx, exnObject, filename_str, &val)) {
JSString *tmp = ToString<CanGC>(cx, val);
if (tmp)
filename.encodeLatin1(cx, tmp);
else
cx->clearPendingException();
} else {
cx->clearPendingException();
}
uint32_t lineno;
if (!JS_GetProperty(cx, exnObject, js_lineNumber_str, &val) ||
!ToUint32(cx, val, &lineno))
{
cx->clearPendingException();
lineno = 0;
}
uint32_t column;
if (!JS_GetProperty(cx, exnObject, js_columnNumber_str, &val) ||
!ToUint32(cx, val, &column))
{
cx->clearPendingException();
column = 0;
}
reportp = &ownedReport;
PodZero(&ownedReport);
ownedReport.filename = filename.ptr();
ownedReport.lineno = lineno;
ownedReport.exnType = int16_t(JSEXN_NONE);
ownedReport.column = column;
if (str) {
// Note that using |str| for |ucmessage| here is kind of wrong,
// because |str| is supposed to be of the format
// |ErrorName: ErrorMessage|, and |ucmessage| is supposed to
// correspond to |ErrorMessage|. But this is what we've historically
// done for duck-typed error objects.
//
// If only this stuff could get specced one day...
if (str->ensureFlat(cx) && strChars.initTwoByte(cx, str))
ownedReport.ucmessage = strChars.twoByteChars();
}
}
if (str)
message_ = bytesStorage.encodeLatin1(cx, str);
if (!message_)
message_ = "unknown (can't convert to string)";
if (!reportp) {
// This is basically an inlined version of
//
// JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
// JSMSG_UNCAUGHT_EXCEPTION, message_);
//
// but without the reporting bits. Instead it just puts all
// the stuff we care about in our ownedReport and message_.
populateUncaughtExceptionReport(cx, message_);
} else {
/* Flag the error as an exception. */
reportp->flags |= JSREPORT_EXCEPTION;
}
return true;
}
void
ErrorReport::populateUncaughtExceptionReport(JSContext *cx, ...)
{
va_list ap;
va_start(ap, cx);
populateUncaughtExceptionReportVA(cx, ap);
va_end(ap);
}
void
ErrorReport::populateUncaughtExceptionReportVA(JSContext *cx, va_list ap)
{
PodZero(&ownedReport);
ownedReport.flags = JSREPORT_ERROR;
ownedReport.errorNumber = JSMSG_UNCAUGHT_EXCEPTION;
// XXXbz this assumes the stack we have right now is still
// related to our exception object. It would be better if we
// could accept a passed-in stack of some sort instead.
NonBuiltinFrameIter iter(cx);
if (!iter.done()) {
ownedReport.filename = iter.scriptFilename();
ownedReport.lineno = iter.computeLine(&ownedReport.column);
ownedReport.originPrincipals = iter.originPrincipals();
}
if (!js_ExpandErrorArguments(cx, js_GetErrorMessage, nullptr,
JSMSG_UNCAUGHT_EXCEPTION, &ownedMessage,
&ownedReport, ArgumentsAreASCII, ap)) {
return;
}
reportp = &ownedReport;
message_ = ownedMessage;
ownsMessageAndReport = true;
}
JSObject *
js_CopyErrorObject(JSContext *cx, Handle<ErrorObject*> err)
{
js::ScopedJSFreePtr<JSErrorReport> copyReport;
if (JSErrorReport *errorReport = err->getErrorReport()) {
copyReport = CopyErrorReport(cx, errorReport);
if (!copyReport)
return nullptr;
}
RootedString message(cx, err->getMessage());
if (message && !cx->compartment()->wrap(cx, message.address()))
return nullptr;
RootedString fileName(cx, err->fileName(cx));
if (!cx->compartment()->wrap(cx, fileName.address()))
return nullptr;
RootedString stack(cx, err->stack(cx));
if (!cx->compartment()->wrap(cx, stack.address()))
return nullptr;
uint32_t lineNumber = err->lineNumber();
uint32_t columnNumber = err->columnNumber();
JSExnType errorType = err->type();
// Create the Error object.
return ErrorObject::create(cx, errorType, stack, fileName,
lineNumber, columnNumber, ©Report, message);
}
JS_PUBLIC_API(bool)
JS::CreateError(JSContext *cx, JSExnType type, HandleString stack, HandleString fileName,
uint32_t lineNumber, uint32_t columnNumber, JSErrorReport *report,
HandleString message, MutableHandleValue rval)
{
assertSameCompartment(cx, stack, fileName, message);
js::ScopedJSFreePtr<JSErrorReport> rep;
if (report)
rep = CopyErrorReport(cx, report);
RootedObject obj(cx,
js::ErrorObject::create(cx, type, stack, fileName,
lineNumber, columnNumber, &rep, message));
if (!obj)
return false;
rval.setObject(*obj);
return true;
}