Skip to content

Commit

Permalink
feat: add test for .wasm files
Browse files Browse the repository at this point in the history
A potential gotcha is MIME type. See
deltachat/deltachat-android#2635

Closes #20
  • Loading branch information
WofWca committed Aug 13, 2023
1 parent 72adcdc commit ab6c5e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Binary file added js/call-with-42.wasm
Binary file not shown.
34 changes: 28 additions & 6 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,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;
Expand Down

0 comments on commit ab6c5e3

Please sign in to comment.