esm: sync-ify module translation

This completes the TODO to compile WASM synchronously and thus
making translation (i.e. compilation + instantiation) synchronous.

PR-URL: https://github.com/nodejs/node/pull/59453
Refs: https://github.com/nodejs/node/issues/55782
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Joyee Cheung authored and Michaël Zasso committed 2025-08-21 19:01:00 +02:00
1 parent dd5f835af7
commit b56d8af2fe
2 files changed
+6 -8

No files matched your search

+4 -2
View File
@@ -537,7 +537,7 @@ class ModuleLoader {
* matching translators.
* @param {ModuleSource} source Source of the module to be translated.
* @param {boolean} isMain Whether the module to be translated is the entry point.
* @returns {ModuleWrap | Promise<ModuleWrap>}
* @returns {ModuleWrap}
*/
#translate(url, format, source, isMain) {
this.validateLoadResult(url, format);
@@ -547,7 +547,9 @@ class ModuleLoader {
throw new ERR_UNKNOWN_MODULE_FORMAT(format, url);
}
return FunctionPrototypeCall(translator, this, url, source, isMain);
const result = FunctionPrototypeCall(translator, this, url, source, isMain);
assert(result instanceof ModuleWrap);
return result;
}
/**
+2 -6
View File
@@ -494,18 +494,14 @@ translators.set('json', function jsonStrategy(url, source) {
* >} [[Instance]] slot proxy for WebAssembly Module Record
*/
const wasmInstances = new SafeWeakMap();
translators.set('wasm', async function(url, source) {
translators.set('wasm', function(url, source) {
assertBufferSource(source, false, 'load');
debug(`Translating WASMModule ${url}`);
let compiled;
try {
// TODO(joyeecheung): implement a translator that just uses
// compiled = new WebAssembly.Module(source) to compile it
// synchronously.
compiled = await WebAssembly.compile(source, {
// The ESM Integration auto-enables Wasm JS builtins by default when available.
compiled = new WebAssembly.Module(source, {
builtins: ['js-string'],
});
} catch (err) {