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 <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
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

+2-5
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
const ModuleMap = require('internal/loader/ModuleMap');
118
const ModuleJob = require('internal/loader/ModuleJob');
@@ -100,7 +97,7 @@ class Loader {
10097
async import(specifier, parentURL = this.base) {
10198
const job = await this.getModuleJob(specifier, parentURL);
10299
const module = await job.run();
103-
return getNamespaceOfModuleWrap(module);
100+
return module.namespace();
104101
}
105102
}
106103
Object.setPrototypeOf(Loader.prototype, null);

lib/internal/loader/ModuleWrap.js

-8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ const debug = require('util').debuglog('esm');
66
const ArrayJoin = Function.call.bind(Array.prototype.join);
77
const ArrayMap = Function.call.bind(Array.prototype.map);
88

9-
const getNamespaceOfModuleWrap = (m) => {
10-
const tmp = new ModuleWrap('import * as _ from "";_;', '');
11-
tmp.link(async () => m);
12-
tmp.instantiate();
13-
return tmp.evaluate();
14-
};
15-
169
const createDynamicModule = (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

+24
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+
void ModuleWrap::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+
auto module = 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

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ModuleWrap : public BaseObject {
3434
static void Link(const v8::FunctionCallbackInfo<v8::Value>& args);
3535
static void Instantiate(const v8::FunctionCallbackInfo<v8::Value>& args);
3636
static void Evaluate(const v8::FunctionCallbackInfo<v8::Value>& args);
37+
static void Namespace(const v8::FunctionCallbackInfo<v8::Value>& args);
3738
static void GetUrl(v8::Local<v8::String> property,
3839
const v8::PropertyCallbackInfo<v8::Value>& info);
3940
static void Resolve(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)