esm: add support for dynamic source phase hook

PR-URL: https://github.com/nodejs/node/pull/58147
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Guy Bedford 2025-05-03 12:26:41 -07:00 committed by Guy Bedford
parent 27edff5cb9
commit f5ac35ee15
4 changed files with 19 additions and 18 deletions

View file

@ -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>

View file

@ -104,6 +104,7 @@ export default [
parser: babelEslintParser,
parserOptions: {
babelOptions: {
parserOpts: { createImportExpressions: true },
plugins: [
babelPluginProposalExplicitResourceManagement,
babelPluginSyntaxImportAttributes,

View file

@ -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(

View file

@ -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',
@ -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',
@ -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',
@ -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'),
]);
@ -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',