From af5d1c93ce0ea2dd448c6113b2593f1bc7157b8f Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 14 Aug 2025 16:28:40 +0200 Subject: [PATCH] esm: sync-ify module translation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Chengzhong Wu --- lib/internal/modules/esm/loader.js | 6 ++++-- lib/internal/modules/esm/translators.js | 8 ++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index bc36fa2e26b..5d90b5f4c8c 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -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} + * @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; } /** diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index b6ac42302a1..98bdb384269 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -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) {