1;;! component_model_async = true
2;;! reference_types = true
3;;! gc_types = true
4;;! multi_memory = true
5
6;; synchronous future.read; sync lift
7(component
8  (component $child
9    (core module $libc (memory (export "memory") 1))
10    (core instance $libc (instantiate $libc))
11
12    (type $future (future))
13    (core func $read (canon future.read $future (memory $libc "memory")))
14
15    (core module $m
16      (import "" "read" (func $read (param i32 i32) (result i32)))
17
18      (func (export "run") (param $future i32)
19        (call $read (local.get $future) (i32.const 0))
20        drop
21      )
22    )
23    (core instance $i (instantiate $m
24      (with "" (instance
25        (export "read" (func $read))
26      ))
27    ))
28    (func (export "run") (param "x" $future)
29      (canon lift (core func $i "run")))
30  )
31  (instance $child (instantiate $child))
32
33  (type $future (future))
34  (core func $new (canon future.new $future))
35  (core func $child-run (canon lower (func $child "run")))
36
37  (core module $m
38    (import "" "new" (func $new (result i64)))
39    (import "" "child-run" (func $child-run (param i32)))
40
41    (func (export "run")
42      (call $child-run (i32.wrap_i64 (call $new)))
43    )
44  )
45  (core instance $i (instantiate $m
46    (with "" (instance
47      (export "new" (func $new))
48      (export "child-run" (func $child-run))
49    ))
50  ))
51
52  (func (export "run") async
53    (canon lift (core func $i "run")))
54)
55
56;; We expect deadlock since the write end is leaked:
57(assert_trap (invoke "run") "deadlock detected: event loop cannot make further progress")
58
59;; asynchronous future.read; sync lift
60(component
61  (component $child
62    (core module $libc (memory (export "memory") 1))
63    (core instance $libc (instantiate $libc))
64
65    (type $future (future))
66    (core func $read (canon future.read $future (memory $libc "memory") async))
67
68    (core module $m
69      (import "" "read" (func $read (param i32 i32) (result i32)))
70
71      (func (export "run") (param $future i32)
72        (call $read (local.get $future) (i32.const 0))
73        drop
74      )
75    )
76    (core instance $i (instantiate $m
77      (with "" (instance
78        (export "read" (func $read))
79      ))
80    ))
81    (func (export "run") (param "x" $future)
82      (canon lift (core func $i "run")))
83  )
84  (instance $child (instantiate $child))
85
86  (type $future (future))
87  (core func $new (canon future.new $future))
88  (core func $child-run (canon lower (func $child "run")))
89
90  (core module $m
91    (import "" "new" (func $new (result i64)))
92    (import "" "child-run" (func $child-run (param i32)))
93
94    (func (export "run")
95      (call $child-run (i32.wrap_i64 (call $new)))
96    )
97  )
98  (core instance $i (instantiate $m
99    (with "" (instance
100      (export "new" (func $new))
101      (export "child-run" (func $child-run))
102    ))
103  ))
104
105  (func (export "run")
106    (canon lift (core func $i "run")))
107)
108
109(assert_return (invoke "run"))
110
111;; synchronous future.read; async lift
112(component
113  (component $child
114    (core module $libc (memory (export "memory") 1))
115    (core instance $libc (instantiate $libc))
116
117    (type $future (future))
118    (core func $read (canon future.read $future (memory $libc "memory")))
119
120    (core module $m
121      (import "" "read" (func $read (param i32 i32) (result i32)))
122
123      (func (export "run") (param $future i32) (result i32)
124        (call $read (local.get $future) (i32.const 0))
125        drop
126        i32.const 0 ;; TODO
127      )
128
129      (func (export "cb") (param i32 i32 i32) (result i32)
130        i32.const 0) ;; TODO
131    )
132    (core instance $i (instantiate $m
133      (with "" (instance
134        (export "read" (func $read))
135      ))
136    ))
137    (func (export "run") async (param "x" $future)
138      (canon lift (core func $i "run") async (callback (func $i "cb"))))
139  )
140  (instance $child (instantiate $child))
141
142  (type $future (future))
143  (core func $new (canon future.new $future))
144  (core func $child-run (canon lower (func $child "run")))
145
146  (core module $m
147    (import "" "new" (func $new (result i64)))
148    (import "" "child-run" (func $child-run (param i32)))
149
150    (func (export "run")
151      (call $child-run (i32.wrap_i64 (call $new)))
152    )
153  )
154  (core instance $i (instantiate $m
155    (with "" (instance
156      (export "new" (func $new))
157      (export "child-run" (func $child-run))
158    ))
159  ))
160
161  (func (export "run") async
162    (canon lift (core func $i "run")))
163)
164
165;; We expect deadlock since the write end is leaked:
166(assert_trap (invoke "run") "deadlock detected: event loop cannot make further progress")
167
168;; asynchronous future.read; async lift
169(component
170  (component $child
171    (core module $libc (memory (export "memory") 1))
172    (core instance $libc (instantiate $libc))
173
174    (type $future (future))
175    (core func $read (canon future.read $future (memory $libc "memory") async))
176    (core func $return (canon task.return))
177
178    (core module $m
179      (import "" "read" (func $read (param i32 i32) (result i32)))
180      (import "" "return" (func $return))
181
182      (func (export "run") (param $future i32) (result i32)
183        (call $read (local.get $future) (i32.const 0))
184        drop
185        call $return
186        i32.const 0
187      )
188
189      (func (export "cb") (param i32 i32 i32) (result i32)
190        unreachable)
191    )
192    (core instance $i (instantiate $m
193      (with "" (instance
194        (export "read" (func $read))
195        (export "return" (func $return))
196      ))
197    ))
198    (func (export "run") (param "x" $future)
199      (canon lift (core func $i "run") async (callback (func $i "cb"))))
200  )
201  (instance $child (instantiate $child))
202
203  (type $future (future))
204  (core func $new (canon future.new $future))
205  (core func $child-run (canon lower (func $child "run")))
206
207  (core module $m
208    (import "" "new" (func $new (result i64)))
209    (import "" "child-run" (func $child-run (param i32)))
210
211    (func (export "run")
212      (call $child-run (i32.wrap_i64 (call $new)))
213    )
214  )
215  (core instance $i (instantiate $m
216    (with "" (instance
217      (export "new" (func $new))
218      (export "child-run" (func $child-run))
219    ))
220  ))
221
222  (func (export "run") async
223    (canon lift (core func $i "run")))
224)
225
226(assert_return (invoke "run"))
227
228