From dbe5977809bfbb0f645f758e5a0985cd024e30c7 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 10 Mar 2026 22:12:44 -0400 Subject: [PATCH 1/3] wasm: support js string constant esm import Extends the Wasm ESM Integration for importing WebAssembly modules in either the source phase or instance phase to support importing static JS string constants from the special import name `wasm:js/string-constants`. --- doc/api/esm.md | 9 +++++++++ lib/internal/modules/esm/translators.js | 1 + .../fixtures/es-modules/js-string-builtins.wasm | Bin 325 -> 401 bytes test/fixtures/es-modules/js-string-builtins.wat | 8 ++++++++ .../es-modules/test-wasm-js-string-builtins.mjs | 1 + 5 files changed, 19 insertions(+) diff --git a/doc/api/esm.md b/doc/api/esm.md index 5396acfd53d65c..f6ac6f457b8947 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -809,6 +809,15 @@ imports and they cannot be inspected via `WebAssembly.Module.imports(mod)` or virtualized unless recompiling the module using the direct `WebAssembly.compile` API with string builtins disabled. +String constants may also be imported from the `wasm:js/string-constants` builtin +import URL, allowing static JS string globals to be defined: + +```text +(module + (import "wasm:js/string-constants" "hello" (global $hello externref)) +) +``` + Importing a module in the source phase before it has been instantiated will also use the compile-time builtins automatically: diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 2af52e4b9435ea..968e25ebad9521 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -522,6 +522,7 @@ translators.set('wasm', function(url, translateContext) { try { compiled = new WebAssembly.Module(source, { builtins: ['js-string'], + importedStringConstants: "wasm:js/string-constants" }); } catch (err) { err.message = errPath(url) + ': ' + err.message; diff --git a/test/fixtures/es-modules/js-string-builtins.wasm b/test/fixtures/es-modules/js-string-builtins.wasm index b4c08587dd08e715fa2a79fead8fc46143d949d2..fe520bab1fbbf5af043b905c4729d2e2ce2746fa 100644 GIT binary patch delta 157 zcmX@gG?7__A+b1@k%57MQJf`#F`uzMfhj+qF(n^N)h948<}+opOjPjDkthc$w8|>h zFD@y{%uClz&d)0@Nz5xLX3a>=$;oHVXJBS!VPIrpX18RSSg6a#k)B%O0g_>0XWM*MzU}R#~W1cue tSDj0WnT Date: Wed, 11 Mar 2026 10:09:33 -0400 Subject: [PATCH 2/3] lint fix --- lib/internal/modules/esm/translators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 968e25ebad9521..7a665dfb1cf72f 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -522,7 +522,7 @@ translators.set('wasm', function(url, translateContext) { try { compiled = new WebAssembly.Module(source, { builtins: ['js-string'], - importedStringConstants: "wasm:js/string-constants" + importedStringConstants: "wasm:js/string-constants", }); } catch (err) { err.message = errPath(url) + ': ' + err.message; From 98a6cb3f9fdbe39293331f3635f4f8e67b48882b Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 11 Mar 2026 10:28:50 -0400 Subject: [PATCH 3/3] Update lib/internal/modules/esm/translators.js Co-authored-by: James Sumners --- lib/internal/modules/esm/translators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 7a665dfb1cf72f..c3de26ccc5c4ab 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -522,7 +522,7 @@ translators.set('wasm', function(url, translateContext) { try { compiled = new WebAssembly.Module(source, { builtins: ['js-string'], - importedStringConstants: "wasm:js/string-constants", + importedStringConstants: 'wasm:js/string-constants', }); } catch (err) { err.message = errPath(url) + ': ' + err.message;