Skip to content

Commit f3fe0f7

Browse files
committed
Add some wast tests for async
1 parent 31256b8 commit f3fe0f7

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

test/async/subtasks.wast

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
(component
2+
(component $C
3+
(core module $Memory (memory (export "mem") 1))
4+
(core instance $memory (instantiate $Memory))
5+
(core module $M
6+
(import "" "mem" (memory 1))
7+
(import "" "task.return" (func $return (param i32)))
8+
(import "" "waitable.join" (func $waitable.join (param i32 i32)))
9+
(import "" "waitable-set.new" (func $waitable-set.new (result i32)))
10+
(import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
11+
12+
(global $ws (mut i32) (i32.const 0))
13+
(func $start (global.set $ws (call $waitable-set.new)))
14+
(start $start)
15+
16+
(func (export "f") (result i32)
17+
;; broken
18+
;; (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (global.get $ws) (i32.const 4)))
19+
(i32.const 1 (; YIELD ;))
20+
)
21+
(func (export "cb") (param $event_code i32) (param $index i32) (param $payload i32) (result i32)
22+
;; broken
23+
;; (if (i32.ne (i32.const 4 (; FUTURE_READ ;)) (local.get $event_code))
24+
;; (then unreachable))
25+
(if (i32.ne (i32.const 0 (; NONE ;)) (local.get $event_code))
26+
(then unreachable))
27+
(if (i32.ne (i32.const 0) (local.get $index))
28+
(then unreachable))
29+
(if (i32.ne (i32.const 0) (local.get $payload))
30+
(then unreachable))
31+
32+
(call $return (i32.const 42))
33+
(i32.const 0))
34+
)
35+
(canon task.return (result u32) (core func $task.return))
36+
(canon waitable.join (core func $waitable.join))
37+
(canon waitable-set.new (core func $waitable-set.new))
38+
(canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait))
39+
(core instance $m (instantiate $M (with "" (instance
40+
(export "mem" (memory $memory "mem"))
41+
(export "task.return" (func $task.return))
42+
(export "waitable.join" (func $waitable.join))
43+
(export "waitable-set.new" (func $waitable-set.new))
44+
(export "waitable-set.wait" (func $waitable-set.wait))
45+
))))
46+
(func (export "f") (result u32) (canon lift
47+
(core func $m "f")
48+
async (memory $memory "mem") (callback (func $m "cb"))
49+
))
50+
)
51+
52+
(component $D
53+
(import "f" (func $f (result u32)))
54+
55+
(core module $Memory (memory (export "mem") 1))
56+
(core instance $memory (instantiate $Memory))
57+
(core module $M
58+
(import "" "mem" (memory 1))
59+
(import "" "waitable.join" (func $waitable.join (param i32 i32)))
60+
(import "" "waitable-set.new" (func $waitable-set.new (result i32)))
61+
(import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
62+
(import "" "f" (func $f (param i32 i32) (result i32)))
63+
64+
(global $ws (mut i32) (i32.const 0))
65+
(func $start (global.set $ws (call $waitable-set.new)))
66+
(start $start)
67+
68+
(func (export "g") (result i32)
69+
(local $ret i32) (local $subtaski i32) (local $event_code i32) (local $retp i32)
70+
71+
;; call async import
72+
(local.set $ret (call $f (i32.const 0) (i32.const 0)))
73+
(if (i32.ne (i32.const 1 (; STARTING ;)) (i32.and (local.get $ret) (i32.const 0xf)))
74+
(then unreachable))
75+
(local.set $subtaski (i32.shr_u (local.get $ret) (i32.const 4)))
76+
;; TODO: shared table?
77+
;;(if (i32.ne (i32.const TODO) (local.get $subtaski))
78+
;; (then unreachable))
79+
80+
;; wait on the subtask
81+
(call $waitable.join (local.get $subtaski) (global.get $ws))
82+
(local.set $retp (i32.const 0))
83+
(local.set $event_code (call $waitable-set.wait (global.get $ws) (local.get $retp)))
84+
(if (i32.ne (i32.const 1 (; SUBTASK ;)) (local.get $event_code))
85+
(then unreachable))
86+
(if (i32.ne (local.get $subtaski) (i32.load (local.get $retp)))
87+
(then unreachable))
88+
(if (i32.ne (i32.const 2 (; RETURNED ;)) (i32.load offset=4 (local.get $retp)))
89+
(then unreachable))
90+
91+
(i32.const 42)
92+
)
93+
)
94+
(canon waitable.join (core func $waitable.join))
95+
(canon waitable-set.new (core func $waitable-set.new))
96+
(canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait))
97+
(canon lower (func $f) async (memory $memory "mem") (core func $f'))
98+
(core instance $m (instantiate $M (with "" (instance
99+
(export "mem" (memory $memory "mem"))
100+
(export "waitable.join" (func $waitable.join))
101+
(export "waitable-set.new" (func $waitable-set.new))
102+
(export "waitable-set.wait" (func $waitable-set.wait))
103+
(export "f" (func $f'))
104+
))))
105+
(func (export "f") (result u32) (canon lift (core func $m "g")))
106+
)
107+
108+
(instance $c (instantiate $C))
109+
(instance $d (instantiate $D (with "f" (func $c "f"))))
110+
(func (export "f") (alias export $d "f"))
111+
)
112+
(assert_return (invoke "f") (u32.const 42))
113+
114+
;; TODO: waiting when waitable-set is empty

0 commit comments

Comments
 (0)