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..c3de26ccc5c4ab 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 b4c08587dd08e7..fe520bab1fbbf5 100644 Binary files a/test/fixtures/es-modules/js-string-builtins.wasm and b/test/fixtures/es-modules/js-string-builtins.wasm differ diff --git a/test/fixtures/es-modules/js-string-builtins.wat b/test/fixtures/es-modules/js-string-builtins.wat index 9bc55a8fa750cc..08f4ade8c4151c 100644 --- a/test/fixtures/es-modules/js-string-builtins.wat +++ b/test/fixtures/es-modules/js-string-builtins.wat @@ -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 + ) ) \ No newline at end of file diff --git a/test/fixtures/es-modules/test-wasm-js-string-builtins.mjs b/test/fixtures/es-modules/test-wasm-js-string-builtins.mjs index 2364f246b2558d..c76dcc39894932 100644 --- a/test/fixtures/es-modules/test-wasm-js-string-builtins.mjs +++ b/test/fixtures/es-modules/test-wasm-js-string-builtins.mjs @@ -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');