Debian Codegraph @

js/src/jit-test/tests/wasm/tail-calls/litmus9.js

raw
1 // Tail-call litmus test with multiple results
2 //
3 // Mutually recursive functions implement a multi-entry loop using indirect tail
4 // calls, with exception handling.
5 //
6 // The functions do not have the same signatures, so if all arguments are stack
7 // arguments then these use different amounts of stack space.
8 //
9 // The "even" function will throw when the input is zero, so when an exception
10 // bubbles out of return_call it must not be caught by the exception handler in
11 // "odd", but must instead be delegated to the caller, which is JS code.
12
13 var ins = wasmEvalText(`
14 (module
15 (table 2 2 funcref)
16 (elem (i32.const 0) $even $odd)
17 (type $even_t (func (param i32) (result i32 i32 i32)))
18 (type $odd_t (func (param i32 i32) (result i32 i32 i32)))
19 (tag $t)
20
21 (func $odd (export "odd") (param $n i32) (param $dummy i32) (result i32 i32 i32)
22 try (result i32 i32 i32)
23 (if (result i32 i32 i32) (i32.eqz (local.get $n))
24 (then (return (i32.const 0) (i32.const 32769) (i32.const -37)))
25 (else (return_call_indirect (type $even_t) (i32.sub (local.get $n) (i32.const 1))
26 (i32.const 0))))
27 catch_all
28 unreachable
29 end)
30
31 (func $even (export "even") (param $n i32) (result i32 i32 i32)
32 (if (result i32 i32 i32) (i32.eqz (local.get $n))
33 (then (throw $t))
34 (else (return_call_indirect (type $odd_t) (i32.sub (local.get $n) (i32.const 1)) (i32.const 33)
35 (i32.const 1)))))
36 )
37 `);
38
39 assertErrorMessage(() => ins.exports.even(TailCallIterations), WebAssembly.Exception, /.*/);
40 assertSame(ins.exports.odd(TailCallIterations, 33), [0, 32769, -37]);