8 #include "src/arguments-inl.h" 9 #include "src/ast/prettyprinter.h" 10 #include "src/bootstrapper.h" 11 #include "src/builtins/builtins.h" 12 #include "src/conversions.h" 13 #include "src/counters.h" 14 #include "src/debug/debug.h" 15 #include "src/frames-inl.h" 16 #include "src/isolate-inl.h" 17 #include "src/message-template.h" 18 #include "src/objects/js-array-inl.h" 19 #include "src/parsing/parse-info.h" 20 #include "src/parsing/parsing.h" 21 #include "src/runtime/runtime-utils.h" 22 #include "src/snapshot/snapshot.h" 23 #include "src/string-builder-inl.h" 28 RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping) {
29 SealHandleScope shs(isolate);
30 DCHECK_EQ(0, args.length());
31 CHECK(isolate->bootstrapper()->IsActive());
32 return ReadOnlyRoots(isolate).undefined_value();
35 RUNTIME_FUNCTION(Runtime_ExportFromRuntime) {
36 HandleScope scope(isolate);
37 DCHECK_EQ(1, args.length());
38 CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0);
39 CHECK(isolate->bootstrapper()->IsActive());
40 JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10,
42 Bootstrapper::ExportFromRuntime(isolate, container);
43 JSObject::MigrateSlowToFast(container, 0,
"ExportFromRuntime");
47 RUNTIME_FUNCTION(Runtime_InstallToContext) {
48 HandleScope scope(isolate);
49 DCHECK_EQ(1, args.length());
50 CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
51 CHECK(array->HasFastElements());
52 CHECK(isolate->bootstrapper()->IsActive());
53 Handle<Context> native_context = isolate->native_context();
54 Handle<FixedArray> fixed_array(FixedArray::cast(array->elements()), isolate);
55 int length = Smi::ToInt(array->length());
56 for (
int i = 0;
i < length;
i += 2) {
57 CHECK(fixed_array->get(
i)->IsString());
58 Handle<String> name(String::cast(fixed_array->get(
i)), isolate);
59 CHECK(fixed_array->get(
i + 1)->IsJSObject());
60 Handle<JSObject> object(JSObject::cast(fixed_array->get(
i + 1)), isolate);
61 int index = Context::ImportedFieldIndexForName(name);
62 if (index == Context::kNotFound) {
63 index = Context::IntrinsicIndexForName(name);
65 CHECK_NE(index, Context::kNotFound);
66 native_context->set(index, *
object);
68 return ReadOnlyRoots(isolate).undefined_value();
71 RUNTIME_FUNCTION(Runtime_Throw) {
72 HandleScope scope(isolate);
73 DCHECK_EQ(1, args.length());
74 return isolate->Throw(args[0]);
77 RUNTIME_FUNCTION(Runtime_ReThrow) {
78 HandleScope scope(isolate);
79 DCHECK_EQ(1, args.length());
80 return isolate->ReThrow(args[0]);
83 RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
84 SealHandleScope shs(isolate);
85 DCHECK_LE(0, args.length());
86 return isolate->StackOverflow();
89 RUNTIME_FUNCTION(Runtime_ThrowSymbolAsyncIteratorInvalid) {
90 HandleScope scope(isolate);
91 DCHECK_EQ(0, args.length());
92 THROW_NEW_ERROR_RETURN_FAILURE(
93 isolate, NewTypeError(MessageTemplate::kSymbolAsyncIteratorInvalid));
96 #define THROW_ERROR(isolate, args, call) \ 97 HandleScope scope(isolate); \ 98 DCHECK_LE(1, args.length()); \ 99 CONVERT_SMI_ARG_CHECKED(message_id_smi, 0); \ 101 Handle<Object> undefined = isolate->factory()->undefined_value(); \ 102 Handle<Object> arg0 = (args.length() > 1) ? args.at(1) : undefined; \ 103 Handle<Object> arg1 = (args.length() > 2) ? args.at(2) : undefined; \ 104 Handle<Object> arg2 = (args.length() > 3) ? args.at(3) : undefined; \ 106 MessageTemplate message_id = MessageTemplateFromInt(message_id_smi); \ 108 THROW_NEW_ERROR_RETURN_FAILURE(isolate, call(message_id, arg0, arg1, arg2)); 110 RUNTIME_FUNCTION(Runtime_ThrowRangeError) {
111 THROW_ERROR(isolate, args, NewRangeError);
114 RUNTIME_FUNCTION(Runtime_ThrowTypeError) {
115 THROW_ERROR(isolate, args, NewTypeError);
122 const char* ElementsKindToType(ElementsKind fixed_elements_kind) {
123 switch (fixed_elements_kind) {
124 #define ELEMENTS_KIND_CASE(Type, type, TYPE, ctype) \ 125 case TYPE##_ELEMENTS: \ 126 return #Type "Array"; 128 TYPED_ARRAYS(ELEMENTS_KIND_CASE)
129 #undef ELEMENTS_KIND_CASE 138 RUNTIME_FUNCTION(Runtime_ThrowInvalidTypedArrayAlignment) {
139 HandleScope scope(isolate);
140 DCHECK_EQ(2, args.length());
141 CONVERT_ARG_HANDLE_CHECKED(Map, map, 0);
142 CONVERT_ARG_HANDLE_CHECKED(String, problem_string, 1);
144 ElementsKind kind = map->elements_kind();
146 Handle<String> type =
147 isolate->factory()->NewStringFromAsciiChecked(ElementsKindToType(kind));
149 ExternalArrayType external_type;
151 Factory::TypeAndSizeForElementsKind(kind, &external_type, &size);
152 Handle<Object> element_size =
153 handle(Smi::FromInt(static_cast<int>(size)), isolate);
155 THROW_NEW_ERROR_RETURN_FAILURE(
156 isolate, NewRangeError(MessageTemplate::kInvalidTypedArrayAlignment,
157 problem_string, type, element_size));
160 RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
161 SealHandleScope shs(isolate);
162 DCHECK_EQ(0, args.length());
163 return isolate->UnwindAndFindHandler();
166 RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
167 SealHandleScope shs(isolate);
168 DCHECK_EQ(0, args.length());
169 return isolate->PromoteScheduledException();
172 RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
173 HandleScope scope(isolate);
174 DCHECK_EQ(1, args.length());
175 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
176 THROW_NEW_ERROR_RETURN_FAILURE(
177 isolate, NewReferenceError(MessageTemplate::kNotDefined, name));
180 RUNTIME_FUNCTION(Runtime_NewTypeError) {
181 HandleScope scope(isolate);
182 DCHECK_EQ(2, args.length());
183 CONVERT_INT32_ARG_CHECKED(template_index, 0);
184 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
185 MessageTemplate message_template = MessageTemplateFromInt(template_index);
186 return *isolate->factory()->NewTypeError(message_template, arg0);
189 RUNTIME_FUNCTION(Runtime_NewReferenceError) {
190 HandleScope scope(isolate);
191 DCHECK_EQ(2, args.length());
192 CONVERT_INT32_ARG_CHECKED(template_index, 0);
193 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
194 MessageTemplate message_template = MessageTemplateFromInt(template_index);
195 return *isolate->factory()->NewReferenceError(message_template, arg0);
198 RUNTIME_FUNCTION(Runtime_NewSyntaxError) {
199 HandleScope scope(isolate);
200 DCHECK_EQ(2, args.length());
201 CONVERT_INT32_ARG_CHECKED(template_index, 0);
202 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
203 MessageTemplate message_template = MessageTemplateFromInt(template_index);
204 return *isolate->factory()->NewSyntaxError(message_template, arg0);
207 RUNTIME_FUNCTION(Runtime_ThrowInvalidStringLength) {
208 HandleScope scope(isolate);
209 THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
212 RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) {
213 HandleScope scope(isolate);
214 DCHECK_EQ(1, args.length());
215 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
216 THROW_NEW_ERROR_RETURN_FAILURE(
218 NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
221 RUNTIME_FUNCTION(Runtime_ThrowThrowMethodMissing) {
222 HandleScope scope(isolate);
223 DCHECK_EQ(0, args.length());
224 THROW_NEW_ERROR_RETURN_FAILURE(
225 isolate, NewTypeError(MessageTemplate::kThrowMethodMissing));
228 RUNTIME_FUNCTION(Runtime_ThrowSymbolIteratorInvalid) {
229 HandleScope scope(isolate);
230 DCHECK_EQ(0, args.length());
231 THROW_NEW_ERROR_RETURN_FAILURE(
232 isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
235 RUNTIME_FUNCTION(Runtime_ThrowNotConstructor) {
236 HandleScope scope(isolate);
237 DCHECK_EQ(1, args.length());
238 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 0);
239 THROW_NEW_ERROR_RETURN_FAILURE(
240 isolate, NewTypeError(MessageTemplate::kNotConstructor,
object));
243 RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) {
244 HandleScope scope(isolate);
245 DCHECK_EQ(1, args.length());
246 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 0);
247 Handle<String> type = Object::TypeOf(isolate,
object);
248 THROW_NEW_ERROR_RETURN_FAILURE(
249 isolate, NewTypeError(MessageTemplate::kApplyNonFunction,
object, type));
252 RUNTIME_FUNCTION(Runtime_StackGuard) {
253 SealHandleScope shs(isolate);
254 DCHECK_EQ(0, args.length());
257 StackLimitCheck check(isolate);
258 if (check.JsHasOverflowed()) {
259 return isolate->StackOverflow();
262 return isolate->stack_guard()->HandleInterrupts();
265 RUNTIME_FUNCTION(Runtime_Interrupt) {
266 SealHandleScope shs(isolate);
267 DCHECK_EQ(0, args.length());
268 return isolate->stack_guard()->HandleInterrupts();
271 RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) {
272 HandleScope scope(isolate);
273 DCHECK_EQ(1, args.length());
274 CONVERT_SMI_ARG_CHECKED(size, 0);
275 CHECK(IsAligned(size, kPointerSize));
277 CHECK_LE(size, kMaxRegularHeapObjectSize);
278 return *isolate->factory()->NewFillerObject(size,
false, NEW_SPACE);
281 RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) {
282 HandleScope scope(isolate);
283 DCHECK_EQ(2, args.length());
284 CONVERT_SMI_ARG_CHECKED(size, 0);
285 CONVERT_SMI_ARG_CHECKED(flags, 1);
286 CHECK(IsAligned(size, kPointerSize));
288 bool double_align = AllocateDoubleAlignFlag::decode(flags);
289 AllocationSpace space = AllocateTargetSpace::decode(flags);
290 CHECK(size <= kMaxRegularHeapObjectSize || space == LO_SPACE);
291 return *isolate->factory()->NewFillerObject(size, double_align, space);
294 RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString) {
295 HandleScope scope(isolate);
296 DCHECK_EQ(1, args.length());
297 CONVERT_SMI_ARG_CHECKED(length, 0);
298 if (length == 0)
return ReadOnlyRoots(isolate).empty_string();
299 Handle<SeqOneByteString> result;
300 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
301 isolate, result, isolate->factory()->NewRawOneByteString(length));
305 RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString) {
306 HandleScope scope(isolate);
307 DCHECK_EQ(1, args.length());
308 CONVERT_SMI_ARG_CHECKED(length, 0);
309 if (length == 0)
return ReadOnlyRoots(isolate).empty_string();
310 Handle<SeqTwoByteString> result;
311 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
312 isolate, result, isolate->factory()->NewRawTwoByteString(length));
318 bool ComputeLocation(Isolate* isolate, MessageLocation* target) {
319 JavaScriptFrameIterator it(isolate);
324 std::vector<FrameSummary> frames;
325 it.frame()->Summarize(&frames);
326 auto& summary = frames.back().AsJavaScript();
327 Handle<SharedFunctionInfo> shared(summary.function()->shared(), isolate);
328 Handle<Object> script(shared->script(), isolate);
329 int pos = summary.abstract_code()->SourcePosition(summary.code_offset());
330 if (script->IsScript() &&
331 !(Handle<Script>::cast(script)->source()->IsUndefined(isolate))) {
332 Handle<Script> casted_script = Handle<Script>::cast(script);
333 *target = MessageLocation(casted_script, pos, pos + 1, shared);
340 Handle<String> BuildDefaultCallSite(Isolate* isolate, Handle<Object>
object) {
341 IncrementalStringBuilder builder(isolate);
343 builder.AppendString(Object::TypeOf(isolate,
object));
344 if (object->IsString()) {
345 builder.AppendCString(
" \"");
346 builder.AppendString(Handle<String>::cast(
object));
347 builder.AppendCString(
"\"");
348 }
else if (object->IsNull(isolate)) {
349 builder.AppendCString(
" ");
350 builder.AppendString(isolate->factory()->null_string());
351 }
else if (object->IsTrue(isolate)) {
352 builder.AppendCString(
" ");
353 builder.AppendString(isolate->factory()->true_string());
354 }
else if (object->IsFalse(isolate)) {
355 builder.AppendCString(
" ");
356 builder.AppendString(isolate->factory()->false_string());
357 }
else if (object->IsNumber()) {
358 builder.AppendCString(
" ");
359 builder.AppendString(isolate->factory()->NumberToString(
object));
362 return builder.Finish().ToHandleChecked();
365 Handle<String> RenderCallSite(Isolate* isolate, Handle<Object>
object,
366 CallPrinter::ErrorHint* hint) {
367 MessageLocation location;
368 if (ComputeLocation(isolate, &location)) {
369 ParseInfo info(isolate, location.shared());
370 if (parsing::ParseAny(&info, location.shared(), isolate)) {
371 info.ast_value_factory()->Internalize(isolate);
372 CallPrinter printer(isolate, location.shared()->IsUserJavaScript());
373 Handle<String> str = printer.Print(info.literal(), location.start_pos());
374 *hint = printer.GetErrorHint();
375 if (str->length() > 0)
return str;
377 isolate->clear_pending_exception();
380 return BuildDefaultCallSite(isolate,
object);
383 MessageTemplate UpdateErrorTemplate(CallPrinter::ErrorHint hint,
384 MessageTemplate default_id) {
386 case CallPrinter::ErrorHint::kNormalIterator:
387 return MessageTemplate::kNotIterable;
389 case CallPrinter::ErrorHint::kCallAndNormalIterator:
390 return MessageTemplate::kNotCallableOrIterable;
392 case CallPrinter::ErrorHint::kAsyncIterator:
393 return MessageTemplate::kNotAsyncIterable;
395 case CallPrinter::ErrorHint::kCallAndAsyncIterator:
396 return MessageTemplate::kNotCallableOrAsyncIterable;
398 case CallPrinter::ErrorHint::kNone:
406 MaybeHandle<Object> Runtime::ThrowIteratorError(Isolate* isolate,
407 Handle<Object>
object) {
408 CallPrinter::ErrorHint hint = CallPrinter::kNone;
409 Handle<String> callsite = RenderCallSite(isolate,
object, &hint);
410 MessageTemplate
id = MessageTemplate::kNotIterableNoSymbolLoad;
412 if (hint == CallPrinter::kNone) {
413 Handle<Symbol> iterator_symbol = isolate->factory()->iterator_symbol();
414 THROW_NEW_ERROR(isolate, NewTypeError(
id, callsite, iterator_symbol),
418 id = UpdateErrorTemplate(hint,
id);
419 THROW_NEW_ERROR(isolate, NewTypeError(
id, callsite), Object);
422 RUNTIME_FUNCTION(Runtime_ThrowIteratorError) {
423 HandleScope scope(isolate);
424 DCHECK_EQ(1, args.length());
425 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 0);
426 RETURN_RESULT_OR_FAILURE(isolate,
427 Runtime::ThrowIteratorError(isolate,
object));
430 RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable) {
431 HandleScope scope(isolate);
432 DCHECK_EQ(1, args.length());
433 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 0);
434 CallPrinter::ErrorHint hint = CallPrinter::kNone;
435 Handle<String> callsite = RenderCallSite(isolate,
object, &hint);
436 MessageTemplate
id = MessageTemplate::kCalledNonCallable;
437 id = UpdateErrorTemplate(hint,
id);
438 THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(
id, callsite));
441 RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable) {
442 HandleScope scope(isolate);
443 DCHECK_EQ(1, args.length());
444 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 0);
445 CallPrinter::ErrorHint hint = CallPrinter::kNone;
446 Handle<String> callsite = RenderCallSite(isolate,
object, &hint);
447 MessageTemplate
id = MessageTemplate::kNotConstructor;
448 THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(
id, callsite));
451 RUNTIME_FUNCTION(Runtime_ThrowConstructorReturnedNonObject) {
452 HandleScope scope(isolate);
453 DCHECK_EQ(0, args.length());
455 THROW_NEW_ERROR_RETURN_FAILURE(
457 NewTypeError(MessageTemplate::kDerivedConstructorReturnedNonObject));
461 RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike) {
462 HandleScope scope(isolate);
463 DCHECK_EQ(1, args.length());
464 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 0);
465 RETURN_RESULT_OR_FAILURE(isolate, Object::CreateListFromArrayLike(
466 isolate,
object, ElementTypes::kAll));
469 RUNTIME_FUNCTION(Runtime_IncrementUseCounter) {
470 HandleScope scope(isolate);
471 DCHECK_EQ(1, args.length());
472 CONVERT_SMI_ARG_CHECKED(counter, 0);
473 isolate->CountUsage(static_cast<v8::Isolate::UseCounterFeature>(counter));
474 return ReadOnlyRoots(isolate).undefined_value();
477 RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats) {
478 HandleScope scope(isolate);
482 isolate->counters()->worker_thread_runtime_call_stats()->AddToMainTable(
483 isolate->counters()->runtime_call_stats());
485 if (args.length() == 0) {
487 DCHECK_EQ(0, args.length());
488 std::stringstream stats_stream;
489 isolate->counters()->runtime_call_stats()->Print(stats_stream);
490 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(
491 stats_stream.str().c_str());
492 isolate->counters()->runtime_call_stats()->Reset();
495 DCHECK_LE(args.length(), 2);
497 if (args[0]->IsString()) {
499 CONVERT_ARG_HANDLE_CHECKED(String, arg0, 0);
500 String::FlatContent flat = arg0->GetFlatContent();
501 const char* filename =
502 reinterpret_cast<const char*
>(&(flat.ToOneByteVector()[0]));
503 f = std::fopen(filename,
"a");
507 CONVERT_SMI_ARG_CHECKED(fd, 0);
508 DCHECK(fd == 1 || fd == 2);
509 f = fd == 1 ? stdout : stderr;
512 if (args.length() >= 2) {
513 CONVERT_ARG_HANDLE_CHECKED(String, arg1, 1);
518 OFStream stats_stream(f);
519 isolate->counters()->runtime_call_stats()->Print(stats_stream);
520 isolate->counters()->runtime_call_stats()->Reset();
521 if (args[0]->IsString())
525 return ReadOnlyRoots(isolate).undefined_value();
529 RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) {
530 HandleScope scope(isolate);
531 DCHECK_EQ(2, args.length());
532 CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0);
533 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 1);
534 RETURN_RESULT_OR_FAILURE(
535 isolate, Object::OrdinaryHasInstance(isolate, callable,
object));
538 RUNTIME_FUNCTION(Runtime_Typeof) {
539 HandleScope scope(isolate);
540 DCHECK_EQ(1, args.length());
541 CONVERT_ARG_HANDLE_CHECKED(Object,
object, 0);
542 return *Object::TypeOf(isolate,
object);
545 RUNTIME_FUNCTION(Runtime_AllowDynamicFunction) {
546 HandleScope scope(isolate);
547 DCHECK_EQ(1, args.length());
548 CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0);
549 Handle<JSObject> global_proxy(target->global_proxy(), isolate);
550 return *isolate->factory()->ToBoolean(
551 Builtins::AllowDynamicFunction(isolate, target, global_proxy));
554 RUNTIME_FUNCTION(Runtime_CreateAsyncFromSyncIterator) {
555 HandleScope scope(isolate);
556 DCHECK_EQ(1, args.length());
558 CONVERT_ARG_HANDLE_CHECKED(Object, sync_iterator, 0);
560 if (!sync_iterator->IsJSReceiver()) {
561 THROW_NEW_ERROR_RETURN_FAILURE(
562 isolate, NewTypeError(MessageTemplate::kSymbolIteratorInvalid));
566 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
568 Object::GetProperty(isolate, sync_iterator,
569 isolate->factory()->next_string()));
571 return *isolate->factory()->NewJSAsyncFromSyncIterator(
572 Handle<JSReceiver>::cast(sync_iterator), next);
575 RUNTIME_FUNCTION(Runtime_CreateTemplateObject) {
576 HandleScope scope(isolate);
577 DCHECK_EQ(1, args.length());
578 CONVERT_ARG_HANDLE_CHECKED(TemplateObjectDescription, description, 0);
580 return *TemplateObjectDescription::CreateTemplateObject(isolate, description);
583 RUNTIME_FUNCTION(Runtime_ReportMessage) {
588 HandleScope scope(isolate);
589 DCHECK_EQ(1, args.length());
591 CONVERT_ARG_HANDLE_CHECKED(Object, message_obj, 0);
593 DCHECK(!isolate->has_pending_exception());
594 isolate->set_pending_exception(*message_obj);
595 isolate->ReportPendingMessagesFromJavaScript();
596 isolate->clear_pending_exception();
597 return ReadOnlyRoots(isolate).undefined_value();
600 RUNTIME_FUNCTION(Runtime_GetInitializerFunction) {
601 HandleScope scope(isolate);
602 DCHECK_EQ(1, args.length());
604 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, constructor, 0);
605 Handle<Symbol> key = isolate->factory()->class_fields_symbol();
606 Handle<Object> initializer = JSReceiver::GetDataProperty(constructor, key);