// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/runtime/runtime-utils.h" #include "src/arguments.h" #include "src/ast/prettyprinter.h" #include "src/bootstrapper.h" #include "src/conversions.h" #include "src/debug/debug.h" #include "src/frames-inl.h" #include "src/isolate-inl.h" #include "src/messages.h" #include "src/parsing/parser.h" #include "src/wasm/wasm-module.h" namespace v8 { namespace internal { RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping) { SealHandleScope shs(isolate); DCHECK(args.length() == 0); CHECK(isolate->bootstrapper()->IsActive()); return isolate->heap()->undefined_value(); } RUNTIME_FUNCTION(Runtime_ExportFromRuntime) { HandleScope scope(isolate); DCHECK(args.length() == 1); CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0); CHECK(isolate->bootstrapper()->IsActive()); JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10, "ExportFromRuntime"); Bootstrapper::ExportFromRuntime(isolate, container); JSObject::MigrateSlowToFast(container, 0, "ExportFromRuntime"); return *container; } RUNTIME_FUNCTION(Runtime_ExportExperimentalFromRuntime) { HandleScope scope(isolate); DCHECK(args.length() == 1); CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0); CHECK(isolate->bootstrapper()->IsActive()); JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10, "ExportExperimentalFromRuntime"); Bootstrapper::ExportExperimentalFromRuntime(isolate, container); JSObject::MigrateSlowToFast(container, 0, "ExportExperimentalFromRuntime"); return *container; } RUNTIME_FUNCTION(Runtime_InstallToContext) { HandleScope scope(isolate); DCHECK(args.length() == 1); CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0); CHECK(array->HasFastElements()); CHECK(isolate->bootstrapper()->IsActive()); Handle native_context = isolate->native_context(); Handle fixed_array(FixedArray::cast(array->elements())); int length = Smi::cast(array->length())->value(); for (int i = 0; i < length; i += 2) { CHECK(fixed_array->get(i)->IsString()); Handle name(String::cast(fixed_array->get(i))); CHECK(fixed_array->get(i + 1)->IsJSObject()); Handle object(JSObject::cast(fixed_array->get(i + 1))); int index = Context::ImportedFieldIndexForName(name); if (index == Context::kNotFound) { index = Context::IntrinsicIndexForName(name); } CHECK(index != Context::kNotFound); native_context->set(index, *object); } return isolate->heap()->undefined_value(); } RUNTIME_FUNCTION(Runtime_Throw) { HandleScope scope(isolate); DCHECK(args.length() == 1); return isolate->Throw(args[0]); } RUNTIME_FUNCTION(Runtime_ReThrow) { HandleScope scope(isolate); DCHECK(args.length() == 1); return isolate->ReThrow(args[0]); } RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) { SealHandleScope shs(isolate); DCHECK_LE(0, args.length()); return isolate->StackOverflow(); } RUNTIME_FUNCTION(Runtime_ThrowWasmError) { HandleScope scope(isolate); DCHECK_EQ(2, args.length()); CONVERT_SMI_ARG_CHECKED(message_id, 0); CONVERT_SMI_ARG_CHECKED(byte_offset, 1); Handle error_obj = isolate->factory()->NewError( static_cast(message_id)); // For wasm traps, the byte offset (a.k.a source position) can not be // determined from relocation info, since the explicit checks for traps // converge in one singe block which calls this runtime function. // We hence pass the byte offset explicitely, and patch it into the top-most // frame (a wasm frame) on the collected stack trace. // TODO(wasm): This implementation is temporary, see bug #5007: // https://bugs.chromium.org/p/v8/issues/detail?id=5007 Handle error = Handle::cast(error_obj); Handle stack_trace_obj = JSReceiver::GetDataProperty( error, isolate->factory()->stack_trace_symbol()); // Patch the stack trace (array of ). if (stack_trace_obj->IsJSArray()) { Handle stack_elements( FixedArray::cast(JSArray::cast(*stack_trace_obj)->elements())); DCHECK_EQ(1, stack_elements->length() % 4); DCHECK(Code::cast(stack_elements->get(3))->kind() == Code::WASM_FUNCTION); DCHECK(stack_elements->get(4)->IsSmi() && Smi::cast(stack_elements->get(4))->value() >= 0); stack_elements->set(4, Smi::FromInt(-1 - byte_offset)); } Handle detailed_stack_trace_obj = JSReceiver::GetDataProperty( error, isolate->factory()->detailed_stack_trace_symbol()); // Patch the detailed stack trace (array of JSObjects with various // properties). if (detailed_stack_trace_obj->IsJSArray()) { Handle stack_elements( FixedArray::cast(JSArray::cast(*detailed_stack_trace_obj)->elements())); DCHECK_GE(stack_elements->length(), 1); Handle top_frame(JSObject::cast(stack_elements->get(0))); Handle wasm_offset_key = isolate->factory()->InternalizeOneByteString( STATIC_CHAR_VECTOR("column")); LookupIterator it(top_frame, wasm_offset_key, top_frame, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); if (it.IsFound()) { DCHECK(JSReceiver::GetDataProperty(&it)->IsSmi()); // Make column number 1-based here. Maybe data_set = JSReceiver::SetDataProperty( &it, handle(Smi::FromInt(byte_offset + 1), isolate)); DCHECK(data_set.IsJust() && data_set.FromJust() == true); USE(data_set); } } return isolate->Throw(*error_obj); } RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) { SealHandleScope shs(isolate); DCHECK(args.length() == 0); return isolate->UnwindAndFindHandler(); } RUNTIME_FUNCTION(Runtime_PromoteScheduledException) { SealHandleScope shs(isolate); DCHECK(args.length() == 0); return isolate->PromoteScheduledException(); } RUNTIME_FUNCTION(Runtime_ThrowReferenceError) { HandleScope scope(isolate); DCHECK(args.length() == 1); CONVERT_ARG_HANDLE_CHECKED(Object, name, 0); THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewReferenceError(MessageTemplate::kNotDefined, name)); } RUNTIME_FUNCTION(Runtime_NewTypeError) { HandleScope scope(isolate); DCHECK(args.length() == 2); CONVERT_INT32_ARG_CHECKED(template_index, 0); CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1); auto message_template = static_cast(template_index); return *isolate->factory()->NewTypeError(message_template, arg0); } RUNTIME_FUNCTION(Runtime_NewReferenceError) { HandleScope scope(isolate); DCHECK(args.length() == 2); CONVERT_INT32_ARG_CHECKED(template_index, 0); CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1); auto message_template = static_cast(template_index); return *isolate->factory()->NewReferenceError(message_template, arg0); } RUNTIME_FUNCTION(Runtime_NewSyntaxError) { HandleScope scope(isolate); DCHECK(args.length() == 2); CONVERT_INT32_ARG_CHECKED(template_index, 0); CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1); auto message_template = static_cast(template_index); return *isolate->factory()->NewSyntaxError(message_template, arg0); } RUNTIME_FUNCTION(Runtime_ThrowIllegalInvocation) { HandleScope scope(isolate); DCHECK(args.length() == 0); THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewTypeError(MessageTemplate::kIllegalInvocation)); } RUNTIME_FUNCTION(Runtime_ThrowIncompatibleMethodReceiver) { HandleScope scope(isolate); DCHECK_EQ(2, args.length()); CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 0); CONVERT_ARG_HANDLE_CHECKED(Object, arg1, 1); THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, arg0, arg1)); } RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) { HandleScope scope(isolate); DCHECK(args.length() == 1); CONVERT_ARG_HANDLE_CHECKED(Object, value, 0); THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value)); } RUNTIME_FUNCTION(Runtime_ThrowGeneratorRunning) { HandleScope scope(isolate); DCHECK_EQ(0, args.length()); THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewTypeError(MessageTemplate::kGeneratorRunning)); } RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) { HandleScope scope(isolate); DCHECK_EQ(1, args.length()); CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); Handle type = Object::TypeOf(isolate, object); THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type)); } RUNTIME_FUNCTION(Runtime_PromiseRejectEvent) { DCHECK(args.length() == 3); HandleScope scope(isolate); CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2); if (debug_event) isolate->debug()->OnPromiseReject(promise, value); Handle key = isolate->factory()->promise_has_handler_symbol(); // Do not report if we actually have a handler. if (JSReceiver::GetDataProperty(promise, key)->IsUndefined(isolate)) { isolate->ReportPromiseReject(promise, value, v8::kPromiseRejectWithNoHandler); } return isolate->heap()->undefined_value(); } RUNTIME_FUNCTION(Runtime_PromiseRevokeReject) { DCHECK(args.length() == 1); HandleScope scope(isolate); CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0); Handle key = isolate->factory()->promise_has_handler_symbol(); // At this point, no revocation has been issued before CHECK(JSReceiver::GetDataProperty(promise, key)->IsUndefined(isolate)); isolate->ReportPromiseReject(promise, Handle(), v8::kPromiseHandlerAddedAfterReject); return isolate->heap()->undefined_value(); } RUNTIME_FUNCTION(Runtime_StackGuard) { SealHandleScope shs(isolate); DCHECK(args.length() == 0); // First check if this is a real stack overflow. StackLimitCheck check(isolate); if (check.JsHasOverflowed()) { return isolate->StackOverflow(); } return isolate->stack_guard()->HandleInterrupts(); } RUNTIME_FUNCTION(Runtime_Interrupt) { SealHandleScope shs(isolate); DCHECK(args.length() == 0); return isolate->stack_guard()->HandleInterrupts(); } RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) { HandleScope scope(isolate); DCHECK(args.length() == 1); CONVERT_SMI_ARG_CHECKED(size, 0); CHECK(IsAligned(size, kPointerSize)); CHECK(size > 0); CHECK(size <= Page::kMaxRegularHeapObjectSize); return *isolate->factory()->NewFillerObject(size, false, NEW_SPACE); } RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) { HandleScope scope(isolate); DCHECK(args.length() == 2); CONVERT_SMI_ARG_CHECKED(size, 0); CONVERT_SMI_ARG_CHECKED(flags, 1); CHECK(IsAligned(size, kPointerSize)); CHECK(size > 0); CHECK(size <= Page::kMaxRegularHeapObjectSize); bool double_align = AllocateDoubleAlignFlag::decode(flags); AllocationSpace space = AllocateTargetSpace::decode(flags); return *isolate->factory()->NewFillerObject(size, double_align, space); } RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString) { HandleScope scope(isolate); DCHECK_EQ(1, args.length()); CONVERT_SMI_ARG_CHECKED(length, 0); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, result, isolate->factory()->NewRawOneByteString(length)); return *result; } RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString) { HandleScope scope(isolate); DCHECK_EQ(1, args.length()); CONVERT_SMI_ARG_CHECKED(length, 0); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, result, isolate->factory()->NewRawTwoByteString(length)); return *result; } // Collect the raw data for a stack trace. Returns an array of 4 // element segments each containing a receiver, function, code and // native code offset. RUNTIME_FUNCTION(Runtime_CollectStackTrace) { HandleScope scope(isolate); DCHECK(args.length() == 2); CONVERT_ARG_HANDLE_CHECKED(JSReceiver, error_object, 0); CONVERT_ARG_HANDLE_CHECKED(Object, caller, 1); if (!isolate->bootstrapper()->IsActive()) { // Optionally capture a more detailed stack trace for the message. RETURN_FAILURE_ON_EXCEPTION( isolate, isolate->CaptureAndSetDetailedStackTrace(error_object)); // Capture a simple stack trace for the stack property. RETURN_FAILURE_ON_EXCEPTION( isolate, isolate->CaptureAndSetSimpleStackTrace(error_object, caller)); } return isolate->heap()->undefined_value(); } RUNTIME_FUNCTION(Runtime_MessageGetStartPosition) { SealHandleScope shs(isolate); DCHECK(args.length() == 1); CONVERT_ARG_CHECKED(JSMessageObject, message, 0); return Smi::FromInt(message->start_position()); } RUNTIME_FUNCTION(Runtime_MessageGetScript) { SealHandleScope shs(isolate); DCHECK(args.length() == 1); CONVERT_ARG_CHECKED(JSMessageObject, message, 0); return message->script(); } RUNTIME_FUNCTION(Runtime_FormatMessageString) { HandleScope scope(isolate); DCHECK(args.length() == 4); CONVERT_INT32_ARG_CHECKED(template_index, 0); CONVERT_ARG_HANDLE_CHECKED(String, arg0, 1); CONVERT_ARG_HANDLE_CHECKED(String, arg1, 2); CONVERT_ARG_HANDLE_CHECKED(String, arg2, 3); isolate->native_context()->IncrementErrorsThrown(); RETURN_RESULT_OR_FAILURE(isolate, MessageTemplate::FormatMessage( template_index, arg0, arg1, arg2)); } #define CALLSITE_GET(NAME, RETURN) \ RUNTIME_FUNCTION(Runtime_CallSite##NAME##RT) { \ HandleScope scope(isolate); \ DCHECK(args.length() == 1); \ CONVERT_ARG_HANDLE_CHECKED(JSObject, call_site_obj, 0); \ Handle result; \ CallSite call_site(isolate, call_site_obj); \ CHECK(call_site.IsJavaScript() || call_site.IsWasm()); \ return RETURN(call_site.NAME(), isolate); \ } static inline Object* ReturnDereferencedHandle(Handle obj, Isolate* isolate) { return *obj; } static inline Object* ReturnPositiveNumberOrNull(int value, Isolate* isolate) { if (value >= 0) return *isolate->factory()->NewNumberFromInt(value); return isolate->heap()->null_value(); } static inline Object* ReturnBoolean(bool value, Isolate* isolate) { return isolate->heap()->ToBoolean(value); } CALLSITE_GET(GetFileName, ReturnDereferencedHandle) CALLSITE_GET(GetFunctionName, ReturnDereferencedHandle) CALLSITE_GET(GetScriptNameOrSourceUrl, ReturnDereferencedHandle) CALLSITE_GET(GetMethodName, ReturnDereferencedHandle) CALLSITE_GET(GetLineNumber, ReturnPositiveNumberOrNull) CALLSITE_GET(GetColumnNumber, ReturnPositiveNumberOrNull) CALLSITE_GET(IsNative, ReturnBoolean) CALLSITE_GET(IsToplevel, ReturnBoolean) CALLSITE_GET(IsEval, ReturnBoolean) CALLSITE_GET(IsConstructor, ReturnBoolean) #undef CALLSITE_GET RUNTIME_FUNCTION(Runtime_IS_VAR) { UNREACHABLE(); // implemented as macro in the parser return NULL; } namespace { bool ComputeLocation(Isolate* isolate, MessageLocation* target) { JavaScriptFrameIterator it(isolate); if (!it.done()) { JavaScriptFrame* frame = it.frame(); JSFunction* fun = frame->function(); Object* script = fun->shared()->script(); if (script->IsScript() && !(Script::cast(script)->source()->IsUndefined(isolate))) { Handle