Skip to content

Commit ec51688

Browse files
bmeckgibfahn
authored andcommitted
src: use V8 function to get Module Namespace
PR-URL: #16261 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 415fb56 commit ec51688

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

‎lib/internal/loader/Loader.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
const{ getURLFromFilePath }=require('internal/url');
44

5-
const{
6-
getNamespaceOfModuleWrap,
7-
createDynamicModule
8-
}=require('internal/loader/ModuleWrap');
5+
const{ createDynamicModule }=require('internal/loader/ModuleWrap');
96

107
constModuleMap=require('internal/loader/ModuleMap');
118
constModuleJob=require('internal/loader/ModuleJob');
@@ -100,7 +97,7 @@ class Loader{
10097
asyncimport(specifier,parentURL=this.base){
10198
constjob=awaitthis.getModuleJob(specifier,parentURL);
10299
constmodule=awaitjob.run();
103-
returngetNamespaceOfModuleWrap(module);
100+
returnmodule.namespace();
104101
}
105102
}
106103
Object.setPrototypeOf(Loader.prototype,null);

‎lib/internal/loader/ModuleWrap.js‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ const debug = require('util').debuglog('esm');
66
constArrayJoin=Function.call.bind(Array.prototype.join);
77
constArrayMap=Function.call.bind(Array.prototype.map);
88

9-
constgetNamespaceOfModuleWrap=(m)=>{
10-
consttmp=newModuleWrap('import * as _ from ""_;','');
11-
tmp.link(async()=>m);
12-
tmp.instantiate();
13-
returntmp.evaluate();
14-
};
15-
169
constcreateDynamicModule=(exports,url='',evaluate)=>{
1710
debug(
1811
`creating ESM facade for ${url} with exports: ${ArrayJoin(exports,', ')}`
@@ -57,6 +50,5 @@ const createDynamicModule = (exports, url = '', evaluate) =>{
5750

5851
module.exports={
5952
createDynamicModule,
60-
getNamespaceOfModuleWrap,
6153
ModuleWrap
6254
};

‎src/module_wrap.cc‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,29 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args){
207207
args.GetReturnValue().Set(ret);
208208
}
209209

210+
voidModuleWrap::Namespace(const FunctionCallbackInfo<Value>& args){
211+
Environment* env = Environment::GetCurrent(args);
212+
auto isolate = args.GetIsolate();
213+
auto that = args.This();
214+
ModuleWrap* obj = Unwrap<ModuleWrap>(that);
215+
CHECK_NE(obj, nullptr);
216+
217+
automodule = obj->module_.Get(isolate);
218+
219+
switch (module->GetStatus()){
220+
default:
221+
return env->ThrowError(
222+
"cannot get namespace, Module has not been instantiated");
223+
case v8::Module::Status::kInstantiated:
224+
case v8::Module::Status::kEvaluating:
225+
case v8::Module::Status::kEvaluated:
226+
break;
227+
}
228+
229+
auto result = module->GetModuleNamespace();
230+
args.GetReturnValue().Set(result);
231+
}
232+
210233
MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
211234
Local<String> specifier,
212235
Local<Module> referrer){
@@ -520,6 +543,7 @@ void ModuleWrap::Initialize(Local<Object> target,
520543
env->SetProtoMethod(tpl, "link", Link);
521544
env->SetProtoMethod(tpl, "instantiate", Instantiate);
522545
env->SetProtoMethod(tpl, "evaluate", Evaluate);
546+
env->SetProtoMethod(tpl, "namespace", Namespace);
523547

524548
target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), tpl->GetFunction());
525549
env->SetMethod(target, "resolve", node::loader::ModuleWrap::Resolve);

‎src/module_wrap.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ModuleWrap : public BaseObject{
3434
staticvoidLink(const v8::FunctionCallbackInfo<v8::Value>& args);
3535
staticvoidInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args);
3636
staticvoidEvaluate(const v8::FunctionCallbackInfo<v8::Value>& args);
37+
staticvoidNamespace(const v8::FunctionCallbackInfo<v8::Value>& args);
3738
staticvoidGetUrl(v8::Local<v8::String> property,
3839
const v8::PropertyCallbackInfo<v8::Value>& info);
3940
staticvoidResolve(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
(0)