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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
|
// Copyright 2011 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/execution/messages.h"
#include <memory>
#include "src/api/api-inl.h"
#include "src/ast/ast.h"
#include "src/ast/prettyprinter.h"
#include "src/base/v8-fallthrough.h"
#include "src/execution/execution.h"
#include "src/execution/frames-inl.h"
#include "src/execution/frames.h"
#include "src/execution/isolate-inl.h"
#include "src/logging/counters.h"
#include "src/objects/foreign-inl.h"
#include "src/objects/frame-array-inl.h"
#include "src/objects/js-array-inl.h"
#include "src/objects/keys.h"
#include "src/objects/stack-frame-info-inl.h"
#include "src/objects/struct-inl.h"
#include "src/parsing/parse-info.h"
#include "src/parsing/parsing.h"
#include "src/roots/roots.h"
#include "src/strings/string-builder-inl.h"
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-objects.h"
namespace v8 {
namespace internal {
MessageLocation::MessageLocation(Handle<Script> script, int start_pos,
int end_pos)
: script_(script),
start_pos_(start_pos),
end_pos_(end_pos),
bytecode_offset_(-1) {}
MessageLocation::MessageLocation(Handle<Script> script, int start_pos,
int end_pos, Handle<SharedFunctionInfo> shared)
: script_(script),
start_pos_(start_pos),
end_pos_(end_pos),
bytecode_offset_(-1),
shared_(shared) {}
MessageLocation::MessageLocation(Handle<Script> script,
Handle<SharedFunctionInfo> shared,
int bytecode_offset)
: script_(script),
start_pos_(-1),
end_pos_(-1),
bytecode_offset_(bytecode_offset),
shared_(shared) {}
MessageLocation::MessageLocation()
: start_pos_(-1), end_pos_(-1), bytecode_offset_(-1) {}
// If no message listeners have been registered this one is called
// by default.
void MessageHandler::DefaultMessageReport(Isolate* isolate,
const MessageLocation* loc,
Handle<Object> message_obj) {
std::unique_ptr<char[]> str = GetLocalizedMessage(isolate, message_obj);
if (loc == nullptr) {
PrintF("%s\n", str.get());
} else {
HandleScope scope(isolate);
Handle<Object> data(loc->script()->name(), isolate);
std::unique_ptr<char[]> data_str;
if (data->IsString())
data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>",
loc->start_pos(), str.get());
}
}
Handle<JSMessageObject> MessageHandler::MakeMessageObject(
Isolate* isolate, MessageTemplate message, const MessageLocation* location,
Handle<Object> argument, Handle<FixedArray> stack_frames) {
Factory* factory = isolate->factory();
int start = -1;
int end = -1;
int bytecode_offset = -1;
Handle<Script> script_handle = isolate->factory()->empty_script();
Handle<SharedFunctionInfo> shared_info;
if (location != nullptr && !FLAG_correctness_fuzzer_suppressions) {
start = location->start_pos();
end = location->end_pos();
script_handle = location->script();
bytecode_offset = location->bytecode_offset();
shared_info = location->shared();
}
Handle<Object> stack_frames_handle =
stack_frames.is_null() ? Handle<Object>::cast(factory->undefined_value())
: Handle<Object>::cast(stack_frames);
Handle<JSMessageObject> message_obj = factory->NewJSMessageObject(
message, argument, start, end, shared_info, bytecode_offset,
script_handle, stack_frames_handle);
return message_obj;
}
void MessageHandler::ReportMessage(Isolate* isolate, const MessageLocation* loc,
Handle<JSMessageObject> message) {
v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
if (api_message_obj->ErrorLevel() == v8::Isolate::kMessageError) {
// We are calling into embedder's code which can throw exceptions.
// Thus we need to save current exception state, reset it to the clean one
// and ignore scheduled exceptions callbacks can throw.
// We pass the exception object into the message handler callback though.
Object exception_object = ReadOnlyRoots(isolate).undefined_value();
if (isolate->has_pending_exception()) {
exception_object = isolate->pending_exception();
}
Handle<Object> exception(exception_object, isolate);
Isolate::ExceptionScope exception_scope(isolate);
isolate->clear_pending_exception();
isolate->set_external_caught_exception(false);
// Turn the exception on the message into a string if it is an object.
if (message->argument().IsJSObject()) {
HandleScope scope(isolate);
Handle<Object> argument(message->argument(), isolate);
MaybeHandle<Object> maybe_stringified;
Handle<Object> stringified;
// Make sure we don't leak uncaught internally generated Error objects.
if (argument->IsJSError()) {
maybe_stringified = Object::NoSideEffectsToString(isolate, argument);
} else {
v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
catcher.SetVerbose(false);
catcher.SetCaptureMessage(false);
maybe_stringified = Object::ToString(isolate, argument);
}
if (!maybe_stringified.ToHandle(&stringified)) {
DCHECK(isolate->has_pending_exception());
isolate->clear_pending_exception();
isolate->set_external_caught_exception(false);
stringified =
isolate->factory()->NewStringFromAsciiChecked("exception");
}
message->set_argument(*stringified);
}
v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception);
ReportMessageNoExceptions(isolate, loc, message, api_exception_obj);
} else {
ReportMessageNoExceptions(isolate, loc, message, v8::Local<v8::Value>());
}
}
void MessageHandler::ReportMessageNoExceptions(
Isolate* isolate, const MessageLocation* loc, Handle<Object> message,
v8::Local<v8::Value> api_exception_obj) {
v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
int error_level = api_message_obj->ErrorLevel();
Handle<TemplateList> global_listeners =
isolate->factory()->message_listeners();
int global_length = global_listeners->length();
if (global_length == 0) {
DefaultMessageReport(isolate, loc, message);
if (isolate->has_scheduled_exception()) {
isolate->clear_scheduled_exception();
}
} else {
for (int i = 0; i < global_length; i++) {
HandleScope scope(isolate);
if (global_listeners->get(i).IsUndefined(isolate)) continue;
FixedArray listener = FixedArray::cast(global_listeners->get(i));
Foreign callback_obj = Foreign::cast(listener.get(0));
int32_t message_levels =
static_cast<int32_t>(Smi::ToInt(listener.get(2)));
if (!(message_levels & error_level)) {
continue;
}
v8::MessageCallback callback =
FUNCTION_CAST<v8::MessageCallback>(callback_obj.foreign_address());
Handle<Object> callback_data(listener.get(1), isolate);
{
RuntimeCallTimerScope timer(
isolate, RuntimeCallCounterId::kMessageListenerCallback);
// Do not allow exceptions to propagate.
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
callback(api_message_obj, callback_data->IsUndefined(isolate)
? api_exception_obj
: v8::Utils::ToLocal(callback_data));
}
if (isolate->has_scheduled_exception()) {
isolate->clear_scheduled_exception();
}
}
}
}
Handle<String> MessageHandler::GetMessage(Isolate* isolate,
Handle<Object> data) {
Handle<JSMessageObject> message = Handle<JSMessageObject>::cast(data);
Handle<Object> arg = Handle<Object>(message->argument(), isolate);
return MessageFormatter::Format(isolate, message->type(), arg);
}
std::unique_ptr<char[]> MessageHandler::GetLocalizedMessage(
Isolate* isolate, Handle<Object> data) {
HandleScope scope(isolate);
return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
}
namespace {
Object EvalFromFunctionName(Isolate* isolate, Handle<Script> script) {
if (!script->has_eval_from_shared()) {
return ReadOnlyRoots(isolate).undefined_value();
}
Handle<SharedFunctionInfo> shared(script->eval_from_shared(), isolate);
// Find the name of the function calling eval.
if (shared->Name().BooleanValue(isolate)) {
return shared->Name();
}
return shared->inferred_name();
}
MaybeHandle<String> FormatEvalOrigin(Isolate* isolate, Handle<Script> script) {
Handle<Object> sourceURL(script->GetNameOrSourceURL(), isolate);
if (!sourceURL->IsUndefined(isolate)) {
DCHECK(sourceURL->IsString());
return Handle<String>::cast(sourceURL);
}
IncrementalStringBuilder builder(isolate);
builder.AppendCString("eval at ");
Handle<Object> eval_from_function_name =
handle(EvalFromFunctionName(isolate, script), isolate);
if (eval_from_function_name->BooleanValue(isolate)) {
Handle<String> str;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, str, Object::ToString(isolate, eval_from_function_name),
String);
builder.AppendString(str);
} else {
builder.AppendCString("<anonymous>");
}
if (script->has_eval_from_shared()) {
Handle<SharedFunctionInfo> eval_from_shared(script->eval_from_shared(),
isolate);
if (eval_from_shared->script().IsScript()) {
Handle<Script> eval_from_script =
handle(Script::cast(eval_from_shared->script()), isolate);
builder.AppendCString(" (");
if (eval_from_script->compilation_type() ==
Script::COMPILATION_TYPE_EVAL) {
// Eval script originated from another eval.
Handle<String> str;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, str, FormatEvalOrigin(isolate, eval_from_script), String);
builder.AppendString(str);
} else {
DCHECK(eval_from_script->compilation_type() !=
Script::COMPILATION_TYPE_EVAL);
// eval script originated from "real" source.
Handle<Object> name_obj = handle(eval_from_script->name(), isolate);
if (eval_from_script->name().IsString()) {
builder.AppendString(Handle<String>::cast(name_obj));
Script::PositionInfo info;
if (Script::GetPositionInfo(eval_from_script,
Script::GetEvalPosition(isolate, script),
&info, Script::NO_OFFSET)) {
builder.AppendCString(":");
Handle<String> str = isolate->factory()->NumberToString(
handle(Smi::FromInt(info.line + 1), isolate));
builder.AppendString(str);
builder.AppendCString(":");
str = isolate->factory()->NumberToString(
handle(Smi::FromInt(info.column + 1), isolate));
builder.AppendString(str);
}
} else {
DCHECK(!eval_from_script->name().IsString());
builder.AppendCString("unknown source");
}
}
}
builder.AppendCString(")");
}
Handle<String> result;
ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String);
return result;
}
} // namespace
Handle<Object> StackFrameBase::GetEvalOrigin() {
if (!HasScript() || !IsEval()) return isolate_->factory()->undefined_value();
return FormatEvalOrigin(isolate_, GetScript()).ToHandleChecked();
}
Handle<Object> StackFrameBase::GetWasmModuleName() {
return isolate_->factory()->undefined_value();
}
int StackFrameBase::GetWasmFunctionIndex() { return StackFrameBase::kNone; }
Handle<Object> StackFrameBase::GetWasmInstance() {
return isolate_->factory()->undefined_value();
}
int StackFrameBase::GetScriptId() const {
if (!HasScript()) return kNone;
return GetScript()->id();
}
bool StackFrameBase::IsEval() {
return HasScript() &&
GetScript()->compilation_type() == Script::COMPILATION_TYPE_EVAL;
}
void JSStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array,
int frame_ix) {
DCHECK(!array->IsWasmFrame(frame_ix));
isolate_ = isolate;
receiver_ = handle(array->Receiver(frame_ix), isolate);
function_ = handle(array->Function(frame_ix), isolate);
code_ = handle(array->Code(frame_ix), isolate);
offset_ = array->Offset(frame_ix).value();
cached_position_ = base::nullopt;
const int flags = array->Flags(frame_ix).value();
is_constructor_ = (flags & FrameArray::kIsConstructor) != 0;
is_strict_ = (flags & FrameArray::kIsStrict) != 0;
is_async_ = (flags & FrameArray::kIsAsync) != 0;
is_promise_all_ = (flags & FrameArray::kIsPromiseAll) != 0;
}
JSStackFrame::JSStackFrame(Isolate* isolate, Handle<Object> receiver,
Handle<JSFunction> function,
Handle<AbstractCode> code, int offset)
: StackFrameBase(isolate),
receiver_(receiver),
function_(function),
code_(code),
offset_(offset),
cached_position_(base::nullopt),
is_async_(false),
is_constructor_(false),
is_strict_(false) {}
Handle<Object> JSStackFrame::GetFunction() const {
return Handle<Object>::cast(function_);
}
Handle<Object> JSStackFrame::GetFileName() {
if (!HasScript()) return isolate_->factory()->null_value();
return handle(GetScript()->name(), isolate_);
}
Handle<Object> JSStackFrame::GetFunctionName() {
Handle<String> result = JSFunction::GetDebugName(function_);
if (result->length() != 0) return result;
if (HasScript() &&
GetScript()->compilation_type() == Script::COMPILATION_TYPE_EVAL) {
return isolate_->factory()->eval_string();
}
return isolate_->factory()->null_value();
}
namespace {
bool CheckMethodName(Isolate* isolate, Handle<JSReceiver> receiver,
Handle<Name> name, Handle<JSFunction> fun,
LookupIterator::Configuration config) {
LookupIterator::Key key(isolate, name);
LookupIterator iter(isolate, receiver, key, config);
if (iter.state() == LookupIterator::DATA) {
return iter.GetDataValue().is_identical_to(fun);
} else if (iter.state() == LookupIterator::ACCESSOR) {
Handle<Object> accessors = iter.GetAccessors();
if (accessors->IsAccessorPair()) {
Handle<AccessorPair> pair = Handle<AccessorPair>::cast(accessors);
return pair->getter() == *fun || pair->setter() == *fun;
}
}
return false;
}
Handle<Object> ScriptNameOrSourceUrl(Handle<Script> script, Isolate* isolate) {
Object name_or_url = script->source_url();
if (!name_or_url.IsString()) name_or_url = script->name();
return handle(name_or_url, isolate);
}
} // namespace
Handle<Object> JSStackFrame::GetScriptNameOrSourceUrl() {
if (!HasScript()) return isolate_->factory()->null_value();
return ScriptNameOrSourceUrl(GetScript(), isolate_);
}
Handle<Object> JSStackFrame::GetMethodName() {
if (receiver_->IsNullOrUndefined(isolate_)) {
return isolate_->factory()->null_value();
}
Handle<JSReceiver> receiver;
if (!Object::ToObject(isolate_, receiver_).ToHandle(&receiver)) {
DCHECK(isolate_->has_pending_exception());
isolate_->clear_pending_exception();
isolate_->set_external_caught_exception(false);
return isolate_->factory()->null_value();
}
Handle<String> name(function_->shared().Name(), isolate_);
name = String::Flatten(isolate_, name);
// The static initializer function is not a method, so don't add a
// class name, just return the function name.
if (name->HasOneBytePrefix(CStrVector("<static_fields_initializer>"))) {
return name;
}
// ES2015 gives getters and setters name prefixes which must
// be stripped to find the property name.
if (name->HasOneBytePrefix(CStrVector("get ")) ||
name->HasOneBytePrefix(CStrVector("set "))) {
name = isolate_->factory()->NewProperSubString(name, 4, name->length());
}
if (CheckMethodName(isolate_, receiver, name, function_,
LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR)) {
return name;
}
HandleScope outer_scope(isolate_);
Handle<Object> result;
for (PrototypeIterator iter(isolate_, receiver, kStartAtReceiver);
!iter.IsAtEnd(); iter.Advance()) {
Handle<Object> current = PrototypeIterator::GetCurrent(iter);
if (!current->IsJSObject()) break;
Handle<JSObject> current_obj = Handle<JSObject>::cast(current);
if (current_obj->IsAccessCheckNeeded()) break;
Handle<FixedArray> keys =
KeyAccumulator::GetOwnEnumPropertyKeys(isolate_, current_obj);
for (int i = 0; i < keys->length(); i++) {
HandleScope inner_scope(isolate_);
if (!keys->get(i).IsName()) continue;
Handle<Name> name_key(Name::cast(keys->get(i)), isolate_);
if (!CheckMethodName(isolate_, current_obj, name_key, function_,
LookupIterator::OWN_SKIP_INTERCEPTOR))
continue;
// Return null in case of duplicates to avoid confusion.
if (!result.is_null()) return isolate_->factory()->null_value();
result = inner_scope.CloseAndEscape(name_key);
}
}
if (!result.is_null()) return outer_scope.CloseAndEscape(result);
return isolate_->factory()->null_value();
}
Handle<Object> JSStackFrame::GetTypeName() {
// TODO(jgruber): Check for strict/constructor here as in
// CallSitePrototypeGetThis.
if (receiver_->IsNullOrUndefined(isolate_)) {
return isolate_->factory()->null_value();
} else if (receiver_->IsJSProxy()) {
return isolate_->factory()->Proxy_string();
}
Handle<JSReceiver> receiver;
if (!Object::ToObject(isolate_, receiver_).ToHandle(&receiver)) {
DCHECK(isolate_->has_pending_exception());
isolate_->clear_pending_exception();
isolate_->set_external_caught_exception(false);
return isolate_->factory()->null_value();
}
return JSReceiver::GetConstructorName(receiver);
}
int JSStackFrame::GetLineNumber() {
DCHECK_LE(0, GetPosition());
if (HasScript()) return Script::GetLineNumber(GetScript(), GetPosition()) + 1;
return kNone;
}
int JSStackFrame::GetColumnNumber() {
DCHECK_LE(0, GetPosition());
if (HasScript()) {
return Script::GetColumnNumber(GetScript(), GetPosition()) + 1;
}
return kNone;
}
int JSStackFrame::GetPromiseIndex() const {
return is_promise_all_ ? offset_ : kNone;
}
bool JSStackFrame::IsNative() {
return HasScript() && GetScript()->type() == Script::TYPE_NATIVE;
}
bool JSStackFrame::IsToplevel() {
return receiver_->IsJSGlobalProxy() || receiver_->IsNullOrUndefined(isolate_);
}
int JSStackFrame::GetPosition() const {
if (cached_position_) return *cached_position_;
Handle<SharedFunctionInfo> shared = handle(function_->shared(), isolate_);
SharedFunctionInfo::EnsureSourcePositionsAvailable(isolate_, shared);
cached_position_ = code_->SourcePosition(offset_);
return *cached_position_;
}
bool JSStackFrame::HasScript() const {
return function_->shared().script().IsScript();
}
Handle<Script> JSStackFrame::GetScript() const {
return handle(Script::cast(function_->shared().script()), isolate_);
}
void WasmStackFrame::FromFrameArray(Isolate* isolate, Handle<FrameArray> array,
int frame_ix) {
// This function is called for compiled and interpreted wasm frames, and for
// asm.js->wasm frames.
DCHECK(array->IsWasmFrame(frame_ix) ||
array->IsAsmJsWasmFrame(frame_ix));
isolate_ = isolate;
wasm_instance_ = handle(array->WasmInstance(frame_ix), isolate);
wasm_func_index_ = array->WasmFunctionIndex(frame_ix).value();
// The {WasmCode*} is held alive by the {GlobalWasmCodeRef}.
auto global_wasm_code_ref =
Managed<wasm::GlobalWasmCodeRef>::cast(array->WasmCodeObject(frame_ix));
code_ = global_wasm_code_ref.get()->code();
offset_ = array->Offset(frame_ix).value();
}
Handle<Object> WasmStackFrame::GetReceiver() const { return wasm_instance_; }
Handle<Object> WasmStackFrame::GetFunction() const {
return handle(Smi::FromInt(wasm_func_index_), isolate_);
}
Handle<Object> WasmStackFrame::GetFunctionName() {
Handle<Object> name;
Handle<WasmModuleObject> module_object(wasm_instance_->module_object(),
isolate_);
if (!WasmModuleObject::GetFunctionNameOrNull(isolate_, module_object,
wasm_func_index_)
.ToHandle(&name)) {
name = isolate_->factory()->null_value();
}
return name;
}
Handle<Object> WasmStackFrame::GetScriptNameOrSourceUrl() {
Handle<Script> script = GetScript();
DCHECK_EQ(Script::TYPE_WASM, script->type());
return ScriptNameOrSourceUrl(script, isolate_);
}
Handle<Object> WasmStackFrame::GetWasmModuleName() {
Handle<Object> module_name;
Handle<WasmModuleObject> module_object(wasm_instance_->module_object(),
isolate_);
if (!WasmModuleObject::GetModuleNameOrNull(isolate_, module_object)
.ToHandle(&module_name)) {
module_name = isolate_->factory()->null_value();
}
return module_name;
}
Handle<Object> WasmStackFrame::GetWasmInstance() { return wasm_instance_; }
int WasmStackFrame::GetPosition() const {
return IsInterpreted() ? offset_ : code_->GetSourcePositionBefore(offset_);
}
int WasmStackFrame::GetColumnNumber() { return GetModuleOffset(); }
int WasmStackFrame::GetModuleOffset() const {
const int function_offset =
GetWasmFunctionOffset(wasm_instance_->module(), wasm_func_index_);
return function_offset + GetPosition();
}
Handle<Object> WasmStackFrame::Null() const {
return isolate_->factory()->null_value();
}
bool WasmStackFrame::HasScript() const { return true; }
Handle<Script> WasmStackFrame::GetScript() const {
return handle(wasm_instance_->module_object().script(), isolate_);
}
void AsmJsWasmStackFrame::FromFrameArray(Isolate* isolate,
Handle<FrameArray> array,
int frame_ix) {
DCHECK(array->IsAsmJsWasmFrame(frame_ix));
WasmStackFrame::FromFrameArray(isolate, array, frame_ix);
is_at_number_conversion_ =
array->Flags(frame_ix).value() & FrameArray::kAsmJsAtNumberConversion;
}
Handle<Object> AsmJsWasmStackFrame::GetReceiver() const {
return isolate_->global_proxy();
}
Handle<Object> AsmJsWasmStackFrame::GetFunction() const {
// TODO(clemensb): Return lazily created JSFunction.
return Null();
}
Handle<Object> AsmJsWasmStackFrame::GetFileName() {
Handle<Script> script(wasm_instance_->module_object().script(), isolate_);
DCHECK(script->IsUserJavaScript());
return handle(script->name(), isolate_);
}
Handle<Object> AsmJsWasmStackFrame::GetScriptNameOrSourceUrl() {
Handle<Script> script(wasm_instance_->module_object().script(), isolate_);
DCHECK_EQ(Script::TYPE_NORMAL, script->type());
return ScriptNameOrSourceUrl(script, isolate_);
}
int AsmJsWasmStackFrame::GetPosition() const {
DCHECK_LE(0, offset_);
int byte_offset = code_->GetSourcePositionBefore(offset_);
const wasm::WasmModule* module = wasm_instance_->module();
return GetSourcePosition(module, wasm_func_index_, byte_offset,
is_at_number_conversion_);
}
int AsmJsWasmStackFrame::GetLineNumber() {
DCHECK_LE(0, GetPosition());
Handle<Script> script(wasm_instance_->module_object().script(), isolate_);
DCHECK(script->IsUserJavaScript());
return Script::GetLineNumber(script, GetPosition()) + 1;
}
int AsmJsWasmStackFrame::GetColumnNumber() {
DCHECK_LE(0, GetPosition());
Handle<Script> script(wasm_instance_->module_object().script(), isolate_);
DCHECK(script->IsUserJavaScript());
return Script::GetColumnNumber(script, GetPosition()) + 1;
}
FrameArrayIterator::FrameArrayIterator(Isolate* isolate,
Handle<FrameArray> array, int frame_ix)
: isolate_(isolate), array_(array), frame_ix_(frame_ix) {}
bool FrameArrayIterator::HasFrame() const {
return (frame_ix_ < array_->FrameCount());
}
void FrameArrayIterator::Advance() { frame_ix_++; }
StackFrameBase* FrameArrayIterator::Frame() {
DCHECK(HasFrame());
const int flags = array_->Flags(frame_ix_).value();
int flag_mask = FrameArray::kIsWasmFrame | FrameArray::kIsAsmJsWasmFrame;
switch (flags & flag_mask) {
case 0:
js_frame_.FromFrameArray(isolate_, array_, frame_ix_);
return &js_frame_;
case FrameArray::kIsWasmFrame:
wasm_frame_.FromFrameArray(isolate_, array_, frame_ix_);
return &wasm_frame_;
case FrameArray::kIsAsmJsWasmFrame:
asm_wasm_frame_.FromFrameArray(isolate_, array_, frame_ix_);
return &asm_wasm_frame_;
default:
UNREACHABLE();
}
}
namespace {
MaybeHandle<Object> ConstructCallSite(Isolate* isolate,
Handle<StackTraceFrame> frame) {
Handle<JSFunction> target =
handle(isolate->native_context()->callsite_function(), isolate);
Handle<JSObject> obj;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, obj,
JSObject::New(target, target, Handle<AllocationSite>::null()), Object);
// TODO(szuend): Introduce a new symbol "call_site_frame_symbol" and set
// it to the StackTraceFrame. The CallSite API builtins can then
// be implemented using StackFrameInfo objects.
Handle<FrameArray> frame_array(FrameArray::cast(frame->frame_array()),
isolate);
int frame_index = frame->frame_index();
Handle<Symbol> key = isolate->factory()->call_site_frame_array_symbol();
RETURN_ON_EXCEPTION(isolate,
JSObject::SetOwnPropertyIgnoreAttributes(
obj, key, frame_array, DONT_ENUM),
Object);
key = isolate->factory()->call_site_frame_index_symbol();
Handle<Object> value(Smi::FromInt(frame_index), isolate);
RETURN_ON_EXCEPTION(
isolate,
JSObject::SetOwnPropertyIgnoreAttributes(obj, key, value, DONT_ENUM),
Object);
return obj;
}
// Convert the raw frames as written by Isolate::CaptureSimpleStackTrace into
// a JSArray of JSCallSite objects.
MaybeHandle<JSArray> GetStackFrames(Isolate* isolate,
Handle<FixedArray> elems) {
const int frame_count = elems->length();
Handle<FixedArray> frames = isolate->factory()->NewFixedArray(frame_count);
for (int i = 0; i < frame_count; i++) {
Handle<Object> site;
Handle<StackTraceFrame> frame(StackTraceFrame::cast(elems->get(i)),
isolate);
ASSIGN_RETURN_ON_EXCEPTION(isolate, site, ConstructCallSite(isolate, frame),
JSArray);
frames->set(i, *site);
}
return isolate->factory()->NewJSArrayWithElements(frames);
}
MaybeHandle<Object> AppendErrorString(Isolate* isolate, Handle<Object> error,
IncrementalStringBuilder* builder) {
MaybeHandle<String> err_str =
ErrorUtils::ToString(isolate, Handle<Object>::cast(error));
if (err_str.is_null()) {
// Error.toString threw. Try to return a string representation of the thrown
// exception instead.
DCHECK(isolate->has_pending_exception());
Handle<Object> pending_exception =
handle(isolate->pending_exception(), isolate);
isolate->clear_pending_exception();
isolate->set_external_caught_exception(false);
err_str = ErrorUtils::ToString(isolate, pending_exception);
if (err_str.is_null()) {
// Formatting the thrown exception threw again, give up.
DCHECK(isolate->has_pending_exception());
isolate->clear_pending_exception();
isolate->set_external_caught_exception(false);
builder->AppendCString("<error>");
} else {
// Formatted thrown exception successfully, append it.
builder->AppendCString("<error: ");
builder->AppendString(err_str.ToHandleChecked());
builder->AppendCharacter('>');
}
} else {
builder->AppendString(err_str.ToHandleChecked());
}
return error;
}
class PrepareStackTraceScope {
public:
explicit PrepareStackTraceScope(Isolate* isolate) : isolate_(isolate) {
DCHECK(!isolate_->formatting_stack_trace());
isolate_->set_formatting_stack_trace(true);
}
~PrepareStackTraceScope() { isolate_->set_formatting_stack_trace(false); }
private:
Isolate* isolate_;
DISALLOW_COPY_AND_ASSIGN(PrepareStackTraceScope);
};
} // namespace
// static
MaybeHandle<Object> ErrorUtils::FormatStackTrace(Isolate* isolate,
Handle<JSObject> error,
Handle<Object> raw_stack) {
DCHECK(raw_stack->IsFixedArray());
Handle<FixedArray> elems = Handle<FixedArray>::cast(raw_stack);
const bool in_recursion = isolate->formatting_stack_trace();
if (!in_recursion) {
Handle<Context> error_context = error->GetCreationContext();
DCHECK(error_context->IsNativeContext());
if (isolate->HasPrepareStackTraceCallback()) {
PrepareStackTraceScope scope(isolate);
Handle<JSArray> sites;
ASSIGN_RETURN_ON_EXCEPTION(isolate, sites, GetStackFrames(isolate, elems),
Object);
Handle<Object> result;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, result,
isolate->RunPrepareStackTraceCallback(error_context, error, sites),
Object);
return result;
} else {
Handle<JSFunction> global_error =
handle(error_context->error_function(), isolate);
// If there's a user-specified "prepareStackTrace" function, call it on
// the frames and use its result.
Handle<Object> prepare_stack_trace;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, prepare_stack_trace,
JSFunction::GetProperty(isolate, global_error, "prepareStackTrace"),
Object);
if (prepare_stack_trace->IsJSFunction()) {
PrepareStackTraceScope scope(isolate);
isolate->CountUsage(v8::Isolate::kErrorPrepareStackTrace);
Handle<JSArray> sites;
ASSIGN_RETURN_ON_EXCEPTION(isolate, sites,
GetStackFrames(isolate, elems), Object);
const int argc = 2;
ScopedVector<Handle<Object>> argv(argc);
argv[0] = error;
argv[1] = sites;
Handle<Object> result;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, result,
Execution::Call(isolate, prepare_stack_trace, global_error, argc,
argv.begin()),
Object);
return result;
}
}
}
// Otherwise, run our internal formatting logic.
IncrementalStringBuilder builder(isolate);
RETURN_ON_EXCEPTION(isolate, AppendErrorString(isolate, error, &builder),
Object);
wasm::WasmCodeRefScope wasm_code_ref_scope;
for (int i = 0; i < elems->length(); ++i) {
builder.AppendCString("\n at ");
Handle<StackTraceFrame> frame(StackTraceFrame::cast(elems->get(i)),
isolate);
SerializeStackTraceFrame(isolate, frame, &builder);
if (isolate->has_pending_exception()) {
// CallSite.toString threw. Parts of the current frame might have been
// stringified already regardless. Still, try to append a string
// representation of the thrown exception.
Handle<Object> pending_exception =
handle(isolate->pending_exception(), isolate);
isolate->clear_pending_exception();
isolate->set_external_caught_exception(false);
MaybeHandle<String> exception_string =
ErrorUtils::ToString(isolate, pending_exception);
if (exception_string.is_null()) {
// Formatting the thrown exception threw again, give up.
builder.AppendCString("<error>");
} else {
// Formatted thrown exception successfully, append it.
builder.AppendCString("<error: ");
builder.AppendString(exception_string.ToHandleChecked());
builder.AppendCString("<error>");
}
}
}
return builder.Finish();
}
Handle<String> MessageFormatter::Format(Isolate* isolate, MessageTemplate index,
Handle<Object> arg0,
Handle<Object> arg1,
Handle<Object> arg2) {
Factory* factory = isolate->factory();
Handle<String> arg0_string = factory->empty_string();
if (!arg0.is_null()) {
arg0_string = Object::NoSideEffectsToString(isolate, arg0);
}
Handle<String> arg1_string = factory->empty_string();
if (!arg1.is_null()) {
arg1_string = Object::NoSideEffectsToString(isolate, arg1);
}
Handle<String> arg2_string = factory->empty_string();
if (!arg2.is_null()) {
arg2_string = Object::NoSideEffectsToString(isolate, arg2);
}
MaybeHandle<String> maybe_result_string = MessageFormatter::Format(
isolate, index, arg0_string, arg1_string, arg2_string);
Handle<String> result_string;
if (!maybe_result_string.ToHandle(&result_string)) {
DCHECK(isolate->has_pending_exception());
isolate->clear_pending_exception();
return factory->InternalizeString(StaticCharVector("<error>"));
}
// A string that has been obtained from JS code in this way is
// likely to be a complicated ConsString of some sort. We flatten it
// here to improve the efficiency of converting it to a C string and
// other operations that are likely to take place (see GetLocalizedMessage
// for example).
return String::Flatten(isolate, result_string);
}
const char* MessageFormatter::TemplateString(MessageTemplate index) {
switch (index) {
#define CASE(NAME, STRING) \
case MessageTemplate::k##NAME: \
return STRING;
MESSAGE_TEMPLATES(CASE)
#undef CASE
case MessageTemplate::kMessageCount:
default:
return nullptr;
}
}
MaybeHandle<String> MessageFormatter::Format(Isolate* isolate,
MessageTemplate index,
Handle<String> arg0,
Handle<String> arg1,
Handle<String> arg2) {
const char* template_string = TemplateString(index);
if (template_string == nullptr) {
isolate->ThrowIllegalOperation();
return MaybeHandle<String>();
}
IncrementalStringBuilder builder(isolate);
unsigned int i = 0;
Handle<String> args[] = {arg0, arg1, arg2};
for (const char* c = template_string; *c != '\0'; c++) {
if (*c == '%') {
// %% results in verbatim %.
if (*(c + 1) == '%') {
c++;
builder.AppendCharacter('%');
} else {
DCHECK(i < arraysize(args));
Handle<String> arg = args[i++];
builder.AppendString(arg);
}
} else {
builder.AppendCharacter(*c);
}
}
return builder.Finish();
}
MaybeHandle<JSObject> ErrorUtils::Construct(Isolate* isolate,
Handle<JSFunction> target,
Handle<Object> new_target,
Handle<Object> message) {
FrameSkipMode mode = SKIP_FIRST;
Handle<Object> caller;
// When we're passed a JSFunction as new target, we can skip frames until that
// specific function is seen instead of unconditionally skipping the first
// frame.
if (new_target->IsJSFunction()) {
mode = SKIP_UNTIL_SEEN;
caller = new_target;
}
return ErrorUtils::Construct(isolate, target, new_target, message, mode,
caller,
ErrorUtils::StackTraceCollection::kDetailed);
}
MaybeHandle<JSObject> ErrorUtils::Construct(
Isolate* isolate, Handle<JSFunction> target, Handle<Object> new_target,
Handle<Object> message, FrameSkipMode mode, Handle<Object> caller,
StackTraceCollection stack_trace_collection) {
if (FLAG_correctness_fuzzer_suppressions) {
// Abort range errors in correctness fuzzing, as their causes differ
// accross correctness-fuzzing scenarios.
if (target.is_identical_to(isolate->range_error_function())) {
FATAL("Aborting on range error");
}
// Ignore error messages in correctness fuzzing, because the spec leaves
// room for undefined behavior.
message = isolate->factory()->InternalizeUtf8String(
"Message suppressed for fuzzers (--correctness-fuzzer-suppressions)");
}
// 1. If NewTarget is undefined, let newTarget be the active function object,
// else let newTarget be NewTarget.
Handle<JSReceiver> new_target_recv =
new_target->IsJSReceiver() ? Handle<JSReceiver>::cast(new_target)
: Handle<JSReceiver>::cast(target);
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%",
// « [[ErrorData]] »).
Handle<JSObject> err;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, err,
JSObject::New(target, new_target_recv, Handle<AllocationSite>::null()),
JSObject);
// 3. If message is not undefined, then
// a. Let msg be ? ToString(message).
// b. Let msgDesc be the PropertyDescriptor{[[Value]]: msg, [[Writable]]:
// true, [[Enumerable]]: false, [[Configurable]]: true}.
// c. Perform ! DefinePropertyOrThrow(O, "message", msgDesc).
// 4. Return O.
if (!message->IsUndefined(isolate)) {
Handle<String> msg_string;
ASSIGN_RETURN_ON_EXCEPTION(isolate, msg_string,
Object::ToString(isolate, message), JSObject);
RETURN_ON_EXCEPTION(
isolate,
JSObject::SetOwnPropertyIgnoreAttributes(
err, isolate->factory()->message_string(), msg_string, DONT_ENUM),
JSObject);
}
switch (stack_trace_collection) {
case StackTraceCollection::kDetailed:
RETURN_ON_EXCEPTION(
isolate, isolate->CaptureAndSetDetailedStackTrace(err), JSObject);
V8_FALLTHROUGH;
case StackTraceCollection::kSimple:
RETURN_ON_EXCEPTION(
isolate, isolate->CaptureAndSetSimpleStackTrace(err, mode, caller),
JSObject);
break;
case StackTraceCollection::kNone:
break;
}
return err;
}
namespace {
MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate,
Handle<JSReceiver> recv,
Handle<String> key,
Handle<String> default_str) {
Handle<Object> obj;
ASSIGN_RETURN_ON_EXCEPTION(isolate, obj,
JSObject::GetProperty(isolate, recv, key), String);
Handle<String> str;
if (obj->IsUndefined(isolate)) {
str = default_str;
} else {
ASSIGN_RETURN_ON_EXCEPTION(isolate, str, Object::ToString(isolate, obj),
String);
}
return str;
}
} // namespace
// ES6 section 19.5.3.4 Error.prototype.toString ( )
MaybeHandle<String> ErrorUtils::ToString(Isolate* isolate,
Handle<Object> receiver) {
// 1. Let O be the this value.
// 2. If Type(O) is not Object, throw a TypeError exception.
if (!receiver->IsJSReceiver()) {
return isolate->Throw<String>(isolate->factory()->NewTypeError(
MessageTemplate::kIncompatibleMethodReceiver,
isolate->factory()->NewStringFromAsciiChecked(
"Error.prototype.toString"),
receiver));
}
Handle<JSReceiver> recv = Handle<JSReceiver>::cast(receiver);
// 3. Let name be ? Get(O, "name").
// 4. If name is undefined, let name be "Error"; otherwise let name be
// ? ToString(name).
Handle<String> name_key = isolate->factory()->name_string();
Handle<String> name_default = isolate->factory()->Error_string();
Handle<String> name;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, name,
GetStringPropertyOrDefault(isolate, recv, name_key, name_default),
String);
// 5. Let msg be ? Get(O, "message").
// 6. If msg is undefined, let msg be the empty String; otherwise let msg be
// ? ToString(msg).
Handle<String> msg_key = isolate->factory()->message_string();
Handle<String> msg_default = isolate->factory()->empty_string();
Handle<String> msg;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, msg,
GetStringPropertyOrDefault(isolate, recv, msg_key, msg_default), String);
// 7. If name is the empty String, return msg.
// 8. If msg is the empty String, return name.
if (name->length() == 0) return msg;
if (msg->length() == 0) return name;
// 9. Return the result of concatenating name, the code unit 0x003A (COLON),
// the code unit 0x0020 (SPACE), and msg.
IncrementalStringBuilder builder(isolate);
builder.AppendString(name);
builder.AppendCString(": ");
builder.AppendString(msg);
Handle<String> result;
ASSIGN_RETURN_ON_EXCEPTION(isolate, result, builder.Finish(), String);
return result;
}
namespace {
Handle<String> DoFormatMessage(Isolate* isolate, MessageTemplate index,
Handle<Object> arg0, Handle<Object> arg1,
Handle<Object> arg2) {
Handle<String> arg0_str = Object::NoSideEffectsToString(isolate, arg0);
Handle<String> arg1_str = Object::NoSideEffectsToString(isolate, arg1);
Handle<String> arg2_str = Object::NoSideEffectsToString(isolate, arg2);
isolate->native_context()->IncrementErrorsThrown();
Handle<String> msg;
if (!MessageFormatter::Format(isolate, index, arg0_str, arg1_str, arg2_str)
.ToHandle(&msg)) {
DCHECK(isolate->has_pending_exception());
isolate->clear_pending_exception();
isolate->set_external_caught_exception(false);
return isolate->factory()->NewStringFromAsciiChecked("<error>");
}
return msg;
}
} // namespace
// static
Handle<JSObject> ErrorUtils::MakeGenericError(
Isolate* isolate, Handle<JSFunction> constructor, MessageTemplate index,
Handle<Object> arg0, Handle<Object> arg1, Handle<Object> arg2,
FrameSkipMode mode) {
if (FLAG_clear_exceptions_on_js_entry) {
// This function used to be implemented in JavaScript, and JSEntry
// clears any pending exceptions - so whenever we'd call this from C++,
// pending exceptions would be cleared. Preserve this behavior.
isolate->clear_pending_exception();
}
Handle<String> msg = DoFormatMessage(isolate, index, arg0, arg1, arg2);
DCHECK(mode != SKIP_UNTIL_SEEN);
Handle<Object> no_caller;
// The call below can't fail because constructor is a builtin.
DCHECK(constructor->shared().HasBuiltinId());
return ErrorUtils::Construct(isolate, constructor, constructor, msg, mode,
no_caller, StackTraceCollection::kDetailed)
.ToHandleChecked();
}
namespace {
bool ComputeLocation(Isolate* isolate, MessageLocation* target) {
JavaScriptFrameIterator it(isolate);
if (!it.done()) {
// Compute the location from the function and the relocation info of the
// baseline code. For optimized code this will use the deoptimization
// information to get canonical location information.
std::vector<FrameSummary> frames;
it.frame()->Summarize(&frames);
auto& summary = frames.back().AsJavaScript();
Handle<SharedFunctionInfo> shared(summary.function()->shared(), isolate);
Handle<Object> script(shared->script(), isolate);
SharedFunctionInfo::EnsureSourcePositionsAvailable(isolate, shared);
int pos = summary.abstract_code()->SourcePosition(summary.code_offset());
if (script->IsScript() &&
!(Handle<Script>::cast(script)->source().IsUndefined(isolate))) {
Handle<Script> casted_script = Handle<Script>::cast(script);
*target = MessageLocation(casted_script, pos, pos + 1, shared);
return true;
}
}
return false;
}
Handle<String> BuildDefaultCallSite(Isolate* isolate, Handle<Object> object) {
IncrementalStringBuilder builder(isolate);
builder.AppendString(Object::TypeOf(isolate, object));
if (object->IsString()) {
builder.AppendCString(" \"");
builder.AppendString(Handle<String>::cast(object));
builder.AppendCString("\"");
} else if (object->IsNull(isolate)) {
builder.AppendCString(" ");
builder.AppendString(isolate->factory()->null_string());
} else if (object->IsTrue(isolate)) {
builder.AppendCString(" ");
builder.AppendString(isolate->factory()->true_string());
} else if (object->IsFalse(isolate)) {
builder.AppendCString(" ");
builder.AppendString(isolate->factory()->false_string());
} else if (object->IsNumber()) {
builder.AppendCString(" ");
builder.AppendString(isolate->factory()->NumberToString(object));
}
return builder.Finish().ToHandleChecked();
}
Handle<String> RenderCallSite(Isolate* isolate, Handle<Object> object,
MessageLocation* location,
CallPrinter::ErrorHint* hint) {
if (ComputeLocation(isolate, location)) {
UnoptimizedCompileFlags flags = UnoptimizedCompileFlags::ForFunctionCompile(
isolate, *location->shared());
UnoptimizedCompileState compile_state(isolate);
ParseInfo info(isolate, flags, &compile_state);
if (parsing::ParseAny(&info, location->shared(), isolate)) {
info.ast_value_factory()->Internalize(isolate);
CallPrinter printer(isolate, location->shared()->IsUserJavaScript());
Handle<String> str = printer.Print(info.literal(), location->start_pos());
*hint = printer.GetErrorHint();
if (str->length() > 0) return str;
} else {
isolate->clear_pending_exception();
}
}
return BuildDefaultCallSite(isolate, object);
}
MessageTemplate UpdateErrorTemplate(CallPrinter::ErrorHint hint,
MessageTemplate default_id) {
switch (hint) {
case CallPrinter::ErrorHint::kNormalIterator:
return MessageTemplate::kNotIterable;
case CallPrinter::ErrorHint::kCallAndNormalIterator:
return MessageTemplate::kNotCallableOrIterable;
case CallPrinter::ErrorHint::kAsyncIterator:
return MessageTemplate::kNotAsyncIterable;
case CallPrinter::ErrorHint::kCallAndAsyncIterator:
return MessageTemplate::kNotCallableOrAsyncIterable;
case CallPrinter::ErrorHint::kNone:
return default_id;
}
return default_id;
}
} // namespace
Handle<Object> ErrorUtils::NewIteratorError(Isolate* isolate,
Handle<Object> source) {
MessageLocation location;
CallPrinter::ErrorHint hint = CallPrinter::kNone;
Handle<String> callsite = RenderCallSite(isolate, source, &location, &hint);
MessageTemplate id = MessageTemplate::kNotIterableNoSymbolLoad;
if (hint == CallPrinter::kNone) {
Handle<Symbol> iterator_symbol = isolate->factory()->iterator_symbol();
return isolate->factory()->NewTypeError(id, callsite, iterator_symbol);
}
id = UpdateErrorTemplate(hint, id);
return isolate->factory()->NewTypeError(id, callsite);
}
Object ErrorUtils::ThrowSpreadArgError(Isolate* isolate, MessageTemplate id,
Handle<Object> object) {
MessageLocation location;
Handle<String> callsite;
if (ComputeLocation(isolate, &location)) {
UnoptimizedCompileFlags flags = UnoptimizedCompileFlags::ForFunctionCompile(
isolate, *location.shared());
UnoptimizedCompileState compile_state(isolate);
ParseInfo info(isolate, flags, &compile_state);
if (parsing::ParseAny(&info, location.shared(), isolate)) {
info.ast_value_factory()->Internalize(isolate);
CallPrinter printer(isolate, location.shared()->IsUserJavaScript(),
CallPrinter::SpreadErrorInArgsHint::kErrorInArgs);
Handle<String> str = printer.Print(info.literal(), location.start_pos());
callsite =
str->length() > 0 ? str : BuildDefaultCallSite(isolate, object);
if (printer.spread_arg() != nullptr) {
// Change the message location to point at the property name.
int pos = printer.spread_arg()->position();
location =
MessageLocation(location.script(), pos, pos + 1, location.shared());
}
} else {
isolate->clear_pending_exception();
callsite = BuildDefaultCallSite(isolate, object);
}
}
Handle<Object> exception =
isolate->factory()->NewTypeError(id, callsite, object);
return isolate->Throw(*exception, &location);
}
Handle<Object> ErrorUtils::NewCalledNonCallableError(Isolate* isolate,
Handle<Object> source) {
MessageLocation location;
CallPrinter::ErrorHint hint = CallPrinter::kNone;
Handle<String> callsite = RenderCallSite(isolate, source, &location, &hint);
MessageTemplate id = MessageTemplate::kCalledNonCallable;
id = UpdateErrorTemplate(hint, id);
return isolate->factory()->NewTypeError(id, callsite);
}
Handle<Object> ErrorUtils::NewConstructedNonConstructable(
Isolate* isolate, Handle<Object> source) {
MessageLocation location;
CallPrinter::ErrorHint hint = CallPrinter::kNone;
Handle<String> callsite = RenderCallSite(isolate, source, &location, &hint);
MessageTemplate id = MessageTemplate::kNotConstructor;
return isolate->factory()->NewTypeError(id, callsite);
}
Object ErrorUtils::ThrowLoadFromNullOrUndefined(Isolate* isolate,
Handle<Object> object) {
return ThrowLoadFromNullOrUndefined(isolate, object, MaybeHandle<Object>());
}
Object ErrorUtils::ThrowLoadFromNullOrUndefined(Isolate* isolate,
Handle<Object> object,
MaybeHandle<Object> key) {
DCHECK(object->IsNullOrUndefined());
MaybeHandle<String> maybe_property_name;
// Try to extract the property name from the given key, if any.
Handle<Object> key_handle;
if (key.ToHandle(&key_handle)) {
if (key_handle->IsString()) {
maybe_property_name = Handle<String>::cast(key_handle);
}
}
Handle<String> callsite;
// Inline the RenderCallSite logic here so that we can additonally access the
// destructuring property.
bool location_computed = false;
bool is_destructuring = false;
MessageLocation location;
if (ComputeLocation(isolate, &location)) {
location_computed = true;
UnoptimizedCompileFlags flags = UnoptimizedCompileFlags::ForFunctionCompile(
isolate, *location.shared());
UnoptimizedCompileState compile_state(isolate);
ParseInfo info(isolate, flags, &compile_state);
if (parsing::ParseAny(&info, location.shared(), isolate)) {
info.ast_value_factory()->Internalize(isolate);
CallPrinter printer(isolate, location.shared()->IsUserJavaScript());
Handle<String> str = printer.Print(info.literal(), location.start_pos());
int pos = -1;
is_destructuring = printer.destructuring_assignment() != nullptr;
if (is_destructuring) {
// If we don't have one yet, try to extract the property name from the
// destructuring property in the AST.
ObjectLiteralProperty* destructuring_prop =
printer.destructuring_prop();
if (maybe_property_name.is_null() && destructuring_prop != nullptr &&
destructuring_prop->key()->IsPropertyName()) {
maybe_property_name = destructuring_prop->key()
->AsLiteral()
->AsRawPropertyName()
->string();
// Change the message location to point at the property name.
pos = destructuring_prop->key()->position();
}
if (maybe_property_name.is_null()) {
// Change the message location to point at the destructured value.
pos = printer.destructuring_assignment()->value()->position();
}
// If we updated the pos to a valid pos, rewrite the location.
if (pos != -1) {
location = MessageLocation(location.script(), pos, pos + 1,
location.shared());
}
}
if (str->length() > 0) callsite = str;
} else {
isolate->clear_pending_exception();
}
}
if (callsite.is_null()) {
callsite = BuildDefaultCallSite(isolate, object);
}
Handle<Object> error;
Handle<String> property_name;
if (is_destructuring) {
if (maybe_property_name.ToHandle(&property_name)) {
error = isolate->factory()->NewTypeError(
MessageTemplate::kNonCoercibleWithProperty, property_name, callsite,
object);
} else {
error = isolate->factory()->NewTypeError(MessageTemplate::kNonCoercible,
callsite, object);
}
} else {
Handle<Object> key_handle;
if (!key.ToHandle(&key_handle)) {
key_handle = ReadOnlyRoots(isolate).undefined_value_handle();
}
if (*key_handle == ReadOnlyRoots(isolate).iterator_symbol()) {
error = NewIteratorError(isolate, object);
} else {
error = isolate->factory()->NewTypeError(
MessageTemplate::kNonObjectPropertyLoad, key_handle, object);
}
}
return isolate->Throw(*error, location_computed ? &location : nullptr);
}
} // namespace internal
} // namespace v8
|