From 001dc16cf173a5dc52903f5178c00335eba9fd25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 30 Jan 2021 13:30:19 +0100 Subject: [PATCH] src: use non-deprecated V8 module and script APIs PR-URL: https://github.com/nodejs/node/pull/37330 Reviewed-By: Jiawen Geng Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott --- src/inspector_agent.cc | 2 +- src/module_wrap.cc | 70 +++++++++++++++++++++++---------------- src/module_wrap.h | 3 +- src/node_contextify.cc | 36 +++++++++----------- src/node_errors.cc | 4 +-- src/node_native_module.cc | 5 +-- 6 files changed, 63 insertions(+), 57 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 9c3721f0689b0e..2226c432839e18 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -497,7 +497,7 @@ class NodeInspectorClient : public V8InspectorClient { Isolate* isolate = env_->isolate(); Local context = env_->context(); - int script_id = message->GetScriptOrigin().ScriptID()->Value(); + int script_id = message->GetScriptOrigin().ScriptId(); Local stack_trace = message->GetStackTrace(); diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 4885e65f9c5d47..f4a7ad6b0c3eee 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -26,10 +26,12 @@ using v8::Array; using v8::ArrayBufferView; using v8::Context; using v8::EscapableHandleScope; +using v8::FixedArray; using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; +using v8::Int32; using v8::Integer; using v8::IntegrityLevel; using v8::Isolate; @@ -37,6 +39,7 @@ using v8::Local; using v8::MaybeLocal; using v8::MicrotaskQueue; using v8::Module; +using v8::ModuleRequest; using v8::Number; using v8::Object; using v8::PrimitiveArray; @@ -128,8 +131,8 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { context = contextify_context->context(); } - Local line_offset; - Local column_offset; + int line_offset = 0; + int column_offset = 0; bool synthetic = args[2]->IsArray(); if (synthetic) { @@ -139,9 +142,9 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { // new ModuleWrap(url, context, source, lineOffset, columOffset, cachedData) CHECK(args[2]->IsString()); CHECK(args[3]->IsNumber()); - line_offset = args[3].As(); + line_offset = args[3].As()->Value(); CHECK(args[4]->IsNumber()); - column_offset = args[4].As(); + column_offset = args[4].As()->Value(); } Local host_defined_options = @@ -185,14 +188,14 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { Local source_text = args[2].As(); ScriptOrigin origin(url, - line_offset, // line offset - column_offset, // column offset - True(isolate), // is cross origin - Local(), // script id + line_offset, + column_offset, + true, // is cross origin + -1, // script id Local(), // source map URL - False(isolate), // is opaque (?) - False(isolate), // is WASM - True(isolate), // is ES Module + false, // is opaque (?) + false, // is WASM + true, // is ES Module host_defined_options); ScriptCompiler::Source source(source_text, origin, cached_data); ScriptCompiler::CompileOptions options; @@ -270,12 +273,15 @@ void ModuleWrap::Link(const FunctionCallbackInfo& args) { Local mod_context = obj->context(); Local module = obj->module_.Get(isolate); - const int module_requests_length = module->GetModuleRequestsLength(); + Local module_requests = module->GetModuleRequests(); + const int module_requests_length = module_requests->Length(); MaybeStackBuffer, 16> promises(module_requests_length); // call the dependency resolve callbacks for (int i = 0; i < module_requests_length; i++) { - Local specifier = module->GetModuleRequest(i); + Local module_request = + module_requests->Get(env->context(), i).As(); + Local specifier = module_request->GetSpecifier(); Utf8Value specifier_utf8(env->isolate(), specifier); std::string specifier_std(*specifier_utf8, specifier_utf8.length()); @@ -311,7 +317,7 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo& args) { Local context = obj->context(); Local module = obj->module_.Get(isolate); TryCatchScope try_catch(env); - USE(module->InstantiateModule(context, ResolveCallback)); + USE(module->InstantiateModule(context, ResolveModuleCallback)); // clear resolve cache on instantiate obj->resolve_cache_.clear(); @@ -453,12 +459,16 @@ void ModuleWrap::GetStaticDependencySpecifiers( Local module = obj->module_.Get(env->isolate()); - int count = module->GetModuleRequestsLength(); + Local module_requests = module->GetModuleRequests(); + int count = module_requests->Length(); MaybeStackBuffer, 16> specifiers(count); - for (int i = 0; i < count; i++) - specifiers[i] = module->GetModuleRequest(i); + for (int i = 0; i < count; i++) { + Local module_request = + module_requests->Get(env->context(), i).As(); + specifiers[i] = module_request->GetSpecifier(); + } args.GetReturnValue().Set( Array::New(env->isolate(), specifiers.out(), count)); @@ -473,9 +483,11 @@ void ModuleWrap::GetError(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(module->GetException()); } -MaybeLocal ModuleWrap::ResolveCallback(Local context, - Local specifier, - Local referrer) { +MaybeLocal ModuleWrap::ResolveModuleCallback( + Local context, + Local specifier, + Local import_assertions, + Local referrer) { Environment* env = Environment::GetCurrent(context); if (env == nullptr) { Isolate* isolate = context->GetIsolate(); @@ -524,14 +536,14 @@ static MaybeLocal ImportModuleDynamically( Local context, Local referrer, Local specifier) { - Isolate* iso = context->GetIsolate(); + Isolate* isolate = context->GetIsolate(); Environment* env = Environment::GetCurrent(context); if (env == nullptr) { - THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(iso); + THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(isolate); return MaybeLocal(); } - EscapableHandleScope handle_scope(iso); + EscapableHandleScope handle_scope(isolate); Local import_callback = env->host_import_module_dynamically_callback(); @@ -550,11 +562,11 @@ static MaybeLocal ImportModuleDynamically( Local object; - int type = options->Get(iso, HostDefinedOptions::kType) + int type = options->Get(isolate, HostDefinedOptions::kType) .As() ->Int32Value(context) .ToChecked(); - uint32_t id = options->Get(iso, HostDefinedOptions::kID) + uint32_t id = options->Get(isolate, HostDefinedOptions::kID) .As() ->Uint32Value(context) .ToChecked(); @@ -580,7 +592,7 @@ static MaybeLocal ImportModuleDynamically( Local result; if (import_callback->Call( context, - Undefined(iso), + Undefined(isolate), arraysize(import_args), import_args).ToLocal(&result)) { CHECK(result->IsPromise()); @@ -592,16 +604,16 @@ static MaybeLocal ImportModuleDynamically( void ModuleWrap::SetImportModuleDynamicallyCallback( const FunctionCallbackInfo& args) { - Isolate* iso = args.GetIsolate(); + Isolate* isolate = args.GetIsolate(); Environment* env = Environment::GetCurrent(args); - HandleScope handle_scope(iso); + HandleScope handle_scope(isolate); CHECK_EQ(args.Length(), 1); CHECK(args[0]->IsFunction()); Local import_callback = args[0].As(); env->set_host_import_module_dynamically_callback(import_callback); - iso->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically); + isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically); } void ModuleWrap::HostInitializeImportMetaObjectCallback( diff --git a/src/module_wrap.h b/src/module_wrap.h index dc7726f11d94fd..58b233d036515c 100644 --- a/src/module_wrap.h +++ b/src/module_wrap.h @@ -93,9 +93,10 @@ class ModuleWrap : public BaseObject { const v8::FunctionCallbackInfo& args); static void CreateCachedData(const v8::FunctionCallbackInfo& args); - static v8::MaybeLocal ResolveCallback( + static v8::MaybeLocal ResolveModuleCallback( v8::Local context, v8::Local specifier, + v8::Local import_assertions, v8::Local referrer); static ModuleWrap* GetFromModule(node::Environment*, v8::Local); diff --git a/src/node_contextify.cc b/src/node_contextify.cc index a0acdb75eede98..a18659d7e8fe8d 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -47,7 +47,6 @@ using v8::FunctionTemplate; using v8::HandleScope; using v8::IndexedPropertyHandlerConfiguration; using v8::Int32; -using v8::Integer; using v8::Isolate; using v8::Local; using v8::Maybe; @@ -678,8 +677,8 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { CHECK(args[1]->IsString()); Local filename = args[1].As(); - Local line_offset; - Local column_offset; + int line_offset = 0; + int column_offset = 0; Local cached_data_buf; bool produce_cached_data = false; Local parsing_context = context; @@ -689,9 +688,9 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { // cachedData, produceCachedData, parsingContext) CHECK_EQ(argc, 7); CHECK(args[2]->IsNumber()); - line_offset = args[2].As(); + line_offset = args[2].As()->Value(); CHECK(args[3]->IsNumber()); - column_offset = args[3].As(); + column_offset = args[3].As()->Value(); if (!args[4]->IsUndefined()) { CHECK(args[4]->IsArrayBufferView()); cached_data_buf = args[4].As(); @@ -706,9 +705,6 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { CHECK_NOT_NULL(sandbox); parsing_context = sandbox->context(); } - } else { - line_offset = Integer::New(isolate, 0); - column_offset = Integer::New(isolate, 0); } ContextifyScript* contextify_script = @@ -742,12 +738,12 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { ScriptOrigin origin(filename, line_offset, // line offset column_offset, // column offset - True(isolate), // is cross origin - Local(), // script id + true, // is cross origin + -1, // script id Local(), // source map URL - False(isolate), // is opaque (?) - False(isolate), // is WASM - False(isolate), // is ES Module + false, // is opaque (?) + false, // is WASM + false, // is ES Module host_defined_options); ScriptCompiler::Source source(code, origin, cached_data); ScriptCompiler::CompileOptions compile_options = @@ -1037,11 +1033,11 @@ void ContextifyContext::CompileFunction( // Argument 3: line offset CHECK(args[2]->IsNumber()); - Local line_offset = args[2].As(); + int line_offset = args[2].As()->Value(); // Argument 4: column offset CHECK(args[3]->IsNumber()); - Local column_offset = args[3].As(); + int column_offset = args[3].As()->Value(); // Argument 5: cached data (optional) Local cached_data_buf; @@ -1106,12 +1102,12 @@ void ContextifyContext::CompileFunction( ScriptOrigin origin(filename, line_offset, // line offset column_offset, // column offset - True(isolate), // is cross origin - Local(), // script id + true, // is cross origin + -1, // script id Local(), // source map URL - False(isolate), // is opaque (?) - False(isolate), // is WASM - False(isolate), // is ES Module + false, // is opaque (?) + false, // is WASM + false, // is ES Module host_defined_options); ScriptCompiler::Source source(code, origin, cached_data); diff --git a/src/node_errors.cc b/src/node_errors.cc index 3107f46b4dc652..ab8b513cceee5d 100644 --- a/src/node_errors.cc +++ b/src/node_errors.cc @@ -98,8 +98,8 @@ static std::string GetErrorSource(Isolate* isolate, const char* filename_string = *filename; int linenum = message->GetLineNumber(context).FromJust(); - int script_start = (linenum - origin.ResourceLineOffset()->Value()) == 1 - ? origin.ResourceColumnOffset()->Value() + int script_start = (linenum - origin.LineOffset()) == 1 + ? origin.ColumnOffset() : 0; int start = message->GetStartColumn(context).FromMaybe(0); int end = message->GetEndColumn(context).FromMaybe(0); diff --git a/src/node_native_module.cc b/src/node_native_module.cc index 5a8cf12347d5de..c201c3c491b882 100644 --- a/src/node_native_module.cc +++ b/src/node_native_module.cc @@ -7,7 +7,6 @@ namespace native_module { using v8::Context; using v8::EscapableHandleScope; using v8::Function; -using v8::Integer; using v8::Isolate; using v8::Local; using v8::MaybeLocal; @@ -260,9 +259,7 @@ MaybeLocal NativeModuleLoader::LookupAndCompile( std::string filename_s = std::string("node:") + id; Local filename = OneByteString(isolate, filename_s.c_str(), filename_s.size()); - Local line_offset = Integer::New(isolate, 0); - Local column_offset = Integer::New(isolate, 0); - ScriptOrigin origin(filename, line_offset, column_offset, True(isolate)); + ScriptOrigin origin(filename, 0, 0, true); ScriptCompiler::CachedData* cached_data = nullptr; {