| 1 |
(module $pipes |
| 2 |
(type $pfun (;0;) (func (result i32))) |
| 3 |
(type $cfun (;1;) (func (param i32) (result i32))) |
| 4 |
(type $producer (;2;) (cont $pfun)) |
| 5 |
(type $consumer (;3;) (cont $cfun)) |
| 6 |
(type (;4;) (func (param i32))) |
| 7 |
(type (;5;) (func (param (ref $producer) (ref $consumer)))) |
| 8 |
(type (;6;) (func (result i32 (ref $producer)))) |
| 9 |
(tag $send (;0;) (type 4) (param i32)) |
| 10 |
(tag $receive (;1;) (type $pfun) (result i32)) |
| 11 |
(export "send" (tag 0)) |
| 12 |
(export "receive" (tag 1)) |
| 13 |
(export "pipe" (func $piper)) |
| 14 |
(func $piper (;0;) (type 5) (param $p (ref $producer)) (param $c (ref $consumer)) |
| 15 |
(local $n i32) (local $consuming i32) |
| 16 |
i32.const -1 |
| 17 |
local.set $n |
| 18 |
i32.const 1 |
| 19 |
local.set $consuming |
| 20 |
loop $l |
| 21 |
local.get $consuming |
| 22 |
if ;; label = @2 |
| 23 |
block $on-receive (result (ref $consumer)) |
| 24 |
local.get $n |
| 25 |
local.get $c |
| 26 |
resume $consumer (on $receive $on-receive) |
| 27 |
return |
| 28 |
end |
| 29 |
local.set $c |
| 30 |
i32.const 0 |
| 31 |
local.set $consuming |
| 32 |
br $l |
| 33 |
end |
| 34 |
block $on-send (type 6) (result i32 (ref $producer)) |
| 35 |
local.get $p |
| 36 |
resume $producer (on $send $on-send) |
| 37 |
return |
| 38 |
end |
| 39 |
local.set $p |
| 40 |
local.set $n |
| 41 |
i32.const 1 |
| 42 |
local.set $consuming |
| 43 |
br $l |
| 44 |
end |
| 45 |
) |
| 46 |
) |