(module $scheduler (type $proc (;0;) (func)) (type $cont (;1;) (cont $proc)) (type (;2;) (func (param (ref $cont)))) (type (;3;) (func (result i32))) (type (;4;) (func (result (ref null $cont)))) (type (;5;) (func (result (ref $cont) (ref $cont)))) (table $queue (;0;) 0 (ref null $cont)) (tag $yield (;0;) (type $proc)) (tag $spawn (;1;) (type 2) (param (ref $cont))) (global $qdelta (;0;) i32 i32.const 10) (global $qback (;1;) (mut i32) i32.const 0) (global $qfront (;2;) (mut i32) i32.const 0) (export "yield" (tag 0)) (export "spawn" (tag 1)) (export "scheduler" (func $scheduler)) (func $queue-empty (;0;) (type 3) (result i32) global.get $qfront global.get $qback i32.eq ) (func $dequeue (;1;) (type 4) (result (ref null $cont)) (local $i i32) call $queue-empty if ;; label = @1 ref.null $cont return end global.get $qfront local.set $i local.get $i i32.const 1 i32.add global.set $qfront local.get $i table.get $queue ) (func $enqueue (;2;) (type 2) (param $k (ref $cont)) global.get $qback table.size $queue i32.eq if ;; label = @1 global.get $qfront global.get $qdelta i32.lt_u if ;; label = @2 ref.null $cont global.get $qdelta table.grow $queue drop else global.get $qback global.get $qfront i32.sub global.set $qback i32.const 0 global.get $qfront global.get $qback table.copy global.get $qback ref.null $cont global.get $qfront table.fill $queue i32.const 0 global.set $qfront end end global.get $qback local.get $k table.set $queue global.get $qback i32.const 1 i32.add global.set $qback ) (func $scheduler (;3;) (type 2) (param $main (ref $cont)) local.get $main call $enqueue loop $l call $queue-empty if ;; label = @2 return end block $on_yield (result (ref $cont)) block $on_spawn (type 5) (result (ref $cont) (ref $cont)) call $dequeue resume $cont (on $yield $on_yield) (on $spawn $on_spawn) br $l end call $enqueue call $enqueue br $l end call $enqueue br $l end ) )