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