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!
This commit is contained in:
Tim Ledbetter 2024-04-10 18:56:14 +01:00 committed by Ali Mohammad Pur
parent da60529ab8
commit 8892c25520

View file

@ -4,10 +4,10 @@
const bytes = new Uint8Array([ const bytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60,
0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03,
0x66, 0x69, 0x62, 0x00, 0x00, 0x0a, 0x1f, 0x01, 0x1d, 0x00, 0x20, 0x00, 0x66, 0x69, 0x62, 0x00, 0x00, 0x0a, 0x29, 0x01, 0x27, 0x00, 0x20, 0x00,
0x41, 0x02, 0x48, 0x04, 0x40, 0x41, 0x01, 0x0f, 0x0b, 0x20, 0x00, 0x41, 0x41, 0x00, 0x4c, 0x04, 0x40, 0x41, 0x00, 0x0f, 0x0b, 0x20, 0x00, 0x41,
0x02, 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x10, 0x00, 0x6a, 0x01, 0x46, 0x04, 0x40, 0x41, 0x01, 0x0f, 0x0b, 0x20, 0x00, 0x41, 0x02,
0x0f, 0x0b 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x10, 0x00, 0x6a, 0x0b
]); ]);
WebAssembly.compile(bytes) WebAssembly.compile(bytes)
.then(module => WebAssembly.instantiate(module)) .then(module => WebAssembly.instantiate(module))