Skip to content

Commit

Permalink
src: use non-deprecated V8 module and script APIs
Browse files Browse the repository at this point in the history
PR-URL: #37330
Reviewed-By: Jiawen Geng <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
targos committed Feb 24, 2021
1 parent 43cc8e4 commit 001dc16
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class NodeInspectorClient : public V8InspectorClient {
Isolate* isolate = env_->isolate();
Local<Context> context = env_->context();

int script_id = message->GetScriptOrigin().ScriptID()->Value();
int script_id = message->GetScriptOrigin().ScriptId();

Local<v8::StackTrace> stack_trace = message->GetStackTrace();

Expand Down
70 changes: 41 additions & 29 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ 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;
using v8::Local;
using v8::MaybeLocal;
using v8::MicrotaskQueue;
using v8::Module;
using v8::ModuleRequest;
using v8::Number;
using v8::Object;
using v8::PrimitiveArray;
Expand Down Expand Up @@ -128,8 +131,8 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
context = contextify_context->context();
}

Local<Integer> line_offset;
Local<Integer> column_offset;
int line_offset = 0;
int column_offset = 0;

bool synthetic = args[2]->IsArray();
if (synthetic) {
Expand All @@ -139,9 +142,9 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
// new ModuleWrap(url, context, source, lineOffset, columOffset, cachedData)
CHECK(args[2]->IsString());
CHECK(args[3]->IsNumber());
line_offset = args[3].As<Integer>();
line_offset = args[3].As<Int32>()->Value();
CHECK(args[4]->IsNumber());
column_offset = args[4].As<Integer>();
column_offset = args[4].As<Int32>()->Value();
}

