Skip to content

Instantly share code, notes, and snippets.

@saki007ster
Created December 29, 2017 11:09
Show Gist options
  • Save saki007ster/34549cedc53b5579b79a165f8e666fbd to your computer and use it in GitHub Desktop.
Save saki007ster/34549cedc53b5579b79a165f8e666fbd to your computer and use it in GitHub Desktop.
helper function for web assembly fetch and instantiate
function fetchAndInstantiateWasm (url, imports) {
return fetch(url)
.then(res => {
if (res.ok)
return res.arrayBuffer();
throw new Error('Unable to fetch Web Assembly file ${url}.');
})
.then(bytes => WebAssembly.compile(bytes))
.then(module => WebAssembly.instantiate(module, imports || {}))
.then(instance => instance.exports);
}
fetchAndInstantiateWasm('./program.wasm')
.then(m => {
window.getSqrt = m.getSqrt;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment