Skip to content

Commit dbe5977

Browse files
committed
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`.
1 parent 9ff27fd commit dbe5977

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

doc/api/esm.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,15 @@ imports and they cannot be inspected via `WebAssembly.Module.imports(mod)`
809809
or virtualized unless recompiling the module using the direct
810810
`WebAssembly.compile` API with string builtins disabled.
811811
812+
String constants may also be imported from the `wasm:js/string-constants` builtin
813+
import URL, allowing static JS string globals to be defined:
814+
815+
```text
816+
(module
817+
(import "wasm:js/string-constants" "hello" (global $hello externref))
818+
)
819+
```
820+
812821
Importing a module in the source phase before it has been instantiated will also
813822
use the compile-time builtins automatically:
814823

lib/internal/modules/esm/translators.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ translators.set('wasm', function(url, translateContext) {
522522
try {
523523
compiled = new WebAssembly.Module(source, {
524524
builtins: ['js-string'],
525+
importedStringConstants: "wasm:js/string-constants"
525526
});
526527
} catch (err) {
527528
err.message = errPath(url) + ': ' + err.message;
76 Bytes
Binary file not shown.

test/fixtures/es-modules/js-string-builtins.wat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
(import "wasm:js-string" "length" (func $string_length (param externref) (result i32)))
55
(import "wasm:js-string" "concat" (func $string_concat (param externref externref) (result (ref extern))))
66
(import "wasm:js-string" "equals" (func $string_equals (param externref externref) (result i32)))
7+
8+
;; Import a string constant via importedStringConstants
9+
(import "wasm:js/string-constants" "hello" (global $hello externref))
710

811
;; Export functions that use the builtins
912
(export "getLength" (func $get_length))
1013
(export "concatStrings" (func $concat_strings))
1114
(export "compareStrings" (func $compare_strings))
15+
(export "getHello" (func $get_hello))
1216

1317
(func $get_length (param $str externref) (result i32)
1418
local.get $str
@@ -26,4 +30,8 @@
2630
local.get $str2
2731
call $string_equals
2832
)
33+
34+
(func $get_hello (result externref)
35+
global.get $hello
36+
)
2937
)

test/fixtures/es-modules/test-wasm-js-string-builtins.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ strictEqual(wasmExports.getLength('hello'), 5);
66
strictEqual(wasmExports.concatStrings('hello', ' world'), 'hello world');
77
strictEqual(wasmExports.compareStrings('test', 'test'), 1);
88
strictEqual(wasmExports.compareStrings('test', 'different'), 0);
9+
strictEqual(wasmExports.getHello(), 'hello');

0 commit comments

Comments
 (0)