Skip to content

Instantly share code, notes, and snippets.

@surma
Created February 1, 2023 15:48
Show Gist options
  • Save surma/f4e7097dcbd635415215201969dd3c3b to your computer and use it in GitHub Desktop.
Save surma/f4e7097dcbd635415215201969dd3c3b to your computer and use it in GitHub Desktop.
Wasm GC
all: module.wasm
.PHONEY: run
%.wasm: %.wat
wasm-as --enable-gc --enable-reference-types -o $@ $<
run:
v8 --experimental-wasm-gc --module ./runner.js
(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))))
)
)
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