|
| 1 | +;; Triple suspend/resume on same continuation. |
| 2 | +;; Tests repeated compress/decompress cycles, varying PC each time. |
| 3 | +;; Flow: yield(1)->10, yield(12)->20, yield(32)->3, return 34 |
| 4 | +(module |
| 5 | + (type $f (func (result i32))) |
| 6 | + (type $c (cont $f)) |
| 7 | + (type $fb (func (param i32) (result i32))) |
| 8 | + (type $cb (cont $fb)) |
| 9 | + (tag $yield (param i32) (result i32)) |
| 10 | + |
| 11 | + (func $worker (result i32) |
| 12 | + (local $acc i32) |
| 13 | + ;; yield 1, receive 10: acc = 1 + 10 = 11 |
| 14 | + (local.set $acc (i32.add (i32.const 1) (suspend $yield (i32.const 1)))) |
| 15 | + ;; yield 12, receive 20: acc = 11 + 20 = 31 |
| 16 | + (local.set $acc (i32.add (local.get $acc) |
| 17 | + (suspend $yield (i32.add (local.get $acc) (i32.const 1))))) |
| 18 | + ;; yield 32, receive 3: acc = 31 + 3 = 34 |
| 19 | + (local.set $acc (i32.add (local.get $acc) |
| 20 | + (suspend $yield (i32.add (local.get $acc) (i32.const 1))))) |
| 21 | + (local.get $acc) |
| 22 | + ) |
| 23 | + (elem declare func $worker) |
| 24 | + |
| 25 | + (func (export "main") (result i32) |
| 26 | + (local $k (ref null $cb)) |
| 27 | + ;; Start worker, catch first yield (value 1) |
| 28 | + (block (result i32 (ref null $cb)) |
| 29 | + (resume $c (on $yield 0) (cont.new $c (ref.func $worker))) |
| 30 | + (return) |
| 31 | + ) |
| 32 | + (local.set $k) |
| 33 | + (drop) |
| 34 | + ;; Resume with 10, catch second yield (value 12) |
| 35 | + (block (result i32 (ref null $cb)) |
| 36 | + (resume $cb (on $yield 0) (i32.const 10) (local.get $k)) |
| 37 | + (return) |
| 38 | + ) |
| 39 | + (local.set $k) |
| 40 | + (drop) |
| 41 | + ;; Resume with 20, catch third yield (value 32) |
| 42 | + (block (result i32 (ref null $cb)) |
| 43 | + (resume $cb (on $yield 0) (i32.const 20) (local.get $k)) |
| 44 | + (return) |
| 45 | + ) |
| 46 | + (local.set $k) |
| 47 | + (drop) |
| 48 | + ;; Final resume with 3, worker returns 34 |
| 49 | + (i32.const 3) |
| 50 | + (local.get $k) |
| 51 | + (resume $cb) |
| 52 | + ) |
| 53 | +) |
| 54 | + |
| 55 | +(assert_return (invoke "main") (i32.const 34)) |
0 commit comments