Skip to content

Instantly share code, notes, and snippets.

@DanielRapp
Created January 14, 2022 22:55
Show Gist options
  • Save DanielRapp/f798e3e0f21d640f4e8fe3dce8c5acc0 to your computer and use it in GitHub Desktop.
Save DanielRapp/f798e3e0f21d640f4e8fe3dce8c5acc0 to your computer and use it in GitHub Desktop.
minimal wasm
<html>
<body>
<script type="module">
const res = await fetch('main.wasm');
const buffer = await res.arrayBuffer();
const compiled = await WebAssembly.compile(buffer);
const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
const prog = new WebAssembly.Instance(compiled, {env: { memory }});
const wasm_func = prog.exports.double;
console.log( wasm_func(4) );
</script>
</body>
</html>
// emcc main.c -s WASM=1 -s SIDE_MODULE=1 -O1 -o main.wasm
int double(int x) {
return 2 * x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment