1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/* Copyright 2021 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ./test/core/throw.wast
// ./test/core/throw.wast:3
let $0 = instantiate(`(module
(tag $$e0)
(tag $$e-i32 (param i32))
(tag $$e-f32 (param f32))
(tag $$e-i64 (param i64))
(tag $$e-f64 (param f64))
(tag $$e-i32-i32 (param i32 i32))
(func $$throw-if (export "throw-if") (param i32) (result i32)
(local.get 0)
(i32.const 0) (if (i32.ne) (then (throw $$e0)))
(i32.const 0)
)
(func (export "throw-param-f32") (param f32) (local.get 0) (throw $$e-f32))
(func (export "throw-param-i64") (param i64) (local.get 0) (throw $$e-i64))
(func (export "throw-param-f64") (param f64) (local.get 0) (throw $$e-f64))
(func $$throw-1-2 (i32.const 1) (i32.const 2) (throw $$e-i32-i32))
(func (export "test-throw-1-2")
(try
(do (call $$throw-1-2))
(catch $$e-i32-i32
(i32.const 2)
(if (i32.ne) (then (unreachable)))
(i32.const 1)
(if (i32.ne) (then (unreachable)))
)
)
)
)`);
// ./test/core/throw.wast:37
assert_return(() => invoke($0, `throw-if`, [0]), [value("i32", 0)]);
// ./test/core/throw.wast:38
assert_exception(() => invoke($0, `throw-if`, [10]));
// ./test/core/throw.wast:39
assert_exception(() => invoke($0, `throw-if`, [-1]));
// ./test/core/throw.wast:41
assert_exception(() => invoke($0, `throw-param-f32`, [value("f32", 5)]));
// ./test/core/throw.wast:42
assert_exception(() => invoke($0, `throw-param-i64`, [5n]));
// ./test/core/throw.wast:43
assert_exception(() => invoke($0, `throw-param-f64`, [value("f64", 5)]));
// ./test/core/throw.wast:45
assert_return(() => invoke($0, `test-throw-1-2`, []), []);
// ./test/core/throw.wast:47
assert_invalid(() => instantiate(`(module (func (throw 0)))`), `unknown tag 0`);
// ./test/core/throw.wast:48
assert_invalid(
() => instantiate(`(module (tag (param i32)) (func (throw 0)))`),
`type mismatch: instruction requires [i32] but stack has []`,
);
// ./test/core/throw.wast:50
assert_invalid(
() => instantiate(`(module (tag (param i32)) (func (i64.const 5) (throw 0)))`),
`type mismatch: instruction requires [i32] but stack has [i64]`,
);
|