Local<PrimitiveArray> host_defined_options =
Expand Down Expand Up @@ -185,14 +188,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {

Local<String> source_text = args[2].As<String>();
ScriptOrigin origin(url,
line_offset, // line offset
column_offset, // column offset
True(isolate), // is cross origin
Local<Integer>(), // script id
line_offset,
column_offset,
true, // is cross origin
-1, // script id
Local<Value>(), // 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;
Expand Down Expand Up @@ -270,12 +273,15 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
Local<Context> mod_context = obj->context();
Local<Module> module = obj->module_.Get(isolate);

const int module_requests_length = module->GetModuleRequestsLength();
Local<FixedArray> module_requests = module->GetModuleRequests();
const int module_requests_length = module_requests->Length();
MaybeStackBuffer<Local<Value>, 16> promises(module_requests_length);

// call the dependency resolve callbacks
for (int i = 0; i < module_requests_length; i++) {
Local<String> specifier = module->GetModuleRequest(i);
Local<ModuleRequest> module_request =
module_requests->Get(env->context(), i).As<ModuleRequest>();
Local<String> specifier = module_request->GetSpecifier();
Utf8Value specifier_utf8(env->isolate(), specifier);
std::string specifier_std(*specifier_utf8, specifier_utf8.length());

Expand Down Expand Up @@ -311,7 +317,7 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
Local<Context> context = obj->context();
Local<Module> 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();
Expand Down Expand Up @@ -453,12 +459,16 @@ void ModuleWrap::GetStaticDependencySpecifiers(

Local<Module> module = obj->module_.Get(env->isolate());

int count = module->GetModuleRequestsLength();
Local<FixedArray> module_requests = module->GetModuleRequests();
int count = module_requests->Length();

MaybeStackBuffer<Local<Value>, 16> specifiers(count);

for (int i = 0; i < count; i++)
specifiers[i] = module->GetModuleRequest(i);
for (int i = 0; i < count; i++) {
Local<ModuleRequest> module_request =
module_requests->Get(env->context(), i).As<ModuleRequest>();
specifiers[i] = module_request->GetSpecifier();
}

args.GetReturnValue().Set(
Array::New(env->isolate(), specifiers.out(), count));
Expand All @@ -473,9 +483,11 @@ void ModuleWrap::GetError(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(module->GetException());
}

MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
Local<String> specifier,
Local<Module> referrer) {
MaybeLocal<Module> ModuleWrap::ResolveModuleCallback(
Local<Context> context,
Local<String> specifier,
Local<FixedArray> import_assertions,
Local<Module> referrer) {
Environment* env = Environment::GetCurrent(context);
if (env == nullptr) {
Isolate* isolate = context->GetIsolate();
Expand Down Expand Up @@ -524,14 +536,14 @@ static MaybeLocal<Promise> ImportModuleDynamically(
Local<Context> context,
Local<ScriptOrModule> referrer,
Local<String> 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<Promise>();
}

EscapableHandleScope handle_scope(iso);
EscapableHandleScope handle_scope(isolate);

Local<Function> import_callback =
env->host_import_module_dynamically_callback();
Expand All @@ -550,11 +562,11 @@ static MaybeLocal<Promise> ImportModuleDynamically(

Local<Value> object;

int type = options->Get(iso, HostDefinedOptions::kType)
int type = options->Get(isolate, HostDefinedOptions::kType)
.As<Number>()
->Int32Value(context)
.ToChecked();
uint32_t id = options->Get(iso, HostDefinedOptions::kID)
uint32_t id = options->Get(isolate, HostDefinedOptions::kID)
.As<Number>()
->Uint32Value(context)
.ToChecked();
Expand All @@ -580,7 +592,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
Local<Value> result;
if (import_callback->Call(
context,
Undefined(iso),
Undefined(isolate),
arraysize(import_args),
import_args).ToLocal(&result)) {
CHECK(result->IsPromise());
Expand All @@ -592,16 +604,16 @@ static MaybeLocal<Promise> ImportModuleDynamically(

void ModuleWrap::SetImportModuleDynamicallyCallback(
const FunctionCallbackInfo<Value>& 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<Function> import_callback = args[0].As<Function>();
env->set_host_import_module_dynamically_callback(import_callback);

iso->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
}

void ModuleWrap::HostInitializeImportMetaObjectCallback(
Expand Down
3 changes: 2 additions & 1 deletion src/module_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ class ModuleWrap : public BaseObject {
const v8::FunctionCallbackInfo<v8::Value>& args);
static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);

static v8::MaybeLocal<v8::Module> ResolveCallback(
static v8::MaybeLocal<v8::Module> ResolveModuleCallback(
v8::Local<v8::Context> context,
v8::Local<v8::String> specifier,
v8::Local<v8::FixedArray> import_assertions,
v8::Local<v8::Module> referrer);
static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>);

Expand Down
36 changes: 16 additions & 20 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -678,8 +677,8 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args[1]->IsString());
Local<String> filename = args[1].As<String>();

Local<Integer> line_offset;
Local<Integer> column_offset;
int line_offset = 0;
int column_offset = 0;
Local<ArrayBufferView> cached_data_buf;
bool produce_cached_data = false;
Local<Context> parsing_context = context;
Expand All @@ -689,9 +688,9 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
// cachedData, produceCachedData, parsingContext)
CHECK_EQ(argc, 7);
CHECK(args[2]->IsNumber());
line_offset = args[2].As<Integer>();
line_offset = args[2].As<Int32>()->Value();
CHECK(args[3]->IsNumber());
column_offset = args[3].As<Integer>();
column_offset = args[3].As<Int32>()->Value();
if (!args[4]->IsUndefined()) {
CHECK(args[4]->IsArrayBufferView());
cached_data_buf = args[4].As<ArrayBufferView>();
Expand All @@ -706,9 +705,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& 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 =
Expand Down Expand Up @@ -742,12 +738,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
ScriptOrigin origin(filename,
line_offset, // line offset
column_offset, // column offset
True(isolate), // is cross origin
Local<Integer>(), // script id
true, // is cross origin
-1, // script id
Local<Value>(), // 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 =
Expand Down Expand Up @@ -1037,11 +1033,11 @@ void ContextifyContext::CompileFunction(

// Argument 3: line offset
CHECK(args[2]->IsNumber());
Local<Integer> line_offset = args[2].As<Integer>();
int line_offset = args[2].As<Int32>()->Value();

// Argument 4: column offset
CHECK(args[3]->IsNumber());
Local<Integer> column_offset = args[3].As<Integer>();
int column_offset = args[3].As<Int32>()->Value();

// Argument 5: cached data (optional)
Local<ArrayBufferView> cached_data_buf;
Expand Down Expand Up @@ -1106,12 +1102,12 @@ void ContextifyContext::CompileFunction(
ScriptOrigin origin(filename,
line_offset, // line offset
column_offset, // column offset
True(isolate), // is cross origin
Local<Integer>(), // script id
true, // is cross origin
-1, // script id
Local<Value>(), // 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);
Expand Down
4 changes: 2 additions & 2 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/node_native_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -260,9 +259,7 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
std::string filename_s = std::string("node:") + id;
Local<String> filename =
OneByteString(isolate, filename_s.c_str(), filename_s.size());
Local<Integer> line_offset = Integer::New(isolate, 0);
Local<Integer> 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;
{
Expand Down

0 comments on commit 001dc16

Please sign in to comment.