Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@
try {
compiled = new WebAssembly.Module(source, {
builtins: ['js-string'],
importedStringConstants: "wasm:js/string-constants",

Check failure on line 525 in lib/internal/modules/esm/translators.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
});
} catch (err) {
err.message = errPath(url) + ': ' + err.message;
Expand Down
Binary file modified test/fixtures/es-modules/js-string-builtins.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions test/fixtures/es-modules/js-string-builtins.wat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +30,8 @@
local.get $str2
call $string_equals
)

(func $get_hello (result externref)
global.get $hello
)
)
1 change: 1 addition & 0 deletions test/fixtures/es-modules/test-wasm-js-string-builtins.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Loading