Skip to content

Commit

Permalink
Update examples to conform with the recent changes to the syntax (con…
Browse files Browse the repository at this point in the history
…trol-lwt.wast still needs to be refactored to use cont.bind+new syntax) (#30)
  • Loading branch information
dhil authored Apr 11, 2023
1 parent 7ad4f4b commit d24a9bd
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 100 deletions.
30 changes: 15 additions & 15 deletions proposals/continuations/examples/actor-lwt.wast
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
(loop $l
(if (i32.eqz (local.get $n))
(then (suspend $send (i32.const 42) (local.get $p)))
(else (local.set $p (suspend $spawn (cont.bind (type $cont) (local.get $p) (cont.new (type $i-cont) (ref.func $next)))))
(else (local.set $p (suspend $spawn (cont.bind $i-cont $cont (local.get $p) (cont.new $i-cont (ref.func $next)))))
(local.set $n (i32.sub (local.get $n) (i32.const 1)))
(br $l))
)
Expand Down Expand Up @@ -274,7 +274,7 @@
(loop $l
(if (i32.eqz (local.get $n))
(then (suspend $send (i32.const 42) (local.get $p)))
(else (local.set $p (suspend $spawn (cont.bind (type $cont) (local.get $p) (cont.new (type $i-cont) (ref.func $next)))))
(else (local.set $p (suspend $spawn (cont.bind $i-cont $cont (local.get $p) (cont.new $i-cont (ref.func $next)))))
(local.set $n (i32.sub (local.get $n) (i32.const 1)))
(br $l))
)
Expand Down Expand Up @@ -371,7 +371,7 @@
(if (call $queue-empty) (then (return)))
(block $on_yield (result (ref $cont))
(block $on_fork (result (ref $cont) (ref $cont))
(resume (tag $yield $on_yield) (tag $fork $on_fork)
(resume $cont (tag $yield $on_yield) (tag $fork $on_fork)
(call $dequeue)
)
(br $l) ;; thread terminated
Expand Down Expand Up @@ -498,11 +498,11 @@
(block $on_spawn (result (ref $cont) (ref $i-cont))
(block $on_send (result i32 i32 (ref $cont))
(block $on_recv (result (ref $i-cont))
(resume (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $nextk)
(resume $cont (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $nextk)
)
(return)
) ;; $on_recv (result (ref $i-cont))
Expand All @@ -514,7 +514,7 @@
(br $blocked))
)
)
(local.set $nextk (cont.bind (type $cont) (call $recv-from-mb (local.get $mine)) (local.get $ik)))
(local.set $nextk (cont.bind $i-cont $cont (call $recv-from-mb (local.get $mine)) (local.get $ik)))
(br $l)
) ;; $on_send (result i32 i32 (ref $cont))
(local.set $k)
Expand All @@ -526,15 +526,15 @@
(local.set $you)
(call $new-mb)
(local.set $yours)
(suspend $fork (cont.bind (type $cont)
(suspend $fork (cont.bind $ic-cont $cont
(local.get $yours)
(local.get $you)
(cont.new (type $ic-cont) (ref.func $actk))))
(local.set $nextk (cont.bind (type $cont) (local.get $yours) (local.get $ik)))
(cont.new $ic-cont (ref.func $actk))))
(local.set $nextk (cont.bind $i-cont $cont (local.get $yours) (local.get $ik)))
(br $l)
) ;; $on_self (result (ref $i-cont))
(local.set $ik)
(local.set $nextk (cont.bind (type $cont) (local.get $mine) (local.get $ik)))
(local.set $nextk (cont.bind $i-cont $cont (local.get $mine) (local.get $ik)))
(br $l)
)
)
Expand All @@ -560,7 +560,7 @@
(func $scheduler (import "scheduler" "run") (param $k (ref $cont)))

(func $run-actor (export "run-actor") (param $k (ref $cont))
(call $scheduler (cont.bind (type $cont) (local.get $k) (cont.new (type $cont-cont) (ref.func $act))))
(call $scheduler (cont.bind $cont-cont $cont (local.get $k) (cont.new $cont-cont (ref.func $act))))
)
)
(register "actor-scheduler")
Expand All @@ -578,7 +578,7 @@
(func $chain (import "chain" "chain") (param $n i32))

(func $run-chain (export "run-chain") (param $n i32)
(call $run-actor (cont.bind (type $cont) (local.get $n) (cont.new (type $i-cont) (ref.func $chain))))
(call $run-actor (cont.bind $i-cont $cont (local.get $n) (cont.new $i-cont (ref.func $chain))))
)
)

Expand Down
24 changes: 12 additions & 12 deletions proposals/continuations/examples/actor.wast
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
(loop $l
(if (i32.eqz (local.get $n))
(then (suspend $send (i32.const 42) (local.get $p)))
(else (local.set $p (suspend $spawn (cont.bind (type $cont) (local.get $p) (cont.new (type $i-cont) (ref.func $next)))))
(else (local.set $p (suspend $spawn (cont.bind $i-cont $cont (local.get $p) (cont.new $i-cont (ref.func $next)))))
(local.set $n (i32.sub (local.get $n) (i32.const 1)))
(br $l))
)
Expand Down Expand Up @@ -296,10 +296,10 @@
(local $res i32)
(suspend $recv)
(local.set $res)
(resume (local.get $res) (local.get $ik))
(resume $i-cont (local.get $res) (local.get $ik))
)
(func $recv-again (param $ik (ref $i-cont)) (result (ref $cont))
(cont.bind (type $cont) (local.get $ik) (cont.new (type $i-cont-cont) (ref.func $recv-againf)))
(cont.bind $i-cont-cont $cont (local.get $ik) (cont.new $i-cont-cont (ref.func $recv-againf)))
)

;; There are multiple ways of avoiding the need for
Expand Down Expand Up @@ -329,11 +329,11 @@
(block $on_spawn (result (ref $cont) (ref $i-cont))
(block $on_send (result i32 i32 (ref $cont))
(block $on_recv (result (ref $i-cont))
(resume (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $nextk)
(resume $cont (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $nextk)
)
(local.set $mine (call $dequeue-mb))
(local.set $nextk (call $dequeue-k))
Expand All @@ -348,7 +348,7 @@
(local.set $nextk (call $dequeue-k))
(br $l))
)
(local.set $nextk (cont.bind (type $cont) (call $recv-from-mb (local.get $mine)) (local.get $ik)))
(local.set $nextk (cont.bind $i-cont $cont (call $recv-from-mb (local.get $mine)) (local.get $ik)))
(br $l)
) ;; $on_send (result i32 i32 (ref $cont))
(local.set $k)
Expand All @@ -362,11 +362,11 @@
(local.set $yours)
(call $enqueue-mb (local.get $yours))
(call $enqueue-k (local.get $you))
(local.set $nextk (cont.bind (type $cont) (local.get $yours) (local.get $ik)))
(local.set $nextk (cont.bind $i-cont $cont (local.get $yours) (local.get $ik)))
(br $l)
) ;; $on_self (result (ref $i-cont))
(local.set $ik)
(local.set $nextk (cont.bind (type $cont) (local.get $mine) (local.get $ik)))
(local.set $nextk (cont.bind $i-cont $cont (local.get $mine) (local.get $ik)))
(br $l)
)
)
Expand All @@ -388,7 +388,7 @@
(func $chain (import "chain" "chain") (param $n i32))

(func $run-chain (export "run-chain") (param $n i32)
(call $act (cont.bind (type $cont) (local.get $n) (cont.new (type $i-cont) (ref.func $chain))))
(call $act (cont.bind $i-cont $cont (local.get $n) (cont.new $i-cont (ref.func $chain))))
)
)

