Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added js/call-with-42.wasm
Binary file not shown.
38 changes: 31 additions & 7 deletions js/wasm.js
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -10,12 +10,36 @@ 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) {}
} catch (e) {
console.error(e);
}
return false;
})();

Expand Down