Skip to content

Commit 5eec509

Browse files
committed
esm: add support for dynamic source phase hook
1 parent 1b2bf2d commit 5eec509

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

doc/api/esm.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,17 @@ const instance2 = await WebAssembly.instantiate(libraryModule, {
722722
});
723723
```
724724
725+
In addition to the static source phase, there is also a dynamic variant of the
726+
source phase via the `import.source` dynamic phase import syntax:
727+
728+
```js
729+
const dynamicLibrary = await import.source('./library.wasm');
730+
731+
const instance = await WebAssembly.instantiate(dynamicLibrary, {
732+
custom: import
733+
});
734+
```
735+
725736
<i id="esm_experimental_top_level_await"></i>
726737
727738
## Top-level `await`

src/module_wrap.cc

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

10501050
isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
1051-
// TODO(guybedford): Enable this once
1052-
// https://github.com/nodejs/node/pull/56842 lands.
1053-
// isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
1054-
// ImportModuleDynamicallyWithPhase);
1051+
isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
1052+
ImportModuleDynamicallyWithPhase);
10551053
}
10561054

10571055
void ModuleWrap::HostInitializeImportMetaObjectCallback(

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

Lines changed: 4 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',
@@ -238,8 +235,7 @@ describe('ESM: WASM modules', { concurrency: !process.env.TEST_PARALLEL }, () =>
238235
notStrictEqual(code, 0);
239236
});
240237

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 () => {
238+
it('should throw for vm source phase dynamic import', async () => {
243239
const { code, stderr, stdout } = await spawnPromisified(execPath, [
244240
'--no-warnings',
245241
'--experimental-wasm-modules',

0 commit comments

Comments
 (0)