Skip to content

esm: add support for dynamic source phase hook #58147

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 11 additions & 6 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,18 @@ into a new instance of `library.wasm`:
```js
import source libraryModule from './library.wasm';

const instance1 = await WebAssembly.instantiate(libraryModule, {
custom: import1,
});
const instance1 = await WebAssembly.instantiate(libraryModule, importObject1);

const instance2 = await WebAssembly.instantiate(libraryModule, {
custom: import2,
});
const instance2 = await WebAssembly.instantiate(libraryModule, importObject2);
```

In addition to the static source phase, there is also a dynamic variant of the
source phase via the `import.source` dynamic phase import syntax:

```js
const dynamicLibrary = await import.source('./library.wasm');

const instance = await WebAssembly.instantiate(dynamicLibrary, importObject);
```

<i id="esm_experimental_top_level_await"></i>
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default [
parser: babelEslintParser,
parserOptions: {
babelOptions: {
parserOpts: { createImportExpressions: true },
plugins: [
babelPluginProposalExplicitResourceManagement,
babelPluginSyntaxImportAttributes,
Expand Down
6 changes: 2 additions & 4 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1065,10 +1065,8 @@ void ModuleWrap::SetImportModuleDynamicallyCallback(
realm->set_host_import_module_dynamically_callback(import_callback);

isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
// TODO(guybedford): Enable this once
// https://github.com/nodejs/node/pull/56842 lands.
// isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
// ImportModuleDynamicallyWithPhase);
isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
ImportModuleDynamicallyWithPhase);
}

void ModuleWrap::HostInitializeImportMetaObjectCallback(
Expand Down
13 changes: 5 additions & 8 deletions test/es-module/test-esm-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
strictEqual(code, 0);
});

// TODO: Enable this once https://github.com/nodejs/node/pull/56842 lands.
it.skip('should support dynamic source phase imports', async () => {
it('should support dynamic source phase imports', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-wasm-modules',
Expand Down Expand Up @@ -166,8 +165,7 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
strictEqual(code, 0);
});

// TODO: Enable this once https://github.com/nodejs/node/pull/56842 lands.
it.skip('should not execute dynamic source phase imports', async () => {
it('should not execute dynamic source phase imports', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-wasm-modules',
Expand All @@ -181,8 +179,7 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
strictEqual(code, 0);
});

// TODO: Enable this once https://github.com/nodejs/node/pull/56842 lands.
it.skip('should throw for dynamic source phase imports not defined', async () => {
it('should throw for dynamic source phase imports not defined', async () => {
const fileUrl = fixtures.fileURL('es-modules/wasm-source-phase.js');
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
Expand All @@ -195,6 +192,7 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
' strictEqual(e instanceof SyntaxError, true);',
' strictEqual(e.message.includes("Source phase import object is not defined for module"), true);',
` strictEqual(e.message.includes(${JSON.stringify(fileUrl)}), true);`,
` return true`,
'});',
].join('\n'),
]);
Expand Down Expand Up @@ -238,8 +236,7 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
notStrictEqual(code, 0);
});

// TODO: Enable this once https://github.com/nodejs/node/pull/56842 lands.
it.skip('should throw for vm source phase dynamic import', async () => {
it('should throw for vm source phase dynamic import', async () => {
const { code, stderr, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-wasm-modules',
Expand Down
Loading