From ab6c5e323bdf41b53deffba09c1f22724d7c195e Mon Sep 17 00:00:00 2001 From: WofWca Date: Sun, 13 Aug 2023 16:09:48 +0400 Subject: [PATCH 1/2] feat: add test for `.wasm` files A potential gotcha is MIME type. See https://github.com/deltachat/deltachat-android/issues/2635 Closes #20 --- js/call-with-42.wasm | Bin 0 -> 78 bytes js/wasm.js | 34 ++++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 js/call-with-42.wasm diff --git a/js/call-with-42.wasm b/js/call-with-42.wasm new file mode 100644 index 0000000000000000000000000000000000000000..a2e9ad6acbc70204dba743dd5064f0afab5a6e38 GIT binary patch literal 78 zcmZQbEY4+QU|?Y6U`k-DXGmaRV3K5H&&(~zFDfbKh0v)f@oA-b$qWq4OpJ`|f{eVW U6(DJtFe4WSBO8OGmH-1c0JS<1egFUf literal 0 HcmV?d00001 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; From 30aff554c8dceddbfe6c350c5d4ca561c25549ee Mon Sep 17 00:00:00 2001 From: WofWca Date: Sun, 13 Aug 2023 16:11:55 +0400 Subject: [PATCH 2/2] improvement: print Wasm test errors in console --- js/wasm.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/wasm.js b/js/wasm.js index dbb3d9d..def7015 100644 --- a/js/wasm.js +++ b/js/wasm.js @@ -37,7 +37,9 @@ window.addEventListener("load", async () => { return true; } - } catch (e) {} + } catch (e) { + console.error(e); + } return false; })();