mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:17:35 +00:00
LibWeb: Implement ReadableStreamBYOBReader::read
Similiarly to the underlying AO's, currently only TypedArrays are supported.
This commit is contained in:
parent
dcb7bb4a92
commit
1daded768d
5 changed files with 138 additions and 1 deletions
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include <LibJS/Runtime/PromiseCapability.h>
|
||||
#include <LibJS/Runtime/TypedArray.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Streams/AbstractOperations.h>
|
||||
#include <LibWeb/Streams/ReadableStream.h>
|
||||
|
@ -56,4 +57,101 @@ void ReadableStreamBYOBReader::visit_edges(Cell::Visitor& visitor)
|
|||
visitor.visit(request);
|
||||
}
|
||||
|
||||
class BYOBReaderReadIntoRequest : public ReadIntoRequest {
|
||||
JS_CELL(BYOBReaderReadIntoRequest, ReadIntoRequest);
|
||||
|
||||
public:
|
||||
BYOBReaderReadIntoRequest(JS::Realm& realm, WebIDL::Promise& promise)
|
||||
: m_realm(realm)
|
||||
, m_promise(promise)
|
||||
{
|
||||
}
|
||||
|
||||
// chunk steps, given chunk
|
||||
virtual void on_chunk(JS::Value chunk) override
|
||||
{
|
||||
// 1. Resolve promise with «[ "value" → chunk, "done" → false ]».
|
||||
WebIDL::resolve_promise(m_realm, m_promise, JS::create_iterator_result_object(m_realm.vm(), chunk, false));
|
||||
}
|
||||
|
||||
// close steps, given chunk
|
||||
virtual void on_close(JS::Value chunk) override
|
||||
{
|
||||
// 1. Resolve promise with «[ "value" → chunk, "done" → true ]».
|
||||
WebIDL::resolve_promise(m_realm, m_promise, JS::create_iterator_result_object(m_realm.vm(), chunk, true));
|
||||
}
|
||||
|
||||
// error steps, given e
|
||||
virtual void on_error(JS::Value error) override
|
||||
{
|
||||
// 1. Reject promise with e.
|
||||
WebIDL::reject_promise(m_realm, m_promise, error);
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void visit_edges(Visitor& visitor) override
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_realm);
|
||||
visitor.visit(m_promise);
|
||||
}
|
||||
|
||||
JS::Realm& m_realm;
|
||||
WebIDL::Promise& m_promise;
|
||||
};
|
||||
|
||||
// https://streams.spec.whatwg.org/#byob-reader-read
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> ReadableStreamBYOBReader::read(JS::Value view_value)
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
|
||||
// FIXME: Support DataViews
|
||||
auto& view = verify_cast<JS::TypedArrayBase>(view_value.as_object());
|
||||
|
||||
// 1. If view.[[ByteLength]] is 0, return a promise rejected with a TypeError exception.
|
||||
if (view.byte_length() == 0) {
|
||||
auto exception = JS::TypeError::create(realm, "Cannot read in an empty buffer"sv);
|
||||
auto promise_capability = WebIDL::create_rejected_promise(realm, exception);
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise()) };
|
||||
}
|
||||
|
||||
// 2. If view.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is 0, return a promise rejected with a TypeError exception.
|
||||
if (view.viewed_array_buffer()->byte_length() == 0) {
|
||||
auto exception = JS::TypeError::create(realm, "Cannot read in an empty buffer"sv);
|
||||
auto promise_capability = WebIDL::create_rejected_promise(realm, exception);
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise()) };
|
||||
}
|
||||
|
||||
// 3. If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, return a promise rejected with a TypeError exception.
|
||||
if (view.viewed_array_buffer()->is_detached()) {
|
||||
auto exception = JS::TypeError::create(realm, "Cannot read in a detached buffer"sv);
|
||||
auto promise_capability = WebIDL::create_rejected_promise(realm, exception);
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise()) };
|
||||
}
|
||||
|
||||
// 4. If this.[[stream]] is undefined, return a promise rejected with a TypeError exception.
|
||||
if (!m_stream) {
|
||||
auto exception = JS::TypeError::create(realm, "Cannot read from an empty stream"sv);
|
||||
auto promise_capability = WebIDL::create_rejected_promise(realm, exception);
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise()) };
|
||||
}
|
||||
|
||||
// 5. Let promise be a new promise.
|
||||
auto promise_capability = WebIDL::create_promise(realm);
|
||||
|
||||
// 6. Let readIntoRequest be a new read-into request with the following items:
|
||||
// chunk steps, given chunk
|
||||
// Resolve promise with «[ "value" → chunk, "done" → false ]».
|
||||
// close steps, given chunk
|
||||
// Resolve promise with «[ "value" → chunk, "done" → true ]».
|
||||
// error steps, given e
|
||||
// Reject promise with e.
|
||||
auto read_into_request = heap().allocate_without_realm<BYOBReaderReadIntoRequest>(realm, promise_capability);
|
||||
|
||||
// 7. Perform ! ReadableStreamBYOBReaderRead(this, view, readIntoRequest).
|
||||
readable_stream_byob_reader_read(*this, view_value, *read_into_request);
|
||||
|
||||
// 8. Return promise.
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise()) };
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue