Skip to content

Commit 5f759ab

Browse files
committed
Change stream/future built-ins' typeidx to refer to full stream/future type
1 parent 3ffb650 commit 5f759ab

4 files changed

Lines changed: 230 additions & 220 deletions

File tree

design/mvp/CanonicalABI.md

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,11 +3653,12 @@ async def canon_subtask_drop(task, i):
36533653

36543654
For canonical definitions:
36553655
```wat
3656-
(canon stream.new $t (core func $f))
3657-
(canon future.new $t (core func $f))
3656+
(canon stream.new $stream_t (core func $f))
3657+
(canon future.new $future_t (core func $f))
36583658
```
36593659
validation specifies:
36603660
* `$f` is given type `(func (result i64))`
3661+
* `$stream_t`/`$future_t` is a type of the form `(stream $t?)`/`(future $t?)`
36613662

36623663
Calling `$f` calls `canon_{stream,future}_new` which adds two elements to the
36633664
current component instance's `waitables` table and returns the indices packed
@@ -3668,16 +3669,16 @@ the readable end is subsequently transferred to another component (or the host)
36683669
via `stream` or `future` parameter/result type (see `lift_{stream,future}`
36693670
above).
36703671
```python
3671-
async def canon_stream_new(elem_type, task):
3672+
async def canon_stream_new(stream_t, task):
36723673
trap_if(not task.inst.may_leave)
3673-
stream = ReadableStreamGuestImpl(elem_type)
3674+
stream = ReadableStreamGuestImpl(stream_t.t)
36743675
ri = task.inst.waitables.add(ReadableStreamEnd(stream))
36753676
wi = task.inst.waitables.add(WritableStreamEnd(stream))
36763677
return [ ri | (wi << 32) ]
36773678

3678-
async def canon_future_new(t, task):
3679+
async def canon_future_new(future_t, task):
36793680
trap_if(not task.inst.may_leave)
3680-
future = ReadableStreamGuestImpl(t)
3681+
future = ReadableStreamGuestImpl(future_t.t)
36813682
ri = task.inst.waitables.add(ReadableFutureEnd(future))
36823683
wi = task.inst.waitables.add(WritableFutureEnd(future))
36833684
return [ ri | (wi << 32) ]
@@ -3692,45 +3693,49 @@ the future built-ins below.
36923693

36933694
For canonical definitions:
36943695
```wat
3695-
(canon stream.read $t $opts (core func $f))
3696-
(canon stream.write $t $opts (core func $f))
3696+
(canon stream.read $stream_t $opts (core func $f))
3697+
(canon stream.write $stream_t $opts (core func $f))
36973698
```
36983699
In addition to [general validation of `$opts`](#canonopt-validation) validation
36993700
specifies:
37003701
* `$f` is given type `(func (param i32 i32 i32) (result i32))`
3701-
* [`lower($t)` above](#canonopt-validation) defines required options for `stream.write`
3702-
* [`lift($t)` above](#canonopt-validation) defines required options for `stream.read`
3703-
* `memory` is required to be present
3702+
* `$stream_t` is a type of the form `(stream $t?)`
3703+
* If `$t` is present:
3704+
* [`lower($t)` above](#canonopt-validation) defines required options for `stream.write`
3705+
* [`lift($t)` above](#canonopt-validation) defines required options for `stream.read`
3706+
* `memory` is required to be present
37043707

37053708
For canonical definitions:
37063709
```wat
3707-
(canon future.read $t $opts (core func $f))
3708-
(canon future.write $t $opts (core func $f))
3710+
(canon future.read $future_t $opts (core func $f))
3711+
(canon future.write $future_t $opts (core func $f))
37093712
```
37103713
validation specifies:
37113714
* `$f` is given type `(func (param i32 i32) (result i32))`
3712-
* [`lower($t)` above](#canonopt-validation) defines required options for `future.write`
3713-
* [`lift($t)` above](#canonopt-validation) defines required options for `future.read`
3714-
* `memory` is required to be present
3715+
* `$future_t` is a type of the form `(future $t?)`
3716+
* If `$t` is present:
3717+
* [`lower($t)` above](#canonopt-validation) defines required options for `future.write`
3718+
* [`lift($t)` above](#canonopt-validation) defines required options for `future.read`
3719+
* `memory` is required to be present
37153720

37163721
The implementation of these four built-ins all funnel down to a single
37173722
parameterized `copy` function:
37183723
```python
3719-
async def canon_stream_read(t, opts, task, i, ptr, n):
3724+
async def canon_stream_read(stream_t, opts, task, i, ptr, n):
37203725
return await copy(ReadableStreamEnd, WritableBufferGuestImpl, EventCode.STREAM_READ,
3721-
t, opts, task, i, ptr, n)
3726+
stream_t, opts, task, i, ptr, n)
37223727

