Skip to content

Commit 562666f

Browse files
committed
Add tests/async/wait-during-callback.wast
1 parent 25bb536 commit 562666f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
;; This test calls waitable-set.wait from under an async-callback-lifted export.
2+
(component
3+
(core module $Memory (memory (export "mem") 1))
4+
(core instance $memory (instantiate $Memory))
5+
(core module $CM
6+
(import "" "mem" (memory 1))
7+
(import "" "task.return" (func $task.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+
(import "" "future.new" (func $future.new (result i64)))
12+
(import "" "future.read" (func $future.read (param i32 i32) (result i32)))
13+
(import "" "future.write" (func $future.write (param i32 i32) (result i32)))
14+
(import "" "future.drop-readable" (func $future.drop-readable (param i32)))
15+
(import "" "future.drop-writable" (func $future.drop-writable (param i32)))
16+
17+
;; $ws is waited on by 'blocker' and added to by 'unblocker'
18+
19+
(func $run (export "run") (result i32)
20+
(local $ret i32) (local $ret64 i64)
21+
(local $futr i32) (local $futw i32) (local $ws i32)
22+
(local $event_code i32) (local $retp i32)
23+
24+
;; create a future pair
25+
(local.set $ret64 (call $future.new))
26+
(local.set $futr (i32.wrap_i64 (local.get $ret64)))
27+
(local.set $futw (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32))))
28+
29+
;; start a pending read that will block
30+
(local.set $ret (call $future.read (local.get $futr) (i32.const 0xdeadbeef)))
31+
(if (i32.ne (i32.const -1 (; BLOCKED ;)) (local.get $ret))
32+
(then unreachable))
33+
34+
;; perform a write to make the above read ready
35+
(local.set $ret (call $future.write (local.get $futw) (i32.const 0xdeadbeef)))
36+
(if (i32.ne (i32.const 0 (; COMPLETED ;)) (local.get $ret))
37+
(then unreachable))
38+
39+
;; wait on a waitable set containing our now-ready future.read which
40+
;; should then immediately resolve
41+
(local.set $ws (call $waitable-set.new))
42+
(call $waitable.join (local.get $futr) (local.get $ws))
43+
(local.set $retp (i32.const 0))
44+
(local.set $event_code (call $waitable-set.wait (local.get $ws) (local.get $retp)))
45+
(if (i32.ne (i32.const 4 (; FUTURE_READ ;)) (local.get $event_code))
46+
(then unreachable))
47+
(if (i32.ne (local.get $futr) (i32.load (local.get $retp)))
48+
(then unreachable))
49+
(if (i32.ne (i32.const 2 (; RETURNED=2 ;)) (i32.load offset=4 (local.get $retp)))
50+
(then unreachable))
51+
52+
;; return 42
53+
(call $task.return (i32.const 42))
54+
(i32.const 0 (; EXIT ;))
55+
)
56+
(func (export "run_cb") (param i32 i32 i32) (result i32)
57+
unreachable
58+
)
59+
)
60+
(type $FT (future))
61+
(canon task.return (result u32) (core func $task.return))
62+
(canon waitable.join (core func $waitable.join))
63+
(canon waitable-set.new (core func $waitable-set.new))
64+
(canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait))
65+
(canon future.new $FT (core func $future.new))
66+
(canon future.read $FT async (core func $future.read))
67+
(canon future.write $FT async (core func $future.write))
68+
(canon future.drop-readable $FT (core func $future.drop-readable))
69+
(canon future.drop-writable $FT (core func $future.drop-writable))
70+
(core instance $cm (instantiate $CM (with "" (instance
71+
(export "mem" (memory $memory "mem"))
72+
(export "task.return" (func $task.return))
73+
(export "waitable.join" (func $waitable.join))
74+
(export "waitable-set.new" (func $waitable-set.new))
75+
(export "waitable-set.wait" (func $waitable-set.wait))
76+
(export "future.new" (func $future.new))
77+
(export "future.read" (func $future.read))
78+
(export "future.write" (func $future.write))
79+
(export "future.drop-readable" (func $future.drop-readable))
80+
(export "future.drop-writable" (func $future.drop-writable))
81+
))))
82+
(func (export "run") (result u32) (canon lift
83+
(core func $cm "run")
84+
async (callback (func $cm "run_cb"))
85+
))
86+
)
87+
(assert_return (invoke "run") (u32.const 42))

0 commit comments

Comments
 (0)