;; Test `call` operator (module ;; Auxiliary definitions (func $const-i32 (result i32) (i32.const 0x132)) (func $const-i64 (result i64) (i64.const 0x164)) (func $id-i32 (param i32) (result i32) (local.get 0)) (func $id-i64 (param i64) (result i64) (local.get 0)) (func $i32-i64 (param i32 i64) (result i64) (local.get 1)) ;; Typing (func (export "type-i32") (result i32) (call $const-i32)) (func (export "type-i64") (result i64) (call $const-i64)) (func (export "type-first-i32") (result i32) (call $id-i32 (i32.const 32))) (func (export "type-first-i64") (result i64) (call $id-i64 (i64.const 64))) (func (export "type-second-i64") (result i64) (call $i32-i64 (i32.const 32) (i64.const 64)) ) ;; Recursion (func $fac (export "fac") (param i64) (result i64) (if (result i64) (i64.eqz (local.get 0)) (then (i64.const 1)) (else (i64.mul (local.get 0) (call $fac (i64.sub (local.get 0) (i64.const 1))) ) ) ) ) (func $fac-acc (export "fac-acc") (param i64 i64) (result i64) (if (result i64) (i64.eqz (local.get 0)) (then (local.get 1)) (else (call $fac-acc (i64.sub (local.get 0) (i64.const 1)) (i64.mul (local.get 0) (local.get 1)) ) ) ) ) (func $fib (export "fib") (param i64) (result i64) (if (result i64) (i64.le_u (local.get 0) (i64.const 1)) (then (i64.const 1)) (else (i64.add (call $fib (i64.sub (local.get 0) (i64.const 2))) (call $fib (i64.sub (local.get 0) (i64.const 1))) ) ) ) ) (func $even (export "even") (param i64) (result i32) (if (result i32) (i64.eqz (local.get 0)) (then (i32.const 44)) (else (call $odd (i64.sub (local.get 0) (i64.const 1)))) ) ) (func $odd (export "odd") (param i64) (result i32) (if (result i32) (i64.eqz (local.get 0)) (then (i32.const 99)) (else (call $even (i64.sub (local.get 0) (i64.const 1)))) ) ) ;; Stack exhaustion ;; Implementations are required to have every call consume some abstract ;; resource towards exhausting some abstract finite limit, such that ;; infinitely recursive test cases reliably trap in finite time. This is ;; because otherwise applications could come to depend on it on those ;; implementations and be incompatible with implementations that don't do ;; it (or don't do it under the same circumstances). (func $runaway (export "runaway") (call $runaway)) (func $mutual-runaway1 (export "mutual-runaway") (call $mutual-runaway2)) (func $mutual-runaway2 (call $mutual-runaway1)) ;; As parameter of control constructs and instructions (memory 1) (func (export "as-if-condition") (result i32) (if (result i32) (call $const-i32) (then (i32.const 1)) (else (i32.const 2))) ) (func (export "as-br_if-first") (result i32) (block (result i32) (br_if 0 (call $const-i32) (i32.const 2))) ) (func (export "as-br_if-last") (result i32) (block (result i32) (br_if 0 (i32.const 2) (call $const-i32))) ) (func (export "as-return-value") (result i32) (call $const-i32) (return) ) (func (export "as-br-value") (result i32) (block (result i32) (br 0 (call $const-i32))) ) (func (export "as-local.set-value") (result i32) (local i32) (local.set 0 (call $const-i32)) (local.get 0) ) (func $dummy (param i32) (result i32) (local.get 0)) (func (export "as-binary-left") (result i32) (block (result i32) (i32.add (call $dummy (i32.const 1)) (i32.const 10))) ) (func (export "as-binary-right") (result i32) (block (result i32) (i32.sub (i32.const 10) (call $dummy (i32.const 1)))) ) (func (export "as-test-operand") (result i32) (block (result i32) (i32.eqz (call $dummy (i32.const 1)))) ) (func (export "as-compare-left") (result i32) (block (result i32) (i32.le_u (call $dummy (i32.const 1)) (i32.const 10))) ) (func (export "as-compare-right") (result i32) (block (result i32) (i32.ne (i32.const 10) (call $dummy (i32.const 1)))) ) ) (assert_return (invoke "type-i32") (i32.const 0x132)) (assert_return (invoke "type-i64") (i64.const 0x164)) (assert_return (invoke "type-first-i32") (i32.const 32)) (assert_return (invoke "type-first-i64") (i64.const 64)) (assert_return (invoke "type-second-i64") (i64.const 64)) (assert_return (invoke "fac" (i64.const 0)) (i64.const 1)) (assert_return (invoke "fac" (i64.const 1)) (i64.const 1)) (assert_return (invoke "fac" (i64.const 5)) (i64.const 120)) (assert_return (invoke "fac" (i64.const 25)) (i64.const 7034535277573963776)) (assert_return (invoke "fac-acc" (i64.const 0) (i64.const 1)) (i64.const 1)) (assert_return (invoke "fac-acc" (i64.const 1) (i64.const 1)) (i64.const 1)) (assert_return (invoke "fac-acc" (i64.const 5) (i64.const 1)) (i64.const 120)) (assert_return (invoke "fac-acc" (i64.const 25) (i64.const 1)) (i64.const 7034535277573963776) ) (assert_return (invoke "fib" (i64.const 0)) (i64.const 1)) (assert_return (invoke "fib" (i64.const 1)) (i64.const 1)) (assert_return (invoke "fib" (i64.const 2)) (i64.const 2)) (assert_return (invoke "fib" (i64.const 5)) (i64.const 8)) (assert_return (invoke "fib" (i64.const 20)) (i64.const 10946)) (assert_return (invoke "even" (i64.const 0)) (i32.const 44)) (assert_return (invoke "even" (i64.const 1)) (i32.const 99)) (assert_return (invoke "even" (i64.const 100)) (i32.const 44)) (assert_return (invoke "even" (i64.const 77)) (i32.const 99)) (assert_return (invoke "odd" (i64.const 0)) (i32.const 99)) (assert_return (invoke "odd" (i64.const 1)) (i32.const 44)) (assert_return (invoke "odd" (i64.const 200)) (i32.const 99)) (assert_return (invoke "odd" (i64.const 77)) (i32.const 44)) (assert_return (invoke "as-if-condition") (i32.const 1)) (assert_return (invoke "as-br_if-first") (i32.const 0x132)) (assert_return (invoke "as-br_if-last") (i32.const 2)) (assert_return (invoke "as-return-value") (i32.const 0x132)) (assert_return (invoke "as-br-value") (i32.const 0x132)) (assert_return (invoke "as-local.set-value") (i32.const 0x132)) (assert_return (invoke "as-binary-left") (i32.const 11)) (assert_return (invoke "as-binary-right") (i32.const 9)) (assert_return (invoke "as-test-operand") (i32.const 0)) (assert_return (invoke "as-compare-left") (i32.const 1)) (assert_return (invoke "as-compare-right") (i32.const 1))