-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chrome 130 supports [WebAssembly JS String builtins](https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md). This PR adds some examples to accompany the documentation I am writing for JS String builtins, plus an update to the readme to explain how to successfully compile the wasm modules.
- Loading branch information
1 parent
7e4038f
commit 01fcd00
Showing
21 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# WebAssembly JavaScript builtins examples | ||
|
||
WebAssembly JavaScript builtins examples. | ||
|
||
## Testing/modifying the JavaScript builtins examples | ||
|
||
The `wat2wasm` tool does not support the [JS builtins](https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md) examples. To compile `.wat` files that make use of JavaScript builtins, you need to use the `wasm-as` tool, which is part of the [Binaryen library](https://github.com/WebAssembly/binaryen). You'll need to run `wasm-as` with reference types and GC enabled for these examples to compile successfully: | ||
|
||
```sh | ||
wasm-as --enable-reference-types -–enable-gc module.wat -o module.wasm | ||
``` | ||
|
||
Or you can use the `-all` flag in place of `--enable-reference-types -–enable-gc`: | ||
|
||
```sh | ||
wasm-as -all module.wat -o module.wasm | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>WebAssembly JS String Builtins example</title> | ||
|
||
<script defer src="index.js"></script> | ||
</head> | ||
|
||
<body> | ||
<p>My template</p> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const importObject = { // Regular import | ||
m: { | ||
log: console.log | ||
} | ||
}; | ||
|
||
const compileOptions = { | ||
builtins: ["js-string"], // Opt-in to get magic imported functions | ||
importedStringConstants: "#" // Opt-in to get magic imported globals | ||
} | ||
|
||
WebAssembly.compileStreaming(fetch("log-concat.wasm"), compileOptions) | ||
.then(module => WebAssembly.instantiate(module, importObject)) | ||
.then(instance => instance.exports.main()); | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>WebAssembly JS String Builtins example</title> | ||
|
||
<script defer src="index.js"></script> | ||
</head> | ||
|
||
<body> | ||
<p>My template</p> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const importObject = { // Regular import | ||
m: { | ||
log: console.log | ||
} | ||
}; | ||
|
||
const compileOptions = { | ||
builtins: ["js-string"], // Opt-in to get magic imported functions | ||
importedStringConstants: "#" // Opt-in to get magic imported globals | ||
} | ||
|
||
fetch("log-concat.wasm") | ||
.then(response => response.arrayBuffer()) | ||
.then(bytes => WebAssembly.compile(bytes, compileOptions)) | ||
.then(module => WebAssembly.instantiate(module, importObject)) | ||
.then(instance => instance.exports.main()); | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>WebAssembly JS String Builtins example</title> | ||
|
||
<script defer src="index.js"></script> | ||
</head> | ||
|
||
<body> | ||
<p>My template</p> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const importObject = { // Regular import | ||
m: { | ||
log: console.log | ||
} | ||
}; | ||
|
||
const compileOptions = { | ||
builtins: ["js-string"], // Opt-in to get magic imported functions | ||
importedStringConstants: "#" // Opt-in to get magic imported globals | ||
} | ||
|
||
WebAssembly.instantiateStreaming(fetch("log-concat.wasm"), importObject, compileOptions) | ||
.then(result => result.instance.exports.main()); | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>WebAssembly JS String Builtins example</title> | ||
|
||
<script defer src="index.js"></script> | ||
</head> | ||
|
||
<body> | ||
<p>My template</p> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const importObject = { // Regular import | ||
m: { | ||
log: console.log | ||
} | ||
}; | ||
|
||
const compileOptions = { | ||
builtins: ["js-string"], // Opt-in to get magic imported functions | ||
importedStringConstants: "#" // Opt-in to get magic imported globals | ||
} | ||
|
||
fetch("log-concat.wasm") | ||
.then(response => response.arrayBuffer()) | ||
.then(bytes => WebAssembly.instantiate(bytes, importObject, compileOptions)) | ||
.then(result => result.instance.exports.main()); | ||
|
||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(module | ||
(global $h (import "#" "hello ") externref) | ||
(global $w (import "#" "world!") externref) | ||
(func $concat (import "wasm:js-string" "concat") | ||
(param externref externref) (result (ref extern))) | ||
(func $log (import "m" "log") (param externref)) | ||
(func (export "main") | ||
(call $log (call $concat (global.get $h) (global.get $w)))) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>WebAssembly JS String Builtins example</title> | ||
|
||
<script defer src="index.js"></script> | ||
</head> | ||
|
||
<body> | ||
<p>My template</p> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const importObject = { // Regular import | ||
m: { | ||
log: console.log | ||
} | ||
}; | ||
|
||
const compileOptions = { | ||
builtins: ["js-string"], // Opt-in to get magic imported functions | ||
importedStringConstants: "#" // Opt-in to get magic imported globals | ||
} | ||
|
||
fetch("log-concat.wasm") | ||
.then(response => response.arrayBuffer()) | ||
.then(bytes => { | ||
const module = new WebAssembly.Module(bytes, compileOptions); | ||
WebAssembly.instantiate(module, importObject) | ||
.then(instance => instance.exports.main()); | ||
}) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>WebAssembly JS String Builtins example</title> | ||
|
||
<script defer src="index.js"></script> | ||
</head> | ||
|
||
<body> | ||
<p>My template</p> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const compileOptions = { | ||
builtins: ["js-string"], // Opt-in to get magic imported functions | ||
importedStringConstants: "#" // Opt-in to get magic imported globals | ||
} | ||
|
||
fetch("log-concat.wasm") | ||
.then(response => response.arrayBuffer()) | ||
.then(bytes => WebAssembly.validate(bytes, compileOptions)) | ||
.then(result => console.log(`wasm module valid: ${result}`)); | ||
|
||
|
Binary file not shown.