|
| 1 | +;; Test throw instruction. |
| 2 | + |
| 3 | +(module |
| 4 | + (tag $e0) |
| 5 | + (tag $e-i32 (param i32)) |
| 6 | + (tag $e-f32 (param f32)) |
| 7 | + (tag $e-i64 (param i64)) |
| 8 | + (tag $e-f64 (param f64)) |
| 9 | + (tag $e-i32-i32 (param i32 i32)) |
| 10 | + |
| 11 | + (func $throw-if (export "throw-if") (param i32) (result i32) |
| 12 | + (local.get 0) |
| 13 | + (i32.const 0) (if (i32.ne) (then (throw $e0))) |
| 14 | + (i32.const 0) |
| 15 | + ) |
| 16 | + |
| 17 | + (func (export "throw-param-f32") (param f32) (local.get 0) (throw $e-f32)) |
| 18 | + |
| 19 | + (func (export "throw-param-i64") (param i64) (local.get 0) (throw $e-i64)) |
| 20 | + |
| 21 | + (func (export "throw-param-f64") (param f64) (local.get 0) (throw $e-f64)) |
| 22 | + |
| 23 | + (func $throw-1-2 (i32.const 1) (i32.const 2) (throw $e-i32-i32)) |
| 24 | + (func (export "test-throw-1-2") |
| 25 | + try |
| 26 | + call $throw-1-2 |
| 27 | + catch $e-i32-i32 |
| 28 | + i32.const 2 |
| 29 | + i32.ne |
| 30 | + if |
| 31 | + unreachable |
| 32 | + end |
| 33 | + i32.const 1 |
| 34 | + i32.ne |
| 35 | + if |
| 36 | + unreachable |
| 37 | + end |
| 38 | + end |
| 39 | + ) |
| 40 | +) |
| 41 | + |
| 42 | +(assert_return (invoke "throw-if" (i32.const 0)) (i32.const 0)) |
| 43 | +(assert_exception (invoke "throw-if" (i32.const 10))) |
| 44 | +(assert_exception (invoke "throw-if" (i32.const -1))) |
| 45 | + |
| 46 | +(assert_exception (invoke "throw-param-f32" (f32.const 5.0))) |
| 47 | +(assert_exception (invoke "throw-param-i64" (i64.const 5))) |
| 48 | +(assert_exception (invoke "throw-param-f64" (f64.const 5.0))) |
| 49 | + |
| 50 | +(assert_return (invoke "test-throw-1-2")) |
| 51 | + |
| 52 | +(assert_invalid (module (func (throw 0))) "unknown tag 0") |
| 53 | +(assert_invalid (module (tag (param i32)) (func (throw 0))) |
| 54 | + "type mismatch: instruction requires [i32] but stack has []") |
| 55 | +(assert_invalid (module (tag (param i32)) (func (i64.const 5) (throw 0))) |
| 56 | + "type mismatch: instruction requires [i32] but stack has [i64]") |
0 commit comments