(module $examples (type $func (;0;) (func)) (type $cont (;1;) (cont $func)) (type (;2;) (func (param i32))) (type (;3;) (func (param i32 (ref $cont)))) (type (;4;) (func (result i32 (ref $cont)))) (type (;5;) (func (param i32 (ref $cont)) (result i32))) (import "generator" "yield" (tag $yield (;0;) (type 2) (param i32))) (import "spectest" "print_i32" (func $log (;0;) (type 2))) (export "naturals" (func $naturals)) (export "one-two-three" (func $one-two-three)) (export "fibonacci" (func $fibonacci)) (export "print-first" (func $print-first)) (export "sum-first" (func $sum-first)) (func $naturals (;1;) (type $func) (local $n i32) loop $l local.get $n suspend $yield local.get $n i32.const 1 i32.add local.set $n br $l end ) (func $one-two-three (;2;) (type $func) i32.const 1 suspend $yield i32.const 2 suspend $yield i32.const 3 suspend $yield ) (func $fibonacci (;3;) (type $func) (local $a i32) (local $b i32) (local $t i32) i32.const 1 local.set $b loop $l local.get $a suspend $yield local.get $a local.set $t local.get $b local.set $a local.get $t local.get $a i32.add local.set $b br $l end ) (func $print-first (;4;) (type 3) (param $n i32) (param $k (ref $cont)) loop $l block $on_yield (type 4) (result i32 (ref $cont)) local.get $n if ;; label = @3 local.get $k resume $cont (on $yield $on_yield) end return end local.set $k call $log local.get $n i32.const -1 i32.add local.set $n br $l end unreachable ) (func $sum-first (;5;) (type 5) (param $n i32) (param $k (ref $cont)) (result i32) (local $sum i32) loop $l block $on_yield (type 4) (result i32 (ref $cont)) local.get $n if ;; label = @3 local.get $k resume $cont (on $yield $on_yield) end local.get $sum return end local.set $k local.get $sum i32.add local.set $sum local.get $n i32.const -1 i32.add local.set $n br $l end unreachable ) )