Expand Down
26 changes: 13 additions & 13 deletions proposals/continuations/examples/async-await.wast
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@
(local $y i32)

(call $log (i32.const -1))
(local.set $p (suspend $async (cont.bind (type $i-cont) (i32.const 1) (i32.const 3) (cont.new (type $iii-cont) (ref.func $sum)))))
(local.set $p (suspend $async (cont.bind $iii-cont $i-cont (i32.const 1) (i32.const 3) (cont.new $iii-cont (ref.func $sum)))))
(call $log (i32.const -2))
(local.set $q (suspend $async (cont.bind (type $i-cont) (i32.const 5) (i32.const 7) (cont.new (type $iii-cont) (ref.func $sum)))))
(local.set $q (suspend $async (cont.bind $iii-cont $i-cont (i32.const 5) (i32.const 7) (cont.new $iii-cont (ref.func $sum)))))
(call $log (i32.const -3))
(local.set $r (suspend $async (cont.bind (type $i-cont) (i32.const 10) (i32.const 15) (cont.new (type $iii-cont) (ref.func $sum)))))
(local.set $r (suspend $async (cont.bind $iii-cont $i-cont (i32.const 10) (i32.const 15) (cont.new $iii-cont (ref.func $sum)))))
(call $log (i32.const -4))

