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