3723-
async def canon_stream_write(t, opts, task, i, ptr, n):
3728+
async def canon_stream_write(stream_t, opts, task, i, ptr, n):
37243729
return await copy(WritableStreamEnd, ReadableBufferGuestImpl, EventCode.STREAM_WRITE,
3725-
t, opts, task, i, ptr, n)
3730+
stream_t, opts, task, i, ptr, n)
37263731

3727-
async def canon_future_read(t, opts, task, i, ptr):
3732+
async def canon_future_read(future_t, opts, task, i, ptr):
37283733
return await copy(ReadableFutureEnd, WritableBufferGuestImpl, EventCode.FUTURE_READ,
3729-
t, opts, task, i, ptr, 1)
3734+
future_t, opts, task, i, ptr, 1)
37303735

3731-
async def canon_future_write(t, opts, task, i, ptr):
3736+
async def canon_future_write(future_t, opts, task, i, ptr):
37323737
return await copy(WritableFutureEnd, ReadableBufferGuestImpl, EventCode.FUTURE_WRITE,
3733-
t, opts, task, i, ptr, 1)
3738+
future_t, opts, task, i, ptr, 1)
37343739
```
37353740

37363741
Introducing the `copy` function in chunks, `copy` first checks that the
@@ -3740,15 +3745,15 @@ finite number of pipelined reads or writes.) Then a readable or writable buffer
37403745
is created which (in `Buffer`'s constructor) eagerly checks the alignment and
37413746
bounds of (`i`, `n`).
37423747
```python
3743-
async def copy(EndT, BufferT, event_code, t, opts, task, i, ptr, n):
3748+
async def copy(EndT, BufferT, event_code, stream_or_future_t, opts, task, i, ptr, n):
37443749
trap_if(not task.inst.may_leave)
37453750
e = task.inst.waitables.get(i)
37463751
trap_if(not isinstance(e, EndT))
3747-
trap_if(e.stream.t != t)
3752+
trap_if(e.stream.t != stream_or_future_t.t)
37483753
trap_if(e.copying)
3749-
assert(not contains_borrow(t))
3754+
assert(not contains_borrow(stream_or_future_t))
37503755
cx = LiftLowerContext(opts, task.inst, borrow_scope = None)
3751-
buffer = BufferT(t, cx, ptr, n)
3756+
buffer = BufferT(stream_or_future_t.t, cx, ptr, n)
37523757
```
37533758

37543759
Next, in the synchronous case, `Task.wait_on` is used to synchronously and
@@ -3830,35 +3835,36 @@ the `read` or `write` and so this number is packed into the `i32` result.
38303835

38313836
For canonical definitions:
38323837
```wat
3833-
(canon stream.cancel-read $t $async? (core func $f))
3834-
(canon stream.cancel-write $t $async? (core func $f))
3835-
(canon future.cancel-read $t $async? (core func $f))
3836-
(canon future.cancel-write $t $async? (core func $f))
3838+
(canon stream.cancel-read $stream_t $async? (core func $f))
3839+
(canon stream.cancel-write $stream_t $async? (core func $f))
3840+
(canon future.cancel-read $future_t $async? (core func $f))
3841+
(canon future.cancel-write $future_t $async? (core func $f))
38373842
```
38383843
validation specifies:
38393844
* `$f` is given type `(func (param i32) (result i32))`
3845+
* `$stream_t`/`$future_t` is a type of the form `(stream $t?)`/`(future $t?)`
38403846
* 🚝 - `async` is allowed (otherwise it must be `false`)
38413847

38423848
The implementation of these four built-ins all funnel down to a single
38433849
parameterized `cancel_copy` function:
38443850
```python
3845-
async def canon_stream_cancel_read(t, sync, task, i):
3846-
return await cancel_copy(ReadableStreamEnd, EventCode.STREAM_READ, t, sync, task, i)
3851+
async def canon_stream_cancel_read(stream_t, sync, task, i):
3852+
return await cancel_copy(ReadableStreamEnd, EventCode.STREAM_READ, stream_t, sync, task, i)
38473853

3848-
async def canon_stream_cancel_write(t, sync, task, i):
3849-
return await cancel_copy(WritableStreamEnd, EventCode.STREAM_WRITE, t, sync, task, i)
3854+
async def canon_stream_cancel_write(stream_t, sync, task, i):
3855+
return await cancel_copy(WritableStreamEnd, EventCode.STREAM_WRITE, stream_t, sync, task, i)
38503856

3851-
async def canon_future_cancel_read(t, sync, task, i):
3852-
return await cancel_copy(ReadableFutureEnd, EventCode.FUTURE_READ, t, sync, task, i)
3857+
async def canon_future_cancel_read(future_t, sync, task, i):
3858+
return await cancel_copy(ReadableFutureEnd, EventCode.FUTURE_READ, future_t, sync, task, i)
38533859

3854-
async def canon_future_cancel_write(t, sync, task, i):
3855-
return await cancel_copy(WritableFutureEnd, EventCode.FUTURE_WRITE, t, sync, task, i)
3860+
async def canon_future_cancel_write(future_t, sync, task, i):
3861+
return await cancel_copy(WritableFutureEnd, EventCode.FUTURE_WRITE, future_t, sync, task, i)
38563862

3857-
async def cancel_copy(EndT, event_code, t, sync, task, i):
3863+
async def cancel_copy(EndT, event_code, stream_or_future_t, sync, task, i):
38583864
trap_if(not task.inst.may_leave)
38593865
e = task.inst.waitables.get(i)
38603866
trap_if(not isinstance(e, EndT))
3861-
trap_if(e.stream.t != t)
3867+
trap_if(e.stream.t != stream_or_future_t.t)
38623868
trap_if(not e.copying)
38633869
if not e.has_pending_event():
38643870
e.stream.cancel()
@@ -3897,36 +3903,37 @@ caller can assume that ownership of the buffer has been returned.
38973903

38983904
For canonical definitions:
38993905
```wat
3900-
(canon stream.close-readable $t (core func $f))
3901-
(canon stream.close-writable $t (core func $f))
3902-
(canon future.close-readable $t (core func $f))
3903-
(canon future.close-writable $t (core func $f))
3906+
(canon stream.close-readable $stream_t (core func $f))
3907+
(canon stream.close-writable $stream_t (core func $f))
3908+
(canon future.close-readable $future_t (core func $f))
3909+
(canon future.close-writable $future_t (core func $f))
39043910
```
39053911
validation specifies:
39063912
* `$f` is given type `(func (param i32 i32))`
3913+
* `$stream_t`/`$future_t` is a type of the form `(stream $t?)`/`(future $t?)`
39073914

39083915
Calling `$f` removes the readable or writable end of the stream or future at
39093916
the given index from the current component instance's `waitable` table,
39103917
performing the guards and bookkeeping defined by
39113918
`{Readable,Writable}{Stream,Future}End.drop()` above.
39123919
```python
3913-
async def canon_stream_close_readable(t, task, i):
3914-
return await close(ReadableStreamEnd, t, task, i)
3920+
async def canon_stream_close_readable(stream_t, task, i):
3921+
return await close(ReadableStreamEnd, stream_t, task, i)
39153922

3916-
async def canon_stream_close_writable(t, task, hi):
3917-
return await close(WritableStreamEnd, t, task, hi)
3923+
async def canon_stream_close_writable(stream_t, task, hi):
3924+
return await close(WritableStreamEnd, stream_t, task, hi)
39183925

3919-
async def canon_future_close_readable(t, task, i):
3920-
return await close(ReadableFutureEnd, t, task, i)
3926+
async def canon_future_close_readable(future_t, task, i):
3927+
return await close(ReadableFutureEnd, future_t, task, i)
39213928

3922-
async def canon_future_close_writable(t, task, hi):
3923-
return await close(WritableFutureEnd, t, task, hi)
3929+
async def canon_future_close_writable(future_t, task, hi):
3930+
return await close(WritableFutureEnd, future_t, task, hi)
39243931

3925-
async def close(EndT, t, task, hi):
3932+
async def close(EndT, stream_or_future_t, task, hi):
39263933
trap_if(not task.inst.may_leave)
39273934
e = task.inst.waitables.remove(hi)
39283935
trap_if(not isinstance(e, EndT))
3929-
trap_if(e.stream.t != t)
3936+
trap_if(e.stream.t != stream_or_future_t.t)
39303937
e.drop()
39313938
return []
39323939
```

0 commit comments

Comments
 (0)