|
| 1 | +;;! component_model_async = true |
| 2 | +;;! reference_types = true |
| 3 | +;;! gc_types = true |
| 4 | +;;! multi_memory = true |
| 5 | + |
| 6 | +;; Create a future, start a write, let it complete, and cancel the write prior |
| 7 | +;; to receiving the completion event. |
| 8 | +(component |
| 9 | + (type $f (future)) |
| 10 | + |
| 11 | + (component $c |
| 12 | + (type $f (future)) |
| 13 | + |
| 14 | + (core module $libc (memory (export "mem") 1)) |
| 15 | + (core instance $libc (instantiate $libc)) |
| 16 | + (core func $read (canon future.read $f async (memory $libc "mem"))) |
| 17 | + (core func $close-read (canon future.close-readable $f)) |
| 18 | + (core module $inner |
| 19 | + (import "" "read" (func $read (param i32 i32) (result i32))) |
| 20 | + (import "" "close-read" (func $close-read (param i32))) |
| 21 | + |
| 22 | + (func (export "f") (param i32) |
| 23 | + ;; start a read, asserting it completes with one item |
| 24 | + local.get 0 |
| 25 | + i32.const 0 |
| 26 | + call $read |
| 27 | + i32.const 0x10 |
| 28 | + i32.ne |
| 29 | + if unreachable end |
| 30 | + |
| 31 | + ;; close the read end |
| 32 | + local.get 0 |
| 33 | + call $close-read |
| 34 | + ) |
| 35 | + ) |
| 36 | + |
| 37 | + (core instance $i (instantiate $inner |
| 38 | + (with "" (instance |
| 39 | + (export "read" (func $read)) |
| 40 | + (export "close-read" (func $close-read)) |
| 41 | + )) |
| 42 | + )) |
| 43 | + |
| 44 | + (func (export "f") (param "x" $f) (canon lift (core func $i "f"))) |
| 45 | + ) |
| 46 | + (instance $c (instantiate $c)) |
| 47 | + |
| 48 | + (core func $new (canon future.new $f)) |
| 49 | + (core module $libc (memory (export "mem") 1)) |
| 50 | + (core instance $libc (instantiate $libc)) |
| 51 | + (core func $write (canon future.write $f async (memory $libc "mem"))) |
| 52 | + (core func $cancel (canon future.cancel-write $f)) |
| 53 | + (core func $drain (canon lower (func $c "f"))) |
| 54 | + (core module $m |
| 55 | + (import "" "new" (func $new (result i64))) |
| 56 | + (import "" "write" (func $write (param i32 i32) (result i32))) |
| 57 | + (import "" "cancel" (func $cancel (param i32) (result i32))) |
| 58 | + (import "" "drain" (func $drain (param i32))) |
| 59 | + |
| 60 | + (func (export "f") (result i32) |
| 61 | + (local $read i32) |
| 62 | + (local $write i32) |
| 63 | + (local $new i64) |
| 64 | + |
| 65 | + (local.set $new (call $new)) |
| 66 | + (local.set $read (i32.wrap_i64 (local.get $new))) |
| 67 | + (local.set $write (i32.wrap_i64 (i64.shr_u (local.get $new) (i64.const 32)))) |
| 68 | + |
| 69 | + ;; start a write |
| 70 | + local.get $write |
| 71 | + i32.const 0 |
| 72 | + call $write |
| 73 | + i32.const -1 |
| 74 | + i32.ne |
| 75 | + if unreachable end |
| 76 | + |
| 77 | + ;; drain the read end |
| 78 | + local.get $read |
| 79 | + call $drain |
| 80 | + |
| 81 | + ;; cancel the write, returning the result |
| 82 | + local.get $write |
| 83 | + call $cancel |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + (core instance $i (instantiate $m |
| 88 | + (with "" (instance |
| 89 | + (export "new" (func $new)) |
| 90 | + (export "write" (func $write)) |
| 91 | + (export "cancel" (func $cancel)) |
| 92 | + (export "drain" (func $drain)) |
| 93 | + )) |
| 94 | + )) |
| 95 | + |
| 96 | + (func (export "f") (result u32) (canon lift (core func $i "f"))) |
| 97 | +) |
| 98 | + |
| 99 | +(assert_return (invoke "f") (u32.const 0x10)) |
0 commit comments