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 f7a408d

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 af8d092 commit f7a408d
Copy full SHA for f7a408d

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
@@ -800,6 +800,15 @@ imports and they cannot be inspected via `WebAssembly.Module.imports(mod)`
800800
or virtualized unless recompiling the module using the direct
801801
`WebAssembly.compile` API with string builtins disabled.
802802
803+
String constants may also be imported from the `wasm:js/string-constants` builtin
804+
import URL, allowing static JS string globals to be defined:
805+
806+
```text
807+
(module
808+
(import "wasm:js/string-constants" "hello" (global $hello externref))
809+
)
810+
```
811+
803812
Importing a module in the source phase before it has been instantiated will also
804813
use the compile-time builtins automatically:
805814
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
@@ -521,6 +521,7 @@ translators.set('wasm', function(url, translateContext) {
521521
try {
522522
compiled = new WebAssembly.Module(source, {
523523
builtins: ['js-string'],
524+
importedStringConstants: 'wasm:js/string-constants',
524525
});
525526
} catch (err) {
526527
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.