Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ReadableByteStream to follow the style rule employed for the main... #283

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 59 additions & 66 deletions BinaryExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ By admitting returning the ArrayBuffer with no data written on it, `ReadableByte

### ReadableByteStream

#### Class Definition

```
class ReadableByteStream {
constructor(underlyingByteSource = {})
Expand All @@ -42,26 +44,21 @@ class ReadableByteStream {
pipeTo(dest, { preventClose, preventAbort, preventCancel } = {})
read()
readInto(arrayBuffer, offset, size)

// Internal slots
[[state]] = "waiting"
[[storedError]]

[[readyPromise]]
[[closedPromise]]

// Holders for stuff given by the underlying source
[[onReadInto]]
[[onCancel]]

// Internal methods for use by the underlying source
[[notifyReady]]()
[[error]](any e)
}
```

`underlyingByteSource` has optional methods `start`, `readInto` and `cancel`, and an optional property `readBufferSize`.

#### Internal Slots

| Internal Slot | Description (_non-normative_) |
|---------------|-------------------------------|
|[[closedPromise]]|Same as `ReadableStream`|
|[[readyPromise]]|Same as `ReadableStream`|
|[[state]]|Same as `ReadableStream`. Initially "waiting"|
|[[storedError]]|Same as `ReadableStream`|
|[[underlyingByteSource]]|An object representation of the stream's underlying byte source; also used for the `IsReadableByteStream` brand check|

#### Abstract Operations For ReadableByteStream Objects

##### Notify Ready Function
Expand All @@ -71,18 +68,18 @@ A notify ready function is an anonymous built-in function that has [[Stream]] in
When a notify ready function _F_ is called, the following steps are taken:

1. Let _stream_ be the value of _F_'s [[Stream]] internal slot.
1. If _stream_.[[state]] is not `"waiting"`, return.
1. Set _stream_.[[state]] to `"readable"`.
1. Resolve _stream_.[[readyPromise]] with **undefined**.
1. If _stream_@[[state]] is not `"waiting"`, return.
1. Set _stream_@[[state]] to `"readable"`.
1. Resolve _stream_@[[readyPromise]] with **undefined**.

##### ErrorReadableByteStream( stream, error )

1. If _stream_.[[state]] is `"errored"` or `"closed"`, return.
1. If _stream_.[[state]] is `"waiting"`, reject _stream_.[[readyPromise]] with _error_.
1. If _stream_.[[state]] is `"readable"`, let _stream_.[[readyPromise]] be a new promise rejected with _error_.
1. Set _stream_.[[state]] to `"errored"`.
1. Set _stream_.[[storedError]] to _error_.
1. Reject _stream_.[[closedPromise]] with _error_.
1. If _stream_@[[state]] is `"errored"` or `"closed"`, return.
1. If _stream_@[[state]] is `"waiting"`, reject _stream_@[[readyPromise]] with _error_.
1. If _stream_@[[state]] is `"readable"`, let _stream_@[[readyPromise]] be a new promise rejected with _error_.
1. Set _stream_@[[state]] to `"errored"`.
1. Set _stream_@[[storedError]] to _error_.
1. Reject _stream_@[[closedPromise]] with _error_.

##### Error Function

Expand All @@ -93,40 +90,39 @@ When an error function _F_ is called with argument _error_, the following steps
1. Let _stream_ be the value of _F_'s [[Stream]] internal slot.
1. ErrorReadableByteStream(_stream_, _error_).

#### Properties of the ReadableByteStream Prototype Object
#### constructor({ start, readInto, cancel, readBufferSize })

##### constructor({ start, readInto, cancel, readBufferSize })

1. Let _stream_ be the **this** value.
1. If IsCallable(_start_) is false, then throw a **TypeError** exception.
1. If IsCallable(_readInto_) is false, then throw a **TypeError** exception.
1. If IsCallable(_cancel_) is false, then throw a **TypeError** exception.
1. If _readBufferSize_ is not **undefined**,
1. Let _readBufferSize_ be ToInteger(_readBufferSize_).
1. If _readBufferSize_ < 0, throw a **RangeError** exception.
1. Set _stream_.[[onReadInto]] to _readInto_.
1. Set _stream_.[[onCancel]] to _cancel_.
1. Set _stream_.[[readBufferSize]] to _readBufferSize_.
1. Let _stream_.[[readyPromise]] be a new promise.
1. Let _stream_.[[closedPromise]] be a new promise.
1. Let _stream_.[[notifyReady]] be a new built-in function object as defined in Notify Ready Function with [[Stream]] internal slot set to _stream_.
1. Let _stream_.[[error]] be a new built-in function object as defined in Error Function with [[Stream]] internal slot set to _stream_.
1. Let _startResult_ be the result of calling the [[Call]] internal method of _start_ with **undefined** as _thisArgument_ and (_stream_.[[notifyReady]], _stream_.[[error]]) as _argumentList_.
1. Set **this**@[[onReadInto]] to _readInto_.
1. Set **this**@[[onCancel]] to _cancel_.
1. Set **this**@[[readBufferSize]] to _readBufferSize_.
1. Let **this**@[[readyPromise]] be a new promise.
1. Let **this**@[[closedPromise]] be a new promise.
1. Let **this**@[[notifyReady]] be a new built-in function object as defined in Notify Ready Function with [[Stream]] internal slot set to _stream_.
1. Let **this**@[[error]] be a new built-in function object as defined in Error Function with [[Stream]] internal slot set to _stream_.
1. Let _startResult_ be the result of calling the [[Call]] internal method of _start_ with **undefined** as _thisArgument_ and (**this**@[[notifyReady]], **this**@[[error]]) as _argumentList_.
1. ReturnIfAbrupt(_startResult_).

##### ReadableByteStream.prototype.read ()
#### Properties of the ReadableByteStream Prototype

##### read()

1. If **this**.[[readBufferSize]] is **undefined**, throw a **TypeError** exception.
1. Let _arrayBuffer_ be a new array buffer with length equals to **this**.[[readBufferSize]].
1. Let _bytesRead_ be Invoke(**this**, `"readInto"`, (_arrayBuffer_, 0, **this**.[[readBufferSize]])).
1. If **this**@[[readBufferSize]] is **undefined**, throw a **TypeError** exception.
1. Let _arrayBuffer_ be a new array buffer with length equals to **this**@[[readBufferSize]].
1. Let _bytesRead_ be Invoke(**this**, `"readInto"`, (_arrayBuffer_, 0, **this**@[[readBufferSize]])).
1. Let _resizedArrayBuffer_ be a new array buffer with length equal to _bytesRead_ created by transferring _arrayBuffer_ using the semantics of [the proposed `ArrayBuffer.transfer`](https://gist.github.com/andhow/95fb9e49996615764eff).
1. Return _resizedArrayBuffer_.

##### ReadableByteStream.prototype.readInto ( arrayBuffer, offset, size )
##### readInto(arrayBuffer, offset, size)

1. If **this**.[[state]] is `"waiting"` or `"closed"`, throw a **TypeError** exception.
1. If **this**.[[state]] is `"errored"`, throw **this**.[[storedError]].
1. Assert: **this**.[[state]] is `"readable"`.
1. If **this**@[[state]] is `"waiting"` or `"closed"`, throw a **TypeError** exception.
1. If **this**@[[state]] is `"errored"`, throw **this**@[[storedError]].
1. Assert: **this**@[[state]] is `"readable"`.
1. If Type(_arrayBuffer_) is not Object, throw a **TypeError** exception.
1. If _arrayBuffer_ does not have an [[ArrayBufferData]] internal slot, throw a **TypeError** exception.
1. If the value of _arrayBuffer_'s [[ArrayBufferData]] internal slot is **undefined**, then throw a **TypeError** exception.
Expand All @@ -142,49 +138,46 @@ When an error function _F_ is called with argument _error_, the following steps
1. Let _size_ be ToInteger(_size_).
1. ReturnIfAbrupt(_size_).
1. If _size_ < 0 or _offset_ + _size_ > _bufferLength_, throw a **RangeError** exception.
1. Let _bytesRead_ be the result of calling the [[Call]] internal method of **this**.[[onReadInto]] with **undefined** as _thisArgument_ and (_arrayBuffer_, _offset_, _size_) as _argumentsList_.
1. Let _bytesRead_ be the result of calling the [[Call]] internal method of **this**@[[onReadInto]] with **undefined** as _thisArgument_ and (_arrayBuffer_, _offset_, _size_) as _argumentsList_.
1. If _bytesRead_ is an abrupt completion,
1. ErrorReadableByteStream(**this**, _bytesRead_.[[value]]).
1. ErrorReadableByteStream(**this**, _bytesRead_@[[value]]).
1. Return _bytesRead_.
1. Let _bytesRead_ be ToNumber(_bytesRead_).
1. If _bytesRead_ is **NaN** or _bytesRead_ < -2 or _bytesRead_ > _bufferLength_,
1. Let _error_ be a **RangeError** exception.
1. ErrorReadableByteStream(**this**, _error_).
1. Throw _error_.
1. If _bytesRead_ is -2,
1. Set **this**.[[state]] to `"waiting"`.
1. Let **this**.[[readyPromise]] be a new promise.
1. Set **this**@[[state]] to `"waiting"`.
1. Let **this**@[[readyPromise]] be a new promise.
1. Return 0.
1. If _bytesRead_ is -1,
1. Set **this**.[[state]] to `"closed"`
1. Resolve **this**.[[closedPromise]] with **undefined**.
1. Set **this**@[[state]] to `"closed"`
1. Resolve **this**@[[closedPromise]] with **undefined**.
1. Return 0.
1. Return _bytesRead_.

##### ReadableByteStream.prototype.cancel ( reason )
##### cancel(reason)

1. If `this.[[state]]` is `"closed"`, return a new promise resolved with **undefined**.
1. If `this.[[state]]` is `"errored"`, return a new promise rejected with `this.[[storedError]]`.
1. If `this.[[state]]` is `"waiting"`, resolve `this.[[readyPromise]]` with **undefined**.
1. Set `this.[[state]]` to `"closed"`.
1. Resolve `this.[[closedPromise]]` with **undefined**.
1. If **this**@[[state]] is `"closed"`, return a new promise resolved with **undefined**.
1. If **this**@[[state]] is `"errored"`, return a new promise rejected with **this**@[[storedError]]`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover backtick at end of line

1. If **this**@[[state]] is `"waiting"`, resolve **this**@[[readyPromise]] with **undefined**.
1. Set **this**@[[state]] to `"closed"`.
1. Resolve **this**@[[closedPromise]] with **undefined**.
1. Let _cancelPromise_ be a new promise.
1. Let _sourceCancelPromise_ be the result of promise-calling **this**.\[\[onCancel]](_reason_).
1. Let _sourceCancelPromise_ be the result of promise-calling **this**@\[[onCancel]](_reason_).
1. Upon fulfillment of _sourceCancelPromise_, resolve _cancelPromise_ with **undefined**.
1. Upon rejection of _sourceCancelPromise_ with reason _r_, reject _cancelPromise_ with _r_.
1. Return _cancelPromise_.

##### get ReadableByteStream.prototype.state
##### get state

1. Let _stream_ be the **this** value.
1. Return _stream_.[[state]].
1. Return **this**@[[state]].

##### get ReadableByteStream.prototype.ready
##### get ready

1. Let _stream_ be the **this** value.
1. Return _stream_.[[readyPromise]].
1. Return **this**@[[readyPromise]].

##### get ReadableByteStream.prototype.closed
##### get closed

1. Let _stream_ be the **this** value.
1. Return _stream_.[[closedPromise]].
1. Return **this**@[[closedPromise]].