1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:07:34 +00:00

LibWeb: Mostly implement ReadableByteStreamController.[[PullSteps]]

This commit is contained in:
Matthew Olsson 2023-04-08 11:46:11 -07:00 committed by Linus Groh
parent c97f6b7701
commit 51abecc8bc
4 changed files with 116 additions and 0 deletions

View file

@ -765,6 +765,34 @@ void readable_byte_stream_controller_clear_pending_pull_intos(ReadableByteStream
controller.pending_pull_intos().clear();
}
// https://streams.spec.whatwg.org/#abstract-opdef-readablebytestreamcontrollerfillreadrequestfromqueue
WebIDL::ExceptionOr<void> readable_byte_stream_controller_fill_read_request_from_queue(ReadableByteStreamController& controller, NonnullRefPtr<ReadRequest> read_request)
{
auto& vm = controller.vm();
auto& realm = controller.realm();
// 1. Assert: controller.[[queueTotalSize]] > 0.
VERIFY(controller.queue_total_size() > 0);
// 2. Let entry be controller.[[queue]][0].
// 3. Remove entry from controller.[[queue]].
auto entry = controller.queue().take_first();
// 4. Set controller.[[queueTotalSize]] to controller.[[queueTotalSize]] entrys byte length.
controller.set_queue_total_size(controller.queue_total_size() - entry.byte_length);
// 5. Perform ! ReadableByteStreamControllerHandleQueueDrain(controller).
readable_byte_stream_controller_handle_queue_drain(controller);
// 6. Let view be ! Construct(%Uint8Array%, « entrys buffer, entrys byte offset, entrys byte length »).
auto view = MUST_OR_THROW_OOM(JS::construct(vm, *realm.intrinsics().uint8_array_constructor(), entry.buffer, JS::Value(entry.byte_offset), JS::Value(entry.byte_length)));
// 7. Perform readRequests chunk steps, given view.
read_request->on_chunk(view);
return {};
}
// https://streams.spec.whatwg.org/#readable-byte-stream-controller-get-desired-size
Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteStreamController const& controller)
{
@ -783,6 +811,26 @@ Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteSt
return controller.strategy_hwm() - controller.queue_total_size();
}
// https://streams.spec.whatwg.org/#readable-byte-stream-controller-handle-queue-drain
void readable_byte_stream_controller_handle_queue_drain(ReadableByteStreamController& controller)
{
// 1. Assert: controller.[[stream]].[[state]] is "readable".
VERIFY(controller.stream()->state() == ReadableStream::State::Readable);
// 2. If controller.[[queueTotalSize]] is 0 and controller.[[closeRequested]] is true,
if (controller.queue_total_size() == 0 && controller.close_requested()) {
// 1. Perform ! ReadableByteStreamControllerClearAlgorithms(controller).
readable_byte_stream_controller_clear_algorithms(controller);
// 2. Perform ! ReadableStreamClose(controller.[[stream]]).
readable_stream_close(*controller.stream());
}
// 3. Otherwise,
else {
// FIXME: 1. Perform ! ReadableByteStreamControllerCallPullIfNeeded(controller).
}
}
// https://streams.spec.whatwg.org/#readable-byte-stream-controller-invalidate-byob-request
void readable_byte_stream_controller_invalidate_byob_request(ReadableByteStreamController& controller)
{