Created
December 29, 2017 11:09
-
-
Save saki007ster/34549cedc53b5579b79a165f8e666fbd to your computer and use it in GitHub Desktop.
helper function for web assembly fetch and instantiate
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
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