Skip to content

Instantly share code, notes, and snippets.

@jackyef
Last active July 16, 2019 03:44
Show Gist options
  • Save jackyef/f8b1cc7c885adcd71c2cf83a7b49a30b to your computer and use it in GitHub Desktop.
Save jackyef/f8b1cc7c885adcd71c2cf83a7b49a30b to your computer and use it in GitHub Desktop.
wasm generated binding
function init(module) {
let result;
const imports = {};
if (module instanceof URL || typeof module === 'string' || module instanceof Request) {
const response = fetch(module);
if (typeof WebAssembly.instantiateStreaming === 'function') {
result = WebAssembly.instantiateStreaming(response, imports)
.catch(e => {
console.warn("`WebAssembly.instantiateStreaming` failed. Assuming this is because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
return response
.then(r => r.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports));
});
} else {
result = response
.then(r => r.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports));
}
} else {
result = WebAssembly.instantiate(module, imports)
.then(result => {
if (result instanceof WebAssembly.Instance) {
return { instance: result, module };
} else {
return result;
}
});
}
return result.then(({instance, module}) => {
wasm = instance.exports;
init.__wbindgen_wasm_module = module;
return wasm;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment