Skip to content

Commit 001dc16

Browse files
committedFeb 24, 2021
src: use non-deprecated V8 module and script APIs
PR-URL: nodejs#37330 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 43cc8e4 commit 001dc16

6 files changed

+63
-57
lines changed
 

‎src/inspector_agent.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ class NodeInspectorClient : public V8InspectorClient {
497497
Isolate* isolate = env_->isolate();
498498
Local<Context> context = env_->context();
499499

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

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

‎src/module_wrap.cc

+41-29
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ using v8::Array;
2626
using v8::ArrayBufferView;
2727
using v8::Context;
2828
using v8::EscapableHandleScope;
29+
using v8::FixedArray;
2930
using v8::Function;
3031
using v8::FunctionCallbackInfo;
3132
using v8::FunctionTemplate;
3233
using v8::HandleScope;
34+
using v8::Int32;
3335
using v8::Integer;
3436
using v8::IntegrityLevel;
3537
using v8::Isolate;
3638
using v8::Local;
3739
using v8::MaybeLocal;
3840
using v8::MicrotaskQueue;
3941
using v8::Module;
42+
using v8::ModuleRequest;
4043
using v8::Number;
4144
using v8::Object;
4245
using v8::PrimitiveArray;
@@ -128,8 +131,8 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
128131
context = contextify_context->context();
129132
}
130133

131-
Local<Integer> line_offset;
132-
Local<Integer> column_offset;
134+
int line_offset = 0;
135+
int column_offset = 0;
133136

134137
bool synthetic = args[2]->IsArray();
135138
if (synthetic) {
@@ -139,9 +142,9 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
139142
// new ModuleWrap(url, context, source, lineOffset, columOffset, cachedData)
140143
CHECK(args[2]->IsString());
141144
CHECK(args[3]->IsNumber());
142-
line_offset = args[3].As<Integer>();
145+
line_offset = args[3].As<Int32>()->Value();
143146
CHECK(args[4]->IsNumber());
144-
column_offset = args[4].As<Integer>();
147+
column_offset = args[4].As<Int32>()->Value();
145148
}
146149

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

186189
Local<String> source_text = args[2].As<String>();
187190
ScriptOrigin origin(url,
188-
line_offset, // line offset
189-
column_offset, // column offset
190-
True(isolate), // is cross origin
191-
Local<Integer>(), // script id
191+
line_offset,
192+
column_offset,
193+
true, // is cross origin
194+
-1, // script id
192195
Local<Value>(), // source map URL
193-
False(isolate), // is opaque (?)
194-
False(isolate), // is WASM
195-
True(isolate), // is ES Module
196+
false, // is opaque (?)
197+
false, // is WASM
198+
true, // is ES Module
196199
host_defined_options);
197200
ScriptCompiler::Source source(source_text, origin, cached_data);
198201
ScriptCompiler::CompileOptions options;
@@ -270,12 +273,15 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
270273
Local<Context> mod_context = obj->context();
271274
Local<Module> module = obj->module_.Get(isolate);
272275

273-
const int module_requests_length = module->GetModuleRequestsLength();
276+
Local<FixedArray> module_requests = module->GetModuleRequests();
277+
const int module_requests_length = module_requests->Length();
274278
MaybeStackBuffer<Local<Value>, 16> promises(module_requests_length);
275279

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

@@ -311,7 +317,7 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
311317
Local<Context> context = obj->context();
312318
Local<Module> module = obj->module_.Get(isolate);
313319
TryCatchScope try_catch(env);
314-
USE(module->InstantiateModule(context, ResolveCallback));
320+
USE(module->InstantiateModule(context, ResolveModuleCallback));
315321

