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") async (param "x" $future)
29      (canon lift (core func $i "run")))
30  )
31  (instance $child (instantiate $child))
32
33  (component $other-child
34    (type $future (future))
35    (import "child" (instance $child
36      (export "run" (func async (param "x" $future)))
37    ))
38
39    (core func $new (canon future.new $future))
40    (core func $child-run (canon lower (func $child "run")))
41    (core module $m
42      (import "" "new" (func $new (result i64)))
43      (import "" "child-run" (func $child-run (param i32)))
44
45      (func (export "run")
46        (call $child-run (i32.wrap_i64 (call $new)))
47      )
48    )
49    (core instance $i (instantiate $m
50      (with "" (instance
51        (export "new" (func $new))
52        (export "child-run" (func $child-run))
53      ))
54    ))
55
56    (func (export "run") async
57      (canon lift (core func $i "run")))
58  )
59  (instance $other-child (instantiate $other-child (with "child" (instance $child))))
60
61  (func (export "run") (alias export $other-child "run"))
62)
63
64;; We expect deadlock since the write end is leaked:
65(assert_trap (invoke "run") "deadlock detected: event loop cannot make further progress")
66
67;; asynchronous future.read; sync lift
68(component
69  (component $child
70    (core module $libc (memory (export "memory") 1))
71    (core instance $libc (instantiate $libc))
72
73    (type $future (future))
74    (core func $read (canon future.read $future (memory $libc "memory") async))
75
76    (core module $m
77      (import "" "read" (func $read (param i32 i32) (result i32)))
78
79      (func (export "run") (param $future i32)
80        (call $read (local.get $future) (i32.const 0))
81        drop
82      )
83    )
84    (core instance $i (instantiate $m
85      (with "" (instance
86        (export "read" (func $read))
87      ))
88    ))
89    (func (export "run") (param "x" $future)
90      (canon lift (core func $i "run")))
91  )
92  (instance $child (instantiate $child))
93
94  (component $other-child
95    (type $future (future))
96    (import "child" (instance $child
97      (export "run" (func (param "x" $future)))
98    ))
99    (core func $new (canon future.new $future))
100    (core func $child-run (canon lower (func $child "run")))
101
102    (core module $m
103      (import "" "new" (func $new (result i64)))
104      (import "" "child-run" (func $child-run (param i32)))
105
106      (func (export "run")
107        (call $child-run (i32.wrap_i64 (call $new)))
108      )
109    )
110    (core instance $i (instantiate $m
111      (with "" (instance
112        (export "new" (func $new))
113        (export "child-run" (func $child-run))
114      ))
115    ))
116
117    (func (export "run")
118      (canon lift (core func $i "run")))
119  )
120  (instance $other-child (instantiate $other-child (with "child" (instance $child))))
121
122  (func (export "run") (alias export $other-child "run"))
123)
124
125(assert_return (invoke "run"))
126
127;; synchronous future.read; async lift
128(component
129  (component $child
130    (core module $libc (memory (export "memory") 1))
131    (core instance $libc (instantiate $libc))
132
133    (type $future (future))
134    (core func $read (canon future.read $future (memory $libc "memory")))
135
136    (core module $m
137      (import "" "read" (func $read (param i32 i32) (result i32)))
138
139      (func (export "run") (param $future i32) (result i32)
140        (call $read (local.get $future) (i32.const 0))
141        drop
142        i32.const 0 ;; TODO
143      )
144
145      (func (export "cb") (param i32 i32 i32) (result i32)
146        i32.const 0) ;; TODO
147    )
148    (core instance $i (instantiate $m
149      (with "" (instance
150        (export "read" (func $read))
151      ))
152    ))
153    (func (export "run") async (param "x" $future)
154      (canon lift (core func $i "run") async (callback (func $i "cb"))))
155  )
156  (instance $child (instantiate $child))
157
158  (component $other-child
159    (type $future (future))
160    (import "child" (instance $child
161      (export "run" (func async (param "x" $future)))
162    ))
163    (core func $new (canon future.new $future))
164    (core func $child-run (canon lower (func $child "run")))
165
166    (core module $m
167      (import "" "new" (func $new (result i64)))
168      (import "" "child-run" (func $child-run (param i32)))
169
170      (func (export "run")
171        (call $child-run (i32.wrap_i64 (call $new)))
172      )
173    )
174    (core instance $i (instantiate $m
175      (with "" (instance
176        (export "new" (func $new))
177        (export "child-run" (func $child-run))
178      ))
179    ))
180
181    (func (export "run") async
182      (canon lift (core func $i "run")))
183  )
184  (instance $other-child (instantiate $other-child (with "child" (instance $child))))
185
186  (func (export "run") (alias export $other-child "run"))
187)
188
189;; We expect deadlock since the write end is leaked:
190(assert_trap (invoke "run") "deadlock detected: event loop cannot make further progress")
191
192;; asynchronous future.read; async lift
193(component
194  (component $child
195    (core module $libc (memory (export "memory") 1))
196    (core instance $libc (instantiate $libc))
197
198    (type $future (future))
199    (core func $read (canon future.read $future (memory $libc "memory") async))
200    (core func $return (canon task.return))
201
202    (core module $m
203      (import "" "read" (func $read (param i32 i32) (result i32)))
204      (import "" "return" (func $return))
205
206      (func (export "run") (param $future i32) (result i32)
207        (call $read (local.get $future) (i32.const 0))
208        drop
209        call $return
210        i32.const 0
211      )
212
213      (func (export "cb") (param i32 i32 i32) (result i32)
214        unreachable)
215    )
216    (core instance $i (instantiate $m
217      (with "" (instance
218        (export "read" (func $read))
219        (export "return" (func $return))
220      ))
221    ))
222    (func (export "run") (param "x" $future)
223      (canon lift (core func $i "run") async (callback (func $i "cb"))))
224  )
225  (instance $child (instantiate $child))
226
227  (component $other-child
228    (type $future (future))
229    (import "child" (instance $child
230      (export "run" (func (param "x" $future)))
231    ))
232    (core func $new (canon future.new $future))
233    (core func $child-run (canon lower (func $child "run")))
234
235    (core module $m
236      (import "" "new" (func $new (result i64)))
237      (import "" "child-run" (func $child-run (param i32)))
238
239      (func (export "run")
240        (call $child-run (i32.wrap_i64 (call $new)))
241      )
242    )
243    (core instance $i (instantiate $m
244      (with "" (instance
245        (export "new" (func $new))
246        (export "child-run" (func $child-run))
247      ))
248    ))
249
250    (func (export "run") async
251      (canon lift (core func $i "run")))
252  )
253  (instance $other-child (instantiate $other-child (with "child" (instance $child))))
254
255  (func (export "run") (alias export $other-child "run"))
256)
257
258(assert_return (invoke "run"))
259
260