Debian Codegraph @

tests/cli/stack-switching/cont.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching
2
3 ;; Unhandled tags & guards
4
5 (module
6 (tag $exn)
7 (tag $e1)
8 (tag $e2)
9
10 (type $f1 (func))
11 (type $k1 (cont $f1))
12
13 (func $f1 (export "unhandled-1")
14 (suspend $e1)
15 )
16
17 (func (export "unhandled-2")
18 (resume $k1 (cont.new $k1 (ref.func $f1)))
19 )
20
21 (func (export "unhandled-3")
22 (block $h (result (ref $k1))
23 (resume $k1 (on $e2 $h) (cont.new $k1 (ref.func $f1)))
24 (unreachable)
25 )
26 (drop)
27 )
28
29 (func (export "handled")
30 (block $h (result (ref $k1))
31 (resume $k1 (on $e1 $h) (cont.new $k1 (ref.func $f1)))
32 (unreachable)
33 )
34 (drop)
35 )
36
37 (elem declare func $f2)
38 (func $f2
39 (throw $exn)
40 )
41
42 (func (export "uncaught-1")
43 (block $h (result (ref $k1))
44 (resume $k1 (on $e1 $h) (cont.new $k1 (ref.func $f2)))
45 (unreachable)
46 )
47 (drop)
48 )
49
50 (func (export "uncaught-2")
51 (block $h (result (ref $k1))
52 (resume $k1 (on $e1 $h) (cont.new $k1 (ref.func $f1)))
53 (unreachable)
54 )
55 (resume_throw $k1 $exn)
56 )
57
58 (elem declare func $r0 $r1)
59 (func $r0)
60 (func $r1 (suspend $e1) (suspend $e1))
61
62 (func $nl1 (param $k (ref $k1))
63 (resume $k1 (local.get $k))
64 (resume $k1 (local.get $k))
65 )
66 (func $nl2 (param $k (ref $k1))
67 (block $h (result (ref $k1))
68 (resume $k1 (on $e1 $h) (local.get $k))
69 (unreachable)
70 )
71 (resume $k1 (local.get $k))
72 (unreachable)
73 )
74 (func $nl3 (param $k (ref $k1))
75 (local $k' (ref null $k1))
76 (block $h1 (result (ref $k1))
77 (resume $k1 (on $e1 $h1) (local.get $k))
78 (unreachable)
79 )
80 (local.set $k')
81 (block $h2 (result (ref $k1))
82 (resume $k1 (on $e1 $h2) (local.get $k'))
83 (unreachable)
84 )
85 (resume $k1 (local.get $k'))
86 (unreachable)
87 )
88 (func $nl4 (param $k (ref $k1))
89 (drop (cont.bind $k1 $k1 (local.get $k)))
90 (resume $k1 (local.get $k))
91 )
92
93 (func (export "non-linear-1")
94 (call $nl1 (cont.new $k1 (ref.func $r0)))
95 )
96 (func (export "non-linear-2")
97 (call $nl2 (cont.new $k1 (ref.func $r1)))
98 )
99 (func (export "non-linear-3")
100 (call $nl3 (cont.new $k1 (ref.func $r1)))
101 )
102 (func (export "non-linear-4")
103 (call $nl4 (cont.new $k1 (ref.func $r1)))
104 )
105 )
106
107 (assert_suspension (invoke "unhandled-1") "unhandled")
108 (assert_suspension (invoke "unhandled-2") "unhandled")
109 (assert_suspension (invoke "unhandled-3") "unhandled")
110 (assert_return (invoke "handled"))
111
112 (assert_exception (invoke "uncaught-1"))
113 (assert_exception (invoke "uncaught-2"))
114
115 (assert_trap (invoke "non-linear-1") "continuation already consumed")
116 (assert_trap (invoke "non-linear-2") "continuation already consumed")
117 (assert_trap (invoke "non-linear-3") "continuation already consumed")
118 (assert_trap (invoke "non-linear-4") "continuation already consumed")
119
120 (assert_invalid
121 (module
122 (type $ft (func))
123 (func
124 (cont.new $ft (ref.null $ft))
125 (drop)))
126 "non-continuation type 0")
127
128 (assert_invalid
129 (module
130 (type $ft (func))
131 (type $ct (cont $ft))
132 (func
133 (resume $ft (ref.null $ct))
134 (unreachable)))
135 "non-continuation type 0")
136
137 (assert_invalid
138 (module
139 (type $ft (func))
140 (type $ct (cont $ft))
141 (tag $exn)
142 (func
143 (resume_throw $ft $exn (ref.null $ct))
144 (unreachable)))
145 "non-continuation type 0")
146
147 (assert_invalid
148 (module
149 (type $ft (func))
150 (type $ct (cont $ft))
151 (func
152 (cont.bind $ft $ct (ref.null $ct))
153 (unreachable)))
154 "non-continuation type 0")
155
156 (assert_invalid
157 (module
158 (type $ft (func))
159 (type $ct (cont $ft))
160 (func
161 (cont.bind $ct $ft (ref.null $ct))
162 (unreachable)))
163 "non-continuation type 0")
164
165 (assert_invalid
166 (module
167 (type $ft (func))
168 (type $ct (cont $ft))
169 (tag $foo)
170 (func
171 (block $on_foo (result (ref $ft))
172 (resume $ct (on $foo $on_foo) (ref.null $ct))
173 (unreachable)
174 )
175 (drop)))
176 "non-continuation type")
177
178 (assert_invalid
179 (module
180 (type $ft (func))
181 (type $ct (cont $ft))
182 (tag $foo)
183 (func
184 (block $on_foo (result (ref $ct) (ref $ft))
185 (resume $ct (on $foo $on_foo) (ref.null $ct))
186 (unreachable)
187 )
188 (drop)
189 (drop)))
190 "non-continuation type")
191
192 (assert_invalid
193 (module
194 (type $ct (cont $ct)))
195 "non-function type 0")
196
197 (assert_invalid
198 (module
199 (rec
200 (type $s0 (struct (field (ref 0) (ref 1) (ref $s0) (ref $s1))))
201 (type $s1 (struct (field (ref 0) (ref 1) (ref $s0) (ref $s1))))
202 )
203 (type $ct (cont $s0)))
204 "non-function type 0")
205
206 (module
207 (rec
208 (type $f1 (func (param (ref $f2))))
209 (type $f2 (func (param (ref $f1))))
210 )
211 (type $c1 (cont $f1))
212 (type $c2 (cont $f2))
213 )
214
215 ;; Simple state example
216
217 (module $state
218 (tag $get (result i32))
219 (tag $set (param i32) (result i32))
220
221 (type $f (func (param i32) (result i32)))
222 (type $k (cont $f))
223
224 (func $runner (param $s i32) (param $k (ref $k)) (result i32)
225 (loop $loop
226 (block $on_get (result (ref $k))
227 (block $on_set (result i32 (ref $k))
228 (resume $k (on $get $on_get) (on $set $on_set)
229 (local.get $s) (local.get $k)
230 )
231 (return)
232 )
233 ;; on set
234 (local.set $k)
235 (local.set $s)
236 (br $loop)
237 )
238 ;; on get
239 (local.set $k)
240 (br $loop)
241 )
242 (unreachable)
243 )
244
245 (func $f (param i32) (result i32)
246 (drop (suspend $set (i32.const 7)))
247 (i32.add
248 (suspend $get)
249 (i32.mul
250 (i32.const 2)
251 (i32.add
252 (suspend $set (i32.const 3))
253 (suspend $get)
254 )
255 )
256 )
257 )
258
259 (elem declare func $f)
260 (func (export "run") (result i32)
261 (call $runner (i32.const 0) (cont.new $k (ref.func $f)))
262 )
263 )
264
265 (assert_return (invoke "run") (i32.const 19))
266
267
268 ;; Simple generator example
269
270 (module $generator
271 (type $gen (func (param i64)))
272 (type $geny (func (param i32)))
273 (type $cont0 (cont $gen))
274 (type $cont (cont $geny))
275
276 (tag $yield (param i64) (result i32))
277
278 ;; Hook for logging purposes
279 (global $hook (export "hook") (mut (ref $gen)) (ref.func $dummy))
280 (func $dummy (param i64))
281
282 (func $gen (export "start") (param $i i64)
283 (loop $l
284 (br_if 1 (suspend $yield (local.get $i)))
285 (call_ref $gen (local.get $i) (global.get $hook))
286 (local.set $i (i64.add (local.get $i) (i64.const 1)))
287 (br $l)
288 )
289 )
290
291 (elem declare func $gen)
292
293 (func (export "sum") (param $i i64) (param $j i64) (result i64)
294 (local $sum i64)
295 (local $n i64)
296 (local $k (ref null $cont))
297 (local.get $i)
298 (cont.new $cont0 (ref.func $gen))
299 (block $on_first_yield (param i64 (ref $cont0)) (result i64 (ref $cont))
300 (resume $cont0 (on $yield $on_first_yield))
301 (unreachable)
302 )
303 (loop $on_yield (param i64) (param (ref $cont))
304 (local.set $k)
305 (local.set $n)
306 (local.set $sum (i64.add (local.get $sum) (local.get $n)))
307 (i64.eq (local.get $n) (local.get $j))
308 (local.get $k)
309 (resume $cont (on $yield $on_yield))
310 )
311 (return (local.get $sum))
312 )
313 )
314
315 (register "generator")
316
317 (assert_return (invoke "sum" (i64.const 0) (i64.const 0)) (i64.const 0))
318 (assert_return (invoke "sum" (i64.const 2) (i64.const 2)) (i64.const 2))
319 (assert_return (invoke "sum" (i64.const 0) (i64.const 3)) (i64.const 6))
320 (assert_return (invoke "sum" (i64.const 1) (i64.const 10)) (i64.const 55))
321 (assert_return (invoke "sum" (i64.const 100) (i64.const 2000)) (i64.const 1_996_050))
322
323
324 ;; Simple scheduler example
325
326 (module $scheduler
327 (type $proc (func))
328 (type $cont (cont $proc))
329
330 (tag $yield (export "yield"))
331 (tag $spawn (export "spawn") (param (ref $cont)))
332
333 ;; Table as simple queue (keeping it simple, no ring buffer)
334 (table $queue 0 (ref null $cont))
335 (global $qdelta i32 (i32.const 10))
336 (global $qback (mut i32) (i32.const 0))
337 (global $qfront (mut i32) (i32.const 0))
338
339 (func $queue-empty (result i32)
340 (i32.eq (global.get $qfront) (global.get $qback))
341 )
342
343 (func $dequeue (result (ref null $cont))
344 (local $i i32)
345 (if (call $queue-empty)
346 (then (return (ref.null $cont)))
347 )
348 (local.set $i (global.get $qfront))
349 (global.set $qfront (i32.add (local.get $i) (i32.const 1)))
350 (table.get $queue (local.get $i))
351 )
352
353 (func $enqueue (param $k (ref $cont))
354 ;; Check if queue is full
355 (if (i32.eq (global.get $qback) (table.size $queue))
356 (then
357 ;; Check if there is enough space in the front to compact
358 (if (i32.lt_u (global.get $qfront) (global.get $qdelta))
359 (then
360 ;; Space is below threshold, grow table instead
361 (drop (table.grow $queue (ref.null $cont) (global.get $qdelta)))
362 )
363 (else
364 ;; Enough space, move entries up to head of table
365 (global.set $qback (i32.sub (global.get $qback) (global.get $qfront)))
366 (table.copy $queue $queue
367 (i32.const 0) ;; dest = new front = 0
368 (global.get $qfront) ;; src = old front
369 (global.get $qback) ;; len = new back = old back - old front
370 )
371 (table.fill $queue ;; null out old entries to avoid leaks
372 (global.get $qback) ;; start = new back
373 (ref.null $cont) ;; init value
374 (global.get $qfront) ;; len = old front = old front - new front
375 )
376 (global.set $qfront (i32.const 0))
377 )
378 )
379 )
380 )
381 (table.set $queue (global.get $qback) (local.get $k))
382 (global.set $qback (i32.add (global.get $qback) (i32.const 1)))
383 )
384
385 (func $scheduler (export "scheduler") (param $main (ref $cont))
386 (call $enqueue (local.get $main))
387 (loop $l
388 (if (call $queue-empty) (then (return)))
389 (block $on_yield (result (ref $cont))
390 (block $on_spawn (result (ref $cont) (ref $cont))
391 (resume $cont (on $yield $on_yield) (on $spawn $on_spawn)
392 (call $dequeue)
393 )
394 (br $l) ;; thread terminated
395 )
396 ;; on $spawn, proc and cont on stack
397 (call $enqueue) ;; continuation of old thread
398 (call $enqueue) ;; new thread
399 (br $l)
400 )
401 ;; on $yield, cont on stack
402 (call $enqueue)
403 (br $l)
404 )
405 )
406 )
407
408 (register "scheduler")
409
410 (module
411 (type $proc (func))
412 (type $pproc (func (param i32))) ;; parameterised proc
413 (type $cont (cont $proc))
414 (type $pcont (cont $pproc)) ;; parameterised continuation proc
415 (tag $yield (import "scheduler" "yield"))
416 (tag $spawn (import "scheduler" "spawn") (param (ref $cont)))
417 (func $scheduler (import "scheduler" "scheduler") (param $main (ref $cont)))
418
419 (func $log (import "spectest" "print_i32") (param i32))
420
421 (global $width (mut i32) (i32.const 0))
422 (global $depth (mut i32) (i32.const 0))
423
424 (elem declare func $main $thread1 $thread2 $thread3)
425
426 (func $main
427 (call $log (i32.const 0))
428 (suspend $spawn (cont.new $cont (ref.func $thread1)))
429 (call $log (i32.const 1))
430 (suspend $spawn (cont.bind $pcont $cont (global.get $depth) (cont.new $pcont (ref.func $thread2))))
431 (call $log (i32.const 2))
432 (suspend $spawn (cont.new $cont (ref.func $thread3)))
433 (call $log (i32.const 3))
434 )
435
436 (func $thread1
437 (call $log (i32.const 10))
438 (suspend $yield)
439 (call $log (i32.const 11))
440 (suspend $yield)
441 (call $log (i32.const 12))
442 (suspend $yield)
443 (call $log (i32.const 13))
444 )
445
446 (func $thread2 (param $d i32)
447 (local $w i32)
448 (local.set $w (global.get $width))
449 (call $log (i32.const 20))
450 (br_if 0 (i32.eqz (local.get $d)))
451 (call $log (i32.const 21))
452 (loop $l
453 (if (local.get $w)
454 (then
455 (call $log (i32.const 22))
456 (suspend $yield)
457 (call $log (i32.const 23))
458 (suspend $spawn
459 (cont.bind $pcont $cont
460 (i32.sub (local.get $d) (i32.const 1))
461 (cont.new $pcont (ref.func $thread2))
462 )
463 )
464 (call $log (i32.const 24))
465 (local.set $w (i32.sub (local.get $w) (i32.const 1)))
466 (br $l)
467 )
468 )
469 )
470 (call $log (i32.const 25))
471 )
472
473 (func $thread3
474 (call $log (i32.const 30))
475 (suspend $yield)
476 (call $log (i32.const 31))
477 (suspend $yield)
478 (call $log (i32.const 32))
479 )
480
481 (func (export "run") (param $width i32) (param $depth i32)
482 (global.set $depth (local.get $depth))
483 (global.set $width (local.get $width))
484 (call $log (i32.const -1))
485 (call $scheduler (cont.new $cont (ref.func $main)))
486 (call $log (i32.const -2))
487 )
488 )
489
490 (assert_return (invoke "run" (i32.const 0) (i32.const 0)))
491 (assert_return (invoke "run" (i32.const 0) (i32.const 1)))
492 (assert_return (invoke "run" (i32.const 1) (i32.const 0)))
493 (assert_return (invoke "run" (i32.const 1) (i32.const 1)))
494 (assert_return (invoke "run" (i32.const 3) (i32.const 4)))
495
496
497 ;; Nested example: generator in a thread
498
499 (module $concurrent-generator
500 (func $log (import "spectest" "print_i64") (param i64))
501
502 (tag $syield (import "scheduler" "yield"))
503 (tag $spawn (import "scheduler" "spawn") (param (ref $cont)))
504 (func $scheduler (import "scheduler" "scheduler") (param $main (ref $cont)))
505
506 (type $ghook (func (param i64)))
507 (func $gsum (import "generator" "sum") (param i64 i64) (result i64))
508 (global $ghook (import "generator" "hook") (mut (ref $ghook)))
509
510 (global $result (mut i64) (i64.const 0))
511 (global $done (mut i32) (i32.const 0))
512
513 (elem declare func $main $bg-thread $syield)
514
515 (func $syield (param $i i64)
516 (call $log (local.get $i))
517 (suspend $syield)
518 )
519
520 (func $bg-thread
521 (call $log (i64.const -10))
522 (loop $l
523 (call $log (i64.const -11))
524 (suspend $syield)
525 (br_if $l (i32.eqz (global.get $done)))
526 )
527 (call $log (i64.const -12))
528 )
529
530 (func $main (param $i i64) (param $j i64)
531 (suspend $spawn (cont.new $cont (ref.func $bg-thread)))
532 (global.set $ghook (ref.func $syield))
533 (global.set $result (call $gsum (local.get $i) (local.get $j)))
534 (global.set $done (i32.const 1))
535 )
536
537 (type $proc (func))
538 (type $pproc (func (param i64 i64)))
539 (type $cont (cont $proc))
540 (type $pcont (cont $pproc))
541 (func (export "sum") (param $i i64) (param $j i64) (result i64)
542 (call $log (i64.const -1))
543 (call $scheduler
544 (cont.bind $pcont $cont (local.get $i) (local.get $j) (cont.new $pcont (ref.func $main)))
545 )
546 (call $log (i64.const -2))
547 (global.get $result)
548 )
549 )
550
551 (assert_return (invoke "sum" (i64.const 10) (i64.const 20)) (i64.const 165))
552
553
554 ;; cont.bind
555
556 (module
557 (type $f2 (func (param i32 i32) (result i32 i32 i32 i32 i32 i32)))
558 (type $f4 (func (param i32 i32 i32 i32) (result i32 i32 i32 i32 i32 i32)))
559 (type $f6 (func (param i32 i32 i32 i32 i32 i32) (result i32 i32 i32 i32 i32 i32)))
560
561 (type $k2 (cont $f2))
562 (type $k4 (cont $f4))
563 (type $k6 (cont $f6))
564
565 (elem declare func $f)
566 (func $f (param i32 i32 i32 i32 i32 i32) (result i32 i32 i32 i32 i32 i32)
567 (local.get 0) (local.get 1) (local.get 2)
568 (local.get 3) (local.get 4) (local.get 5)
569 )
570
571 (func (export "run") (result i32 i32 i32 i32 i32 i32)
572 (local $k6 (ref null $k6))
573 (local $k4 (ref null $k4))
574 (local $k2 (ref null $k2))
575 (local.set $k6 (cont.new $k6 (ref.func $f)))
576 (local.set $k4 (cont.bind $k6 $k4 (i32.const 1) (i32.const 2) (local.get $k6)))
577 (local.set $k2 (cont.bind $k4 $k2 (i32.const 3) (i32.const 4) (local.get $k4)))
578 (resume $k2 (i32.const 5) (i32.const 6) (local.get $k2))
579 )
580 )
581
582 (assert_return (invoke "run")
583 (i32.const 1) (i32.const 2) (i32.const 3)
584 (i32.const 4) (i32.const 5) (i32.const 6)
585 )
586
587
588 (module
589 (tag $e (result i32 i32 i32 i32 i32 i32))
590
591 (type $f0 (func (result i32 i32 i32 i32 i32 i32 i32)))
592 (type $f2 (func (param i32 i32) (result i32 i32 i32 i32 i32 i32 i32)))
593 (type $f4 (func (param i32 i32 i32 i32) (result i32 i32 i32 i32 i32 i32 i32)))
594 (type $f6 (func (param i32 i32 i32 i32 i32 i32) (result i32 i32 i32 i32 i32 i32 i32)))
595
596 (type $k0 (cont $f0))
597 (type $k2 (cont $f2))
598 (type $k4 (cont $f4))
599 (type $k6 (cont $f6))
600
601 (elem declare func $f)
602 (func $f (result i32 i32 i32 i32 i32 i32 i32)
603 (i32.const 0) (suspend $e)
604 )
605
606 (func (export "run") (result i32 i32 i32 i32 i32 i32 i32)
607 (local $k6 (ref null $k6))
608 (local $k4 (ref null $k4))
609 (local $k2 (ref null $k2))
610 (block $l (result (ref $k6))
611 (resume $k0 (on $e $l) (cont.new $k0 (ref.func $f)))
612 (unreachable)
613 )
614 (local.set $k6)
615 (local.set $k4 (cont.bind $k6 $k4 (i32.const 1) (i32.const 2) (local.get $k6)))
616 (local.set $k2 (cont.bind $k4 $k2 (i32.const 3) (i32.const 4) (local.get $k4)))
617 (resume $k2 (i32.const 5) (i32.const 6) (local.get $k2))
618 )
619 )
620
621 (assert_return (invoke "run")
622 (i32.const 0) (i32.const 1) (i32.const 2) (i32.const 3)
623 (i32.const 4) (i32.const 5) (i32.const 6)
624 )
625
626 ;; Subtyping
627 (module
628 (type $ft1 (func (param i32)))
629 (type $ct1 (sub (cont $ft1)))
630
631 (type $ft0 (func))
632 (type $ct0 (sub (cont $ft0)))
633
634 (func $test (param $x (ref $ct1))
635 (i32.const 123)
636 (local.get $x)
637 (cont.bind $ct1 $ct0)
638 (drop)
639 )
640 )
641
642 ;; Globals
643 (module
644 (type $ft (func))
645 (type $ct (cont $ft))
646
647 (global $k (mut (ref null $ct)) (ref.null $ct))
648 (global $g (ref null $ct) (ref.null $ct))
649
650 (func $f)
651 (elem declare func $f)
652
653 (func (export "set-global")
654 (global.set $k (cont.new $ct (ref.func $f))))
655 )
656 (assert_return (invoke "set-global"))
657
658 ;; Switch
659 (module
660 (rec
661 (type $ft (func (param (ref null $ct))))
662 (type $ct (cont $ft)))
663
664 (func $print-i32 (import "spectest" "print_i32") (param i32))
665
666 (global $fi (mut i32) (i32.const 0))
667 (global $gi (mut i32) (i32.const 1))
668
669 (tag $swap)
670
671 (func $init (export "init") (result i32)
672 (resume $ct (on $swap switch)
673 (cont.new $ct (ref.func $g))
674 (cont.new $ct (ref.func $f)))
675 (return (i32.const 42)))
676 (func $f (type $ft)
677 (local $nextk (ref null $ct))
678 (local.set $nextk (local.get 0))
679 (call $print-i32 (global.get $fi))
680 (switch $ct $swap (local.get $nextk))
681 (local.set $nextk)
682 (call $print-i32 (global.get $fi))
683 (switch $ct $swap (local.get $nextk))
684 (drop))
685 (func $g (type $ft)
686 (local $nextk (ref null $ct))
687 (local.set $nextk (local.get 0))
688 (call $print-i32 (global.get $gi))
689 (switch $ct $swap (local.get $nextk))
690 (local.set $nextk)
691 (call $print-i32 (global.get $gi)))
692 (elem declare func $f $g)
693 )
694 (assert_return (invoke "init") (i32.const 42))
695
696 (module
697 (rec
698 (type $ft (func (param i32) (param (ref null $ct)) (result i32)))
699 (type $ct (cont $ft)))
700
701 (func $print-i32 (import "spectest" "print_i32") (param i32))
702
703 (tag $swap (result i32))
704
705 (func $init (export "init") (result i32)
706 (resume $ct (on $swap switch)
707 (i32.const 1)
708 (cont.new $ct (ref.func $g))
709 (cont.new $ct (ref.func $f))))
710 (func $f (type $ft)
711 (local $i i32)
712 (local $nextk (ref null $ct))
713 (local.set $i (local.get 0))
714 (local.set $nextk (local.get 1))
715 (call $print-i32 (local.get $i))
716 (switch $ct $swap (i32.add (i32.const 1) (local.get $i)) (local.get $nextk))
717 (local.set $nextk)
718 (local.set $i)
719 (call $print-i32 (local.get $i))
720 (switch $ct $swap (i32.add (i32.const 1) (local.get $i)) (local.get $nextk))
721 (unreachable))
722 (func $g (type $ft)
723 (local $i i32)
724 (local $nextk (ref null $ct))
725 (local.set $i (local.get 0))
726 (local.set $nextk (local.get 1))
727 (call $print-i32 (local.get $i))
728 (switch $ct $swap (i32.add (i32.const 1) (local.get $i)) (local.get $nextk))
729 (local.set $nextk)
730 (local.set $i)
731 (call $print-i32 (local.get $i))
732 (return (local.get $i)))
733 (elem declare func $f $g)
734 )
735 (assert_return (invoke "init") (i32.const 4))
736
737
738 (assert_invalid
739 (module
740 (rec
741 (type $ft (func (param (ref null $ct))))
742 (type $ct (cont $ft)))
743 (type $ft2 (func))
744 (type $ct2 (cont $ft2))
745
746 (tag $swap)
747 (func $f (type $ft)
748 (switch $ct $swap (cont.new $ct2 (ref.null $ft2)))
749 (drop)))
750 "type mismatch")
751
752 (assert_invalid
753 (module
754 (rec
755 (type $ft (func (param i32) (param (ref null $ct))))
756 (type $ct (cont $ft)))
757
758 (tag $swap)
759 (func $f (type $ft)
760 (switch $ct $swap (i64.const 0) (local.get 1))
761 (drop)
762 (drop)))
763 "type mismatch")
764
765 (module
766 (type $ft1 (func))
767 (type $ct1 (cont $ft1))
768 (rec
769 (type $ft2 (func (param (ref null $ct2))))
770 (type $ct2 (cont $ft2)))
771
772 (tag $t)
773
774 (func $suspend (type $ft2)
775 (suspend $t))
776
777 (func $switch (type $ft2)
778 (switch $ct2 $t (local.get 0))
779 (drop))
780
781 (func (export "unhandled-suspend-t")
782 (resume $ct2 (on $t switch)
783 (cont.new $ct2 (ref.func $suspend))
784 (cont.new $ct2 (ref.func $suspend))))
785 (func (export "unhandled-switch-t")
786 (block $l (result (ref $ct1))
787 (resume $ct2 (on $t $l)
788 (cont.new $ct2 (ref.func $switch))
789 (cont.new $ct2 (ref.func $switch)))
790 (unreachable)
791 )
792 (unreachable))
793
794 (elem declare func $suspend $switch)
795 )
796 (assert_suspension (invoke "unhandled-suspend-t") "unhandled tag")
797 (assert_suspension (invoke "unhandled-switch-t") "unhandled tag")
798
799 (module
800 (rec
801 (type $ft (func (param (ref null $ct))))
802 (type $ct (cont $ft)))
803
804 (tag $t)
805
806 (func
807 (cont.new $ct (ref.null $ft))
808 (unreachable))
809 (func
810 (cont.bind $ct $ct (ref.null $ct))
811 (unreachable))
812 (func
813 (resume $ct (ref.null $ct) (ref.null $ct))
814 (unreachable))
815 (func
816 (resume_throw $ct $t (ref.null $ct))
817 (unreachable))
818 (func
819 (switch $ct $t (ref.null $ct))
820 (unreachable))
821 )
822
823 (module $co2
824 (type $task (func (result i32))) ;; type alias task = [] -> []
825 (type $ct (cont $task)) ;; type alias ct = $task
826 (tag $pause (export "pause")) ;; pause : [] -> []
827 (tag $cancel (export "cancel")) ;; cancel : [] -> []
828 ;; run : [(ref $task) (ref $task)] -> []
829 ;; implements a 'seesaw' (c.f. Ganz et al. (ICFP@99))
830 (func $run (export "seesaw") (param $up (ref $ct)) (param $down (ref $ct)) (result i32)
831 (local $result i32)
832 ;; run $up
833 (loop $run_next (result i32)
834 (block $on_pause (result (ref $ct))
835 (resume $ct (on $pause $on_pause)
836 (local.get $up))
837 ;; $up finished, store its result
838 (local.set $result)
839 ;; next cancel $down
840 (block $on_cancel
841 (try_table (catch $cancel $on_cancel)
842 ;; inject the cancel exception into $down
843 (resume_throw $ct $cancel (local.get $down))
844 (drop) ;; drop the return value if it handled $cancel
845 ;; itself and returned normally...
846 )
847 ) ;; ... otherwise catch $cancel and return $up's result.
848 (return (local.get $result))
849 ) ;; on_pause clause, stack type: [(cont $ct)]
850 (local.set $up)
851 ;; swap $up and $down
852 (local.get $down)
853 (local.set $down (local.get $up))
854 (local.set $up)
855 (br $run_next)
856 )
857 )
858 )
859 (register "co2")
860
861 (module $client
862 (type $task-0 (func (param i32) (result i32)))
863 (type $ct-0 (cont $task-0))
864 (type $task (func (result i32)))
865 (type $ct (cont $task))
866
867 (func $seesaw (import "co2" "seesaw") (param (ref $ct)) (param (ref $ct)) (result i32))
868 (func $print-i32 (import "spectest" "print_i32") (param i32))
869 (tag $pause (import "co2" "pause"))
870
871 (func $even (param $niter i32) (result i32)
872 (local $next i32) ;; zero initialised.
873 (local $i i32)
874 (loop $print-next
875 (call $print-i32 (local.get $next))
876 (suspend $pause)
877 (local.set $next (i32.add (local.get $next) (i32.const 2)))
878 (local.set $i (i32.add (local.get $i) (i32.const 1)))
879 (br_if $print-next (i32.lt_u (local.get $i) (local.get $niter)))
880 )
881 (local.get $next)
882 )
883 (func $odd (param $niter i32) (result i32)
884 (local $next i32) ;; zero initialised.
885 (local $i i32)
886 (local.set $next (i32.const 1))
887 (loop $print-next
888 (call $print-i32 (local.get $next))
889 (suspend $pause)
890 (local.set $next (i32.add (local.get $next) (i32.const 2)))
891 (local.set $i (i32.add (local.get $i) (i32.const 1)))
892 (br_if $print-next (i32.lt_u (local.get $i) (local.get $niter)))
893 )
894 (local.get $next)
895 )
896
897 (func (export "main") (result i32)
898 (call $seesaw
899 (cont.bind $ct-0 $ct
900 (i32.const 5) (cont.new $ct-0 (ref.func $even)))
901 (cont.bind $ct-0 $ct
902 (i32.const 5) (cont.new $ct-0 (ref.func $odd)))))
903
904 (elem declare func $even $odd)
905 )
906 (assert_return (invoke "main") (i32.const 10))