Created
February 1, 2023 15:48
-
-
Save surma/f4e7097dcbd635415215201969dd3c3b to your computer and use it in GitHub Desktop.
Wasm GC
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
*.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
all: module.wasm | |
.PHONEY: run | |
%.wasm: %.wat | |
wasm-as --enable-gc --enable-reference-types -o $@ $< | |
run: | |
v8 --experimental-wasm-gc --module ./runner.js |
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
(module | |
(type $Point | |
(struct | |
(field $x f64) | |
(field $y f64))) | |
(func $create (export "create_point") (param $x f64) (param $y f64) (result (ref $Point)) | |
(struct.new $Point (local.get $x) (local.get $y)) | |
) | |
(func $length (export "length") (param $p (ref $Point)) (result f64) | |
(f64.add | |
(f64.mul | |
(struct.get $Point $x (local.get $p)) | |
(struct.get $Point $x (local.get $p))) | |
(f64.mul | |
(struct.get $Point $y (local.get $p)) | |
(struct.get $Point $y (local.get $p)))) | |
) | |
) |
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
async function main() { | |
const m = readbuffer("module.wasm"); | |
const {instance} = await WebAssembly.instantiate(m, {}); | |
const p = instance.exports.create_point(40, 2); | |
console.log("Length =", instance.exports.length(p)); | |
} | |
main().catch(err => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment