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/api.h"
8 #include "src/ast/ast-traversal-visitor.h"
9 #include "src/ast/prettyprinter.h"
10 #include "src/builtins/builtins.h"
11 #include "src/common/message-template.h"
12 #include "src/debug/debug.h"
13 #include "src/execution/arguments-inl.h"
14 #include "src/execution/frames-inl.h"
15 #include "src/execution/isolate-inl.h"
16 #include "src/execution/messages.h"
17 #include "src/execution/runtime-profiler.h"
18 #include "src/handles/maybe-handles.h"
19 #include "src/init/bootstrapper.h"
20 #include "src/logging/counters.h"
21 #include "src/numbers/conversions.h"
22 #include "src/objects/feedback-vector-inl.h"
23 #include "src/objects/js-array-inl.h"
24 #include "src/objects/template-objects-inl.h"
25 #include "src/parsing/parse-info.h"
26 #include "src/parsing/parsing.h"
27 #include "src/runtime/runtime-utils.h"
28 #include "src/snapshot/snapshot.h"
29 #include "src/strings/string-builder-inl.h"
30 #include "src/utils/ostreams.h"
31
32 namespace v8 {
33 namespace internal {
34
RUNTIME_FUNCTION(Runtime_AccessCheck)35 RUNTIME_FUNCTION(Runtime_AccessCheck) {
36 HandleScope scope(isolate);
37 DCHECK_EQ(1, args.length());
38 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
39 if (!isolate->MayAccess(handle(isolate->context(), isolate), object)) {
40 isolate->ReportFailedAccessCheck(object);
41 RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
42 }
43 return ReadOnlyRoots(isolate).undefined_value();
44 }
45
RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInAllocateRaw)46 RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInAllocateRaw) {
47 HandleScope scope(isolate);
48 DCHECK_EQ(0, args.length());
49 isolate->heap()->FatalProcessOutOfMemory("CodeStubAssembler::AllocateRaw");
50 UNREACHABLE();
51 }
52
RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInvalidArrayLength)53 RUNTIME_FUNCTION(Runtime_FatalProcessOutOfMemoryInvalidArrayLength) {
54 HandleScope scope(isolate);
55 DCHECK_EQ(0, args.length());
56 isolate->heap()->FatalProcessOutOfMemory("invalid array length");
57 UNREACHABLE();
58 }
59
RUNTIME_FUNCTION(Runtime_Throw)60 RUNTIME_FUNCTION(Runtime_Throw) {
61 HandleScope scope(isolate);
62 DCHECK_EQ(1, args.length());
63 return isolate->Throw(args[0]);
64 }
65
RUNTIME_FUNCTION(Runtime_ReThrow)66 RUNTIME_FUNCTION(Runtime_ReThrow) {
67 HandleScope scope(isolate);
68 DCHECK_EQ(1, args.length());
69 return isolate->ReThrow(args[0]);
70 }
71
RUNTIME_FUNCTION(Runtime_ThrowStackOverflow)72 RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
73 SealHandleScope shs(isolate);
74 DCHECK_LE(0, args.length());
75 return isolate->StackOverflow();
76 }
77
RUNTIME_FUNCTION(Runtime_ThrowSymbolAsyncIteratorInvalid)78 RUNTIME_FUNCTION(Runtime_ThrowSymbolAsyncIteratorInvalid) {
79 HandleScope scope(isolate);
80 DCHECK_EQ(0, args.length());
81 THROW_NEW_ERROR_RETURN_FAILURE(
82 isolate, NewTypeError(MessageTemplate::kSymbolAsyncIteratorInvalid));
83 }
84
85 #define THROW_ERROR(isolate, args, call) \
86 HandleScope scope(isolate); \
87 DCHECK_LE(1, args.length()); \
88 CONVERT_SMI_ARG_CHECKED(message_id_smi, 0); \
89 \
90 Handle<Object> undefined = isolate->factory()->undefined_value(); \
91 Handle<Object> arg0 = (args.length() > 1) ? args.at(1) : undefined; \
92 Handle<Object> arg1 = (args.length() > 2) ? args.at(2) : undefined; \
93 Handle<Object> arg2 = (args.length() > 3) ? args.at(3) : undefined; \
94 \
95 MessageTemplate message_id = MessageTemplateFromInt(message_id_smi); \
96 \
97 THROW_NEW_ERROR_RETURN_FAILURE(isolate, call(message_id, arg0, arg1, arg2));
98
RUNTIME_FUNCTION(Runtime_ThrowRangeError)99 RUNTIME_FUNCTION(Runtime_ThrowRangeError) {
100 if (FLAG_correctness_fuzzer_suppressions) {
101 DCHECK_LE(1, args.length());
102 CONVERT_SMI_ARG_CHECKED(message_id_smi, 0);
103
104 // If the result of a BigInt computation is truncated to 64 bit, Turbofan
105 // can sometimes truncate intermediate results already, which can prevent
106 // those from exceeding the maximum length, effectively preventing a
107 // RangeError from being thrown. As this is a performance optimization, this
108 // behavior is accepted. To prevent the correctness fuzzer from detecting
109 // this difference, we crash the program.
110 if (MessageTemplateFromInt(message_id_smi) ==
111 MessageTemplate::kBigIntTooBig) {
112 FATAL("Aborting on invalid BigInt length");
113 }
114 }
115
116 THROW_ERROR(isolate, args, NewRangeError);
117 }
118
RUNTIME_FUNCTION(Runtime_ThrowTypeError)119 RUNTIME_FUNCTION(Runtime_ThrowTypeError) {
120 THROW_ERROR(isolate, args, NewTypeError);
121 }
122
RUNTIME_FUNCTION(Runtime_ThrowTypeErrorIfStrict)123 RUNTIME_FUNCTION(Runtime_ThrowTypeErrorIfStrict) {
124 if (GetShouldThrow(isolate, Nothing<ShouldThrow>()) ==
125 ShouldThrow::kDontThrow)
126 return ReadOnlyRoots(isolate).undefined_value();
127 THROW_ERROR(isolate, args, NewTypeError);
128 }
129
130 #undef THROW_ERROR
131
132 namespace {
133
ElementsKindToType(ElementsKind fixed_elements_kind)134 const char* ElementsKindToType(ElementsKind fixed_elements_kind) {
135 switch (fixed_elements_kind) {
136 #define ELEMENTS_KIND_CASE(Type, type, TYPE, ctype) \
137 case TYPE##_ELEMENTS: \
138 return #Type "Array";
139
140 TYPED_ARRAYS(ELEMENTS_KIND_CASE)
141 #undef ELEMENTS_KIND_CASE
142
143 default:
144 UNREACHABLE();
145 }
146 }
147
148 } // namespace
149
RUNTIME_FUNCTION(Runtime_ThrowInvalidTypedArrayAlignment)150 RUNTIME_FUNCTION(Runtime_ThrowInvalidTypedArrayAlignment) {
151 HandleScope scope(isolate);
152 DCHECK_EQ(2, args.length());
153 CONVERT_ARG_HANDLE_CHECKED(Map, map, 0);
154 CONVERT_ARG_HANDLE_CHECKED(String, problem_string, 1);
155
156 ElementsKind kind = map->elements_kind();
157
158 Handle<String> type =
159 isolate->factory()->NewStringFromAsciiChecked(ElementsKindToType(kind));
160
161 ExternalArrayType external_type;
162 size_t size;
163 Factory::TypeAndSizeForElementsKind(kind, &external_type, &size);
164 Handle<Object> element_size =
165 handle(Smi::FromInt(static_cast<int>(size)), isolate);
166
167 THROW_NEW_ERROR_RETURN_FAILURE(
168 isolate, NewRangeError(MessageTemplate::kInvalidTypedArrayAlignment,
169 problem_string, type, element_size));
170 }
171
RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler)172 RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
173 SealHandleScope shs(isolate);
174 DCHECK_EQ(0, args.length());
175 return isolate->UnwindAndFindHandler();
176 }
177
RUNTIME_FUNCTION(Runtime_PromoteScheduledException)178 RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
179 SealHandleScope shs(isolate);
180 DCHECK_EQ(0, args.length());
181 return isolate->PromoteScheduledException();
182 }
183
RUNTIME_FUNCTION(Runtime_ThrowReferenceError)184 RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
185 HandleScope scope(isolate);
186 DCHECK_EQ(1, args.length());
187 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
188 THROW_NEW_ERROR_RETURN_FAILURE(
189 isolate, NewReferenceError(MessageTemplate::kNotDefined, name));
190 }
191
RUNTIME_FUNCTION(Runtime_ThrowAccessedUninitializedVariable)192 RUNTIME_FUNCTION(Runtime_ThrowAccessedUninitializedVariable) {
193 HandleScope scope(isolate);
194 DCHECK_EQ(1, args.length());
195 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
196 THROW_NEW_ERROR_RETURN_FAILURE(
197 isolate,
198 NewReferenceError(MessageTemplate::kAccessedUninitializedVariable, name));
199 }
200
RUNTIME_FUNCTION(Runtime_NewError)201 RUNTIME_FUNCTION(Runtime_NewError) {
202 HandleScope scope(isolate);
203 DCHECK_EQ(2, args.length());
204 CONVERT_INT32_ARG_CHECKED(template_index, 0);
205 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
206 MessageTemplate message_template = MessageTemplateFromInt(template_index);
207 return *isolate->factory()->NewError(message_template, arg0);
208 }
209
RUNTIME_FUNCTION(Runtime_NewTypeError)210 RUNTIME_FUNCTION(Runtime_NewTypeError) {
211 HandleScope scope(isolate);
212 DCHECK_LE(args.length(), 4);
213 DCHECK_GE(args.length(), 1);
214 CONVERT_INT32_ARG_CHECKED(template_index, 0);
215 MessageTemplate message_template = MessageTemplateFromInt(template_index);
216
217 Handle<Object> arg0;
218 if (args.length() >= 2) {
219 CHECK(args[1].IsObject());
220 arg0 = args.at<Object>(1);
221 }
222
223 Handle<Object> arg1;
224 if (args.length() >= 3) {
225 CHECK(args[2].IsObject());
226 arg1 = args.at<Object>(2);
227 }
228 Handle<Object> arg2;
229 if (args.length() >= 4) {
230 CHECK(args[3].IsObject());
231 arg2 = args.at<Object>(3);
232 }
233
234 return *isolate->factory()->NewTypeError(message_template, arg0, arg1, arg2);
235 }
236
RUNTIME_FUNCTION(Runtime_NewReferenceError)237 RUNTIME_FUNCTION(Runtime_NewReferenceError) {
238 HandleScope scope(isolate);
239 DCHECK_EQ(2, args.length());
240 CONVERT_INT32_ARG_CHECKED(template_index, 0);
241 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
242 MessageTemplate message_template = MessageTemplateFromInt(template_index);
243 return *isolate->factory()->NewReferenceError(message_template, arg0);
244 }
245
RUNTIME_FUNCTION(Runtime_NewSyntaxError)246 RUNTIME_FUNCTION(Runtime_NewSyntaxError) {
247 HandleScope scope(isolate);
248 DCHECK_EQ(2, args.length());
249 CONVERT_INT32_ARG_CHECKED(template_index, 0);
250 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
251 MessageTemplate message_template = MessageTemplateFromInt(template_index);
252 return *isolate->factory()->NewSyntaxError(message_template, arg0);
253 }
254
RUNTIME_FUNCTION(Runtime_ThrowInvalidStringLength)255 RUNTIME_FUNCTION(Runtime_ThrowInvalidStringLength) {
256 HandleScope scope(isolate);
257 THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
258 }
259
RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject)260 RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) {
261 HandleScope scope(isolate);
262 DCHECK_EQ(1, args.length());
263 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
264 THROW_NEW_ERROR_RETURN_FAILURE(
265 isolate,
266 NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
267 }
268
RUNTIME_FUNCTION(Runtime_ThrowThrowMethodMissing)269 RUNTIME_FUNCTION(Runtime_ThrowThrowMethodMissing) {
270 HandleScope scope(isolate);
271 DCHECK_EQ(0, args.length());
272 THROW_NEW_ERROR_RETURN_FAILURE(
273 isolate, NewTypeError(MessageTemplate::kThrowMethodMissing));
274 }
275
RUNTIME_FUNCTION(Runtime_ThrowSymbolIteratorInvalid)276 RUNTIME_FUNCTION(Runtime_ThrowSymbolIteratorInvalid) {
277 HandleScope scope(isolate);
278 DCHECK_EQ(0, args.length());
279 THROW_NEW_ERROR_RETURN_FAILURE(
280 isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
281 }
282
RUNTIME_FUNCTION(Runtime_ThrowNotConstructor)283 RUNTIME_FUNCTION(Runtime_ThrowNotConstructor) {
284 HandleScope scope(isolate);
285 DCHECK_EQ(1, args.length());
286 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
287 THROW_NEW_ERROR_RETURN_FAILURE(
288 isolate, NewTypeError(MessageTemplate::kNotConstructor, object));
289 }
290
RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction)291 RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) {
292 HandleScope scope(isolate);
293 DCHECK_EQ(1, args.length());
294 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
295 Handle<String> type = Object::TypeOf(isolate, object);
296 THROW_NEW_ERROR_RETURN_FAILURE(
297 isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type));
298 }
299
RUNTIME_FUNCTION(Runtime_StackGuard)300 RUNTIME_FUNCTION(Runtime_StackGuard) {
301 SealHandleScope shs(isolate);
302 DCHECK_EQ(0, args.length());
303 TRACE_EVENT0("v8.execute", "V8.StackGuard");
304
305 // First check if this is a real stack overflow.
306 StackLimitCheck check(isolate);
307 if (check.JsHasOverflowed()) {
308 return isolate->StackOverflow();
309 }
310
311 return isolate->stack_guard()->HandleInterrupts();
312 }
313
RUNTIME_FUNCTION(Runtime_StackGuardWithGap)314 RUNTIME_FUNCTION(Runtime_StackGuardWithGap) {
315 SealHandleScope shs(isolate);
316 DCHECK_EQ(args.length(), 1);
317 CONVERT_UINT32_ARG_CHECKED(gap, 0);
318 TRACE_EVENT0("v8.execute", "V8.StackGuard");
319
320 // First check if this is a real stack overflow.
321 StackLimitCheck check(isolate);
322 if (check.JsHasOverflowed(gap)) {
323 return isolate->StackOverflow();
324 }
325
326 return isolate->stack_guard()->HandleInterrupts();
327 }
328
RUNTIME_FUNCTION(Runtime_BytecodeBudgetInterruptFromBytecode)329 RUNTIME_FUNCTION(Runtime_BytecodeBudgetInterruptFromBytecode) {
330 HandleScope scope(isolate);
331 DCHECK_EQ(1, args.length());
332 CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
333 function->raw_feedback_cell().set_interrupt_budget(FLAG_interrupt_budget);
334 if (!function->has_feedback_vector()) {
335 IsCompiledScope is_compiled_scope(
336 function->shared().is_compiled_scope(isolate));
337 JSFunction::EnsureFeedbackVector(function, &is_compiled_scope);
338 // Also initialize the invocation count here. This is only really needed for
339 // OSR. When we OSR functions with lazy feedback allocation we want to have
340 // a non zero invocation count so we can inline functions.
341 function->feedback_vector().set_invocation_count(1);
342 return ReadOnlyRoots(isolate).undefined_value();
343 }
344 {
345 SealHandleScope shs(isolate);
346 isolate->counters()->runtime_profiler_ticks()->Increment();
347 isolate->runtime_profiler()->MarkCandidatesForOptimizationFromBytecode();
348 return ReadOnlyRoots(isolate).undefined_value();
349 }
350 }
351
RUNTIME_FUNCTION(Runtime_BytecodeBudgetInterruptFromCode)352 RUNTIME_FUNCTION(Runtime_BytecodeBudgetInterruptFromCode) {
353 HandleScope scope(isolate);
354 DCHECK_EQ(1, args.length());
355 CONVERT_ARG_HANDLE_CHECKED(FeedbackCell, feedback_cell, 0);
356
357 DCHECK(feedback_cell->value().IsFeedbackVector());
358
359 feedback_cell->set_interrupt_budget(FLAG_interrupt_budget);
360
361 SealHandleScope shs(isolate);
362 isolate->counters()->runtime_profiler_ticks()->Increment();
363 isolate->runtime_profiler()->MarkCandidatesForOptimizationFromCode();
364 return ReadOnlyRoots(isolate).undefined_value();
365 }
366
RUNTIME_FUNCTION(Runtime_AllocateInYoungGeneration)367 RUNTIME_FUNCTION(Runtime_AllocateInYoungGeneration) {
368 HandleScope scope(isolate);
369 DCHECK_EQ(2, args.length());
370 CONVERT_SMI_ARG_CHECKED(size, 0);
371 CONVERT_SMI_ARG_CHECKED(flags, 1);
372 bool double_align = AllocateDoubleAlignFlag::decode(flags);
373 bool allow_large_object_allocation =
374 AllowLargeObjectAllocationFlag::decode(flags);
375 CHECK(IsAligned(size, kTaggedSize));
376 CHECK_GT(size, 0);
377 CHECK(FLAG_young_generation_large_objects ||
378 size <= kMaxRegularHeapObjectSize);
379 if (!allow_large_object_allocation) {
380 CHECK(size <= kMaxRegularHeapObjectSize);
381 }
382
383 // TODO(v8:9472): Until double-aligned allocation is fixed for new-space
384 // allocations, don't request it.
385 double_align = false;
386
387 return *isolate->factory()->NewFillerObject(size, double_align,
388 AllocationType::kYoung,
389 AllocationOrigin::kGeneratedCode);
390 }
391
RUNTIME_FUNCTION(Runtime_AllocateInOldGeneration)392 RUNTIME_FUNCTION(Runtime_AllocateInOldGeneration) {
393 HandleScope scope(isolate);
394 DCHECK_EQ(2, args.length());
395 CONVERT_SMI_ARG_CHECKED(size, 0);
396 CONVERT_SMI_ARG_CHECKED(flags, 1);
397 bool double_align = AllocateDoubleAlignFlag::decode(flags);
398 bool allow_large_object_allocation =
399 AllowLargeObjectAllocationFlag::decode(flags);
400 CHECK(IsAligned(size, kTaggedSize));
401 CHECK_GT(size, 0);
402 if (!allow_large_object_allocation) {
403 CHECK(size <= kMaxRegularHeapObjectSize);
404 }
405 return *isolate->factory()->NewFillerObject(size, double_align,
406 AllocationType::kOld,
407 AllocationOrigin::kGeneratedCode);
408 }
409
RUNTIME_FUNCTION(Runtime_AllocateByteArray)410 RUNTIME_FUNCTION(Runtime_AllocateByteArray) {
411 HandleScope scope(isolate);
412 DCHECK_EQ(1, args.length());
413 CONVERT_SMI_ARG_CHECKED(length, 0);
414 DCHECK_LT(0, length);
415 return *isolate->factory()->NewByteArray(length);
416 }
417
RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString)418 RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString) {
419 HandleScope scope(isolate);
420 DCHECK_EQ(1, args.length());
421 CONVERT_SMI_ARG_CHECKED(length, 0);
422 if (length == 0) return ReadOnlyRoots(isolate).empty_string();
423 Handle<SeqOneByteString> result;
424 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
425 isolate, result, isolate->factory()->NewRawOneByteString(length));
426 return *result;
427 }
428
RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString)429 RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString) {
430 HandleScope scope(isolate);
431 DCHECK_EQ(1, args.length());
432 CONVERT_SMI_ARG_CHECKED(length, 0);
433 if (length == 0) return ReadOnlyRoots(isolate).empty_string();
434 Handle<SeqTwoByteString> result;
435 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
436 isolate, result, isolate->factory()->NewRawTwoByteString(length));
437 return *result;
438 }
439
RUNTIME_FUNCTION(Runtime_ThrowIteratorError)440 RUNTIME_FUNCTION(Runtime_ThrowIteratorError) {
441 HandleScope scope(isolate);
442 DCHECK_EQ(1, args.length());
443 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
444 return isolate->Throw(*ErrorUtils::NewIteratorError(isolate, object));
445 }
446
RUNTIME_FUNCTION(Runtime_ThrowSpreadArgError)447 RUNTIME_FUNCTION(Runtime_ThrowSpreadArgError) {
448 HandleScope scope(isolate);
449 DCHECK_EQ(2, args.length());
450 CONVERT_SMI_ARG_CHECKED(message_id_smi, 0);
451 MessageTemplate message_id = MessageTemplateFromInt(message_id_smi);
452 CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
453 return ErrorUtils::ThrowSpreadArgError(isolate, message_id, object);
454 }
455
RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable)456 RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable) {
457 HandleScope scope(isolate);
458 DCHECK_EQ(1, args.length());
459 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
460 return isolate->Throw(
461 *ErrorUtils::NewCalledNonCallableError(isolate, object));
462 }
463
RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable)464 RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable) {
465 HandleScope scope(isolate);
466 DCHECK_EQ(1, args.length());
467 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
468 return isolate->Throw(
469 *ErrorUtils::NewConstructedNonConstructable(isolate, object));
470 }
471
RUNTIME_FUNCTION(Runtime_ThrowPatternAssignmentNonCoercible)472 RUNTIME_FUNCTION(Runtime_ThrowPatternAssignmentNonCoercible) {
473 HandleScope scope(isolate);
474 DCHECK_EQ(1, args.length());
475 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
476 return ErrorUtils::ThrowLoadFromNullOrUndefined(isolate, object,
477 MaybeHandle<Object>());
478 }
479
RUNTIME_FUNCTION(Runtime_ThrowConstructorReturnedNonObject)480 RUNTIME_FUNCTION(Runtime_ThrowConstructorReturnedNonObject) {
481 HandleScope scope(isolate);
482 DCHECK_EQ(0, args.length());
483
484 THROW_NEW_ERROR_RETURN_FAILURE(
485 isolate,
486 NewTypeError(MessageTemplate::kDerivedConstructorReturnedNonObject));
487 }
488
489 // ES6 section 7.3.17 CreateListFromArrayLike (obj)
RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike)490 RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike) {
491 HandleScope scope(isolate);
492 DCHECK_EQ(1, args.length());
493 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
494 RETURN_RESULT_OR_FAILURE(isolate, Object::CreateListFromArrayLike(
495 isolate, object, ElementTypes::kAll));
496 }
497
RUNTIME_FUNCTION(Runtime_IncrementUseCounter)498 RUNTIME_FUNCTION(Runtime_IncrementUseCounter) {
499 HandleScope scope(isolate);
500 DCHECK_EQ(1, args.length());
501 CONVERT_SMI_ARG_CHECKED(counter, 0);
502 isolate->CountUsage(static_cast<v8::Isolate::UseCounterFeature>(counter));
503 return ReadOnlyRoots(isolate).undefined_value();
504 }
505
RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats)506 RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats) {
507 HandleScope scope(isolate);
508
509 // Append any worker thread runtime call stats to the main table before
510 // printing.
511 isolate->counters()->worker_thread_runtime_call_stats()->AddToMainTable(
512 isolate->counters()->runtime_call_stats());
513
514 if (args.length() == 0) {
515 // Without arguments, the result is returned as a string.
516 DCHECK_EQ(0, args.length());
517 std::stringstream stats_stream;
518 isolate->counters()->runtime_call_stats()->Print(stats_stream);
519 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(
520 stats_stream.str().c_str());
521 isolate->counters()->runtime_call_stats()->Reset();
522 return *result;
523 } else {
524 DCHECK_LE(args.length(), 2);
525 std::FILE* f;
526 if (args[0].IsString()) {
527 // With a string argument, the results are appended to that file.
528 CONVERT_ARG_HANDLE_CHECKED(String, arg0, 0);
529 DisallowHeapAllocation no_gc;
530 String::FlatContent flat = arg0->GetFlatContent(no_gc);
531 const char* filename =
532 reinterpret_cast<const char*>(&(flat.ToOneByteVector()[0]));
533 f = std::fopen(filename, "a");
534 DCHECK_NOT_NULL(f);
535 } else {
536 // With an integer argument, the results are written to stdout/stderr.
537 CONVERT_SMI_ARG_CHECKED(fd, 0);
538 DCHECK(fd == 1 || fd == 2);
539 f = fd == 1 ? stdout : stderr;
540 }
541 // The second argument (if any) is a message header to be printed.
542 if (args.length() >= 2) {
543 CONVERT_ARG_HANDLE_CHECKED(String, arg1, 1);
544 arg1->PrintOn(f);
545 std::fputc('\n', f);
546 std::fflush(f);
547 }
548 OFStream stats_stream(f);
549 isolate->counters()->runtime_call_stats()->Print(stats_stream);
550 isolate->counters()->runtime_call_stats()->Reset();
551 if (args[0].IsString())
552 std::fclose(f);
553 else
554 std::fflush(f);
555 return ReadOnlyRoots(isolate).undefined_value();
556 }
557 }
558
RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance)559 RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) {
560 HandleScope scope(isolate);
561 DCHECK_EQ(2, args.length());
562 CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0);
563 CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
564 RETURN_RESULT_OR_FAILURE(
565 isolate, Object::OrdinaryHasInstance(isolate, callable, object));
566 }
567
RUNTIME_FUNCTION(Runtime_Typeof)568 RUNTIME_FUNCTION(Runtime_Typeof) {
569 HandleScope scope(isolate);
570 DCHECK_EQ(1, args.length());
571 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
572 return *Object::TypeOf(isolate, object);
573 }
574
RUNTIME_FUNCTION(Runtime_AllowDynamicFunction)575 RUNTIME_FUNCTION(Runtime_AllowDynamicFunction) {
576 HandleScope scope(isolate);
577 DCHECK_EQ(1, args.length());
578 CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0);
579 Handle<JSObject> global_proxy(target->global_proxy(), isolate);
580 return *isolate->factory()->ToBoolean(
581 Builtins::AllowDynamicFunction(isolate, target, global_proxy));
582 }
583
RUNTIME_FUNCTION(Runtime_CreateAsyncFromSyncIterator)584 RUNTIME_FUNCTION(Runtime_CreateAsyncFromSyncIterator) {
585 HandleScope scope(isolate);
586 DCHECK_EQ(1, args.length());
587
588 CONVERT_ARG_HANDLE_CHECKED(Object, sync_iterator, 0);
589
590 if (!sync_iterator->IsJSReceiver()) {
591 THROW_NEW_ERROR_RETURN_FAILURE(
592 isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
593 }
594
595 Handle<Object> next;
596 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
597 isolate, next,
598 Object::GetProperty(isolate, sync_iterator,
599 isolate->factory()->next_string()));
600
601 return *isolate->factory()->NewJSAsyncFromSyncIterator(
602 Handle<JSReceiver>::cast(sync_iterator), next);
603 }
604
RUNTIME_FUNCTION(Runtime_GetTemplateObject)605 RUNTIME_FUNCTION(Runtime_GetTemplateObject) {
606 HandleScope scope(isolate);
607 DCHECK_EQ(3, args.length());
608 CONVERT_ARG_HANDLE_CHECKED(TemplateObjectDescription, description, 0);
609 CONVERT_ARG_HANDLE_CHECKED(SharedFunctionInfo, shared_info, 1);
610 CONVERT_SMI_ARG_CHECKED(slot_id, 2);
611
612 Handle<NativeContext> native_context(isolate->context().native_context(),
613 isolate);
614 return *TemplateObjectDescription::GetTemplateObject(
615 isolate, native_context, description, shared_info, slot_id);
616 }
617
RUNTIME_FUNCTION(Runtime_ReportMessageFromMicrotask)618 RUNTIME_FUNCTION(Runtime_ReportMessageFromMicrotask) {
619 // Helper to report messages and continue JS execution. This is intended to
620 // behave similarly to reporting exceptions which reach the top-level, but
621 // allow the JS code to continue.
622 HandleScope scope(isolate);
623 DCHECK_EQ(1, args.length());
624
625 CONVERT_ARG_HANDLE_CHECKED(Object, exception, 0);
626
627 DCHECK(!isolate->has_pending_exception());
628 isolate->set_pending_exception(*exception);
629 MessageLocation* no_location = nullptr;
630 Handle<JSMessageObject> message =
631 isolate->CreateMessageOrAbort(exception, no_location);
632 MessageHandler::ReportMessage(isolate, no_location, message);
633 isolate->clear_pending_exception();
634 return ReadOnlyRoots(isolate).undefined_value();
635 }
636
RUNTIME_FUNCTION(Runtime_GetInitializerFunction)637 RUNTIME_FUNCTION(Runtime_GetInitializerFunction) {
638 HandleScope scope(isolate);
639 DCHECK_EQ(1, args.length());
640
641 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, constructor, 0);
642 Handle<Symbol> key = isolate->factory()->class_fields_symbol();
643 Handle<Object> initializer = JSReceiver::GetDataProperty(constructor, key);
644 return *initializer;
645 }
646
RUNTIME_FUNCTION(Runtime_DoubleToStringWithRadix)647 RUNTIME_FUNCTION(Runtime_DoubleToStringWithRadix) {
648 HandleScope scope(isolate);
649 DCHECK_EQ(2, args.length());
650 CONVERT_DOUBLE_ARG_CHECKED(number, 0);
651 CONVERT_INT32_ARG_CHECKED(radix, 1);
652
653 char* const str = DoubleToRadixCString(number, radix);
654 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str);
655 DeleteArray(str);
656 return *result;
657 }
658
659 } // namespace internal
660 } // namespace v8
661