Skip to content

Commit

Permalink
Fix all examples apart from delimited control (#27)
Browse files Browse the repository at this point in the history
- replaced exceptions with tags
- removed let blocks
- removed now unnecessary (unreachable)s

The output of all examples is as before.
  • Loading branch information
matijapretnar authored Apr 10, 2023
1 parent c9e60d8 commit 5331635
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 156 deletions.
60 changes: 30 additions & 30 deletions proposals/continuations/examples/actor-lwt.wast
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
(table $queue 0 (ref null $cont))
(memory 1)

(exception $too-many-mailboxes)
(tag $too-many-mailboxes)

(global $qdelta i32 (i32.const 10))

Expand Down Expand Up @@ -190,8 +190,8 @@

(func $log (import "spectest" "print_i32") (param i32))

(exception $too-many-mailboxes)
(exception $too-many-messages)
(tag $too-many-mailboxes)
(tag $too-many-messages)

(memory 1)

Expand Down Expand Up @@ -397,8 +397,8 @@

;; -1 means empty

(exception $too-many-mailboxes)
(exception $too-many-messages)
(tag $too-many-mailboxes)
(tag $too-many-messages)

(memory 1)

Expand Down Expand Up @@ -489,6 +489,10 @@
(elem declare func $actk)

(func $actk (param $mine i32) (param $nextk (ref $cont))
(local $ik (ref $i-cont))
(local $k (ref $cont))
(local $you (ref $cont))
(local $yours i32)
(loop $l
(block $on_self (result (ref $i-cont))
(block $on_spawn (result (ref $cont) (ref $i-cont))
Expand All @@ -502,39 +506,35 @@
)
(return)
) ;; $on_recv (result (ref $i-cont))
(let (local $ik (ref $i-cont))
;; block this thread until the mailbox is non-empty
(loop $blocked
(if (call $empty-mb (local.get $mine))
(then (suspend $yield)
(br $blocked))
)
(local.set $ik)
;; block this thread until the mailbox is non-empty
(loop $blocked
(if (call $empty-mb (local.get $mine))
(then (suspend $yield)
(br $blocked))
)
(local.set $nextk (cont.bind (type $cont) (call $recv-from-mb (local.get $mine)) (local.get $ik)))
)
(local.set $nextk (cont.bind (type $cont) (call $recv-from-mb (local.get $mine)) (local.get $ik)))
(br $l)
) ;; $on_send (result i32 i32 (ref $cont))
(let (param i32 i32) (local $k (ref $cont))
(call $send-to-mb)
(local.set $nextk (local.get $k))
)
(local.set $k)
(call $send-to-mb)
(local.set $nextk (local.get $k))
(br $l)
) ;; $on_spawn (result (ref $cont) (ref $i-cont))
(let (local $you (ref $cont)) (local $ik (ref $i-cont))
(call $new-mb)
(let (local $yours i32)
(suspend $fork (cont.bind (type $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)))
)
)
(local.set $ik)
(local.set $you)
(call $new-mb)
(local.set $yours)
(suspend $fork (cont.bind (type $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)))
(br $l)
) ;; $on_self (result (ref $i-cont))
(let (local $ik (ref $i-cont))
(local.set $nextk (cont.bind (type $cont) (local.get $mine) (local.get $ik)))
)
(local.set $ik)
(local.set $nextk (cont.bind (type $cont) (local.get $mine) (local.get $ik)))
(br $l)
)
)
Expand Down
56 changes: 28 additions & 28 deletions proposals/continuations/examples/actor.wast
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
(table $queue 0 (ref null $cont))
(memory 1)

(exception $too-many-mailboxes)
(tag $too-many-mailboxes)

(global $qdelta i32 (i32.const 10))

Expand Down Expand Up @@ -190,8 +190,8 @@

(func $log (import "spectest" "print_i32") (param i32))

(exception $too-many-mailboxes)
(exception $too-many-messages)
(tag $too-many-mailboxes)
(tag $too-many-messages)

(memory 1)

Expand Down Expand Up @@ -317,6 +317,10 @@

