Debian Codegraph @

tests/cli/legacy-exceptions/try_catch.wast

raw
1 ;; RUN: wast --assert default,no-test-folded --snapshot tests/snapshots % -f legacy-exceptions
2
3 ;; Test try-catch blocks.
4
5 (module
6 (tag $e0 (export "e0"))
7 (func (export "throw") (throw $e0))
8 )
9
10 (register "test")
11
12 (module
13 (tag $imported-e0 (import "test" "e0"))
14 (func $imported-throw (import "test" "throw"))
15 (tag $e0)
16 (tag $e1)
17 (tag $e2)
18 (tag $e-i32 (param i32))
19 (tag $e-f32 (param f32))
20 (tag $e-i64 (param i64))
21 (tag $e-f64 (param f64))
22
23 (func $throw-if (param i32) (result i32)
24 (local.get 0)
25 (i32.const 0) (if (i32.ne) (then (throw $e0)))
26 (i32.const 0)
27 )
28
29 (func (export "empty-catch")
30 try
31 catch $e0
32 end
33 )
34
35 (func (export "simple-throw-catch") (param i32) (result i32)
36 try (result i32)
37 local.get 0
38 i32.eqz
39 if
40 throw 1
41 end
42 i32.const 42
43 catch 1
44 i32.const 23
45 end
46 )
47
48 (func (export "unreachable-not-caught") try unreachable catch_all end)
49
50 (func $div (param i32 i32) (result i32)
51 (local.get 0) (local.get 1) (i32.div_u)
52 )
53 (func (export "trap-in-callee") (param i32 i32) (result i32)
54 try (result i32)
55 local.get 0
56 local.get 1
57 call 5
58 catch_all
59 i32.const 11
60 end
61 )
62
63 (func (export "catch-complex-1") (param i32) (result i32)
64 try (result i32)
65 try (result i32)
66 local.get 0
67 i32.eqz
68 if
69 throw 1
70 else
71 local.get 0
72 i32.const 1
73 i32.eq
74 if
75 throw 2
76 else
77 throw 3
78 end
79 end
80 i32.const 2
81 catch 1
82 i32.const 3
83 end
84 catch 2
85 i32.const 4
86 end
87 )
88
89 (func (export "catch-complex-2") (param i32) (result i32)
90 try (result i32)
91 local.get 0
92 i32.eqz
93 if
94 throw 1
95 else
96 local.get 0
97 i32.const 1
98 i32.eq
99 if
100 throw 2
101 else
102 throw 3
103 end
104 end
105 i32.const 2
106 catch 1
107 i32.const 3
108 catch 2
109 i32.const 4
110 end
111 )
112
113 (func (export "throw-catch-param-i32") (param i32) (result i32)
114 try (result i32)
115 local.get 0
116 throw 4
117 i32.const 2
118 catch 4
119 return
120 end
121 )
122
123 (func (export "throw-catch-param-f32") (param f32) (result f32)
124 try (result f32)
125 local.get 0
126 throw 5
127 f32.const 0
128 catch 5
129 return
130 end
131 )
132
133 (func (export "throw-catch-param-i64") (param i64) (result i64)
134 try (result i64)
135 local.get 0
136 throw 6
137 i64.const 2
138 catch 6
139 return
140 end
141 )
142
143 (func (export "throw-catch-param-f64") (param f64) (result f64)
144 try (result f64)
145 local.get 0
146 throw 7
147 f64.const 0
148 catch 7
149 return
150 end
151 )
152
153 (func $throw-param-i32 (param i32) (local.get 0) (throw $e-i32))
154 (func (export "catch-param-i32") (param i32) (result i32)
155 try (result i32)
156 i32.const 0
157 local.get 0
158 call 13
159 catch 4
160 end
161 )
162
163 (func (export "catch-imported") (result i32)
164 try (result i32)
165 i32.const 1
166 call 0
167 catch 0
168 i32.const 2
169 end
170 )
171
172 (func (export "catchless-try") (param i32) (result i32)
173 try (result i32)
174 try (result i32)
175 local.get 0
176 call 1
177 end
178 catch 1
179 i32.const 1
180 end
181 )
182
183 (func $throw-void (throw $e0))
184 (func (export "return-call-in-try-catch")
185 try
186 return_call 17
187 catch 1
188 end
189 )
190
191 (table funcref (elem $throw-void))
192 (func (export "return-call-indirect-in-try-catch")
193 try
194 i32.const 0
195 return_call_indirect (type 0)
196 catch 1
197 end
198 )
199
200 (func (export "break-try-catch")
201 try
202 br 0
203 catch 1
204 end
205 )
206
207 (func (export "break-try-catch_all")
208 try
209 br 0
210 catch_all
211 end
212 )
213 )
214
215 (assert_return (invoke "empty-catch"))
216
217 (assert_return (invoke "simple-throw-catch" (i32.const 0)) (i32.const 23))
218 (assert_return (invoke "simple-throw-catch" (i32.const 1)) (i32.const 42))
219
220 (assert_trap (invoke "unreachable-not-caught") "unreachable")
221
222 (assert_return (invoke "trap-in-callee" (i32.const 7) (i32.const 2)) (i32.const 3))
223 (assert_trap (invoke "trap-in-callee" (i32.const 1) (i32.const 0)) "integer divide by zero")
224
225 (assert_return (invoke "catch-complex-1" (i32.const 0)) (i32.const 3))
226 (assert_return (invoke "catch-complex-1" (i32.const 1)) (i32.const 4))
227 (assert_exception (invoke "catch-complex-1" (i32.const 2)))
228
229 (assert_return (invoke "catch-complex-2" (i32.const 0)) (i32.const 3))
230 (assert_return (invoke "catch-complex-2" (i32.const 1)) (i32.const 4))
231 (assert_exception (invoke "catch-complex-2" (i32.const 2)))
232
233 (assert_return (invoke "throw-catch-param-i32" (i32.const 0)) (i32.const 0))
234 (assert_return (invoke "throw-catch-param-i32" (i32.const 1)) (i32.const 1))
235 (assert_return (invoke "throw-catch-param-i32" (i32.const 10)) (i32.const 10))
236
237 (assert_return (invoke "throw-catch-param-f32" (f32.const 5.0)) (f32.const 5.0))
238 (assert_return (invoke "throw-catch-param-f32" (f32.const 10.5)) (f32.const 10.5))
239
240 (assert_return (invoke "throw-catch-param-i64" (i64.const 5)) (i64.const 5))
241 (assert_return (invoke "throw-catch-param-i64" (i64.const 0)) (i64.const 0))
242 (assert_return (invoke "throw-catch-param-i64" (i64.const -1)) (i64.const -1))
243
244 (assert_return (invoke "throw-catch-param-f64" (f64.const 5.0)) (f64.const 5.0))
245 (assert_return (invoke "throw-catch-param-f64" (f64.const 10.5)) (f64.const 10.5))
246
247 (assert_return (invoke "catch-param-i32" (i32.const 5)) (i32.const 5))
248
249 (assert_return (invoke "catch-imported") (i32.const 2))
250
251 (assert_return (invoke "catchless-try" (i32.const 0)) (i32.const 0))
252 (assert_return (invoke "catchless-try" (i32.const 1)) (i32.const 1))
253
254 (assert_exception (invoke "return-call-in-try-catch"))
255 (assert_exception (invoke "return-call-indirect-in-try-catch"))
256
257 (assert_return (invoke "break-try-catch"))
258 (assert_return (invoke "break-try-catch_all"))
259
260 (module
261 (func $imported-throw (import "test" "throw"))
262 (tag $e0)
263
264 (func (export "imported-mismatch") (result i32)
265 try (result i32) ;; label = @1
266 try (result i32) ;; label = @2
267 i32.const 1
268 call 0
269 catch 0
270 i32.const 2
271 end
272 catch_all
273 i32.const 3
274 end
275 )
276 )
277
278 (assert_return (invoke "imported-mismatch") (i32.const 3))
279
280 ;; Ignore per https://github.com/bytecodealliance/wasm-tools/issues/1671
281 ;;
282 ;; (assert_malformed
283 ;; (module quote "(module (func (catch_all)))")
284 ;; "unexpected token"
285 ;; )
286
287 ;; (assert_malformed
288 ;; (module quote "(module (tag $e) (func (catch $e)))")
289 ;; "unexpected token"
290 ;; )
291
292 ;; (assert_malformed
293 ;; (module quote
294 ;; "(module (func try catch_all catch_all end))"
295 ;; )
296 ;; "unexpected token"
297 ;; )
298
299 (assert_invalid (module (func (result i32) try (result i32) end))
300 "type mismatch")
301 (assert_invalid (module (func (result i32) try (result i32) i64.const 42 end))
302 "type mismatch")
303 (assert_invalid (module (tag) (func try catch 0 i32.const 42 end))
304 "type mismatch")
305 (assert_invalid (module
306 (tag (param i64))
307 (func (result i32)
308 try (result i32) i32.const 42 catch 0 end))
309 "type mismatch")
310 (assert_invalid (module (func try catch_all i32.const 32 end))
311 "type mismatch")