316322
// clear resolve cache on instantiate
317323
obj->resolve_cache_.clear();
@@ -453,12 +459,16 @@ void ModuleWrap::GetStaticDependencySpecifiers(
453459

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

456-
int count = module->GetModuleRequestsLength();
462+
Local<FixedArray> module_requests = module->GetModuleRequests();
463+
int count = module_requests->Length();
457464

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

460-
for (int i = 0; i < count; i++)
461-
specifiers[i] = module->GetModuleRequest(i);
467+
for (int i = 0; i < count; i++) {
468+
Local<ModuleRequest> module_request =
469+
module_requests->Get(env->context(), i).As<ModuleRequest>();
470+
specifiers[i] = module_request->GetSpecifier();
471+
}
462472

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

476-
MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
477-
Local<String> specifier,
478-
Local<Module> referrer) {
486+
MaybeLocal<Module> ModuleWrap::ResolveModuleCallback(
487+
Local<Context> context,
488+
Local<String> specifier,
489+
Local<FixedArray> import_assertions,
490+
Local<Module> referrer) {
479491
Environment* env = Environment::GetCurrent(context);
480492
if (env == nullptr) {
481493
Isolate* isolate = context->GetIsolate();
@@ -524,14 +536,14 @@ static MaybeLocal<Promise> ImportModuleDynamically(
524536
Local<Context> context,
525537
Local<ScriptOrModule> referrer,
526538
Local<String> specifier) {
527-
Isolate* iso = context->GetIsolate();
539+
Isolate* isolate = context->GetIsolate();
528540
Environment* env = Environment::GetCurrent(context);
529541
if (env == nullptr) {
530-
THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(iso);
542+
THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(isolate);
531543
return MaybeLocal<Promise>();
532544
}
533545

534-
EscapableHandleScope handle_scope(iso);
546+
EscapableHandleScope handle_scope(isolate);
535547

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

551563
Local<Value> object;
552564

553-
int type = options->Get(iso, HostDefinedOptions::kType)
565+
int type = options->Get(isolate, HostDefinedOptions::kType)
554566
.As<Number>()
555567
->Int32Value(context)
556568
.ToChecked();
557-
uint32_t id = options->Get(iso, HostDefinedOptions::kID)
569+
uint32_t id = options->Get(isolate, HostDefinedOptions::kID)
558570
.As<Number>()
559571
->Uint32Value(context)
560572
.ToChecked();
@@ -580,7 +592,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
580592
Local<Value> result;
581593
if (import_callback->Call(
582594
context,
583-
Undefined(iso),
595+
Undefined(isolate),
584596
arraysize(import_args),
585597
import_args).ToLocal(&result)) {
586598
CHECK(result->IsPromise());
@@ -592,16 +604,16 @@ static MaybeLocal<Promise> ImportModuleDynamically(
592604

593605
void ModuleWrap::SetImportModuleDynamicallyCallback(
594606
const FunctionCallbackInfo<Value>& args) {
595-
Isolate* iso = args.GetIsolate();
607+
Isolate* isolate = args.GetIsolate();
596608
Environment* env = Environment::GetCurrent(args);
597-
HandleScope handle_scope(iso);
609+
HandleScope handle_scope(isolate);
598610

599611
CHECK_EQ(args.Length(), 1);
600612
CHECK(args[0]->IsFunction());
601613
Local<Function> import_callback = args[0].As<Function>();
602614
env->set_host_import_module_dynamically_callback(import_callback);
603615

604-
iso->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
616+
isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
605617
}
606618

607619
void ModuleWrap::HostInitializeImportMetaObjectCallback(

‎src/module_wrap.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ class ModuleWrap : public BaseObject {
9393
const v8::FunctionCallbackInfo<v8::Value>& args);
9494
static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
9595

96-
static v8::MaybeLocal<v8::Module> ResolveCallback(
96+
static v8::MaybeLocal<v8::Module> ResolveModuleCallback(
9797
v8::Local<v8::Context> context,
9898
v8::Local<v8::String> specifier,
99+
v8::Local<v8::FixedArray> import_assertions,
99100
v8::Local<v8::Module> referrer);
100101
static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>);
101102

‎src/node_contextify.cc

+16-20
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ using v8::FunctionTemplate;
4747
using v8::HandleScope;
4848
using v8::IndexedPropertyHandlerConfiguration;
4949
using v8::Int32;
50-
using v8::Integer;
5150
using v8::Isolate;
5251
using v8::Local;
5352
using v8::Maybe;
@@ -678,8 +677,8 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
678677
CHECK(args[1]->IsString());
679678
Local<String> filename = args[1].As<String>();
680679

681-
Local<Integer> line_offset;
682-
Local<Integer> column_offset;
680+
int line_offset = 0;
681+
int column_offset = 0;
683682
Local<ArrayBufferView> cached_data_buf;
684683
bool produce_cached_data = false;
685684
Local<Context> parsing_context = context;
@@ -689,9 +688,9 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
689688
// cachedData, produceCachedData, parsingContext)
690689
CHECK_EQ(argc, 7);
691690
CHECK(args[2]->IsNumber());
692-
line_offset = args[2].As<Integer>();
691+
line_offset = args[2].As<Int32>()->Value();
693692
CHECK(args[3]->IsNumber());
694-
column_offset = args[3].As<Integer>();
693+
column_offset = args[3].As<Int32>()->Value();
695694
if (!args[4]->IsUndefined()) {
696695
CHECK(args[4]->IsArrayBufferView());
697696
cached_data_buf = args[4].As<ArrayBufferView>();
@@ -706,9 +705,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
706705
CHECK_NOT_NULL(sandbox);
707706
parsing_context = sandbox->context();
708707
}
709-
} else {
710-
line_offset = Integer::New(isolate, 0);
711-
column_offset = Integer::New(isolate, 0);
712708
}
713709

714710
ContextifyScript* contextify_script =
@@ -742,12 +738,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
742738
ScriptOrigin origin(filename,
743739
line_offset, // line offset
744740
column_offset, // column offset
745-
True(isolate), // is cross origin
746-
Local<Integer>(), // script id
741+
true, // is cross origin
742+
-1, // script id
747743
Local<Value>(), // source map URL
748-
False(isolate), // is opaque (?)
749-
False(isolate), // is WASM
750-
False(isolate), // is ES Module
744+
false, // is opaque (?)
745+
false, // is WASM
746+
false, // is ES Module
751747
host_defined_options);
752748
ScriptCompiler::Source source(code, origin, cached_data);
753749
ScriptCompiler::CompileOptions compile_options =
@@ -1037,11 +1033,11 @@ void ContextifyContext::CompileFunction(
10371033

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

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

10461042
// Argument 5: cached data (optional)
10471043
Local<ArrayBufferView> cached_data_buf;
@@ -1106,12 +1102,12 @@ void ContextifyContext::CompileFunction(
11061102
ScriptOrigin origin(filename,
11071103
line_offset, // line offset
11081104
column_offset, // column offset
1109-
True(isolate), // is cross origin
1110-
Local<Integer>(), // script id
1105+
true, // is cross origin
1106+
-1, // script id
11111107
Local<Value>(), // source map URL
1112-
False(isolate), // is opaque (?)
1113-
False(isolate), // is WASM
1114-
False(isolate), // is ES Module
1108+
false, // is opaque (?)
1109+
false, // is WASM
1110+
false, // is ES Module
11151111
host_defined_options);
11161112

11171113
ScriptCompiler::Source source(code, origin, cached_data);

‎src/node_errors.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ static std::string GetErrorSource(Isolate* isolate,
9898
const char* filename_string = *filename;
9999
int linenum = message->GetLineNumber(context).FromJust();
100100

101-
int script_start = (linenum - origin.ResourceLineOffset()->Value()) == 1
102-
? origin.ResourceColumnOffset()->Value()
101+
int script_start = (linenum - origin.LineOffset()) == 1
102+
? origin.ColumnOffset()
103103
: 0;
104104
int start = message->GetStartColumn(context).FromMaybe(0);
105105
int end = message->GetEndColumn(context).FromMaybe(0);

‎src/node_native_module.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace native_module {
77
using v8::Context;
88
using v8::EscapableHandleScope;
99
using v8::Function;
10-
using v8::Integer;
1110
using v8::Isolate;
1211
using v8::Local;
1312
using v8::MaybeLocal;
@@ -260,9 +259,7 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
260259
std::string filename_s = std::string("node:") + id;
261260
Local<String> filename =
262261
OneByteString(isolate, filename_s.c_str(), filename_s.size());
263-
Local<Integer> line_offset = Integer::New(isolate, 0);
264-
Local<Integer> column_offset = Integer::New(isolate, 0);
265-
ScriptOrigin origin(filename, line_offset, column_offset, True(isolate));
262+
ScriptOrigin origin(filename, 0, 0, true);
266263

267264
ScriptCompiler::CachedData* cached_data = nullptr;
268265
{

0 commit comments

Comments
 (0)
Please sign in to comment.