Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: Use V8 function to get Module Namespace #16261

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/internal/loader/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { getURLFromFilePath } = require('internal/url');

const {
getNamespaceOfModuleWrap,
createDynamicModule
} = require('internal/loader/ModuleWrap');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could fit this on one line now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions lib/internal/loader/ModuleWrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, ', ')}`
Expand Down Expand Up @@ -57,6 +50,5 @@ const createDynamicModule = (exports, url = '', evaluate) => {

module.exports = {
createDynamicModule,
getNamespaceOfModuleWrap,
ModuleWrap
};
9 changes: 9 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(ret);
}

void ModuleWrap::Namespace(const FunctionCallbackInfo<Value>& args) {
auto iso = args.GetIsolate();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: isolate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

auto that = args.This();
ModuleWrap* obj = Unwrap<ModuleWrap>(that);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you CHECK_NE(obj, nullptr);?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

auto result = obj->module_.Get(iso)->GetModuleNamespace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetModuleNamespace() terminates with a fatal error when GetStatus() isn't one of kInstantiated, kEvaluating or kEvaluated.

I believe I understand how the previous code enforced that invariant (by calling .instantiate() and .evaluate(), right?) but what ensures that now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing except when it is called, can add a check and throw though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made it throw after checking status

args.GetReturnValue().Set(result);
}

MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
Local<String> specifier,
Local<Module> referrer) {
Expand Down Expand Up @@ -516,6 +524,7 @@ void ModuleWrap::Initialize(Local<Object> target,
env->SetProtoMethod(tpl, "link", Link);
env->SetProtoMethod(tpl, "instantiate", Instantiate);
env->SetProtoMethod(tpl, "evaluate", Evaluate);
env->SetProtoMethod(tpl, "namespace", Namespace);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNamespace & GetNamespace? At least if this is idempotent I think that’d be a better name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can throw if it isn't instantiated.


target->Set(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"), tpl->GetFunction());
env->SetMethod(target, "resolve", node::loader::ModuleWrap::Resolve);
Expand Down
1 change: 1 addition & 0 deletions src/module_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ModuleWrap : public BaseObject {
static void Link(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 Namespace(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetUrl(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
static void Resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down