| 1 |
;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching |
| 2 |
|
| 3 |
;; Generators |
| 4 |
|
| 5 |
;; generator interface |
| 6 |
(module $generator |
| 7 |
(tag $yield (export "yield") (param i32)) |
| 8 |
) |
| 9 |
(register "generator") |
| 10 |
|
| 11 |
(module $examples |
| 12 |
(type $func (func)) |
| 13 |
(type $cont (cont $func)) |
| 14 |
|
| 15 |
(tag $yield (import "generator" "yield") (param i32)) |
| 16 |
|
| 17 |
(func $log (import "spectest" "print_i32") (param i32)) |
| 18 |
|
| 19 |
;; yields successive natural numbers |
| 20 |
(func $naturals (export "naturals") |
| 21 |
(local $n i32) |
| 22 |
(loop $l |
| 23 |
(suspend $yield (local.get $n)) |
| 24 |
(local.set $n (i32.add (local.get $n) (i32.const 1))) |
| 25 |
(br $l) |
| 26 |
) |
| 27 |
) |
| 28 |
|
| 29 |
;; yields 1-2-3 |
| 30 |
(func $one-two-three (export "one-two-three") |
| 31 |
(suspend $yield (i32.const 1)) |
| 32 |
(suspend $yield (i32.const 2)) |
| 33 |
(suspend $yield (i32.const 3)) |
| 34 |
) |
| 35 |
|
| 36 |
;; yields successive Fibonacci numbers |
| 37 |
(func $fibonacci (export "fibonacci") |
| 38 |
(local $a i32) |
| 39 |
(local $b i32) |
| 40 |
(local $t i32) |
| 41 |
(local.set $b (i32.const 1)) |
| 42 |
(loop $l |
| 43 |
(suspend $yield (local.get $a)) |
| 44 |
(local.set $t (local.get $a)) |
| 45 |
(local.set $a (local.get $b)) |
| 46 |
(local.set $b (i32.add (local.get $t) (local.get $a))) |
| 47 |
(br $l) |
| 48 |
) |
| 49 |
) |
| 50 |
|
| 51 |
(func $print-first (export "print-first") (param $n i32) (param $k (ref $cont)) |
| 52 |
(loop $l |
| 53 |
(block $on_yield (result i32 (ref $cont)) |
| 54 |
(if (local.get $n) |
| 55 |
(then (resume $cont (on $yield $on_yield) (local.get $k))) |
| 56 |
) |
| 57 |
(return) |
| 58 |
) ;; $on_yield (result i32 (ref $cont)) |
| 59 |
(local.set $k) |
| 60 |
(call $log) |
| 61 |
(local.set $n (i32.add (local.get $n) (i32.const -1))) |
| 62 |
(br $l) |
| 63 |
) |
| 64 |
(unreachable) |
| 65 |
) |
| 66 |
|
| 67 |
(func $sum-first (export "sum-first") (param $n i32) (param $k (ref $cont)) (result i32) |
| 68 |
(local $sum i32) |
| 69 |
(loop $l |
| 70 |
(block $on_yield (result i32 (ref $cont)) |
| 71 |
(if (local.get $n) |
| 72 |
(then (resume $cont (on $yield $on_yield) (local.get $k))) |
| 73 |
) |
| 74 |
(return (local.get $sum)) |
| 75 |
) ;; $on_yield (result i32 (ref $cont)) |
| 76 |
(local.set $k) |
| 77 |
(local.set $sum (i32.add (local.get $sum))) |
| 78 |
(local.set $n (i32.add (local.get $n) (i32.const -1))) |
| 79 |
(br $l) |
| 80 |
) |
| 81 |
(unreachable) |
| 82 |
) |
| 83 |
) |
| 84 |
(register "examples") |
| 85 |
|
| 86 |
;; storing generators in a global table and then accessing them through i32 handles |
| 87 |
;; without knowledge of handlers |
| 88 |
(module $manager |
| 89 |
(type $func (func)) |
| 90 |
(type $cont (cont $func)) |
| 91 |
|
| 92 |
(tag $yield (import "generator" "yield") (param i32)) |
| 93 |
|
| 94 |
(table $active 0 (ref null $cont)) |
| 95 |
|
| 96 |
(func $init (export "init") (param $k (ref $cont)) (result i32) |
| 97 |
(table.grow $active (local.get $k) (i32.const 1)) |
| 98 |
) |
| 99 |
|
| 100 |
(func $next (export "next") (param $g i32) (result i32) |
| 101 |
(local $next_k (ref $cont)) |
| 102 |
(local $next_v i32) |
| 103 |
(block $on_yield (result i32 (ref $cont)) |
| 104 |
(resume $cont (on $yield $on_yield) |
| 105 |
(table.get $active (local.get $g)) |
| 106 |
) |
| 107 |
(return (i32.const -1)) |
| 108 |
) ;; $on_yield (result i32 (ref $cont)) |
| 109 |
(local.set $next_k) |
| 110 |
(local.set $next_v) |
| 111 |
(table.set (local.get $g) (local.get $next_k)) |
| 112 |
(return (local.get $next_v)) |
| 113 |
) |
| 114 |
) |
| 115 |
(register "manager") |
| 116 |
|
| 117 |
(module |
| 118 |
(type $func (func)) |
| 119 |
(type $cont (cont $func)) |
| 120 |
|
| 121 |
(elem declare func $naturals $fibonacci $one-two-three) |
| 122 |
|
| 123 |
(func $log (import "spectest" "print_i32") (param i32)) |
| 124 |
(func $naturals (import "examples" "naturals")) |
| 125 |
(func $fibonacci (import "examples" "fibonacci")) |
| 126 |
(func $one-two-three (import "examples" "one-two-three")) |
| 127 |
(func $print-first (import "examples" "print-first") (param $n i32) (param $k (ref $cont))) |
| 128 |
(func $sum-first (import "examples" "sum-first") (param $n i32) (param $k (ref $cont)) (result i32)) |
| 129 |
(func $init (import "manager" "init") (param $k (ref $cont)) (result i32)) |
| 130 |
(func $next (import "manager" "next") (param i32) (result i32)) |
| 131 |
|
| 132 |
(func $print-with-next (param $n i32) (param $gen i32) |
| 133 |
(loop $l |
| 134 |
(if (i32.eqz (local.get $n)) (then (return))) |
| 135 |
(call $next (local.get $gen)) |
| 136 |
(call $log) |
| 137 |
(local.set $n (i32.add (local.get $n) (i32.const -1))) |
| 138 |
(br $l) |
| 139 |
) |
| 140 |
) |
| 141 |
|
| 142 |
(func $interleave-naturals-and-fib |
| 143 |
(local $gen1 i32) |
| 144 |
(local $gen2 i32) |
| 145 |
(local.set $gen1 (call $init (cont.new $cont (ref.func $naturals)))) |
| 146 |
(local.set $gen2 (call $init (cont.new $cont (ref.func $fibonacci)))) |
| 147 |
(call $print-with-next (i32.const 5) (local.get $gen1)) |
| 148 |
(call $print-with-next (i32.const 5) (local.get $gen2)) |
| 149 |
(call $print-with-next (i32.const 5) (local.get $gen1)) |
| 150 |
(call $print-with-next (i32.const 5) (local.get $gen2)) |
| 151 |
(call $print-with-next (i32.const 5) (local.get $gen1)) |
| 152 |
(call $print-with-next (i32.const 5) (local.get $gen2)) |
| 153 |
(call $print-with-next (i32.const 5) (local.get $gen1)) |
| 154 |
(call $print-with-next (i32.const 5) (local.get $gen2)) |
| 155 |
) |
| 156 |
|
| 157 |
(func $main (export "main") |
| 158 |
(call $interleave-naturals-and-fib) |
| 159 |
(call $print-first (i32.const 42) (cont.new $cont (ref.func $naturals))) |
| 160 |
(call $print-first (i32.const 42) (cont.new $cont (ref.func $fibonacci))) |
| 161 |
(call $sum-first (i32.const 101) (cont.new $cont (ref.func $naturals))) |
| 162 |
(call $log) |
| 163 |
(call $sum-first (i32.const 10) (cont.new $cont (ref.func $one-two-three))) |
| 164 |
(call $log) |
| 165 |
) |
| 166 |
) |
| 167 |
|
| 168 |
(invoke "main") |