Debian Codegraph @

internal/integration_test/spectest/exception-handling/testdata/try_table.wast

raw
1 ;; Test try-catch blocks.
2
3 (module
4 (tag $e0 (export "e0"))
5 (func (export "throw") (throw $e0))
6 )
7
8 (register "test")
9
10 (module
11 (tag $imported-e0 (import "test" "e0"))
12 (tag $imported-e0-alias (import "test" "e0"))
13 (func $imported-throw (import "test" "throw"))
14 (tag $e0)
15 (tag $e1)
16 (tag $e2)
17 (tag $e-i32 (param i32))
18 (tag $e-f32 (param f32))
19 (tag $e-i64 (param i64))
20 (tag $e-f64 (param f64))
21
22 (func $throw-if (param i32) (result i32)
23 (local.get 0)
24 (i32.const 0) (if (i32.ne) (then (throw $e0)))
25 (i32.const 0)
26 )
27
28 (func (export "simple-throw-catch") (param i32) (result i32)
29 (block $h
30 (try_table (result i32) (catch $e0 $h)
31 (if (i32.eqz (local.get 0)) (then (throw $e0)) (else))
32 (i32.const 42)
33 )
34 (return)
35 )
36 (i32.const 23)
37 )
38
39 (func (export "unreachable-not-caught")
40 (block $h
41 (try_table (catch_all $h) (unreachable))
42 (return)
43 )
44 )
45
46 (func $div (param i32 i32) (result i32)
47 (local.get 0) (local.get 1) (i32.div_u)
48 )
49 (func (export "trap-in-callee") (param i32 i32) (result i32)
50 (block $h
51 (try_table (result i32) (catch_all $h)
52 (call $div (local.get 0) (local.get 1))
53 )
54 (return)
55 )
56 (i32.const 11)
57 )
58
59 (func (export "catch-complex-1") (param i32) (result i32)
60 (block $h1
61 (try_table (result i32) (catch $e1 $h1)
62 (block $h0
63 (try_table (result i32) (catch $e0 $h0)
64 (if (i32.eqz (local.get 0))
65 (then (throw $e0))
66 (else
67 (if (i32.eq (local.get 0) (i32.const 1))
68 (then (throw $e1))
69 (else (throw $e2))
70 )
71 )
72 )
73 (i32.const 2)
74 )
75 (br 1)
76 )
77 (i32.const 3)
78 )
79 (return)
80 )
81 (i32.const 4)
82 )
83
84 (func (export "catch-complex-2") (param i32) (result i32)
85 (block $h0
86 (block $h1
87 (try_table (result i32) (catch $e0 $h0) (catch $e1 $h1)
88 (if (i32.eqz (local.get 0))
89 (then (throw $e0))
90 (else
91 (if (i32.eq (local.get 0) (i32.const 1))
92 (then (throw $e1))
93 (else (throw $e2))
94 )
95 )
96 )
97 (i32.const 2)
98 )
99 (return)
100 )
101 (return (i32.const 4))
102 )
103 (i32.const 3)
104 )
105
106 (func (export "throw-catch-param-i32") (param i32) (result i32)
107 (block $h (result i32)
108 (try_table (result i32) (catch $e-i32 $h)
109 (throw $e-i32 (local.get 0))
110 (i32.const 2)
111 )
112 (return)
113 )
114 (return)
115 )
116
117 (func (export "throw-catch-param-f32") (param f32) (result f32)
118 (block $h (result f32)
119 (try_table (result f32) (catch $e-f32 $h)
120 (throw $e-f32 (local.get 0))
121 (f32.const 0)
122 )
123 (return)
124 )
125 (return)
126 )
127
128 (func (export "throw-catch-param-i64") (param i64) (result i64)
129 (block $h (result i64)
130 (try_table (result i64) (catch $e-i64 $h)
131 (throw $e-i64 (local.get 0))
132 (i64.const 2)
133 )
134 (return)
135 )
136 (return)
137 )
138
139 (func (export "throw-catch-param-f64") (param f64) (result f64)
140 (block $h (result f64)
141 (try_table (result f64) (catch $e-f64 $h)
142 (throw $e-f64 (local.get 0))
143 (f64.const 0)
144 )
145 (return)
146 )
147 (return)
148 )
149
150 (func (export "throw-catch_ref-param-i32") (param i32) (result i32)
151 (block $h (result i32 exnref)
152 (try_table (result i32) (catch_ref $e-i32 $h)
153 (throw $e-i32 (local.get 0))
154 (i32.const 2)
155 )
156 (return)
157 )
158 (drop) (return)
159 )
160
161 (func (export "throw-catch_ref-param-f32") (param f32) (result f32)
162 (block $h (result f32 exnref)
163 (try_table (result f32) (catch_ref $e-f32 $h)
164 (throw $e-f32 (local.get 0))
165 (f32.const 0)
166 )
167 (return)
168 )
169 (drop) (return)
170 )
171
172 (func (export "throw-catch_ref-param-i64") (param i64) (result i64)
173 (block $h (result i64 exnref)
174 (try_table (result i64) (catch_ref $e-i64 $h)
175 (throw $e-i64 (local.get 0))
176 (i64.const 2)
177 )
178 (return)
179 )
180 (drop) (return)
181 )
182
183 (func (export "throw-catch_ref-param-f64") (param f64) (result f64)
184 (block $h (result f64 exnref)
185 (try_table (result f64) (catch_ref $e-f64 $h)
186 (throw $e-f64 (local.get 0))
187 (f64.const 0)
188 )
189 (return)
190 )
191 (drop) (return)
192 )
193
194 (func $throw-param-i32 (param i32) (throw $e-i32 (local.get 0)))
195 (func (export "catch-param-i32") (param i32) (result i32)
196 (block $h (result i32)
197 (try_table (result i32) (catch $e-i32 $h)
198 (i32.const 0)
199 (call $throw-param-i32 (local.get 0))
200 )
201 (return)
202 )
203 )
204
205 (func (export "catch-imported") (result i32)
206 (block $h
207 (try_table (result i32) (catch $imported-e0 $h)
208 (call $imported-throw (i32.const 1))
209 )
210 (return)
211 )
212 (i32.const 2)
213 )
214
215 (func (export "catch-imported-alias") (result i32)
216 (block $h
217 (try_table (result i32) (catch $imported-e0 $h)
218 (throw $imported-e0-alias (i32.const 1))
219 )
220 (return)
221 )
222 (i32.const 2)
223 )
224
225 (func (export "catchless-try") (param i32) (result i32)
226 (block $h
227 (try_table (result i32) (catch $e0 $h)
228 (try_table (result i32) (call $throw-if (local.get 0)))
229 )
230 (return)
231 )
232 (i32.const 1)
233 )
234
235 (func $throw-void (throw $e0))
236 (func (export "return-call-in-try-catch")
237 (block $h
238 (try_table (catch $e0 $h)
239 (return_call $throw-void)
240 )
241 )
242 )
243
244 (table funcref (elem $throw-void))
245 (func (export "return-call-indirect-in-try-catch")
246 (block $h
247 (try_table (catch $e0 $h)
248 (return_call_indirect (i32.const 0))
249 )
250 )
251 )
252
253 (func (export "try-with-param")
254 (i32.const 0) (try_table (param i32) (drop))
255 )
256
257 (func (export "duplicated-catches") (result i32)
258 (block
259 (block
260 (try_table (catch $e0 0) (catch $e0 1)
261 (throw $e0)
262 )
263 )
264 (return (i32.const 2))
265 )
266 (return (i32.const 3))
267 )
268
269 (func (export "catch-all-before-catch") (result i32)
270 (block
271 (block
272 (try_table (catch_all 0) (catch $e0 1)
273 (throw $e0)
274 )
275 )
276 (return (i32.const 2))
277 )
278 (return (i32.const 3))
279 )
280 )
281
282 (assert_return (invoke "simple-throw-catch" (i32.const 0)) (i32.const 23))
283 (assert_return (invoke "simple-throw-catch" (i32.const 1)) (i32.const 42))
284
285 (assert_trap (invoke "unreachable-not-caught") "unreachable")
286
287 (assert_return (invoke "trap-in-callee" (i32.const 7) (i32.const 2)) (i32.const 3))
288 (assert_trap (invoke "trap-in-callee" (i32.const 1) (i32.const 0)) "integer divide by zero")
289
290 (assert_return (invoke "catch-complex-1" (i32.const 0)) (i32.const 3))
291 (assert_return (invoke "catch-complex-1" (i32.const 1)) (i32.const 4))
292 (assert_exception (invoke "catch-complex-1" (i32.const 2)))
293
294 (assert_return (invoke "catch-complex-2" (i32.const 0)) (i32.const 3))
295 (assert_return (invoke "catch-complex-2" (i32.const 1)) (i32.const 4))
296 (assert_exception (invoke "catch-complex-2" (i32.const 2)))
297
298 (assert_return (invoke "throw-catch-param-i32" (i32.const 0)) (i32.const 0))
299 (assert_return (invoke "throw-catch-param-i32" (i32.const 1)) (i32.const 1))
300 (assert_return (invoke "throw-catch-param-i32" (i32.const 10)) (i32.const 10))
301
302 (assert_return (invoke "throw-catch-param-f32" (f32.const 5.0)) (f32.const 5.0))
303 (assert_return (invoke "throw-catch-param-f32" (f32.const 10.5)) (f32.const 10.5))
304
305 (assert_return (invoke "throw-catch-param-i64" (i64.const 5)) (i64.const 5))
306 (assert_return (invoke "throw-catch-param-i64" (i64.const 0)) (i64.const 0))
307 (assert_return (invoke "throw-catch-param-i64" (i64.const -1)) (i64.const -1))
308
309 (assert_return (invoke "throw-catch-param-f64" (f64.const 5.0)) (f64.const 5.0))
310 (assert_return (invoke "throw-catch-param-f64" (f64.const 10.5)) (f64.const 10.5))
311
312 (assert_return (invoke "throw-catch_ref-param-i32" (i32.const 0)) (i32.const 0))
313 (assert_return (invoke "throw-catch_ref-param-i32" (i32.const 1)) (i32.const 1))
314 (assert_return (invoke "throw-catch_ref-param-i32" (i32.const 10)) (i32.const 10))
315
316 (assert_return (invoke "throw-catch_ref-param-f32" (f32.const 5.0)) (f32.const 5.0))
317 (assert_return (invoke "throw-catch_ref-param-f32" (f32.const 10.5)) (f32.const 10.5))
318
319 (assert_return (invoke "throw-catch_ref-param-i64" (i64.const 5)) (i64.const 5))
320 (assert_return (invoke "throw-catch_ref-param-i64" (i64.const 0)) (i64.const 0))
321 (assert_return (invoke "throw-catch_ref-param-i64" (i64.const -1)) (i64.const -1))
322
323 (assert_return (invoke "throw-catch_ref-param-f64" (f64.const 5.0)) (f64.const 5.0))
324 (assert_return (invoke "throw-catch_ref-param-f64" (f64.const 10.5)) (f64.const 10.5))
325
326 (assert_return (invoke "catch-param-i32" (i32.const 5)) (i32.const 5))
327
328 (assert_return (invoke "catch-imported") (i32.const 2))
329 (assert_return (invoke "catch-imported-alias") (i32.const 2))
330
331 (assert_return (invoke "catchless-try" (i32.const 0)) (i32.const 0))
332 (assert_return (invoke "catchless-try" (i32.const 1)) (i32.const 1))
333
334 (assert_exception (invoke "return-call-in-try-catch"))
335 (assert_exception (invoke "return-call-indirect-in-try-catch"))
336
337 (assert_return (invoke "try-with-param"))
338
339 (assert_return (invoke "duplicated-catches") (i32.const 2))
340 (assert_return (invoke "catch-all-before-catch") (i32.const 2))
341
342 (module
343 (func $imported-throw (import "test" "throw"))
344 (tag $e0)
345
346 (func (export "imported-mismatch") (result i32)
347 (block $h
348 (try_table (result i32) (catch_all $h)
349 (block $h0
350 (try_table (result i32) (catch $e0 $h0)
351 (i32.const 1)
352 (call $imported-throw)
353 )
354 (return)
355 )
356 (i32.const 2)
357 )
358 (return)
359 )
360 (i32.const 3)
361 )
362 )
363
364 (assert_return (invoke "imported-mismatch") (i32.const 3))
365
366 (assert_malformed
367 (module quote "(module (func (catch_all)))")
368 "unexpected token"
369 )
370
371 (assert_malformed
372 (module quote "(module (tag $e) (func (catch $e)))")
373 "unexpected token"
374 )
375
376 (module
377 (tag $e)
378 (func (try_table (catch $e 0) (catch $e 0)))
379 (func (try_table (catch_all 0) (catch $e 0)))
380 (func (try_table (catch_all 0) (catch_all 0)))
381 (func (result exnref) (try_table (catch_ref $e 0) (catch_ref $e 0)) (unreachable))
382 (func (result exnref) (try_table (catch_all_ref 0) (catch_ref $e 0)) (unreachable))
383 (func (result exnref) (try_table (catch_all_ref 0) (catch_all_ref 0)) (unreachable))
384 )
385
386 (assert_invalid
387 (module (func (result i32) (try_table (result i32))))
388 "type mismatch"
389 )
390 (assert_invalid
391 (module (func (result i32) (try_table (result i32) (i64.const 42))))
392 "type mismatch"
393 )
394
395 (assert_invalid
396 (module (tag) (func (try_table (catch_ref 0 0))))
397 "type mismatch"
398 )
399 (assert_invalid
400 (module (tag) (func (result exnref) (try_table (catch 0 0)) (unreachable)))
401 "type mismatch"
402 )
403 (assert_invalid
404 (module (func (try_table (catch_all_ref 0))))
405 "type mismatch"
406 )
407 (assert_invalid
408 (module (func (result exnref) (try_table (catch_all 0)) (unreachable)))
409 "type mismatch"
410 )
411 (assert_invalid
412 (module
413 (tag (param i64))
414 (func (result i32 exnref) (try_table (result i32) (catch_ref 0 0) (i32.const 42)))
415 )
416 "type mismatch"
417 )
418
419
420 (module
421 (type $t (func))
422 (func $dummy)
423 (elem declare func $dummy)
424
425 (tag $e (param (ref $t)))
426 (func $throw (throw $e (ref.func $dummy)))
427
428 (func (export "catch") (result (ref null $t))
429 (block $l (result (ref null $t))
430 (try_table (catch $e $l) (call $throw))
431 (unreachable)
432 )
433 )
434 (func (export "catch_ref1") (result (ref null $t))
435 (block $l (result (ref null $t) (ref exn))
436 (try_table (catch_ref $e $l) (call $throw))
437 (unreachable)
438 )
439 (drop)
440 )
441 (func (export "catch_ref2") (result (ref null $t))
442 (block $l (result (ref null $t) (ref null exn))
443 (try_table (catch_ref $e $l) (call $throw))
444 (unreachable)
445 )
446 (drop)
447 )
448 (func (export "catch_all_ref1")
449 (block $l (result (ref exn))
450 (try_table (catch_all_ref $l) (call $throw))
451 (unreachable)
452 )
453 (drop)
454 )
455 (func (export "catch_all_ref2")
456 (block $l (result (ref null exn))
457 (try_table (catch_all_ref $l) (call $throw))
458 (unreachable)
459 )
460 (drop)
461 )
462 )
463
464 (assert_return (invoke "catch") (ref.func))
465 (assert_return (invoke "catch_ref1") (ref.func))
466 (assert_return (invoke "catch_ref2") (ref.func))
467 (assert_return (invoke "catch_all_ref1"))
468 (assert_return (invoke "catch_all_ref2"))
469
470 (assert_invalid
471 (module
472 (type $t (func))
473 (tag $e (param (ref null $t)))
474 (func (export "catch") (result (ref $t))
475 (block $l (result (ref $t))
476 (try_table (catch $e $l))
477 (unreachable)
478 )
479 )
480 )
481 "type mismatch"
482 )
483 (assert_invalid
484 (module
485 (type $t (func))
486 (tag $e (param (ref null $t)))
487 (func (export "catch_ref") (result (ref $t))
488 (block $l (result (ref $t) (ref exn))
489 (try_table (catch_ref $e $l))
490 (unreachable)
491 )
492 )
493 )
494 "type mismatch"
495 )
496
497 ;; try_table acts a regular block for br, etc.
498
499 (module
500 (func (export "as-br-target") (result i32)
501 (block
502 (try_table
503 (br 0)
504 (unreachable)
505 )
506 (return (i32.const 111))
507 )
508 (i32.const 222)
509 )
510
511 (func (export "as-value-provider") (result i32)
512 (block
513 (try_table (result i32)
514 (br 0 (i32.const 333))
515 )
516 (return)
517 )
518 (unreachable)
519 )
520 )
521
522 (assert_return (invoke "as-br-target") (i32.const 111))
523 (assert_return (invoke "as-value-provider") (i32.const 333))