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`.

PR-URL: https://github.com/nodejs/node/pull/62198
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Guy Bedford authored and Antoine du Hamel committed 2026-03-28 18:14:05 +01:00
1 parent abea0af8a9
commit 0aed332ab4
5 files changed
+19

No files matched your search

+9
View File
@@ -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:
+1
View File
@@ -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;
Binary file not shown.
+8
View File
@@ -4,11 +4,15 @@
(import "wasm:js-string" "length" (func $string_length (param externref) (result i32)))
(import "wasm:js-string" "concat" (func $string_concat (param externref externref) (result (ref extern))))
(import "wasm:js-string" "equals" (func $string_equals (param externref externref) (result i32)))
;; Import a string constant via importedStringConstants
(import "wasm:js/string-constants" "hello" (global $hello externref))
;; Export functions that use the builtins
(export "getLength" (func $get_length))
(export "concatStrings" (func $concat_strings))
(export "compareStrings" (func $compare_strings))
(export "getHello" (func $get_hello))
(func $get_length (param $str externref) (result i32)
local.get $str
@@ -26,4 +30,8 @@
local.get $str2
call $string_equals
)
(func $get_hello (result externref)
global.get $hello
)
)
@@ -6,3 +6,4 @@ strictEqual(wasmExports.getLength('hello'), 5);
strictEqual(wasmExports.concatStrings('hello', ' world'), 'hello world');
strictEqual(wasmExports.compareStrings('test', 'test'), 1);
strictEqual(wasmExports.compareStrings('test', 'different'), 0);
strictEqual(wasmExports.getHello(), 'hello');