diff --git a/js/call-with-42.wasm b/js/call-with-42.wasm new file mode 100644 index 0000000..a2e9ad6 Binary files /dev/null and b/js/call-with-42.wasm differ diff --git a/js/wasm.js b/js/wasm.js index 38db7d7..dbb3d9d 100644 --- a/js/wasm.js +++ b/js/wasm.js @@ -1,6 +1,6 @@ -window.addEventListener("load", () => { +window.addEventListener("load", async () => { let container = h("div", { class: "container" }); - const supported = (() => { + const supported = await (async () => { // thanks to https://stackoverflow.com/a/47880734 for this check try { if ( @@ -10,10 +10,32 @@ window.addEventListener("load", () => { const module = new WebAssembly.Module( Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00) ); - if (module instanceof WebAssembly.Module) - return ( - new WebAssembly.Instance(module) instanceof WebAssembly.Instance - ); + if (!(module instanceof WebAssembly.Module)) { + return false; + } + if (!(new WebAssembly.Instance(module) instanceof WebAssembly.Instance)) { + return false; + } + + let wasmFileWorks = false; + // Example from https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API + const importObject = { + imports: { + imported_func: (arg) => { + wasmFileWorks = arg === 42; + }, + }, + }; + const obj = await WebAssembly.instantiateStreaming( + fetch("/js/call-with-42.wasm"), + importObject + ); + obj.instance.exports.exported_func(); + if (!wasmFileWorks) { + return false; + } + + return true; } } catch (e) {} return false;