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

Call linker when dynamically importing inside a vm.Module #19363

Closed
Jamesernator opened this issue Mar 15, 2018 · 2 comments · Fixed by #22381
Closed

Call linker when dynamically importing inside a vm.Module #19363

Jamesernator opened this issue Mar 15, 2018 · 2 comments · Fixed by #22381
Assignees
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. vm Issues and PRs related to the vm subsystem.

Comments

@Jamesernator
Copy link

Dynamically importing from a Module

Currently vm.Module can construct and evaluate modules just fine, but there's no way for one of those modules to dynamically import another file e.g.:

const mod = new vm.Module(`
    import("./otherModule.mjs");
`);
await mod.link((specifier, module) => {
    console.log("Expected this be called on dynamic import!")
});

mod.instantiate();

const { result } = await mod.evaluate();
// Error: Cannot find module ./otherModule.mjs
await result;

Now for dynamic import reusing the linker function should be sufficient as the algorithm for resolving a dynamic module should be the same as for a static module.

Hence we can do import("some-specifier") with this simple process:

  1. Repeat the linker algorithm starting by letting importedModule be linkerFunction("some-specifier", importingModule).
  2. Perform importedModule.instantiate().
  3. Perform importedModule.evaluate()
  4. Resolve the promise for import("some-specifier") with importedModule.namespace.
  5. NOTE: If any of the above steps fail then reject the Promise for import("some-specifier")

Dynamically importing from a Script

Because the script goal also supports dynamic import we need a way to be able to create a vm.Script with a dynamic import hook, I'm not really sure where this should go, perhaps a linker option to script.runInContext(...) (and variants).

For this to be easily compatible with vm.Module then vm.Script should also support the url option in lieu of filename so that the linker can just inspect the .url property to perform linking.

e.g.:

const script = new vm.Script(`
    import("./someModule.mjs");
`, {
    url: "file:///my-directory/my-cool-script.js",
});

script.runInContext(context, {
    linker(specifier, scriptOrModule) {
        const resolvedUrl = new URL(specifier, scriptOrModule.url);
        // fetch and instantiate module
        return importedModule;
    },
});
@devsnek
Copy link
Member

devsnek commented Mar 15, 2018

I've got this working in my fork, just currently waiting on some conversation with the v8 team to finish this up. check it out: https://github.com/devsnek/node/tree/refactor/module-callbacks

@devsnek devsnek added vm Issues and PRs related to the vm subsystem. feature request Issues that request new features to be added to Node.js. esm Issues and PRs related to the ECMAScript Modules implementation. labels Mar 15, 2018
@devsnek devsnek self-assigned this Mar 15, 2018
@Jamesernator
Copy link
Author

Looks good! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants