Skip to content

Commit ec8980a

Browse files
authored
Merge pull request #22 from webxdc/wasm-file
feat: add test for .wasm files
2 parents 0fae4c2 + 803a608 commit ec8980a

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

js/call-with-42.wasm

78 Bytes
Binary file not shown.

js/wasm.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
window.addEventListener("load", () => {
1+
window.addEventListener("load", async () => {
22
let container = h("div", { class: "container" });
3-
const supported = (() => {
3+
const supported = await (async () => {
44
// thanks to https://stackoverflow.com/a/47880734 for this check
55
try {
66
if (
@@ -10,12 +10,36 @@ window.addEventListener("load", () => {
1010
const module = new WebAssembly.Module(
1111
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
1212
);
13-
if (module instanceof WebAssembly.Module)
14-
return (
15-
new WebAssembly.Instance(module) instanceof WebAssembly.Instance
16-
);
13+
if (!(module instanceof WebAssembly.Module)) {
14+
return false;
15+
}
16+
if (!(new WebAssembly.Instance(module) instanceof WebAssembly.Instance)) {
17+
return false;
18+
}
19+
20+
let wasmFileWorks = false;
21+
// Example from https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API
22+
const importObject = {
23+
imports: {
24+
imported_func: (arg) => {
25+
wasmFileWorks = arg === 42;
26+
},
27+
},
28+
};
29+
const obj = await WebAssembly.instantiateStreaming(
30+
fetch("/js/call-with-42.wasm"),
31+
importObject
32+
);
33+
obj.instance.exports.exported_func();
34+
if (!wasmFileWorks) {
35+
return false;
36+
}
37+
38+
return true;
1739
}
18-
} catch (e) {}
40+
} catch (e) {
41+
console.error(e);
42+
}
1943
return false;
2044
})();
2145

0 commit comments

Comments
 (0)