Skip to content

Commit

Permalink
Update Node status for tail calls (#370)
Browse files Browse the repository at this point in the history
Using [Binaryen
v116](https://github.com/WebAssembly/binaryen/tree/version_116),
assemble `fac.wat` (adapted from
[here](https://github.com/WebAssembly/tail-call/blob/6f44ca27af411a0f6bc4e07520807d7adfc0de88/proposals/tail-call/Overview.md#examples))

```wat
(module
  (func (export "fac") (param $x i64) (result i64)
    (return_call $fac-aux (local.get $x) (i64.const 1)))
  (func $fac-aux (param $x i64) (param $r i64) (result i64)
    (if (i64.eqz (local.get $x))
      (then (return (local.get $r)))
      (else
        (return_call $fac-aux
          (i64.sub (local.get $x) (i64.const 1))
          (i64.mul (local.get $x) (local.get $r)))))))
```

via this command:

```sh
wasm-as --enable-tail-call fac.wat
```

Then try to run `fac.mjs`

```js
import * as fs from "fs/promises";

const bytes = await fs.readFile("fac.wasm");
const mod = await WebAssembly.compile(bytes);
const { fac } = (await WebAssembly.instantiate(mod)).exports;
console.log(fac(5n));
```

via this command:

```sh
node fac.mjs
```

Node v20.0.0 or newer prints `120n` as expected, while Node v19.9.0
instead gives this error message:

```
node:internal/process/esm_loader:100
    internalBinding('errors').triggerUncaughtException(
                              ^

[CompileError: WebAssembly.compile(): Compiling function #0 failed: Invalid opcode 0x12 (enable with --experimental-wasm-return_call) @+45]

Node.js v19.9.0
```
  • Loading branch information
samestep authored Feb 15, 2024
1 parent ba3deae commit b4f2907
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion features.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
"saturatedFloatToInt": "12.5",
"signExtensions": "12.0",
"simd": "16.4",
"tailCall": ["flag", "Requires flag `--experimental-wasm-return-call`"],
"tailCall": "20.0",
"threads": "16.4",
"typeReflection": ["flag", "Requires flag `--experimental-wasm-type-reflection`"]
}
Expand Down

0 comments on commit b4f2907

Please sign in to comment.