From 8892c25520c6ff728e8870f30af068bfd369fd5f Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Wed, 10 Apr 2024 18:56:14 +0100 Subject: [PATCH] Base: Return the correct value for `fib(0)` in Wasm example Previously, using `wasm-decompile` to decompile the Wasm bytecode on the `wasm.html` example page gave this output: ``` export function fib(a:int):int { if (a < 2) { return 1 } return fib(a - 2) + fib(a - 1); } ``` With this change the bytecode now decompiles to: ``` export function fib(a:int):int { if (a <= 0) { return 0 } if (a == 1) { return 1 } return fib(a - 2) + fib(a - 1); } ``` This means that the example page now prints the correct answer of 55 to the console for `fib(10)`. Previously, `fib(10)` returned 89. I also used `wasm-opt -Oz`, which removed an unnecessary `return` instruction, saving 1 byte! --- Base/res/html/misc/wasm.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Base/res/html/misc/wasm.html b/Base/res/html/misc/wasm.html index 3177f9552e..ce0c358dc5 100644 --- a/Base/res/html/misc/wasm.html +++ b/Base/res/html/misc/wasm.html @@ -4,10 +4,10 @@ const bytes = new Uint8Array([ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, - 0x66, 0x69, 0x62, 0x00, 0x00, 0x0a, 0x1f, 0x01, 0x1d, 0x00, 0x20, 0x00, - 0x41, 0x02, 0x48, 0x04, 0x40, 0x41, 0x01, 0x0f, 0x0b, 0x20, 0x00, 0x41, - 0x02, 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x10, 0x00, 0x6a, - 0x0f, 0x0b + 0x66, 0x69, 0x62, 0x00, 0x00, 0x0a, 0x29, 0x01, 0x27, 0x00, 0x20, 0x00, + 0x41, 0x00, 0x4c, 0x04, 0x40, 0x41, 0x00, 0x0f, 0x0b, 0x20, 0x00, 0x41, + 0x01, 0x46, 0x04, 0x40, 0x41, 0x01, 0x0f, 0x0b, 0x20, 0x00, 0x41, 0x02, + 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x10, 0x00, 0x6a, 0x0b ]); WebAssembly.compile(bytes) .then(module => WebAssembly.instantiate(module))