| 1 |
;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching |
| 2 |
|
| 3 |
;; async-await interface |
| 4 |
(module $async-await |
| 5 |
(type $i-func (func (param i32))) |
| 6 |
(type $i-cont (cont $i-func)) |
| 7 |
|
| 8 |
;; We use yield and fulfill to simulate asynchronous operations. |
| 9 |
;; |
| 10 |
;; Given a suitable asynchronous I/O API, they needn't be exposed to |
| 11 |
;; user code. |
| 12 |
;; |
| 13 |
;; yield : [] -> [] |
| 14 |
;; fulfill : [i32] -> [i32] |
| 15 |
(tag $yield (export "yield")) |
| 16 |
(tag $fulfill (export "fulfill") (param i32) (param i32)) |
| 17 |
|
| 18 |
;; async : [cont ([i32] -> [])] -> [i32] |
| 19 |
;; await : [i32] -> [i32] |
| 20 |
(tag $async (export "async") (param (ref $i-cont)) (result i32)) |
| 21 |
(tag $await (export "await") (param i32) (result i32)) |
| 22 |
) |
| 23 |
(register "async-await") |
| 24 |
|
| 25 |
(module $example |
| 26 |
(type $i-func (func (param i32))) |
| 27 |
(type $i-cont (cont $i-func)) |
| 28 |
|
| 29 |
(type $iii-fun (func (param i32 i32 i32))) |
| 30 |
(type $iii-cont (cont $iii-fun)) |
| 31 |
|
| 32 |
;; yield : [] -> [] |
| 33 |
;; fulfill : [i32] -> [i32] |
| 34 |
;; async : [cont ([i32] -> [])] -> [i32] |
| 35 |
;; await : [i32] -> [i32] |
| 36 |
(tag $yield (import "async-await" "yield")) |
| 37 |
(tag $fulfill (import "async-await" "fulfill") (param i32) (param i32)) |
| 38 |
(tag $async (import "async-await" "async") (param (ref $i-cont)) (result i32)) |
| 39 |
(tag $await (import "async-await" "await") (param i32) (result i32)) |
| 40 |
|
| 41 |
(func $log (import "spectest" "print_i32") (param i32)) |
| 42 |
|
| 43 |
(elem declare func $sum) |
| 44 |
|
| 45 |
;; an asynchronous function that computes i + i+1 + ... + j |
| 46 |
;; |
| 47 |
;; (instead of computing synchronously, it allows other computations |
| 48 |
;; to execute each time round the loop) |
| 49 |
;; |
| 50 |
;; the final result is written to the promise $p |
| 51 |
(func $sum (param $i i32) (param $j i32) (param $p i32) |
| 52 |
(local $a i32) |
| 53 |
(loop $l |
| 54 |
(call $log (local.get $i)) |
| 55 |
(local.set $a (i32.add (local.get $a) (local.get $i))) |
| 56 |
(local.set $i (i32.add (local.get $i) (i32.const 1))) |
| 57 |
(if (i32.le_u (local.get $i) (local.get $j)) |
| 58 |
(then (suspend $yield) |
| 59 |
(br $l)) |
| 60 |
) |
| 61 |
) |
| 62 |
(suspend $fulfill (local.get $p) (local.get $a)) |
| 63 |
) |
| 64 |
|
| 65 |
;; compute p = 1+..+3; q = 5+..+7; r = 10+...+15 asynchronously |
| 66 |
;; once p and q have finished computing, compute x = p*q |
| 67 |
;; once r has finished computing, return x+r |
| 68 |
(func $run (export "run") |
| 69 |
(local $p i32) |
| 70 |
(local $q i32) |
| 71 |
(local $r i32) |
| 72 |
|
| 73 |
(local $x i32) |
| 74 |
(local $y i32) |
| 75 |
|
| 76 |
(call $log (i32.const -1)) |
| 77 |
(local.set $p (suspend $async (cont.bind $iii-cont $i-cont (i32.const 1) (i32.const 3) (cont.new $iii-cont (ref.func $sum))))) |
| 78 |
(call $log (i32.const -2)) |
| 79 |
(local.set $q (suspend $async (cont.bind $iii-cont $i-cont (i32.const 5) (i32.const 7) (cont.new $iii-cont (ref.func $sum))))) |
| 80 |
(call $log (i32.const -3)) |
| 81 |
(local.set $r (suspend $async (cont.bind $iii-cont $i-cont (i32.const 10) (i32.const 15) (cont.new $iii-cont (ref.func $sum))))) |
| 82 |
(call $log (i32.const -4)) |
| 83 |
|
| 84 |
(local.set $x (i32.mul (suspend $await (local.get $p)) |
| 85 |
(suspend $await (local.get $q)))) |
| 86 |
|
| 87 |
(call $log (i32.const -5)) |
| 88 |
|
| 89 |
(local.set $y (i32.add (suspend $await (local.get $r)) (local.get $x))) |
| 90 |
|
| 91 |
(call $log (i32.const -6)) |
| 92 |
(call $log (local.get $y)) |
| 93 |
(call $log (i32.const -7)) |
| 94 |
) |
| 95 |
) |
| 96 |
(register "example") |
| 97 |
|
| 98 |
;; queue of threads |
| 99 |
(module $queue |
| 100 |
(type $func (func)) |
| 101 |
(type $cont (cont $func)) |
| 102 |
|
| 103 |
;; Table as simple queue (keeping it simple, no ring buffer) |
| 104 |
(table $queue 0 (ref null $cont)) |
| 105 |
(global $qdelta i32 (i32.const 10)) |
| 106 |
(global $qback (mut i32) (i32.const 0)) |
| 107 |
(global $qfront (mut i32) (i32.const 0)) |
| 108 |
|
| 109 |
(func $queue-empty (export "queue-empty") (result i32) |
| 110 |
(i32.eq (global.get $qfront) (global.get $qback)) |
| 111 |
) |
| 112 |
|
| 113 |
(func $dequeue (export "dequeue") (result (ref null $cont)) |
| 114 |
(local $i i32) |
| 115 |
(if (call $queue-empty) |
| 116 |
(then (return (ref.null $cont))) |
| 117 |
) |
| 118 |
(local.set $i (global.get $qfront)) |
| 119 |
(global.set $qfront (i32.add (local.get $i) (i32.const 1))) |
| 120 |
(table.get $queue (local.get $i)) |
| 121 |
) |
| 122 |
|
| 123 |
(func $enqueue (export "enqueue") (param $k (ref null $cont)) |
| 124 |
;; Check if queue is full |
| 125 |
(if (i32.eq (global.get $qback) (table.size $queue)) |
| 126 |
(then |
| 127 |
;; Check if there is enough space in the front to compact |
| 128 |
(if (i32.lt_u (global.get $qfront) (global.get $qdelta)) |
| 129 |
(then |
| 130 |
;; Space is below threshold, grow table instead |
| 131 |
(drop (table.grow $queue (ref.null $cont) (global.get $qdelta))) |
| 132 |
) |
| 133 |
(else |
| 134 |
;; Enough space, move entries up to head of table |
| 135 |
(global.set $qback (i32.sub (global.get $qback) (global.get $qfront))) |
| 136 |
(table.copy $queue $queue |
| 137 |
(i32.const 0) ;; dest = new front = 0 |
| 138 |
(global.get $qfront) ;; src = old front |
| 139 |
(global.get $qback) ;; len = new back = old back - old front |
| 140 |
) |
| 141 |
(table.fill $queue ;; null out old entries to avoid leaks |
| 142 |
(global.get $qback) ;; start = new back |
| 143 |
(ref.null $cont) ;; init value |
| 144 |
(global.get $qfront) ;; len = old front = old front - new front |
| 145 |
) |
| 146 |
(global.set $qfront (i32.const 0)) |
| 147 |
) |
| 148 |
) |
| 149 |
) |
| 150 |
) |
| 151 |
(table.set $queue (global.get $qback) (local.get $k)) |
| 152 |
(global.set $qback (i32.add (global.get $qback) (i32.const 1))) |
| 153 |
) |
| 154 |
) |
| 155 |
(register "queue") |
| 156 |
|
| 157 |
;; promises |
| 158 |
(module $promise |
| 159 |
(type $func (func)) |
| 160 |
(type $cont (cont $func)) |
| 161 |
|
| 162 |
(type $i-func (func (param i32))) |
| 163 |
(type $i-cont (cont $i-func)) |
| 164 |
|
| 165 |
;; a simplistic implementation of promises that assumes a maximum of |
| 166 |
;; 1000 promises and a maximum of one observer per promise |
| 167 |
|
| 168 |
(tag $too-many-promises) |
| 169 |
(tag $too-many-observers) |
| 170 |
|
| 171 |
(global $num-promises (mut i32) (i32.const 0)) |
| 172 |
(global $max-promises i32 (i32.const 1000)) |
| 173 |
(table $observers 1000 (ref null $i-cont)) ;; observers waiting for promises to be fulfilled |
| 174 |
(memory 1) ;; promise values |
| 175 |
|
| 176 |
;; create and return a new promise |
| 177 |
(func $new (export "new") (result i32) |
| 178 |
(local $offset i32) |
| 179 |
(local $p i32) |
| 180 |
(if (i32.eq (global.get $num-promises) (global.get $max-promises)) |
| 181 |
(then (throw $too-many-promises))) |
| 182 |
(local.set $p (global.get $num-promises)) |
| 183 |
(local.set $offset (i32.mul (local.get $p) (i32.const 4))) |
| 184 |
(table.set $observers (local.get $p) (ref.null $i-cont)) |
| 185 |
(i32.store (local.get $offset) (i32.const -1)) |
| 186 |
(global.set $num-promises (i32.add (local.get $p) (i32.const 1))) |
| 187 |
(return (local.get $p)) |
| 188 |
) |
| 189 |
|
| 190 |
;; check whether promise $p is fulfilled |
| 191 |
(func $fulfilled (export "fulfilled") (param $p i32) (result i32) |
| 192 |
(local $offset i32) |
| 193 |
(local.set $offset (i32.mul (local.get $p) (i32.const 4))) |
| 194 |
(i32.ne (i32.load (local.get $offset)) (i32.const -1)) |
| 195 |
) |
| 196 |
|
| 197 |
;; current value of promise $p |
| 198 |
(func $read (export "read") (param $p i32) (result i32) |
| 199 |
(local $offset i32) |
| 200 |
(local.set $offset (i32.mul (local.get $p) (i32.const 4))) |
| 201 |
(i32.load (local.get $offset)) |
| 202 |
) |
| 203 |
|
| 204 |
;; register an observer for when promise $p is fulfilled |
| 205 |
(func $await (export "await") (param $p i32) (param $k (ref $i-cont)) |
| 206 |
(if (ref.is_null (table.get $observers (local.get $p))) |
| 207 |
(then (table.set $observers (local.get $p) (local.get $k))) |
| 208 |
(else (throw $too-many-observers)) |
| 209 |
) |
| 210 |
) |
| 211 |
|
| 212 |
;; fulfill promise $p with value $v |
| 213 |
(func $fulfill (export "fulfill") (param $p i32) (param $v i32) (result (ref null $cont)) |
| 214 |
(local $offset i32) |
| 215 |
(local $k (ref null $i-cont)) |
| 216 |
(local.set $offset (i32.mul (local.get $p) (i32.const 4))) |
| 217 |
(i32.store (local.get $offset) (local.get $v)) |
| 218 |
(local.set $k (table.get $observers (local.get $p))) |
| 219 |
(if (ref.is_null (local.get $k)) |
| 220 |
(then (return (ref.null $cont))) |
| 221 |
) |
| 222 |
(return (cont.bind $i-cont $cont (local.get $v) (local.get $k))) |
| 223 |
) |
| 224 |
) |
| 225 |
(register "promise") |
| 226 |
|
| 227 |
;; async-await scheduler |
| 228 |
(module $scheduler |
| 229 |
(type $func (func)) |
| 230 |
(type $cont (cont $func)) |
| 231 |
|
| 232 |
(type $i-func (func (param i32))) |
| 233 |
(type $i-cont (cont $i-func)) |
| 234 |
|
| 235 |
;; async-await interface |
| 236 |
;; |
| 237 |
;; yield : [] -> [] |
| 238 |
;; fulfill : [i32] -> [i32] |
| 239 |
;; async : [cont ([i32] -> [])] -> [i32] |
| 240 |
;; await : [i32] -> [i32] |
| 241 |
(tag $yield (import "async-await" "yield")) |
| 242 |
(tag $fulfill (import "async-await" "fulfill") (param i32) (param i32)) |
| 243 |
(tag $async (import "async-await" "async") (param (ref $i-cont)) (result i32)) |
| 244 |
(tag $await (import "async-await" "await") (param i32) (result i32)) |
| 245 |
|
| 246 |
;; queue interface |
| 247 |
(func $queue-empty (import "queue" "queue-empty") (result i32)) |
| 248 |
(func $dequeue (import "queue" "dequeue") (result (ref null $cont))) |
| 249 |
(func $enqueue (import "queue" "enqueue") (param $k (ref null $cont))) |
| 250 |
|
| 251 |
;; promise interface |
| 252 |
(func $new-promise (import "promise" "new") (result i32)) |
| 253 |
(func $promise-fulfilled (import "promise" "fulfilled") (param $p i32) (result i32)) |
| 254 |
(func $promise-value (import "promise" "read") (param $p i32) (result i32)) |
| 255 |
(func $await-promise (import "promise" "await") (param $p i32) (param $k (ref $i-cont))) |
| 256 |
(func $fulfill-promise (import "promise" "fulfill") (param $p i32) (param $v i32) (result (ref null $cont))) |
| 257 |
|
| 258 |
(func $run (export "run") (param $nextk (ref null $cont)) |
| 259 |
(local $p i32) |
| 260 |
(local $v i32) |
| 261 |
(local $ik (ref $i-cont)) |
| 262 |
(local $ak (ref $i-cont)) |
| 263 |
(local $k (ref null $cont)) |
| 264 |
(loop $l |
| 265 |
(if (ref.is_null (local.get $nextk)) (then (return))) |
| 266 |
(block $on_yield (result (ref $cont)) |
| 267 |
(block $on_fulfill (result i32 i32 (ref $cont)) |
| 268 |
(block $on_async (result (ref $i-cont) (ref $i-cont)) |
| 269 |
(block $on_await (result i32 (ref $i-cont)) |
| 270 |
(resume $cont (on $yield $on_yield) |
| 271 |
(on $fulfill $on_fulfill) |
| 272 |
(on $async $on_async) |
| 273 |
(on $await $on_await) |
| 274 |
(local.get $nextk) |
| 275 |
) |
| 276 |
(local.set $nextk (call $dequeue)) |
| 277 |
(br $l) ;; thread terminated |
| 278 |
) ;; $on_await (result i32 (ref $i-cont)) |
| 279 |
(local.set $ik) |
| 280 |
(local.set $p) |
| 281 |
(if (call $promise-fulfilled (local.get $p)) |
| 282 |
;; if promise fulfilled then run continuation partially applied to value |
| 283 |
(then (local.set $nextk (cont.bind $i-cont $cont (call $promise-value (local.get $p)) (local.get $ik)))) |
| 284 |
;; else add continuation to promise and run next continuation from the queue |
| 285 |
(else (call $await-promise (local.get $p) (local.get $ik)) |
| 286 |
(local.set $nextk (call $dequeue))) |
| 287 |
) |
| 288 |
(br $l) |
| 289 |
) ;; $on_async (result (ref $i-func) (ref $i-cont)) |
| 290 |
(local.set $ik) |
| 291 |
(local.set $ak) |
| 292 |
;; create new promise |
| 293 |
(call $new-promise) |
| 294 |
(local.set $p) |
| 295 |
;; enqueue continuation partially applied to promise |
| 296 |
(call $enqueue (cont.bind $i-cont $cont (local.get $p) (local.get $ik))) |
| 297 |
;; run computation partially applied to promise |
| 298 |
(local.set $nextk (cont.bind $i-cont $cont (local.get $p) (local.get $ak))) |
| 299 |
(br $l) |
| 300 |
) ;; $on_fulfill (result i32 i32 (ref $cont)) |
| 301 |
(local.set $nextk) |
| 302 |
(local.set $v) |
| 303 |
(local.set $p) |
| 304 |
(call $fulfill-promise (local.get $p) (local.get $v)) |
| 305 |
(local.set $k) |
| 306 |
(if (ref.is_null (local.get $k)) |
| 307 |
(then) |
| 308 |
(else (call $enqueue (local.get $k))) |
| 309 |
) |
| 310 |
(br $l) |
| 311 |
) ;; $on_yield (result (ref $cont)) |
| 312 |
(call $enqueue) ;; current thread |
| 313 |
(local.set $nextk (call $dequeue)) ;; next thread |
| 314 |
(br $l) |
| 315 |
) |
| 316 |
) |
| 317 |
) |
| 318 |
(register "scheduler") |
| 319 |
|
| 320 |
(module |
| 321 |
(type $func (func)) |
| 322 |
(type $cont (cont $func)) |
| 323 |
|
| 324 |
(func $scheduler (import "scheduler" "run") (param $nextk (ref null $cont))) |
| 325 |
|
| 326 |
(func $log (import "spectest" "print_i32") (param i32)) |
| 327 |
|
| 328 |
(func $run-example (import "example" "run")) |
| 329 |
|
| 330 |
(elem declare func $run-example) |
| 331 |
|
| 332 |
(func (export "run") |
| 333 |
(call $scheduler (cont.new $cont (ref.func $run-example))) |
| 334 |
) |
| 335 |
) |
| 336 |
|
| 337 |
(invoke "run") |