(local.set $x (i32.mul (suspend $await (local.get $p))
Expand Down Expand Up @@ -217,7 +217,7 @@
(if (ref.is_null (local.get $k))
(then (return (ref.null $cont)))
)
(return (cont.bind (type $cont) (local.get $v) (local.get $k)))
(return (cont.bind $i-cont $cont (local.get $v) (local.get $k)))
)
)
(register "promise")
Expand Down Expand Up @@ -265,11 +265,11 @@
(block $on_fulfill (result i32 i32 (ref $cont))
(block $on_async (result (ref $i-cont) (ref $i-cont))
(block $on_await (result i32 (ref $i-cont))
(resume (tag $yield $on_yield)
(tag $fulfill $on_fulfill)
(tag $async $on_async)
(tag $await $on_await)
(local.get $nextk)
(resume $cont (tag $yield $on_yield)
(tag $fulfill $on_fulfill)
(tag $async $on_async)
(tag $await $on_await)
(local.get $nextk)
)
(local.set $nextk (call $dequeue))
(br $l) ;; thread terminated
Expand All @@ -278,7 +278,7 @@
(local.set $p)
(if (call $promise-fulfilled (local.get $p))
;; if promise fulfilled then run continuation partially applied to value
(then (local.set $nextk (cont.bind (type $cont) (call $promise-value (local.get $p)) (local.get $ik))))
(then (local.set $nextk (cont.bind $i-cont $cont (call $promise-value (local.get $p)) (local.get $ik))))
;; else add continuation to promise and run next continuation from the queue
(else (call $await-promise (local.get $p) (local.get $ik))
(local.set $nextk (call $dequeue)))
Expand All @@ -291,9 +291,9 @@
(call $new-promise)
(local.set $p)
;; enqueue continuation partially applied to promise
(call $enqueue (cont.bind (type $cont) (local.get $p) (local.get $ik)))
(call $enqueue (cont.bind $i-cont $cont (local.get $p) (local.get $ik)))
;; run computation partially applied to promise
(local.set $nextk (cont.bind (type $cont) (local.get $p) (local.get $ak)))
(local.set $nextk (cont.bind $i-cont $cont (local.get $p) (local.get $ak)))
(br $l)
) ;; $on_fulfill (result i32 i32 (ref $cont))
(local.set $nextk)
Expand Down Expand Up @@ -328,7 +328,7 @@
(elem declare func $run-example)

(func (export "run")
(call $scheduler (cont.new (type $cont) (ref.func $run-example)))
(call $scheduler (cont.new $cont (ref.func $run-example)))
)
)

Expand Down
36 changes: 18 additions & 18 deletions proposals/continuations/examples/fun-actor-lwt.wast
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
(loop $l
(if (i32.eqz (local.get $n))
(then (suspend $send (i32.const 42) (local.get $p)))
(else (local.set $p (suspend $spawn (cont.bind (type $cont) (local.get $p) (cont.new (type $i-cont) (ref.func $next)))))
(else (local.set $p (suspend $spawn (cont.bind $i-cont $cont (local.get $p) (cont.new $i-cont (ref.func $next)))))
(local.set $n (i32.sub (local.get $n) (i32.const 1)))
(br $l))
)
Expand Down Expand Up @@ -143,7 +143,7 @@
(if (call $queue-empty) (then (return)))
(block $on_yield (result (ref $cont))
(block $on_fork (result (ref $cont) (ref $cont))
(resume (tag $yield $on_yield) (tag $fork $on_fork)
(resume $cont (tag $yield $on_yield) (tag $fork $on_fork)
(call $dequeue)
)
(br $l) ;; thread terminated
Expand Down Expand Up @@ -262,11 +262,11 @@
(block $on_recv (result (ref $i-cont))
;; this should really be a tail call to the continuation
;; do we need a 'return_resume' operator?
(resume (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $res) (local.get $ik)
(resume $i-cont (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $res) (local.get $ik)
)
(return)
) ;; $on_recv (result (ref $i-cont))
Expand All @@ -290,10 +290,10 @@
(local.set $you)
(call $new-mb)
(local.set $yours)
(suspend $fork (cont.bind (type $cont)
(suspend $fork (cont.bind $icont-cont $cont
(local.get $yours)
(local.get $you)
(cont.new (type $icont-cont) (ref.func $act-nullary))))
(cont.new $icont-cont (ref.func $act-nullary))))
(return_call $act-res (local.get $mine) (local.get $yours) (local.get $ik))
) ;; $on_self (result (ref $i-cont))
(local.set $ik)
Expand All @@ -312,11 +312,11 @@
(block $on_recv (result (ref $i-cont))
;; this should really be a tail call to the continuation
;; do we need a 'return_resume' operator?
(resume (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $k)
(resume $cont (tag $self $on_self)
(tag $spawn $on_spawn)
(tag $send $on_send)
(tag $recv $on_recv)
(local.get $k)
)
(return)
) ;; $on_recv (result (ref $i-cont))
Expand All @@ -340,10 +340,10 @@
(local.set $you)
(call $new-mb)
(local.set $yours)
(suspend $fork (cont.bind (type $cont)
(suspend $fork (cont.bind $icont-cont $cont
(local.get $yours)
(local.get $you)
(cont.new (type $icont-cont) (ref.func $act-nullary))))
(cont.new $icont-cont (ref.func $act-nullary))))
(return_call $act-res (local.get $mine) (local.get $yours) (local.get $ik))
) ;; $on_self (result (ref $i-cont))
(local.set $ik)
Expand Down Expand Up @@ -373,7 +373,7 @@
(func $scheduler (import "scheduler" "run") (param $k (ref $cont)))

(func $run-actor (export "run-actor") (param $k (ref $cont))
(call $scheduler (cont.bind (type $cont) (local.get $k) (cont.new (type $cont-cont) (ref.func $act))))
(call $scheduler (cont.bind $cont-cont $cont (local.get $k) (cont.new $cont-cont (ref.func $act))))
)
)
(register "actor-scheduler")
Expand All @@ -391,7 +391,7 @@
(func $chain (import "chain" "chain") (param $n i32))

(func $run-chain (export "run-chain") (param $n i32)
(call $run-actor (cont.bind (type $cont) (local.get $n) (cont.new (type $i-cont) (ref.func $chain))))
(call $run-actor (cont.bind $i-cont $cont (local.get $n) (cont.new $i-cont (ref.func $chain))))
)
)

Expand Down
Loading

0 comments on commit d24a9bd

Please sign in to comment.