Line data Source code
1 : // Copyright 2014 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include <memory>
6 :
7 : #include "src/api.h"
8 : #include "src/arguments-inl.h"
9 : #include "src/ast/ast-traversal-visitor.h"
10 : #include "src/ast/prettyprinter.h"
11 : #include "src/bootstrapper.h"
12 : #include "src/builtins/builtins.h"
13 : #include "src/conversions.h"
14 : #include "src/counters.h"
15 : #include "src/debug/debug.h"
16 : #include "src/frames-inl.h"
17 : #include "src/isolate-inl.h"
18 : #include "src/message-template.h"
19 : #include "src/objects/js-array-inl.h"
20 : #include "src/ostreams.h"
21 : #include "src/parsing/parse-info.h"
22 : #include "src/parsing/parsing.h"
23 : #include "src/runtime/runtime-utils.h"
24 : #include "src/snapshot/snapshot.h"
25 : #include "src/string-builder-inl.h"
26 :
27 : namespace v8 {
28 : namespace internal {
29 :
30 0 : RUNTIME_FUNCTION(Runtime_AccessCheck) {
31 0 : HandleScope scope(isolate);
32 : DCHECK_EQ(1, args.length());
33 0 : CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
34 0 : if (!isolate->MayAccess(handle(isolate->context(), isolate), object)) {
35 0 : isolate->ReportFailedAccessCheck(object);
36 0 : RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
37 : }
38 0 : return ReadOnlyRoots(isolate).undefined_value();
39 : }
40 :
41 0 : RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping) {
42 : SealHandleScope shs(isolate);
43 : DCHECK_EQ(0, args.length());
44 0 : CHECK(isolate->bootstrapper()->IsActive());
45 : return ReadOnlyRoots(isolate).undefined_value();
46 : }
47 :
48 0 : RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInAllocateRaw) {
49 0 : HandleScope scope(isolate);
50 : DCHECK_EQ(0, args.length());
51 0 : isolate->heap()->FatalProcessOutOfMemory("CodeStubAssembler::AllocateRaw");
52 0 : UNREACHABLE();
53 : }
54 :
55 0 : RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInvalidArrayLength) {
56 0 : HandleScope scope(isolate);
57 : DCHECK_EQ(0, args.length());
58 0 : isolate->heap()->FatalProcessOutOfMemory("invalid array length");
59 0 : UNREACHABLE();
60 : }
61 :
62 176958 : RUNTIME_FUNCTION(Runtime_Throw) {
63 176958 : HandleScope scope(isolate);
64 : DCHECK_EQ(1, args.length());
65 176958 : return isolate->Throw(args[0]);
66 : }
67 :
68 83819 : RUNTIME_FUNCTION(Runtime_ReThrow) {
69 83819 : HandleScope scope(isolate);
70 : DCHECK_EQ(1, args.length());
71 83819 : return isolate->ReThrow(args[0]);
72 : }
73 :
74 1965 : RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
75 : SealHandleScope shs(isolate);
76 : DCHECK_LE(0, args.length());
77 1965 : return isolate->StackOverflow();
78 : }
79 :
80 0 : RUNTIME_FUNCTION(Runtime_ThrowSymbolAsyncIteratorInvalid) {
81 0 : HandleScope scope(isolate);
82 : DCHECK_EQ(0, args.length());
83 0 : THROW_NEW_ERROR_RETURN_FAILURE(
84 0 : isolate, NewTypeError(MessageTemplate::kSymbolAsyncIteratorInvalid));
85 : }
86 :
87 : #define THROW_ERROR(isolate, args, call) \
88 : HandleScope scope(isolate); \
89 : DCHECK_LE(1, args.length()); \
90 : CONVERT_SMI_ARG_CHECKED(message_id_smi, 0); \
91 : \
92 : Handle<Object> undefined = isolate->factory()->undefined_value(); \
93 : Handle<Object> arg0 = (args.length() > 1) ? args.at(1) : undefined; \
94 : Handle<Object> arg1 = (args.length() > 2) ? args.at(2) : undefined; \
95 : Handle<Object> arg2 = (args.length() > 3) ? args.at(3) : undefined; \
96 : \
97 : MessageTemplate message_id = MessageTemplateFromInt(message_id_smi); \
98 : \
99 : THROW_NEW_ERROR_RETURN_FAILURE(isolate, call(message_id, arg0, arg1, arg2));
100 :
101 11876 : RUNTIME_FUNCTION(Runtime_ThrowRangeError) {
102 35628 : THROW_ERROR(isolate, args, NewRangeError);
103 : }
104 :
105 75737 : RUNTIME_FUNCTION(Runtime_ThrowTypeError) {
106 227211 : THROW_ERROR(isolate, args, NewTypeError);
107 : }
108 :
109 21114 : RUNTIME_FUNCTION(Runtime_ThrowTypeErrorIfStrict) {
110 21114 : if (GetShouldThrow(isolate, Nothing<ShouldThrow>()) ==
111 : ShouldThrow::kDontThrow)
112 : return ReadOnlyRoots(isolate).undefined_value();
113 1701 : THROW_ERROR(isolate, args, NewTypeError);
114 : }
115 :
116 : #undef THROW_ERROR
117 :
118 : namespace {
119 :
120 562 : const char* ElementsKindToType(ElementsKind fixed_elements_kind) {
121 562 : switch (fixed_elements_kind) {
122 : #define ELEMENTS_KIND_CASE(Type, type, TYPE, ctype) \
123 : case TYPE##_ELEMENTS: \
124 : return #Type "Array";
125 :
126 0 : TYPED_ARRAYS(ELEMENTS_KIND_CASE)
127 : #undef ELEMENTS_KIND_CASE
128 :
129 : default:
130 0 : UNREACHABLE();
131 : }
132 : }
133 :
134 : } // namespace
135 :
136 562 : RUNTIME_FUNCTION(Runtime_ThrowInvalidTypedArrayAlignment) {
137 562 : HandleScope scope(isolate);
138 : DCHECK_EQ(2, args.length());
139 1124 : CONVERT_ARG_HANDLE_CHECKED(Map, map, 0);
140 1124 : CONVERT_ARG_HANDLE_CHECKED(String, problem_string, 1);
141 :
142 562 : ElementsKind kind = map->elements_kind();
143 :
144 : Handle<String> type =
145 562 : isolate->factory()->NewStringFromAsciiChecked(ElementsKindToType(kind));
146 :
147 : ExternalArrayType external_type;
148 : size_t size;
149 562 : Factory::TypeAndSizeForElementsKind(kind, &external_type, &size);
150 : Handle<Object> element_size =
151 562 : handle(Smi::FromInt(static_cast<int>(size)), isolate);
152 :
153 1124 : THROW_NEW_ERROR_RETURN_FAILURE(
154 : isolate, NewRangeError(MessageTemplate::kInvalidTypedArrayAlignment,
155 562 : problem_string, type, element_size));
156 : }
157 :
158 1305026 : RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
159 : SealHandleScope shs(isolate);
160 : DCHECK_EQ(0, args.length());
161 1305026 : return isolate->UnwindAndFindHandler();
162 : }
163 :
164 271 : RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
165 : SealHandleScope shs(isolate);
166 : DCHECK_EQ(0, args.length());
167 271 : return isolate->PromoteScheduledException();
168 : }
169 :
170 0 : RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
171 0 : HandleScope scope(isolate);
172 : DCHECK_EQ(1, args.length());
173 0 : CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
174 0 : THROW_NEW_ERROR_RETURN_FAILURE(
175 0 : isolate, NewReferenceError(MessageTemplate::kNotDefined, name));
176 : }
177 :
178 23395 : RUNTIME_FUNCTION(Runtime_ThrowAccessedUninitializedVariable) {
179 23395 : HandleScope scope(isolate);
180 : DCHECK_EQ(1, args.length());
181 23395 : CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
182 46790 : THROW_NEW_ERROR_RETURN_FAILURE(
183 : isolate,
184 23395 : NewReferenceError(MessageTemplate::kAccessedUninitializedVariable, name));
185 : }
186 :
187 153 : RUNTIME_FUNCTION(Runtime_NewTypeError) {
188 153 : HandleScope scope(isolate);
189 : DCHECK_EQ(2, args.length());
190 306 : CONVERT_INT32_ARG_CHECKED(template_index, 0);
191 153 : CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
192 153 : MessageTemplate message_template = MessageTemplateFromInt(template_index);
193 306 : return *isolate->factory()->NewTypeError(message_template, arg0);
194 : }
195 :
196 117 : RUNTIME_FUNCTION(Runtime_NewReferenceError) {
197 117 : HandleScope scope(isolate);
198 : DCHECK_EQ(2, args.length());
199 234 : CONVERT_INT32_ARG_CHECKED(template_index, 0);
200 117 : CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
201 117 : MessageTemplate message_template = MessageTemplateFromInt(template_index);
202 234 : return *isolate->factory()->NewReferenceError(message_template, arg0);
203 : }
204 :
205 0 : RUNTIME_FUNCTION(Runtime_NewSyntaxError) {
206 0 : HandleScope scope(isolate);
207 : DCHECK_EQ(2, args.length());
208 0 : CONVERT_INT32_ARG_CHECKED(template_index, 0);
209 0 : CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
210 0 : MessageTemplate message_template = MessageTemplateFromInt(template_index);
211 0 : return *isolate->factory()->NewSyntaxError(message_template, arg0);
212 : }
213 :
214 195 : RUNTIME_FUNCTION(Runtime_ThrowInvalidStringLength) {
215 195 : HandleScope scope(isolate);
216 390 : THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
217 : }
218 :
219 360 : RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) {
220 360 : HandleScope scope(isolate);
221 : DCHECK_EQ(1, args.length());
222 360 : CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
223 720 : THROW_NEW_ERROR_RETURN_FAILURE(
224 : isolate,
225 360 : NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
226 : }
227 :
228 9 : RUNTIME_FUNCTION(Runtime_ThrowThrowMethodMissing) {
229 9 : HandleScope scope(isolate);
230 : DCHECK_EQ(0, args.length());
231 18 : THROW_NEW_ERROR_RETURN_FAILURE(
232 9 : isolate, NewTypeError(MessageTemplate::kThrowMethodMissing));
233 : }
234 :
235 266 : RUNTIME_FUNCTION(Runtime_ThrowSymbolIteratorInvalid) {
236 266 : HandleScope scope(isolate);
237 : DCHECK_EQ(0, args.length());
238 532 : THROW_NEW_ERROR_RETURN_FAILURE(
239 266 : isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
240 : }
241 :
242 1085 : RUNTIME_FUNCTION(Runtime_ThrowNotConstructor) {
243 1085 : HandleScope scope(isolate);
244 : DCHECK_EQ(1, args.length());
245 1085 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
246 2170 : THROW_NEW_ERROR_RETURN_FAILURE(
247 1085 : isolate, NewTypeError(MessageTemplate::kNotConstructor, object));
248 : }
249 :
250 1284 : RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) {
251 1284 : HandleScope scope(isolate);
252 : DCHECK_EQ(1, args.length());
253 1284 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
254 1284 : Handle<String> type = Object::TypeOf(isolate, object);
255 2568 : THROW_NEW_ERROR_RETURN_FAILURE(
256 1284 : isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type));
257 : }
258 :
259 23171 : RUNTIME_FUNCTION(Runtime_StackGuard) {
260 : SealHandleScope shs(isolate);
261 : DCHECK_EQ(0, args.length());
262 :
263 : // First check if this is a real stack overflow.
264 23171 : StackLimitCheck check(isolate);
265 23171 : if (check.JsHasOverflowed()) {
266 591 : return isolate->StackOverflow();
267 : }
268 :
269 22580 : return isolate->stack_guard()->HandleInterrupts();
270 : }
271 :
272 145504 : RUNTIME_FUNCTION(Runtime_Interrupt) {
273 : SealHandleScope shs(isolate);
274 : DCHECK_EQ(0, args.length());
275 145504 : return isolate->stack_guard()->HandleInterrupts();
276 : }
277 :
278 162154 : RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) {
279 162154 : HandleScope scope(isolate);
280 : DCHECK_EQ(1, args.length());
281 324308 : CONVERT_SMI_ARG_CHECKED(size, 0);
282 162154 : CHECK(IsAligned(size, kTaggedSize));
283 162154 : CHECK_GT(size, 0);
284 162154 : CHECK_LE(size, kMaxRegularHeapObjectSize);
285 324308 : return *isolate->factory()->NewFillerObject(size, false, NEW_SPACE);
286 : }
287 :
288 32753 : RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) {
289 32753 : HandleScope scope(isolate);
290 : DCHECK_EQ(2, args.length());
291 65506 : CONVERT_SMI_ARG_CHECKED(size, 0);
292 65506 : CONVERT_SMI_ARG_CHECKED(flags, 1);
293 32753 : CHECK(IsAligned(size, kTaggedSize));
294 32753 : CHECK_GT(size, 0);
295 32753 : bool double_align = AllocateDoubleAlignFlag::decode(flags);
296 32753 : AllocationSpace space = AllocateTargetSpace::decode(flags);
297 32753 : CHECK(size <= kMaxRegularHeapObjectSize || space == LO_SPACE);
298 32753 : if (FLAG_young_generation_large_objects && space == LO_SPACE) {
299 : space = NEW_LO_SPACE;
300 : }
301 65506 : return *isolate->factory()->NewFillerObject(size, double_align, space);
302 : }
303 :
304 142 : RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString) {
305 142 : HandleScope scope(isolate);
306 : DCHECK_EQ(1, args.length());
307 284 : CONVERT_SMI_ARG_CHECKED(length, 0);
308 142 : if (length == 0) return ReadOnlyRoots(isolate).empty_string();
309 : Handle<SeqOneByteString> result;
310 284 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
311 : isolate, result, isolate->factory()->NewRawOneByteString(length));
312 142 : return *result;
313 : }
314 :
315 0 : RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString) {
316 0 : HandleScope scope(isolate);
317 : DCHECK_EQ(1, args.length());
318 0 : CONVERT_SMI_ARG_CHECKED(length, 0);
319 0 : if (length == 0) return ReadOnlyRoots(isolate).empty_string();
320 : Handle<SeqTwoByteString> result;
321 0 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
322 : isolate, result, isolate->factory()->NewRawTwoByteString(length));
323 0 : return *result;
324 : }
325 :
326 : namespace {
327 :
328 7877 : bool ComputeLocation(Isolate* isolate, MessageLocation* target) {
329 7877 : JavaScriptFrameIterator it(isolate);
330 7877 : if (!it.done()) {
331 : // Compute the location from the function and the relocation info of the
332 : // baseline code. For optimized code this will use the deoptimization
333 : // information to get canonical location information.
334 : std::vector<FrameSummary> frames;
335 7865 : it.frame()->Summarize(&frames);
336 7865 : auto& summary = frames.back().AsJavaScript();
337 15730 : Handle<SharedFunctionInfo> shared(summary.function()->shared(), isolate);
338 15730 : Handle<Object> script(shared->script(), isolate);
339 7865 : int pos = summary.abstract_code()->SourcePosition(summary.code_offset());
340 30618 : if (script->IsScript() &&
341 21911 : !(Handle<Script>::cast(script)->source()->IsUndefined(isolate))) {
342 7023 : Handle<Script> casted_script = Handle<Script>::cast(script);
343 7023 : *target = MessageLocation(casted_script, pos, pos + 1, shared);
344 : return true;
345 842 : }
346 : }
347 : return false;
348 : }
349 :
350 1809 : Handle<String> BuildDefaultCallSite(Isolate* isolate, Handle<Object> object) {
351 1809 : IncrementalStringBuilder builder(isolate);
352 :
353 1809 : builder.AppendString(Object::TypeOf(isolate, object));
354 3618 : if (object->IsString()) {
355 : builder.AppendCString(" \"");
356 36 : builder.AppendString(Handle<String>::cast(object));
357 : builder.AppendCString("\"");
358 3546 : } else if (object->IsNull(isolate)) {
359 : builder.AppendCString(" ");
360 486 : builder.AppendString(isolate->factory()->null_string());
361 2574 : } else if (object->IsTrue(isolate)) {
362 : builder.AppendCString(" ");
363 36 : builder.AppendString(isolate->factory()->true_string());
364 2502 : } else if (object->IsFalse(isolate)) {
365 : builder.AppendCString(" ");
366 0 : builder.AppendString(isolate->factory()->false_string());
367 2502 : } else if (object->IsNumber()) {
368 : builder.AppendCString(" ");
369 140 : builder.AppendString(isolate->factory()->NumberToString(object));
370 : }
371 :
372 3618 : return builder.Finish().ToHandleChecked();
373 : }
374 :
375 7655 : Handle<String> RenderCallSite(Isolate* isolate, Handle<Object> object,
376 : CallPrinter::ErrorHint* hint) {
377 7655 : MessageLocation location;
378 7655 : if (ComputeLocation(isolate, &location)) {
379 6801 : ParseInfo info(isolate, location.shared());
380 6801 : if (parsing::ParseAny(&info, location.shared(), isolate)) {
381 6409 : info.ast_value_factory()->Internalize(isolate);
382 6409 : CallPrinter printer(isolate, location.shared()->IsUserJavaScript());
383 6409 : Handle<String> str = printer.Print(info.literal(), location.start_pos());
384 6409 : *hint = printer.GetErrorHint();
385 6409 : if (str->length() > 0) return str;
386 : } else {
387 392 : isolate->clear_pending_exception();
388 955 : }
389 : }
390 1809 : return BuildDefaultCallSite(isolate, object);
391 : }
392 :
393 4337 : MessageTemplate UpdateErrorTemplate(CallPrinter::ErrorHint hint,
394 : MessageTemplate default_id) {
395 4365 : switch (hint) {
396 : case CallPrinter::ErrorHint::kNormalIterator:
397 : return MessageTemplate::kNotIterable;
398 :
399 : case CallPrinter::ErrorHint::kCallAndNormalIterator:
400 69 : return MessageTemplate::kNotCallableOrIterable;
401 :
402 : case CallPrinter::ErrorHint::kAsyncIterator:
403 9 : return MessageTemplate::kNotAsyncIterable;
404 :
405 : case CallPrinter::ErrorHint::kCallAndAsyncIterator:
406 0 : return MessageTemplate::kNotCallableOrAsyncIterable;
407 :
408 : case CallPrinter::ErrorHint::kNone:
409 4207 : return default_id;
410 : }
411 0 : return default_id;
412 : }
413 :
414 : } // namespace
415 :
416 1064 : MaybeHandle<Object> Runtime::ThrowIteratorError(Isolate* isolate,
417 : Handle<Object> object) {
418 1064 : CallPrinter::ErrorHint hint = CallPrinter::kNone;
419 1064 : Handle<String> callsite = RenderCallSite(isolate, object, &hint);
420 : MessageTemplate id = MessageTemplate::kNotIterableNoSymbolLoad;
421 :
422 1064 : if (hint == CallPrinter::kNone) {
423 : Handle<Symbol> iterator_symbol = isolate->factory()->iterator_symbol();
424 1036 : THROW_NEW_ERROR(isolate, NewTypeError(id, callsite, iterator_symbol),
425 : Object);
426 : }
427 :
428 : id = UpdateErrorTemplate(hint, id);
429 28 : THROW_NEW_ERROR(isolate, NewTypeError(id, callsite), Object);
430 : }
431 :
432 271 : RUNTIME_FUNCTION(Runtime_ThrowIteratorError) {
433 271 : HandleScope scope(isolate);
434 : DCHECK_EQ(1, args.length());
435 271 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
436 542 : RETURN_RESULT_OR_FAILURE(isolate,
437 271 : Runtime::ThrowIteratorError(isolate, object));
438 : }
439 :
440 4337 : RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable) {
441 4337 : HandleScope scope(isolate);
442 : DCHECK_EQ(1, args.length());
443 4337 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
444 4337 : CallPrinter::ErrorHint hint = CallPrinter::kNone;
445 4337 : Handle<String> callsite = RenderCallSite(isolate, object, &hint);
446 : MessageTemplate id = MessageTemplate::kCalledNonCallable;
447 4337 : id = UpdateErrorTemplate(hint, id);
448 8674 : THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(id, callsite));
449 : }
450 :
451 2254 : RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable) {
452 2254 : HandleScope scope(isolate);
453 : DCHECK_EQ(1, args.length());
454 2254 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
455 2254 : CallPrinter::ErrorHint hint = CallPrinter::kNone;
456 2254 : Handle<String> callsite = RenderCallSite(isolate, object, &hint);
457 : MessageTemplate id = MessageTemplate::kNotConstructor;
458 4508 : THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(id, callsite));
459 : }
460 :
461 : namespace {
462 :
463 : // Helper visitor for ThrowPatternAssignmentNonCoercible which finds an
464 : // object literal (representing a destructuring assignment) at a given source
465 : // position.
466 : class PatternFinder final : public AstTraversalVisitor<PatternFinder> {
467 : public:
468 222 : PatternFinder(Isolate* isolate, Expression* root, int position)
469 : : AstTraversalVisitor(isolate, root),
470 : position_(position),
471 222 : object_literal_(nullptr) {}
472 :
473 222 : ObjectLiteral* object_literal() const { return object_literal_; }
474 :
475 : private:
476 : // This is required so that the overriden Visit* methods can be
477 : // called by the base class (template).
478 : friend class AstTraversalVisitor<PatternFinder>;
479 :
480 294 : void VisitObjectLiteral(ObjectLiteral* lit) {
481 : // TODO(leszeks): This could be smarter in only traversing object literals
482 : // that are known to be a destructuring pattern. We could then also
483 : // potentially find the corresponding assignment value and report that too.
484 294 : if (lit->position() == position_) {
485 222 : object_literal_ = lit;
486 516 : return;
487 : }
488 72 : AstTraversalVisitor::VisitObjectLiteral(lit);
489 : }
490 :
491 : int position_;
492 : ObjectLiteral* object_literal_;
493 : };
494 :
495 : } // namespace
496 :
497 222 : RUNTIME_FUNCTION(Runtime_ThrowPatternAssignmentNonCoercible) {
498 222 : HandleScope scope(isolate);
499 : DCHECK_EQ(0, args.length());
500 :
501 : // Find the object literal representing the destructuring assignment, so that
502 : // we can try to attribute the error to a property name on it rather than to
503 : // the literal itself.
504 : MaybeHandle<String> maybe_property_name;
505 222 : MessageLocation location;
506 222 : if (ComputeLocation(isolate, &location)) {
507 222 : ParseInfo info(isolate, location.shared());
508 222 : if (parsing::ParseAny(&info, location.shared(), isolate)) {
509 222 : info.ast_value_factory()->Internalize(isolate);
510 :
511 222 : PatternFinder finder(isolate, info.literal(), location.start_pos());
512 222 : finder.Run();
513 222 : if (finder.object_literal()) {
514 46 : for (ObjectLiteralProperty* pattern_property :
515 222 : *finder.object_literal()->properties()) {
516 132 : Expression* key = pattern_property->key();
517 132 : if (key->IsPropertyName()) {
518 86 : int pos = key->position();
519 : maybe_property_name =
520 172 : key->AsLiteral()->AsRawPropertyName()->string();
521 : // Change the message location to point at the property name.
522 : location = MessageLocation(location.script(), pos, pos + 1,
523 86 : location.shared());
524 : break;
525 : }
526 : }
527 : }
528 : } else {
529 0 : isolate->clear_pending_exception();
530 222 : }
531 : }
532 :
533 : // Create a "non-coercible" type error with a property name if one is
534 : // available, otherwise create a generic one.
535 : Handle<Object> error;
536 : Handle<String> property_name;
537 222 : if (maybe_property_name.ToHandle(&property_name)) {
538 : error = isolate->factory()->NewTypeError(
539 86 : MessageTemplate::kNonCoercibleWithProperty, property_name);
540 : } else {
541 136 : error = isolate->factory()->NewTypeError(MessageTemplate::kNonCoercible);
542 : }
543 :
544 : // Explicitly pass the calculated location, as we may have updated it to match
545 : // the property name.
546 222 : return isolate->Throw(*error, &location);
547 : }
548 :
549 522 : RUNTIME_FUNCTION(Runtime_ThrowConstructorReturnedNonObject) {
550 522 : HandleScope scope(isolate);
551 : DCHECK_EQ(0, args.length());
552 :
553 1044 : THROW_NEW_ERROR_RETURN_FAILURE(
554 : isolate,
555 522 : NewTypeError(MessageTemplate::kDerivedConstructorReturnedNonObject));
556 : }
557 :
558 : // ES6 section 7.3.17 CreateListFromArrayLike (obj)
559 16482 : RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike) {
560 16482 : HandleScope scope(isolate);
561 : DCHECK_EQ(1, args.length());
562 16482 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
563 32964 : RETURN_RESULT_OR_FAILURE(isolate, Object::CreateListFromArrayLike(
564 16482 : isolate, object, ElementTypes::kAll));
565 : }
566 :
567 648 : RUNTIME_FUNCTION(Runtime_IncrementUseCounter) {
568 648 : HandleScope scope(isolate);
569 : DCHECK_EQ(1, args.length());
570 1296 : CONVERT_SMI_ARG_CHECKED(counter, 0);
571 648 : isolate->CountUsage(static_cast<v8::Isolate::UseCounterFeature>(counter));
572 648 : return ReadOnlyRoots(isolate).undefined_value();
573 : }
574 :
575 0 : RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats) {
576 0 : HandleScope scope(isolate);
577 :
578 : // Append any worker thread runtime call stats to the main table before
579 : // printing.
580 : isolate->counters()->worker_thread_runtime_call_stats()->AddToMainTable(
581 0 : isolate->counters()->runtime_call_stats());
582 :
583 0 : if (args.length() == 0) {
584 : // Without arguments, the result is returned as a string.
585 : DCHECK_EQ(0, args.length());
586 0 : std::stringstream stats_stream;
587 0 : isolate->counters()->runtime_call_stats()->Print(stats_stream);
588 : Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(
589 0 : stats_stream.str().c_str());
590 0 : isolate->counters()->runtime_call_stats()->Reset();
591 0 : return *result;
592 : } else {
593 : DCHECK_LE(args.length(), 2);
594 : std::FILE* f;
595 0 : if (args[0]->IsString()) {
596 : // With a string argument, the results are appended to that file.
597 0 : CONVERT_ARG_HANDLE_CHECKED(String, arg0, 0);
598 : DisallowHeapAllocation no_gc;
599 0 : String::FlatContent flat = arg0->GetFlatContent(no_gc);
600 : const char* filename =
601 0 : reinterpret_cast<const char*>(&(flat.ToOneByteVector()[0]));
602 0 : f = std::fopen(filename, "a");
603 : DCHECK_NOT_NULL(f);
604 : } else {
605 : // With an integer argument, the results are written to stdout/stderr.
606 0 : CONVERT_SMI_ARG_CHECKED(fd, 0);
607 : DCHECK(fd == 1 || fd == 2);
608 0 : f = fd == 1 ? stdout : stderr;
609 : }
610 : // The second argument (if any) is a message header to be printed.
611 0 : if (args.length() >= 2) {
612 0 : CONVERT_ARG_HANDLE_CHECKED(String, arg1, 1);
613 0 : arg1->PrintOn(f);
614 0 : std::fputc('\n', f);
615 0 : std::fflush(f);
616 : }
617 0 : OFStream stats_stream(f);
618 0 : isolate->counters()->runtime_call_stats()->Print(stats_stream);
619 0 : isolate->counters()->runtime_call_stats()->Reset();
620 0 : if (args[0]->IsString())
621 0 : std::fclose(f);
622 : else
623 0 : std::fflush(f);
624 0 : return ReadOnlyRoots(isolate).undefined_value();
625 0 : }
626 : }
627 :
628 107475 : RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) {
629 107475 : HandleScope scope(isolate);
630 : DCHECK_EQ(2, args.length());
631 107475 : CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0);
632 107475 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
633 214950 : RETURN_RESULT_OR_FAILURE(
634 107475 : isolate, Object::OrdinaryHasInstance(isolate, callable, object));
635 : }
636 :
637 18 : RUNTIME_FUNCTION(Runtime_Typeof) {
638 18 : HandleScope scope(isolate);
639 : DCHECK_EQ(1, args.length());
640 18 : CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
641 36 : return *Object::TypeOf(isolate, object);
642 : }
643 :
644 54 : RUNTIME_FUNCTION(Runtime_AllowDynamicFunction) {
645 54 : HandleScope scope(isolate);
646 : DCHECK_EQ(1, args.length());
647 108 : CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0);
648 108 : Handle<JSObject> global_proxy(target->global_proxy(), isolate);
649 : return *isolate->factory()->ToBoolean(
650 108 : Builtins::AllowDynamicFunction(isolate, target, global_proxy));
651 : }
652 :
653 210 : RUNTIME_FUNCTION(Runtime_CreateAsyncFromSyncIterator) {
654 210 : HandleScope scope(isolate);
655 : DCHECK_EQ(1, args.length());
656 :
657 210 : CONVERT_ARG_HANDLE_CHECKED(Object, sync_iterator, 0);
658 :
659 420 : if (!sync_iterator->IsJSReceiver()) {
660 0 : THROW_NEW_ERROR_RETURN_FAILURE(
661 : isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
662 : }
663 :
664 : Handle<Object> next;
665 630 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
666 : isolate, next,
667 : Object::GetProperty(isolate, sync_iterator,
668 : isolate->factory()->next_string()));
669 :
670 : return *isolate->factory()->NewJSAsyncFromSyncIterator(
671 420 : Handle<JSReceiver>::cast(sync_iterator), next);
672 : }
673 :
674 2437 : RUNTIME_FUNCTION(Runtime_CreateTemplateObject) {
675 2437 : HandleScope scope(isolate);
676 : DCHECK_EQ(1, args.length());
677 4874 : CONVERT_ARG_HANDLE_CHECKED(TemplateObjectDescription, description, 0);
678 :
679 4874 : return *TemplateObjectDescription::CreateTemplateObject(isolate, description);
680 : }
681 :
682 33 : RUNTIME_FUNCTION(Runtime_ReportMessage) {
683 : // Helper to report messages and continue JS execution. This is intended to
684 : // behave similarly to reporting exceptions which reach the top-level in
685 : // Execution.cc, but allow the JS code to continue. This is useful for
686 : // implementing algorithms such as RunMicrotasks in JS.
687 33 : HandleScope scope(isolate);
688 : DCHECK_EQ(1, args.length());
689 :
690 33 : CONVERT_ARG_HANDLE_CHECKED(Object, message_obj, 0);
691 :
692 : DCHECK(!isolate->has_pending_exception());
693 33 : isolate->set_pending_exception(*message_obj);
694 33 : isolate->ReportPendingMessagesFromJavaScript();
695 33 : isolate->clear_pending_exception();
696 33 : return ReadOnlyRoots(isolate).undefined_value();
697 : }
698 :
699 18 : RUNTIME_FUNCTION(Runtime_GetInitializerFunction) {
700 18 : HandleScope scope(isolate);
701 : DCHECK_EQ(1, args.length());
702 :
703 36 : CONVERT_ARG_HANDLE_CHECKED(JSReceiver, constructor, 0);
704 18 : Handle<Symbol> key = isolate->factory()->class_fields_symbol();
705 18 : Handle<Object> initializer = JSReceiver::GetDataProperty(constructor, key);
706 18 : return *initializer;
707 : }
708 : } // namespace internal
709 178779 : } // namespace v8
|