esm: support source phase imports for WebAssembly · nodejs/node@57e49ee · GitHub
Skip to content

Commit 57e49ee

Browse files
Guy BedfordRafaelGSS
authored andcommitted
esm: support source phase imports for WebAssembly
PR-URL: #56919 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent ab9b439 commit 57e49ee

22 files changed

Lines changed: 580 additions & 95 deletions

doc/api/errors.md

Lines changed: 11 additions & 0 deletions

doc/api/esm.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -669,17 +669,19 @@ imported from the same path.
669669
670670
> Stability: 1 - Experimental
671671
672-
Importing WebAssembly modules is supported under the
673-
`--experimental-wasm-modules` flag, allowing any `.wasm` files to be
674-
imported as normal modules while also supporting their module imports.
672+
Importing both WebAssembly module instances and WebAssembly source phase
673+
imports are supported under the `--experimental-wasm-modules` flag.
675674
676-
This integration is in line with the
675+
Both of these integrations are in line with the
677676
[ES Module Integration Proposal for WebAssembly][].
678677
679-
For example, an `index.mjs` containing:
678+
Instance imports allow any `.wasm` files to be imported as normal modules,
679+
supporting their module imports in turn.
680+
681+
For example, an `index.js` containing:
680682
681683
```js
682-
import * as M from './module.wasm';
684+
import * as M from './library.wasm';
683685
console.log(M);
684686
```
685687
@@ -689,7 +691,35 @@ executed under:
689691
node --experimental-wasm-modules index.mjs
690692
```
691693
692-
would provide the exports interface for the instantiation of `module.wasm`.
694+
would provide the exports interface for the instantiation of `library.wasm`.
695+
696+
### Wasm Source Phase Imports
697+
698+
<!-- YAML
699+
added: REPLACEME
700+
-->
701+
702+
The [Source Phase Imports][] proposal allows the `import source` keyword
703+
combination to import a `WebAssembly.Module` object directly, instead of getting
704+
a module instance already instantiated with its dependencies.
705+
706+
This is useful when needing custom instantiations for Wasm, while still
707+
resolving and loading it through the ES module integration.
708+
709+
For example, to create multiple instances of a module, or to pass custom imports
710+
into a new instance of `library.wasm`:
711+
712+
```js
713+
import source libraryModule from './library.wasm';
714+
715+
const instance1 = await WebAssembly.instantiate(libraryModule, {
716+
custom: import1,
717+
});
718+
719+
const instance2 = await WebAssembly.instantiate(libraryModule, {
720+
custom: import2,
721+
});
722+
```
693723
694724
<i id="esm_experimental_top_level_await"></i>
695725
@@ -1126,6 +1156,7 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
11261156
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
11271157
[Module customization hooks]: module.md#customization-hooks
11281158
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1159+
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
11291160
[Terminology]: #terminology
11301161
[URL]: https://url.spec.whatwg.org/
11311162
[`"exports"`]: packages.md#exports

doc/api/vm.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,7 @@ has the following signature:
19081908
* `importAttributes` {Object} The `"with"` value passed to the
19091909
[`optionsExpression`][] optional parameter, or an empty object if no value was
19101910
provided.
1911+
* `phase` {string} The phase of the dynamic import (`"source"` or `"evaluation"`).
19111912
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
19121913
recommended in order to take advantage of error tracking, and to avoid issues
19131914
with namespaces that contain `then` function exports.

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ function loadESMFromCJS(mod, filename, format, source) {
15081508
if (isMain) {
15091509
require('internal/modules/run_main').runEntryPointWithESMLoader((cascadedLoader) => {
15101510
const mainURL = pathToFileURL(filename).href;
1511-
return cascadedLoader.import(mainURL, undefined, { __proto__: null }, true);
1511+
return cascadedLoader.import(mainURL, undefined, { __proto__: null }, undefined, true);
15121512
});
15131513
// ESM won't be accessible via process.mainModule.
15141514
setOwnProperty(process, 'mainModule', undefined);

lib/internal/modules/esm/loader.js

Lines changed: 32 additions & 15 deletions

0 commit comments

Comments
 (0)