Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0aed332

Browse filesBrowse files
guybedfordaduh95
authored andcommitted
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: #62198 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent abea0af commit 0aed332
Copy full SHA for 0aed332

5 files changed

+19Lines changed: 19 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/esm.md‎

Copy file name to clipboardExpand all lines: doc/api/esm.md
+9Lines changed: 9 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+1Lines 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;
Collapse file
76 Bytes
Binary file not shown.
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/es-modules/js-string-builtins.wat
+8Lines 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
)
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/es-modules/test-wasm-js-string-builtins.mjs
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.