Skip to content

Commit 5452862

Browse files
committed
Add additional WAST tests to test/async
1 parent aeb5d21 commit 5452862

4 files changed

Lines changed: 276 additions & 4 deletions

File tree

test/async/cross-task-future.wast

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
;; This exercises that future ends are not tied to the task that created them.
2+
(component
3+
(component $C
4+
(core module $Memory (memory (export "mem") 1))
5+
(core instance $memory (instantiate $Memory))
6+
(core module $CM
7+
(import "" "mem" (memory 1))
8+
(import "" "task.return" (func $task.return (param i32)))
9+
(import "" "future.new" (func $future.new (result i64)))
10+
(import "" "future.write" (func $future.write (param i32 i32) (result i32)))
11+
12+
(global $futr (mut i32) (i32.const 0))
13+
(global $futw (mut i32) (i32.const 0))
14+
15+
(func $one (export "one")
16+
(local $ret i32) (local $ret64 i64)
17+
(local.set $ret64 (call $future.new))
18+
(global.set $futr (i32.wrap_i64 (local.get $ret64)))
19+
(global.set $futw (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32))))
20+
)
21+
(func $two (export "two") (result i32)
22+
(local $ret i32) (local $ptr i32)
23+
(call $task.return (global.get $futr))
24+
(local.set $ptr (i32.const 32))
25+
(i32.store (local.get $ptr) (i32.const 0x42))
26+
(local.set $ret (call $future.write (global.get $futw) (local.get $ptr)))
27+
(if (i32.ne (i32.const -1 (; BLOCKED ;)) (local.get $ret))
28+
(then unreachable))
29+
(i32.const 0 (; EXIT ;))
30+
)
31+
(func $two_cb (export "two_cb") (param i32 i32 i32) (result i32)
32+
unreachable
33+
)
34+
)
35+
(type $FT (future u8))
36+
(canon task.return (result $FT) (core func $task.return))
37+
(canon future.new $FT (core func $future.new))
38+
(canon future.write $FT async (memory $memory "mem") (core func $future.write))
39+
(core instance $cm (instantiate $CM (with "" (instance
40+
(export "mem" (memory $memory "mem"))
41+
(export "task.return" (func $task.return))
42+
(export "future.new" (func $future.new))
43+
(export "future.write" (func $future.write))
44+
))))
45+
(func (export "one") (canon lift
46+
(core func $cm "one")
47+
))
48+
(func (export "two") async (result (future u8)) (canon lift
49+
(core func $cm "two")
50+
async (callback (func $cm "two_cb"))
51+
))
52+
)
53+
54+
(component $D
55+
(import "c" (instance $c
56+
(export "one" (func))
57+
(export "two" (func async (result (future u8))))
58+
))
59+
60+
(core module $Memory (memory (export "mem") 1))
61+
(core instance $memory (instantiate $Memory))
62+
(core module $DM
63+
(import "" "mem" (memory 1))
64+
(import "" "future.read" (func $future.read (param i32 i32) (result i32)))
65+
(import "" "one" (func $one))
66+
(import "" "two" (func $two (result i32)))
67+
68+
(func $run (export "run") (result i32)
69+
(local $ret i32)
70+
(local $retp i32)
71+
(local $futr i32)
72+
73+
(call $one)
74+
(local.set $futr (call $two))
75+
(local.set $retp (i32.const 32))
76+
(local.set $ret (call $future.read (local.get $futr) (local.get $retp)))
77+
(if (i32.ne (i32.const 0 (; COMPLETED ;)) (local.get $ret))
78+
(then unreachable))
79+
(if (i32.ne (i32.load8_u (local.get $retp)) (i32.const 0x42))
80+
(then unreachable))
81+
82+
(i32.const 42)
83+
)
84+
)
85+
(type $FT (future u8))
86+
(canon future.read $FT async (memory $memory "mem") (core func $future.read))
87+
(canon lower (func $c "one") (core func $one'))
88+
(canon lower (func $c "two") (core func $two'))
89+
(core instance $dm (instantiate $DM (with "" (instance
90+
(export "mem" (memory $memory "mem"))
91+
(export "future.read" (func $future.read))
92+
(export "one" (func $one'))
93+
(export "two" (func $two'))
94+
))))
95+
(func (export "run") async (result u32) (canon lift (core func $dm "run")))
96+
)
97+
98+
(instance $c (instantiate $C))
99+
(instance $d (instantiate $D (with "c" (instance $c))))
100+
(func (export "run") (alias export $d "run"))
101+
)
102+
103+
(assert_return (invoke "run") (u32.const 42))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(assert_invalid
2+
(component
3+
(core module $M
4+
(func (export "f"))
5+
)
6+
(core instance $i (instantiate $M))
7+
(func (export "f") (canon lift (core func $i "f") async))
8+
)
9+
"the `async` canonical option requires an async function type")
10+
11+
(assert_invalid
12+
(component
13+
(core module $M
14+
(func (export "f") (param i32) (result i32) unreachable)
15+
(func (export "f_cb") (param i32 i32 i32) (result i32) unreachable)
16+
)
17+
(core instance $i (instantiate $M))
18+
(func (export "f") (canon lift (core func $i "f") async (callback (func $i "f_cb"))))
19+
)
20+
"the `async` canonical option requires an async function type")
21+
22+
(assert_invalid
23+
(component
24+
(import "f" (func $f))
25+
(core func $f' (canon lower (func $f) async))
26+
)
27+
"the `async` canonical option requires an async function type")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;; Test that `stream<char>` is rejected as a validation error.
2+
;; This is a temporary limitation; see https://github.com/WebAssembly/component-model/pull/607
3+
(assert_invalid
4+
(component
5+
(type (stream char))
6+
)
7+
"`stream<char>` is not valid")
Lines changed: 139 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
;; $D exports one function for each place in the spec where may_leave is
2-
;; guarded to be true and ensures that the call traps before any other guard.
3-
;; Since each trap tears down the instance, a fresh instance of $Tester is
4-
;; created for each export call.
1+
;; Tests behavior of built-ins called from inside a post-return function.
2+
3+
;; built-ins that trap when called from post-return:
54
(component definition $Tester
65
(component $C
76
(core module $CM
@@ -247,3 +246,139 @@
247246
(assert_trap (invoke "trap-calling-future-drop-readable") "cannot leave component instance")
248247
(component instance $i27 $Tester)
249248
(assert_trap (invoke "trap-calling-future-drop-writable") "cannot leave component instance")
249+
250+
251+
;; built-ins that don't trap:
252+
(component
253+
(canon context.get i32 0 (core func $context.get))
254+
(canon context.set i32 0 (core func $context.set))
255+
(core module $CM
256+
(import "" "context.get" (func $context.get (result i32)))
257+
(import "" "context.set" (func $context.set (param i32)))
258+
(global $saved (mut i32) (i32.const 0))
259+
260+
(func (export "f") (result i32)
261+
(call $context.set (i32.const 42))
262+
(i32.const 7)
263+
)
264+
(func (export "f-pr") (param i32)
265+
(global.set $saved (call $context.get))
266+
(call $context.set (i32.const 99))
267+
)
268+
(func (export "check") (result i32)
269+
(global.get $saved)
270+
)
271+
)
272+
(core instance $cm (instantiate $CM (with "" (instance
273+
(export "context.get" (func $context.get))
274+
(export "context.set" (func $context.set))
275+
))))
276+
(func (export "f") (result u32) (canon lift
277+
(core func $cm "f")
278+
(post-return (func $cm "f-pr"))
279+
))
280+
(func (export "check") (result u32) (canon lift
281+
(core func $cm "check")
282+
))
283+
)
284+
(assert_return (invoke "f") (u32.const 7))
285+
(assert_return (invoke "check") (u32.const 42))
286+
287+
288+
(component
289+
(type $R (resource (rep i32)))
290+
(canon resource.new $R (core func $resource.new))
291+
(canon resource.rep $R (core func $resource.rep))
292+
(core module $CM
293+
(import "" "resource.new" (func $resource.new (param i32) (result i32)))
294+
(import "" "resource.rep" (func $resource.rep (param i32) (result i32)))
295+
296+
(global $handle (mut i32) (i32.const 0))
297+
(global $saved-rep (mut i32) (i32.const 0))
298+
299+
(func (export "f") (result i32)
300+
(global.set $handle (call $resource.new (i32.const 123)))
301+
(i32.const 5)
302+
)
303+
(func (export "f-pr") (param i32)
304+
(global.set $saved-rep (call $resource.rep (global.get $handle)))
305+
)
306+
(func (export "check") (result i32)
307+
(global.get $saved-rep)
308+
)
309+
)
310+
(core instance $cm (instantiate $CM (with "" (instance
311+
(export "resource.new" (func $resource.new))
312+
(export "resource.rep" (func $resource.rep))
313+
))))
314+
(func (export "f") (result u32) (canon lift
315+
(core func $cm "f")
316+
(post-return (func $cm "f-pr"))
317+
))
318+
(func (export "check") (result u32) (canon lift
319+
(core func $cm "check")
320+
))
321+
)
322+
(assert_return (invoke "f") (u32.const 5))
323+
(assert_return (invoke "check") (u32.const 123))
324+
325+
326+
(component
327+
(canon backpressure.inc (core func $bp.inc))
328+
(canon backpressure.dec (core func $bp.dec))
329+
(core module $CM
330+
(import "" "bp.inc" (func $bp.inc))
331+
(import "" "bp.dec" (func $bp.dec))
332+
333+
(func (export "f") (result i32)
334+
(i32.const 11)
335+
)
336+
(func (export "f-pr") (param i32)
337+
(call $bp.inc)
338+
(call $bp.dec)
339+
)
340+
)
341+
(core instance $cm (instantiate $CM (with "" (instance
342+
(export "bp.inc" (func $bp.inc))
343+
(export "bp.dec" (func $bp.dec))
344+
))))
345+
(func (export "f") (result u32) (canon lift
346+
(core func $cm "f")
347+
(post-return (func $cm "f-pr"))
348+
))
349+
)
350+
(assert_return (invoke "f") (u32.const 11))
351+
352+
353+
(component
354+
(canon thread.index (core func $thread.index))
355+
(core module $CM
356+
(import "" "thread.index" (func $thread.index (result i32)))
357+
358+
(global $body-idx (mut i32) (i32.const -1))
359+
(global $pr-idx (mut i32) (i32.const -1))
360+
361+
(func (export "f") (result i32)
362+
(global.set $body-idx (call $thread.index))
363+
(i32.const 13)
364+
)
365+
(func (export "f-pr") (param i32)
366+
(global.set $pr-idx (call $thread.index))
367+
)
368+
(func (export "check") (result i32)
369+
(i32.eq (global.get $body-idx) (global.get $pr-idx))
370+
)
371+
)
372+
(core instance $cm (instantiate $CM (with "" (instance
373+
(export "thread.index" (func $thread.index))
374+
))))
375+
(func (export "f") (result u32) (canon lift
376+
(core func $cm "f")
377+
(post-return (func $cm "f-pr"))
378+
))
379+
(func (export "check") (result u32) (canon lift
380+
(core func $cm "check")
381+
))
382+
)
383+
(assert_return (invoke "f") (u32.const 13))
384+
(assert_return (invoke "check") (u32.const 1))

0 commit comments

Comments
 (0)