Skip to content

Instantly share code, notes, and snippets.

@WA9ACE
Created April 22, 2017 01:16
Show Gist options
  • Save WA9ACE/3076325cf95595ff0cb345795f3fd95c to your computer and use it in GitHub Desktop.
Save WA9ACE/3076325cf95595ff0cb345795f3fd95c to your computer and use it in GitHub Desktop.
Hello World WASM bootstrap
document.addEventListener("DOMContentLoaded", main)
function log(offset, length) {
const bytes = new Uint8Array(memory.buffer, offset, length)
const string = new TextDecoder('utf8').decode(bytes)
console.log(string)
}
var memory = new WebAssembly.Memory({ initial : 20 });
const exposed = {
stdlib: { print: log },
js: { mem: memory }
}
function main(event) {
fetch('hello.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, exposed)
).then(result =>
result.instance.exports.main()
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment