Skip to content

Commit a324dba

Browse files
authored
Fix control-lwt example (#30)
1 parent b0a7c47 commit a324dba

File tree

1 file changed

+55
-40
lines changed

1 file changed

+55
-40
lines changed

proposals/continuations/examples/control-lwt.wast

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
;;
2525
;; (Technically this is control0/prompt0 rather than
2626
;; control/prompt.)
27-
(tag $control (export "control") (param (ref $cont-func))) ;; control : ([cont ([] -> [])] -> []) -> []
27+
(tag $control (export "control") (param (ref $cont-cont))) ;; control : ([cont ([] -> [])] -> []) -> []
2828
(func $prompt (export "prompt") (param $nextk (ref null $cont)) ;; prompt : cont ([] -> []) -> []
29-
(block $on_control (result (ref $cont-func) (ref $cont))
30-
(resume (tag $control $on_control)
31-
(local.get $nextk))
29+
(local $h (ref $cont-cont))
30+
(local $k (ref $cont))
31+
(block $on_control (result (ref $cont-cont) (ref $cont))
32+
(resume $cont (tag $control $on_control)
33+
(local.get $nextk))
3234
(return)
3335
) ;; $on_control (param (ref $cont-func) (ref $cont))
34-
(let (local $h (ref $cont-func)) (local $k (ref $cont))
35-
(call_ref (local.get $k) (local.get $h))
36-
)
36+
(local.set $k)
37+
(local.set $h)
38+
(resume $cont-cont (local.get $k) (local.get $h))
3739
)
3840
)
3941
(register "control")
@@ -57,44 +59,44 @@
5759

5860
(func $main (export "main") (param $yield (ref $func)) (param $fork (ref $cont-func))
5961
(call $log (i32.const 0))
60-
(call_ref
61-
(cont.bind (type $cont) (local.get $yield) (local.get $fork)
62-
(cont.new (type $func-cont-func-cont) (ref.func $thread1)))
62+
(call_ref $cont-func
63+
(cont.bind $func-cont-func-cont $cont (local.get $yield) (local.get $fork)
64+
(cont.new $func-cont-func-cont (ref.func $thread1)))
6365
(local.get $fork))
6466
(call $log (i32.const 1))
65-
(call_ref
66-
(cont.bind (type $cont) (local.get $yield) (local.get $fork)
67-
(cont.new (type $func-cont-func-cont) (ref.func $thread2)))
67+
(call_ref $cont-func
68+
(cont.bind $func-cont-func-cont $cont (local.get $yield) (local.get $fork)
69+
(cont.new $func-cont-func-cont (ref.func $thread2)))
6870
(local.get $fork))
6971
(call $log (i32.const 2))
70-
(call_ref
71-
(cont.bind (type $cont) (local.get $yield) (local.get $fork)
72-
(cont.new (type $func-cont-func-cont) (ref.func $thread3)))
72+
(call_ref $cont-func
73+
(cont.bind $func-cont-func-cont $cont (local.get $yield) (local.get $fork)
74+
(cont.new $func-cont-func-cont (ref.func $thread3)))
7375
(local.get $fork))
7476
(call $log (i32.const 3))
7577
)
7678

7779
(func $thread1 (param $yield (ref $func)) (param $fork (ref $cont-func))
7880
(call $log (i32.const 10))
79-
(call_ref (local.get $yield))
81+
(call_ref $func (local.get $yield))
8082
(call $log (i32.const 11))
81-
(call_ref (local.get $yield))
83+
(call_ref $func (local.get $yield))
8284
(call $log (i32.const 12))
8385
)
8486

8587
(func $thread2 (param $yield (ref $func)) (param $fork (ref $cont-func))
8688
(call $log (i32.const 20))
87-
(call_ref (local.get $yield))
89+
(call_ref $func (local.get $yield))
8890
(call $log (i32.const 21))
89-
(call_ref (local.get $yield))
91+
(call_ref $func (local.get $yield))
9092
(call $log (i32.const 22))
9193
)
9294

9395
(func $thread3 (param $yield (ref $func)) (param $fork (ref $cont-func))
9496
(call $log (i32.const 30))
95-
(call_ref (local.get $yield))
97+
(call_ref $func (local.get $yield))
9698
(call $log (i32.const 31))
97-
(call_ref (local.get $yield))
99+
(call_ref $func (local.get $yield))
98100
(call $log (i32.const 32))
99101
)
100102
)
@@ -170,6 +172,9 @@
170172
(type $func-cont-func-func (func (param (ref $func)) (param (ref $cont-func)))) ;; ([] -> []) -> ([cont ([] -> [])] -> []) -> []
171173
(type $func-cont-func-cont (cont $func-cont-func-func)) ;; cont (([] -> []) -> ([cont ([] -> [])] -> []) -> [])
172174

175+
(type $func-cont-cont (func (param (ref $cont)) (param (ref $cont))))
176+
(type $cont-cont-func (cont $func-cont-cont))
177+
173178
(func $log (import "spectest" "print_i32") (param i32))
174179

175180
;; queue interface
@@ -184,7 +189,7 @@
184189
$fork-sync $fork-kt $fork-tk $fork-ykt $fork-ytk)
185190

186191
;; control/prompt interface
187-
(tag $control (import "control" "control") (param (ref $cont-func))) ;; control : ([cont ([] -> [])] -> []) -> []
192+
(tag $control (import "control" "control") (param (ref $cont-cont))) ;; control : ([cont ([] -> [])] -> []) -> []
188193
(func $prompt (import "control" "prompt") (param $nextk (ref null $cont))) ;; prompt : cont ([] -> []) -> []
189194

190195
;; generic boilerplate scheduler
@@ -215,18 +220,20 @@
215220
(call $scheduler (local.get $k))
216221
)
217222
(func $yield-sync
218-
(suspend $control (ref.func $handle-yield))
223+
(suspend $control (cont.new $cont-cont (ref.func $handle-yield)))
219224
)
220225
(func $handle-fork-sync (param $t (ref $cont)) (param $k (ref $cont))
221226
(call $enqueue (local.get $t))
222227
(call $scheduler (local.get $k))
223228
)
224229
(func $fork-sync (param $t (ref $cont))
225-
(suspend $control (func.bind (type $cont-func) (local.get $t) (ref.func $handle-fork-sync)))
230+
(suspend $control
231+
(cont.bind $cont-cont-func $cont-cont (local.get $t)
232+
(cont.new $cont-cont-func (ref.func $handle-fork-sync))))
226233
)
227234
(func $sync (export "sync") (param $k (ref $func-cont-func-cont))
228235
(call $scheduler
229-
(cont.bind (type $cont) (ref.func $yield) (ref.func $fork-sync) (local.get $k)))
236+
(cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-sync) (local.get $k)))
230237
)
231238

232239
;; asynchronous yield (used by all asynchronous schedulers)
@@ -235,7 +242,7 @@
235242
(call $scheduler (call $dequeue))
236243
)
237244
(func $yield
238-
(suspend $control (ref.func $handle-yield))
245+
(suspend $control (cont.new $cont-cont (ref.func $handle-yield)))
239246
)
240247
;; four asynchronous implementations of fork:
241248
;; * kt and tk don't yield on encountering a fork
@@ -251,11 +258,13 @@
251258
(call $scheduler (local.get $k))
252259
)
253260
(func $fork-kt (param $t (ref $cont))
254-
(suspend $control (func.bind (type $cont-func) (local.get $t) (ref.func $handle-fork-kt)))
261+
(suspend $control
262+
(cont.bind $cont-cont-func $cont-cont (local.get $t)
263+
(cont.new $cont-cont-func (ref.func $handle-fork-kt))))
255264
)
256265
(func $kt (export "kt") (param $k (ref $func-cont-func-cont))
257266
(call $scheduler
258-
(cont.bind (type $cont) (ref.func $yield) (ref.func $fork-kt) (local.get $k)))
267+
(cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-kt) (local.get $k)))
259268
)
260269

261270
;; no yield on fork, new thread first
@@ -264,11 +273,13 @@
264273
(call $scheduler (local.get $t))
265274
)
266275
(func $fork-tk (param $t (ref $cont))
267-
(suspend $control (func.bind (type $cont-func) (local.get $t) (ref.func $handle-fork-tk)))
276+
(suspend $control
277+
(cont.bind $cont-cont-func $cont-cont (local.get $t)
278+
(cont.new $cont-cont-func (ref.func $handle-fork-tk))))
268279
)
269280
(func $tk (export "tk") (param $k (ref $func-cont-func-cont))
270281
(call $scheduler
271-
(cont.bind (type $cont) (ref.func $yield) (ref.func $fork-tk) (local.get $k)))
282+
(cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-tk) (local.get $k)))
272283
)
273284

274285
;; yield on fork, continuation first
@@ -278,11 +289,13 @@
278289
(call $scheduler (call $dequeue))
279290
)
280291
(func $fork-ykt (param $t (ref $cont))
281-
(suspend $control (func.bind (type $cont-func) (local.get $t) (ref.func $handle-fork-ykt)))
292+
(suspend $control
293+
(cont.bind $cont-cont-func $cont-cont (local.get $t)
294+
(cont.new $cont-cont-func (ref.func $handle-fork-ykt))))
282295
)
283296
(func $ykt (export "ykt") (param $k (ref $func-cont-func-cont))
284297
(call $scheduler
285-
(cont.bind (type $cont) (ref.func $yield) (ref.func $fork-ykt) (local.get $k)))
298+
(cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-ykt) (local.get $k)))
286299
)
287300

288301
;; yield on fork, new thread first
@@ -292,11 +305,13 @@
292305
(call $scheduler (call $dequeue))
293306
)
294307
(func $fork-ytk (param $t (ref $cont))
295-
(suspend $control (func.bind (type $cont-func) (local.get $t) (ref.func $handle-fork-ytk)))
308+
(suspend $control
309+
(cont.bind $cont-cont-func $cont-cont (local.get $t)
310+
(cont.new $cont-cont-func (ref.func $handle-fork-ytk))))
296311
)
297312
(func $ytk (export "ytk") (param $k (ref $func-cont-func-cont))
298313
(call $scheduler
299-
(cont.bind (type $cont) (ref.func $yield) (ref.func $fork-ytk) (local.get $k)))
314+
(cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-ytk) (local.get $k)))
300315
)
301316
)
302317
(register "scheduler")
@@ -325,15 +340,15 @@
325340

326341
(func $run (export "run")
327342
(call $log (i32.const -1))
328-
(call $scheduler-sync (cont.new (type $func-cont-func-cont) (ref.func $main)))
343+
(call $scheduler-sync (cont.new $func-cont-func-cont (ref.func $main)))
329344
(call $log (i32.const -2))
330-
(call $scheduler-kt (cont.new (type $func-cont-func-cont) (ref.func $main)))
345+
(call $scheduler-kt (cont.new $func-cont-func-cont (ref.func $main)))
331346
(call $log (i32.const -3))
332-
(call $scheduler-tk (cont.new (type $func-cont-func-cont) (ref.func $main)))
347+
(call $scheduler-tk (cont.new $func-cont-func-cont (ref.func $main)))
333348
(call $log (i32.const -4))
334-
(call $scheduler-ykt (cont.new (type $func-cont-func-cont) (ref.func $main)))
349+
(call $scheduler-ykt (cont.new $func-cont-func-cont (ref.func $main)))
335350
(call $log (i32.const -5))
336-
(call $scheduler-ytk (cont.new (type $func-cont-func-cont) (ref.func $main)))
351+
(call $scheduler-ytk (cont.new $func-cont-func-cont (ref.func $main)))
337352
(call $log (i32.const -6))
338353
)
339354
)

0 commit comments

Comments
 (0)