(func $run (export "run") (param $nextk (ref null $cont))
(local $mine i32) ;; current mailbox
(local $ik (ref $i-cont))
(local $k (ref $cont))
(local $you (ref $cont))
(local $yours i32)
(call $init)
(local.set $mine (call $new-mb))
(loop $l
Expand All @@ -335,38 +339,34 @@
(local.set $nextk (call $dequeue-k))
(br $l)
) ;; $on_recv (result (ref $i-cont))
(let (local $ik (ref $i-cont))
;; block this thread until the mailbox is non-empty
(if (call $empty-mb (local.get $mine))
(then (call $enqueue-mb (local.get $mine))
(call $enqueue-k (call $recv-again (local.get $ik)))
(local.set $mine (call $dequeue-mb))
(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 $ik)
;; block this thread until the mailbox is non-empty
(if (call $empty-mb (local.get $mine))
(then (call $enqueue-mb (local.get $mine))
(call $enqueue-k (call $recv-again (local.get $ik)))
(local.set $mine (call $dequeue-mb))
(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)))
(br $l)
) ;; $on_send (result i32 i32 (ref $cont))
(let (param i32 i32) (local $k (ref $cont))
(call $send-to-mb)
(local.set $nextk (local.get $k))
)
(local.set $k)
(call $send-to-mb)
(local.set $nextk (local.get $k))
(br $l)
) ;; $on_spawn (result (ref $cont) (ref $i-cont))
(let (local $you (ref $cont)) (local $ik (ref $i-cont))
(call $new-mb)
(let (local $yours i32)
(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 $ik)
(local.set $you)
(call $new-mb)
(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)))
(br $l)
) ;; $on_self (result (ref $i-cont))
(let (local $ik (ref $i-cont))
(local.set $nextk (cont.bind (type $cont) (local.get $mine) (local.get $ik)))
)
(local.set $ik)
(local.set $nextk (cont.bind (type $cont) (local.get $mine) (local.get $ik)))
(br $l)
)
)
Expand Down
59 changes: 31 additions & 28 deletions proposals/continuations/examples/async-await.wast
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@
;; a simplistic implementation of promises that assumes a maximum of
;; 1000 promises and a maximum of one observer per promise

(exception $too-many-promises)
(exception $too-many-observers)
(tag $too-many-promises)
(tag $too-many-observers)

(global $num-promises (mut i32) (i32.const 0))
(global $max-promises i32 (i32.const 1000))
Expand Down Expand Up @@ -254,6 +254,11 @@
(func $fulfill-promise (import "promise" "fulfill") (param $p i32) (param $v i32) (result (ref null $cont)))

(func $run (export "run") (param $nextk (ref null $cont))
(local $p i32)
(local $v i32)
(local $ik (ref $i-cont))
(local $ak (ref $i-cont))
(local $k (ref null $cont))
(loop $l
(if (ref.is_null (local.get $nextk)) (then (return)))
(block $on_yield (result (ref $cont))
Expand All @@ -269,38 +274,36 @@
(local.set $nextk (call $dequeue))
(br $l) ;; thread terminated
) ;; $on_await (result i32 (ref $i-cont))
(let (local $p i32) (local $ik (ref $i-cont))
(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))))
;; 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)))
)
(local.set $ik)
(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))))
;; 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)))
)
(br $l)
) ;; $on_async (result (ref $i-func) (ref $i-cont))
(let (local $ak (ref $i-cont)) (local $ik (ref $i-cont))
;; create new promise
(call $new-promise)
(let (local $p i32)
;; enqueue continuation partially applied to promise
(call $enqueue (cont.bind (type $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 $ik)
(local.set $ak)
;; create new promise
(call $new-promise)
(local.set $p)
;; enqueue continuation partially applied to promise
(call $enqueue (cont.bind (type $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)))
(br $l)
) ;; $on_fulfill (result i32 i32 (ref $cont))
(local.set $nextk)
(let (local $p i32) (local $v i32)
(call $fulfill-promise (local.get $p) (local.get $v))
(let (local $k (ref null $cont))
(if (ref.is_null (local.get $k))
(then)
(else (call $enqueue (local.get $k)))
)
)
(local.set $v)
(local.set $p)
(call $fulfill-promise (local.get $p) (local.get $v))
(local.set $k)
(if (ref.is_null (local.get $k))
(then)
(else (call $enqueue (local.get $k)))
)
(br $l)
) ;; $on_yield (result (ref $cont))
Expand Down
Loading

0 comments on commit 5331635

Please sign in to comment.