From 43d99bbc344eef5cec595cdb8a24c4915b2ec931 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Tue, 17 Oct 2017 11:25:48 -0500 Subject: [PATCH 1/3] src: Use V8 function to get Module Namespace --- lib/internal/loader/Loader.js | 3 +-- lib/internal/loader/ModuleWrap.js | 8 -------- src/module_wrap.cc | 9 +++++++++ src/module_wrap.h | 1 + 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/internal/loader/Loader.js b/lib/internal/loader/Loader.js index 57c70188d66b70..994ccaf936fed5 100644 --- a/lib/internal/loader/Loader.js +++ b/lib/internal/loader/Loader.js @@ -3,7 +3,6 @@ const { getURLFromFilePath } = require('internal/url'); const { - getNamespaceOfModuleWrap, createDynamicModule } = require('internal/loader/ModuleWrap'); @@ -99,7 +98,7 @@ class Loader { async import(specifier, parentURL = this.base) { const job = await this.getModuleJob(specifier, parentURL); const module = await job.run(); - return getNamespaceOfModuleWrap(module); + return module.namespace(); } } Object.setPrototypeOf(Loader.prototype, null); diff --git a/lib/internal/loader/ModuleWrap.js b/lib/internal/loader/ModuleWrap.js index 8766041d94e251..c97b4888ea22ce 100644 --- a/lib/internal/loader/ModuleWrap.js +++ b/lib/internal/loader/ModuleWrap.js @@ -6,13 +6,6 @@ const debug = require('util').debuglog('esm'); const ArrayJoin = Function.call.bind(Array.prototype.join); 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) => { debug( `creating ESM facade for ${url} with exports: ${ArrayJoin(exports, ', ')}` @@ -57,6 +50,5 @@ const createDynamicModule = (exports, url = '', evaluate) => { module.exports = { createDynamicModule, - getNamespaceOfModuleWrap, ModuleWrap }; diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 829248b681c4a7..4a18a749693f27 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -207,6 +207,14 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(ret); } +void ModuleWrap::Namespace(const FunctionCallbackInfo& args) { + auto iso = args.GetIsolate(); + auto that = args.This(); + ModuleWrap* obj = Unwrap(that); + auto result = obj->module_.Get(iso)->GetModuleNamespace(); + args.GetReturnValue().Set(result); +} + MaybeLocal ModuleWrap::ResolveCallback(Local context, Local specifier, Local referrer) { @@ -516,6 +524,7 @@ void ModuleWrap::Initialize(Local target, env->SetProtoMethod(tpl, "link", Link); env->SetProtoMethod(tpl, "instantiate", Instantiate); env->SetProtoMethod(tpl, "evaluate", Evaluate); + env->SetProtoMethod(tpl, "namespace", Namespace); target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), tpl->GetFunction()); env->SetMethod(target, "resolve", node::loader::ModuleWrap::Resolve); diff --git a/src/module_wrap.h b/src/module_wrap.h index 82fd1d89ffd547..ac2f90476d5b59 100644 --- a/src/module_wrap.h +++ b/src/module_wrap.h @@ -34,6 +34,7 @@ class ModuleWrap : public BaseObject { static void Link(const v8::FunctionCallbackInfo& args); static void Instantiate(const v8::FunctionCallbackInfo& args); static void Evaluate(const v8::FunctionCallbackInfo& args); + static void Namespace(const v8::FunctionCallbackInfo& args); static void GetUrl(v8::Local property, const v8::PropertyCallbackInfo& info); static void Resolve(const v8::FunctionCallbackInfo& args); From 41cdcabb1a911f7b67a4c43a712173d0e2e150be Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Tue, 17 Oct 2017 15:14:27 -0500 Subject: [PATCH 2/3] nits --- lib/internal/loader/Loader.js | 4 +--- src/module_wrap.cc | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/internal/loader/Loader.js b/lib/internal/loader/Loader.js index 994ccaf936fed5..9ebce9f01ce63e 100644 --- a/lib/internal/loader/Loader.js +++ b/lib/internal/loader/Loader.js @@ -2,9 +2,7 @@ const { getURLFromFilePath } = require('internal/url'); -const { - createDynamicModule -} = require('internal/loader/ModuleWrap'); +const { createDynamicModule } = require('internal/loader/ModuleWrap'); const ModuleMap = require('internal/loader/ModuleMap'); const ModuleJob = require('internal/loader/ModuleJob'); diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 4a18a749693f27..b6e4e95026c7b3 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -208,10 +208,24 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo& args) { } void ModuleWrap::Namespace(const FunctionCallbackInfo& args) { - auto iso = args.GetIsolate(); + Environment* env = Environment::GetCurrent(args); + auto isolate = args.GetIsolate(); auto that = args.This(); ModuleWrap* obj = Unwrap(that); - auto result = obj->module_.Get(iso)->GetModuleNamespace(); + 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); } From 3256d23dc2a445ddc0e524f5756b584a19c16343 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Thu, 19 Oct 2017 09:47:03 -0500 Subject: [PATCH 3/3] long line --- src/module_wrap.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/module_wrap.cc b/src/module_wrap.cc index b6e4e95026c7b3..fd7a7ce580f76f 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -218,7 +218,8 @@ void ModuleWrap::Namespace(const FunctionCallbackInfo& args) { switch (module->GetStatus()) { default: - return env->ThrowError("cannot get namespace, Module has not been instantiated"); + 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: