From 81fdbeb7fa4d8a80d028a988c353c3fa16d4953b Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 23:06:51 -0700 Subject: wasm lol --- entries/adirar111/wasm/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 entries/adirar111/wasm/index.js (limited to 'entries/adirar111/wasm/index.js') diff --git a/entries/adirar111/wasm/index.js b/entries/adirar111/wasm/index.js new file mode 100644 index 0000000..43ee9a6 --- /dev/null +++ b/entries/adirar111/wasm/index.js @@ -0,0 +1,21 @@ +const wasmBinaryBase64 = "AGFzbQEAAAABBgFgAX8BfwMCAQAHBwEDZmliAAAKRwFFAQR/IABBAkgEQCAADwtBACEBQQEhAkECIQMgAEEBaiEAA0AgAiEEIAIgAWohAiAEIQEgA0EBaiEDIAMgAEgNAAsgAg8LACgEbmFtZQEGAQADZmliAhkBAAUAAW4BBGxhc3QCA3N1bQMBaQQDdG1w" +const wasmBinaryDataURL = `data:application/wasm;base64,${wasmBinaryBase64}`; + +WebAssembly.instantiateStreaming(fetch(wasmBinaryDataURL)).then( + (wasmObj) => { + const {fib: fibWasm} = wasmObj.instance.exports + const fib = (event) => { + event.preventDefault() + const input = document.getElementById("input") + const result = document.getElementById("result") + const n = parseInt(input.value) + if (isNaN(n)) { + result.textContent = "that's not a number..." + return + } + result.textContent = fibWasm(n) + } + const button = document.getElementById("compute") + button.onclick = fib + } +); -- cgit v1.2.3-70-g09d2 From 5e2b3d53917bb6a39dbffd0db6888bacaa283536 Mon Sep 17 00:00:00 2001 From: Aymen Dirar Date: Sun, 23 Oct 2022 23:29:34 -0700 Subject: handle negative --- entries/adirar111/wasm/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'entries/adirar111/wasm/index.js') diff --git a/entries/adirar111/wasm/index.js b/entries/adirar111/wasm/index.js index 43ee9a6..667ff82 100644 --- a/entries/adirar111/wasm/index.js +++ b/entries/adirar111/wasm/index.js @@ -10,8 +10,10 @@ WebAssembly.instantiateStreaming(fetch(wasmBinaryDataURL)).then( const result = document.getElementById("result") const n = parseInt(input.value) if (isNaN(n)) { - result.textContent = "that's not a number..." - return + return result.textContent = "that wasn't very wasm of you" + } + if (n < 0) { + return result.textContent = "why are you being so negative" } result.textContent = fibWasm(n) } -- cgit v1.2.3-70-g09d2