|
| 1 | +;;! component_model_async = true |
| 2 | + |
| 3 | +(component definition $A |
| 4 | + (core module $libc (memory (export "mem") 1)) |
| 5 | + (core instance $libc (instantiate $libc)) |
| 6 | + |
| 7 | + (core module $m |
| 8 | + (import "" "mem" (memory 1)) |
| 9 | + (import "" "stream.new" (func $stream.new (result i64))) |
| 10 | + (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32))) |
| 11 | + (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32))) |
| 12 | + (import "" "stream.cancel-read" (func $stream.cancel-read (param i32) (result i32))) |
| 13 | + (import "" "stream.cancel-write" (func $stream.cancel-write (param i32) (result i32))) |
| 14 | + (import "" "stream.drop-writable" (func $stream.drop-writable (param i32))) |
| 15 | + (import "" "stream.drop-readable" (func $stream.drop-readable (param i32))) |
| 16 | + (import "" "waitable-set.new" (func $waitable-set.new (result i32))) |
| 17 | + (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32))) |
| 18 | + (import "" "waitable-set.poll" (func $waitable-set.poll (param i32 i32) (result i32))) |
| 19 | + (import "" "waitable.join" (func $waitable.join (param i32) (param i32))) |
| 20 | + (import "" "waitable-set.drop" (func $waitable-set.drop (param i32))) |
| 21 | + (import "" "future.new" (func $future.new (result i64))) |
| 22 | + (import "" "future.read" (func $future.read (param i32 i32) (result i32))) |
| 23 | + (import "" "future.write" (func $future.write (param i32 i32) (result i32))) |
| 24 | + (import "" "future.cancel-read" (func $future.cancel-read (param i32) (result i32))) |
| 25 | + (import "" "future.cancel-write" (func $future.cancel-write (param i32) (result i32))) |
| 26 | + (import "" "future.drop-writable" (func $future.drop-writable (param i32))) |
| 27 | + (import "" "future.drop-readable" (func $future.drop-readable (param i32))) |
| 28 | + |
| 29 | + (table 5 funcref) |
| 30 | + (elem (i32.const 0) |
| 31 | + func |
| 32 | + $witness-dropped-via-cancel-read |
| 33 | + $witness-dropped-via-cancel-write |
| 34 | + $witness-dropped-via-waitable-set.wait |
| 35 | + $witness-dropped-via-waitable-set.poll |
| 36 | + $witness-dropped-via-future.cancel-write |
| 37 | + ) |
| 38 | + |
| 39 | + (func $witness-dropped-via-cancel-read (param i32) |
| 40 | + (local $ret i32) |
| 41 | + (local.set $ret (call $stream.cancel-read (local.get 0))) |
| 42 | + (if (i32.ne (local.get $ret) (i32.const 0x01 (; DROPPED ;))) |
| 43 | + (then unreachable)) |
| 44 | + ) |
| 45 | + |
| 46 | + (func $witness-dropped-via-cancel-write (param i32) |
| 47 | + (local $ret i32) |
| 48 | + (local.set $ret (call $stream.cancel-write (local.get 0))) |
| 49 | + (if (i32.ne (local.get $ret) (i32.const 0x01 (; DROPPED ;))) |
| 50 | + (then unreachable)) |
| 51 | + ) |
| 52 | + |
| 53 | + (func $witness-dropped-via-waitable-set.wait (param i32) |
| 54 | + (local $ws i32) |
| 55 | + (local.set $ws (call $waitable-set.new)) |
| 56 | + (call $waitable.join (local.get 0) (local.get $ws)) |
| 57 | + (call $waitable-set.wait (local.get $ws) (i32.const 4)) |
| 58 | + drop ;; ignore the event |
| 59 | + (call $waitable.join (local.get 0) (i32.const 0)) |
| 60 | + (call $waitable-set.drop (local.get $ws)) |
| 61 | + |
| 62 | + (if (i32.ne (i32.load (i32.const 4)) (local.get 0)) |
| 63 | + (then unreachable)) |
| 64 | + (if (i32.ne (i32.load (i32.const 8)) (i32.const 0x01 (; DROPPED ;))) |
| 65 | + (then unreachable)) |
| 66 | + ) |
| 67 | + |
| 68 | + (func $witness-dropped-via-waitable-set.poll (param i32) |
| 69 | + (local $ws i32) |
| 70 | + (local.set $ws (call $waitable-set.new)) |
| 71 | + (call $waitable.join (local.get 0) (local.get $ws)) |
| 72 | + (call $waitable-set.poll (local.get $ws) (i32.const 4)) |
| 73 | + drop ;; ignore the event |
| 74 | + (call $waitable.join (local.get 0) (i32.const 0)) |
| 75 | + (call $waitable-set.drop (local.get $ws)) |
| 76 | + |
| 77 | + (if (i32.ne (i32.load (i32.const 4)) (local.get 0)) |
| 78 | + (then unreachable)) |
| 79 | + (if (i32.ne (i32.load (i32.const 8)) (i32.const 0x01 (; DROPPED ;))) |
| 80 | + (then unreachable)) |
| 81 | + ) |
| 82 | + |
| 83 | + (func $witness-dropped-via-future.cancel-write (param i32) |
| 84 | + (local $ret i32) |
| 85 | + (local.set $ret (call $future.cancel-write (local.get 0))) |
| 86 | + (if (i32.ne (local.get $ret) (i32.const 0x01 (; DROPPED ;))) |
| 87 | + (then unreachable)) |
| 88 | + ) |
| 89 | + |
| 90 | + (func (export "test-cancel-read") (param i32) |
| 91 | + (local $ret64 i64) |
| 92 | + (local $reader i32) |
| 93 | + (local $writer i32) |
| 94 | + (local $ret i32) |
| 95 | + |
| 96 | + ;; Create a new stream |
| 97 | + (local.set $ret64 (call $stream.new)) |
| 98 | + (local.set $reader (i32.wrap_i64 (local.get $ret64))) |
| 99 | + (local.set $writer (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32)))) |
| 100 | + |
| 101 | + ;; Start a read which will block (no writer ready) |
| 102 | + (local.set $ret (call $stream.read (local.get $reader) (i32.const 16) (i32.const 100))) |
| 103 | + (if (i32.ne (local.get $ret) (i32.const -1 (; BLOCKED ;))) |
| 104 | + (then unreachable)) |
| 105 | + |
| 106 | + ;; Drop the writer, which queues a DROPPED event for the pending read |
| 107 | + (call $stream.drop-writable (local.get $writer)) |
| 108 | + |
| 109 | + ;; Receive the dropped event |
| 110 | + local.get $reader |
| 111 | + local.get 0 |
| 112 | + call_indirect (param i32) |
| 113 | + |
| 114 | + ;; attempting to read again should fail |
| 115 | + (drop (call $stream.read (local.get $reader) (i32.const 16) (i32.const 100))) |
| 116 | + |
| 117 | + unreachable |
| 118 | + ) |
| 119 | + |
| 120 | + (func (export "test-cancel-write") (param i32) |
| 121 | + (local $ret64 i64) |
| 122 | + (local $reader i32) |
| 123 | + (local $writer i32) |
| 124 | + (local $ret i32) |
| 125 | + |
| 126 | + ;; Create a new stream |
| 127 | + (local.set $ret64 (call $stream.new)) |
| 128 | + (local.set $reader (i32.wrap_i64 (local.get $ret64))) |
| 129 | + (local.set $writer (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32)))) |
| 130 | + |
| 131 | + ;; Write some data (will block since no reader is ready) |
| 132 | + (local.set $ret (call $stream.write (local.get $writer) (i32.const 0) (i32.const 1))) |
| 133 | + (if (i32.ne (local.get $ret) (i32.const -1 (; BLOCKED ;))) |
| 134 | + (then unreachable)) |
| 135 | + |
| 136 | + ;; Drop the reader, which queues a DROPPED event for the pending write |
| 137 | + (call $stream.drop-readable (local.get $reader)) |
| 138 | + |
| 139 | + ;; Receive the dropped event |
| 140 | + local.get $writer |
| 141 | + local.get 0 |
| 142 | + call_indirect (param i32) |
| 143 | + |
| 144 | + ;; attempting to write again should fail |
| 145 | + (drop (call $stream.write (local.get $writer) (i32.const 0) (i32.const 1))) |
| 146 | + |
| 147 | + unreachable |
| 148 | + ) |
| 149 | + |
| 150 | + (func (export "test-cancel-future-write") (param i32) |
| 151 | + (local $ret64 i64) |
| 152 | + (local $reader i32) |
| 153 | + (local $writer i32) |
| 154 | + (local $ret i32) |
| 155 | + |
| 156 | + ;; Create a new future |
| 157 | + (local.set $ret64 (call $future.new)) |
| 158 | + (local.set $reader (i32.wrap_i64 (local.get $ret64))) |
| 159 | + (local.set $writer (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32)))) |
| 160 | + |
| 161 | + ;; Write some data (will block since no reader is ready) |
| 162 | + (local.set $ret (call $future.write (local.get $writer) (i32.const 0))) |
| 163 | + (if (i32.ne (local.get $ret) (i32.const -1 (; BLOCKED ;))) |
| 164 | + (then unreachable)) |
| 165 | + |
| 166 | + ;; Drop the reader, which queues a DROPPED event for the pending write |
| 167 | + (call $future.drop-readable (local.get $reader)) |
| 168 | + |
| 169 | + ;; Receive the dropped event |
| 170 | + local.get $writer |
| 171 | + local.get 0 |
| 172 | + call_indirect (param i32) |
| 173 | + |
| 174 | + ;; attempting to write again should fail |
| 175 | + (drop (call $future.write (local.get $writer) (i32.const 0))) |
| 176 | + |
| 177 | + unreachable |
| 178 | + ) |
| 179 | + ) |
| 180 | + |
| 181 | + (core func $waitable-set.new (canon waitable-set.new)) |
| 182 | + (core func $waitable.join (canon waitable.join)) |
| 183 | + (core func $waitable-set.wait (canon waitable-set.wait (memory $libc "mem"))) |
| 184 | + (core func $waitable-set.poll (canon waitable-set.poll (memory $libc "mem"))) |
| 185 | + (core func $waitable-set.drop (canon waitable-set.drop)) |
| 186 | + (type $s (stream u8)) |
| 187 | + (core func $stream.new (canon stream.new $s)) |
| 188 | + (core func $stream.read (canon stream.read $s async (memory $libc "mem"))) |
| 189 | + (core func $stream.write (canon stream.write $s async (memory $libc "mem"))) |
| 190 | + (core func $stream.cancel-read (canon stream.cancel-read $s)) |
| 191 | + (core func $stream.cancel-write (canon stream.cancel-write $s)) |
| 192 | + (core func $stream.drop-readable (canon stream.drop-readable $s)) |
| 193 | + (core func $stream.drop-writable (canon stream.drop-writable $s)) |
| 194 | + (type $f (future u8)) |
| 195 | + (core func $future.new (canon future.new $f)) |
| 196 | + (core func $future.read (canon future.read $f async (memory $libc "mem"))) |
| 197 | + (core func $future.write (canon future.write $f async (memory $libc "mem"))) |
| 198 | + (core func $future.cancel-read (canon future.cancel-read $f)) |
| 199 | + (core func $future.cancel-write (canon future.cancel-write $f)) |
| 200 | + (core func $future.drop-readable (canon future.drop-readable $f)) |
| 201 | + (core func $future.drop-writable (canon future.drop-writable $f)) |
| 202 | + |
| 203 | + (core instance $i (instantiate $m (with "" (instance |
| 204 | + (export "mem" (memory $libc "mem")) |
| 205 | + (export "stream.new" (func $stream.new)) |
| 206 | + (export "stream.read" (func $stream.read)) |
| 207 | + (export "stream.write" (func $stream.write)) |
| 208 | + (export "stream.cancel-read" (func $stream.cancel-read)) |
| 209 | + (export "stream.cancel-write" (func $stream.cancel-write)) |
| 210 | + (export "stream.drop-writable" (func $stream.drop-writable)) |
| 211 | + (export "stream.drop-readable" (func $stream.drop-readable)) |
| 212 | + (export "waitable-set.new" (func $waitable-set.new)) |
| 213 | + (export "waitable.join" (func $waitable.join)) |
| 214 | + (export "waitable-set.wait" (func $waitable-set.wait)) |
| 215 | + (export "waitable-set.poll" (func $waitable-set.poll)) |
| 216 | + (export "waitable-set.drop" (func $waitable-set.drop)) |
| 217 | + (export "future.new" (func $future.new)) |
| 218 | + (export "future.read" (func $future.read)) |
| 219 | + (export "future.write" (func $future.write)) |
| 220 | + (export "future.cancel-read" (func $future.cancel-read)) |
| 221 | + (export "future.cancel-write" (func $future.cancel-write)) |
| 222 | + (export "future.drop-writable" (func $future.drop-writable)) |
| 223 | + (export "future.drop-readable" (func $future.drop-readable)) |
| 224 | + )))) |
| 225 | + |
| 226 | + (func (export "test-cancel-read") async (param "x" u32) (canon lift (core func $i "test-cancel-read"))) |
| 227 | + (func (export "test-cancel-write") async (param "x" u32) (canon lift (core func $i "test-cancel-write"))) |
| 228 | + (func (export "test-cancel-future-write") async (param "x" u32) (canon lift (core func $i "test-cancel-future-write"))) |
| 229 | +) |
| 230 | + |
| 231 | +(component instance $A $A) |
| 232 | +(assert_trap (invoke "test-cancel-read" (u32.const 0)) "cannot read from stream after being notified that the writable end dropped") |
| 233 | +(component instance $A $A) |
| 234 | +(assert_trap (invoke "test-cancel-read" (u32.const 2)) "cannot read from stream after being notified that the writable end dropped") |
| 235 | +(component instance $A $A) |
| 236 | +(assert_trap (invoke "test-cancel-read" (u32.const 3)) "cannot read from stream after being notified that the writable end dropped") |
| 237 | + |
| 238 | +(component instance $A $A) |
| 239 | +(assert_trap (invoke "test-cancel-write" (u32.const 1)) "cannot write to stream after being notified that the readable end dropped") |
| 240 | +(component instance $A $A) |
| 241 | +(assert_trap (invoke "test-cancel-write" (u32.const 2)) "cannot write to stream after being notified that the readable end dropped") |
| 242 | +(component instance $A $A) |
| 243 | +(assert_trap (invoke "test-cancel-write" (u32.const 3)) "cannot write to stream after being notified that the readable end dropped") |
| 244 | + |
| 245 | + |
| 246 | +(component instance $A $A) |
| 247 | +(assert_trap (invoke "test-cancel-future-write" (u32.const 2)) "cannot write to stream after being notified that the readable end dropped") |
| 248 | +(component instance $A $A) |
| 249 | +(assert_trap (invoke "test-cancel-future-write" (u32.const 3)) "cannot write to stream after being notified that the readable end dropped") |
| 250 | +(component instance $A $A) |
| 251 | +(assert_trap (invoke "test-cancel-future-write" (u32.const 4)) "cannot write to stream after being notified that the readable end dropped") |
0 commit comments