src: use V8 function to get Module Namespace
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
cadc47fe07
commit
1c0772444c
@ -2,10 +2,7 @@
|
|||||||
|
|
||||||
const { getURLFromFilePath } = require('internal/url');
|
const { getURLFromFilePath } = require('internal/url');
|
||||||
|
|
||||||
const {
|
const { createDynamicModule } = require('internal/loader/ModuleWrap');
|
||||||
getNamespaceOfModuleWrap,
|
|
||||||
createDynamicModule
|
|
||||||
} = require('internal/loader/ModuleWrap');
|
|
||||||
|
|
||||||
const ModuleMap = require('internal/loader/ModuleMap');
|
const ModuleMap = require('internal/loader/ModuleMap');
|
||||||
const ModuleJob = require('internal/loader/ModuleJob');
|
const ModuleJob = require('internal/loader/ModuleJob');
|
||||||
@ -100,7 +97,7 @@ class Loader {
|
|||||||
async import(specifier, parentURL = this.base) {
|
async import(specifier, parentURL = this.base) {
|
||||||
const job = await this.getModuleJob(specifier, parentURL);
|
const job = await this.getModuleJob(specifier, parentURL);
|
||||||
const module = await job.run();
|
const module = await job.run();
|
||||||
return getNamespaceOfModuleWrap(module);
|
return module.namespace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Object.setPrototypeOf(Loader.prototype, null);
|
Object.setPrototypeOf(Loader.prototype, null);
|
||||||
|
@ -5,13 +5,6 @@ const debug = require('util').debuglog('esm');
|
|||||||
const ArrayJoin = Function.call.bind(Array.prototype.join);
|
const ArrayJoin = Function.call.bind(Array.prototype.join);
|
||||||
const ArrayMap = Function.call.bind(Array.prototype.map);
|
const ArrayMap = Function.call.bind(Array.prototype.map);
|
||||||
|
|
||||||
const getNamespaceOfModuleWrap = (m) => {
|
|
||||||
const tmp = new ModuleWrap('import * as _ from "";_;', '');
|
|
||||||
tmp.link(async () => m);
|
|
||||||
tmp.instantiate();
|
|
||||||
return tmp.evaluate();
|
|
||||||
};
|
|
||||||
|
|
||||||
const createDynamicModule = (exports, url = '', evaluate) => {
|
const createDynamicModule = (exports, url = '', evaluate) => {
|
||||||
debug(
|
debug(
|
||||||
`creating ESM facade for ${url} with exports: ${ArrayJoin(exports, ', ')}`
|
`creating ESM facade for ${url} with exports: ${ArrayJoin(exports, ', ')}`
|
||||||
@ -56,6 +49,5 @@ const createDynamicModule = (exports, url = '', evaluate) => {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createDynamicModule,
|
createDynamicModule,
|
||||||
getNamespaceOfModuleWrap,
|
|
||||||
ModuleWrap
|
ModuleWrap
|
||||||
};
|
};
|
||||||
|
@ -207,6 +207,29 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
|
|||||||
args.GetReturnValue().Set(ret);
|
args.GetReturnValue().Set(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ModuleWrap::Namespace(const FunctionCallbackInfo<Value>& args) {
|
||||||
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
auto isolate = args.GetIsolate();
|
||||||
|
auto that = args.This();
|
||||||
|
ModuleWrap* obj = Unwrap<ModuleWrap>(that);
|
||||||
|
CHECK_NE(obj, nullptr);
|
||||||
|
|
||||||
|
auto module = obj->module_.Get(isolate);
|
||||||
|
|
||||||
|
switch (module->GetStatus()) {
|
||||||
|
default:
|
||||||
|
return env->ThrowError(
|
||||||
|
"cannot get namespace, Module has not been instantiated");
|
||||||
|
case v8::Module::Status::kInstantiated:
|
||||||
|
case v8::Module::Status::kEvaluating:
|
||||||
|
case v8::Module::Status::kEvaluated:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = module->GetModuleNamespace();
|
||||||
|
args.GetReturnValue().Set(result);
|
||||||
|
}
|
||||||
|
|
||||||
MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
|
MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
|
||||||
Local<String> specifier,
|
Local<String> specifier,
|
||||||
Local<Module> referrer) {
|
Local<Module> referrer) {
|
||||||
@ -520,6 +543,7 @@ void ModuleWrap::Initialize(Local<Object> target,
|
|||||||
env->SetProtoMethod(tpl, "link", Link);
|
env->SetProtoMethod(tpl, "link", Link);
|
||||||
env->SetProtoMethod(tpl, "instantiate", Instantiate);
|
env->SetProtoMethod(tpl, "instantiate", Instantiate);
|
||||||
env->SetProtoMethod(tpl, "evaluate", Evaluate);
|
env->SetProtoMethod(tpl, "evaluate", Evaluate);
|
||||||
|
env->SetProtoMethod(tpl, "namespace", Namespace);
|
||||||
|
|
||||||
target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), tpl->GetFunction());
|
target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), tpl->GetFunction());
|
||||||
env->SetMethod(target, "resolve", node::loader::ModuleWrap::Resolve);
|
env->SetMethod(target, "resolve", node::loader::ModuleWrap::Resolve);
|
||||||
|
@ -34,6 +34,7 @@ class ModuleWrap : public BaseObject {
|
|||||||
static void Link(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void Link(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void Instantiate(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void Instantiate(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void Evaluate(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void Evaluate(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
|
static void Namespace(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void GetUrl(v8::Local<v8::String> property,
|
static void GetUrl(v8::Local<v8::String> property,
|
||||||
const v8::PropertyCallbackInfo<v8::Value>& info);
|
const v8::PropertyCallbackInfo<v8::Value>& info);
|
||||||
static void Resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void Resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user