Skip to content

Commit 308f4ca

Browse files
guybedfordtargos
authored andcommitted
esm: add support for dynamic source phase hook
PR-URL: #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>
1 parent e6e6ae8 commit 308f4ca

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

doc/api/esm.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -713,13 +713,18 @@ into a new instance of `library.wasm`:
713713
```js
714714
import source libraryModule from './library.wasm';
715715

716-
const instance1 = await WebAssembly.instantiate(libraryModule, {
717-
custom: import1,
718-
});
716+
const instance1 = await WebAssembly.instantiate(libraryModule, importObject1);
719717

720-
const instance2 = await WebAssembly.instantiate(libraryModule, {
721-
custom: import2,
722-
});
718+
const instance2 = await WebAssembly.instantiate(libraryModule, importObject2);
719+
```
720+
721+
In addition to the static source phase, there is also a dynamic variant of the
722+
source phase via the `import.source` dynamic phase import syntax:
723+
724+
```js
725+
const dynamicLibrary = await import.source('./library.wasm');
726+
727+
const instance = await WebAssembly.instantiate(dynamicLibrary, importObject);
723728
```
724729
725730
<i id="esm_experimental_top_level_await"></i>

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export default [
104104
parser: babelEslintParser,
105105
parserOptions: {
106106
babelOptions: {
107+
parserOpts: { createImportExpressions: true },
107108
plugins: [
108109
babelPluginProposalExplicitResourceManagement,
109110
babelPluginSyntaxImportAttributes,

src/module_wrap.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,8 @@ void ModuleWrap::SetImportModuleDynamicallyCallback(
10651065
realm->set_host_import_module_dynamically_callback(import_callback);
10661066

10671067
isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
1068-
// TODO(guybedford): Enable this once
1069-
// https://github.com/nodejs/node/pull/56842 lands.
1070-
// isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
1071-
// ImportModuleDynamicallyWithPhase);
1068+
isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
1069+
ImportModuleDynamicallyWithPhase);
10721070
}
10731071

10741072
void ModuleWrap::HostInitializeImportMetaObjectCallback(

test/es-module/test-esm-wasm.mjs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
124124
strictEqual(code, 0);
125125
});
126126

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

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

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

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

0 commit comments

Comments
 (0)