dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / streams / readable_byte_stream_controller.lucb
#==============================================================================================
#
# readable_byte_stream_controller - Streams::ReadableByteStreamController: its members and
# the cancel, pull and release steps a stream calls
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Streams/ReadableByteStreamController.cpp (lines 24-211) and the
# inline members of ReadableByteStreamController.h, as part of the ReadableStream stand-in
# of DESIGN.md §3.6 (region p2s). Pull-into descriptors are only made by BYOB readers and
# autoAllocateChunkSize (phase 4): a path that would make one traps.
#
# Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
# Copyright (c) 2023-2025, Shannon Booth <shannon@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: PullIntoDescriptor ======================================================================
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:24 Web::Streams::PullIntoDescriptor::visit_edges (override).
pub func streams_pull_into_descriptor_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (StreamsPullIntoDescriptor*)base
gc.cell_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.buffer)
gc.cell_visitor_visit_t_2(visitor, this.view_constructor)
# mark: ReadableByteStreamController ============================================================
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:32 Web::Streams::ReadableByteStreamController::desired_size.
## https://streams.spec.whatwg.org/#rbs-controller-desired-size
pub func streams_readable_byte_stream_controller_desired_size(this: StreamsReadableByteStreamController*) -> f64?:
# 1. Return ! ReadableByteStreamControllerGetDesiredSize(this).
return streams_readable_byte_stream_controller_get_desired_size(this)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:39 Web::Streams::ReadableByteStreamController::byob_request.
## https://streams.spec.whatwg.org/#rbs-controller-byob-request
pub func streams_readable_byte_stream_controller_byob_request(this: StreamsReadableByteStreamController*) -> StreamsReadableStreamByobRequest*?:
# 1. Return ! ReadableByteStreamControllerGetBYOBRequest(this).
return streams_readable_byte_stream_controller_get_byob_request(this)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:46 Web::Streams::ReadableByteStreamController::close.
## https://streams.spec.whatwg.org/#rbs-controller-close
pub func streams_readable_byte_stream_controller_close(this: StreamsReadableByteStreamController*) -> !:
# 1. If this.[[closeRequested]] is true, throw a TypeError exception.
if this.m_close_requested:
return try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Controller is already closed")))
# 2. If this.[[stream]].[[state]] is not "readable", throw a TypeError exception.
let stream = this.m_stream else trap("VERIFY: the controller has a stream")
if stream.m_state != StreamsReadableStreamState.readable:
let message = ak.sv("Cannot close a closed stream") if stream.m_state == StreamsReadableStreamState.closed else ak.sv("Cannot close an errored stream")
return try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(message))
# 3. Perform ? ReadableByteStreamControllerClose(this).
try streams_readable_byte_stream_controller_close_2(this)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:65 Web::Streams::ReadableByteStreamController::error.
## https://streams.spec.whatwg.org/#rbs-controller-error
pub func streams_readable_byte_stream_controller_error(this: StreamsReadableByteStreamController*, error_: JsValue):
# 1. Perform ! ReadableByteStreamControllerError(this, e).
streams_readable_byte_stream_controller_error_2(this, error_)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:71 Web::Streams::ReadableByteStreamController::ReadableByteStreamController,
## with the default member initializers (all zero: no queue, nothing requested or pulling, a high water mark of 0).
pub func streams_readable_byte_stream_controller_construct(this: StreamsReadableByteStreamController*, realm: Realm*):
bind_platform_object_construct_realm(this.bind_platform_object(), realm)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:76 Web::Streams::ReadableByteStreamController::initialize (override).
pub func streams_readable_byte_stream_controller_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.readable_byte_stream_controller)
js_cell_initialize_impl(base, realm)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:83 Web::Streams::ReadableByteStreamController::enqueue.
## https://streams.spec.whatwg.org/#rbs-controller-enqueue
pub func streams_readable_byte_stream_controller_enqueue(this: StreamsReadableByteStreamController*, chunk: IdlArrayBufferView*) -> !:
# 1. If chunk.[[ByteLength]] is 0, throw a TypeError exception.
# 2. If chunk.[[ViewedArrayBuffer]].[[ByteLength]] is 0, throw a TypeError exception.
let viewed_array_buffer = idl_bufferable_object_base_viewed_array_buffer(chunk.idl_bufferable_object_base()) else trap("VERIFY: a view's buffer")
if idl_bufferable_object_base_byte_length(chunk.idl_bufferable_object_base()) == 0 or js_array_buffer_byte_length(viewed_array_buffer) == 0:
return try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Cannot enqueue chunk with byte length of zero")))
# 3. If this.[[closeRequested]] is true, throw a TypeError exception.
if this.m_close_requested:
return try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Close is requested for controller")))
# 4. If this.[[stream]].[[state]] is not "readable", throw a TypeError exception.
if not streams_readable_stream_is_readable(this.m_stream else trap("VERIFY: the controller has a stream")):
return try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Stream is not readable")))
# 5. Return ? ReadableByteStreamControllerEnqueue(this, chunk).
try streams_readable_byte_stream_controller_enqueue_2(this, js_value_from_object(idl_bufferable_object_base_raw_object(chunk.idl_bufferable_object_base())))
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:103 Web::Streams::ReadableByteStreamController::cancel_steps.
## https://streams.spec.whatwg.org/#rbs-controller-private-cancel
pub func streams_readable_byte_stream_controller_cancel_steps(this: StreamsReadableByteStreamController*, reason: JsValue) -> JsPromiseCapability*:
# 1. Perform ! ReadableByteStreamControllerClearPendingPullIntos(this).
streams_readable_byte_stream_controller_clear_pending_pull_intos(this)
# 2. Perform ! ResetQueue(this).
streams_reset_queue(this)
# 3. Let result be the result of performing this.[[cancelAlgorithm]], passing in reason.
let cancel_algorithm = this.m_cancel_algorithm else trap("VERIFY: the controller's cancel algorithm")
let result = ak.function1_call(&cancel_algorithm.function, reason)
# 4. Perform ! ReadableByteStreamControllerClearAlgorithms(this).
streams_readable_byte_stream_controller_clear_algorithms(this)
# 5. Return result.
return result
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:122 Web::Streams::ReadableByteStreamController::pull_steps.
## https://streams.spec.whatwg.org/#rbs-controller-private-pull
pub func streams_readable_byte_stream_controller_pull_steps(this: StreamsReadableByteStreamController*, read_request: StreamsReadRequest*):
# 1. Let stream be this.[[stream]].
let stream = this.m_stream else trap("VERIFY: the controller has a stream")
# 2. Assert: ! ReadableStreamHasDefaultReader(stream) is true.
assert(streams_readable_stream_has_default_reader(stream))
# 3. If this.[[queueTotalSize]] > 0,
if this.m_queue_total_size > 0.0:
# 1. Assert: ! ReadableStreamGetNumReadRequests(stream) is 0.
assert(streams_readable_stream_get_num_read_requests(stream) == 0)
# 2. Perform ! ReadableByteStreamControllerFillReadRequestFromQueue(this, readRequest).
streams_readable_byte_stream_controller_fill_read_request_from_queue(this, read_request)
# 3. Return.
return
# 4. Let autoAllocateChunkSize be this.[[autoAllocateChunkSize]].
# 5. If autoAllocateChunkSize is not undefined,
if this.m_auto_allocate_chunk_size != none:
# 1. Let buffer be Construct(%ArrayBuffer%, « autoAllocateChunkSize »).
# 2. If buffer is an abrupt completion,
# 1. Perform readRequest’s error steps, given buffer.[[Value]].
# 2. Return.
# 3. Let pullIntoDescriptor be a new pull-into descriptor with buffer buffer.[[Value]], buffer byte length autoAllocateChunkSize, byte offset 0,
# byte length autoAllocateChunkSize, bytes filled 0, element size 1, view constructor %Uint8Array%, and reader type "default".
# 4. Append pullIntoDescriptor to this.[[pendingPullIntos]].
trap("unported (P4): Web::Streams::ReadableByteStreamController::pull_steps with an autoAllocateChunkSize (pull-into descriptors)")
# 6. Perform ! ReadableStreamAddReadRequest(stream, readRequest).
streams_readable_stream_add_read_request(stream, read_request)
# 7. Perform ! ReadableByteStreamControllerCallPullIfNeeded(this).
streams_readable_byte_stream_controller_call_pull_if_needed(this)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:184 Web::Streams::ReadableByteStreamController::release_steps.
## https://streams.spec.whatwg.org/#abstract-opdef-readablebytestreamcontroller-releasesteps
pub func streams_readable_byte_stream_controller_release_steps(this: StreamsReadableByteStreamController*):
# 1. If this.[[pendingPullIntos]] is not empty,
if not ak.singly_linked_list_is_empty(&this.m_pending_pull_intos):
# 1. Let firstPendingPullInto be this.[[pendingPullIntos]][0].
let first_pending_pull_into = *ak.singly_linked_list_first(&this.m_pending_pull_intos)
# 2. Set firstPendingPullInto’s reader type to "none".
first_pending_pull_into.reader_type = StreamsReaderType.none_
# 3. Set this.[[pendingPullIntos]] to the list « firstPendingPullInto ».
ak.singly_linked_list_clear(&this.m_pending_pull_intos)
ak.singly_linked_list_append(&this.m_pending_pull_intos, first_pending_pull_into)
## Ported from Libraries/LibWeb/Streams/ReadableByteStreamController.cpp:200 Web::Streams::ReadableByteStreamController::visit_edges (override).
pub func streams_readable_byte_stream_controller_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (StreamsReadableByteStreamController*)base
bind_platform_object_visit_edges_impl(base, visitor)
if let byob_request = this.m_byob_request:
gc.cell_visitor_visit_cell(visitor, (gc.Cell*)(void*)byob_request) # ReadableStreamBYOBRequest: opaque until phase 4
for pending_pull_into in ak.singly_linked_list_begin(&this.m_pending_pull_intos):
gc.cell_visitor_visit_t_2(visitor, *pending_pull_into)
for item in ak.singly_linked_list_begin(&this.m_queue):
gc.cell_visitor_visit_t_2(visitor, item.buffer)
gc.cell_visitor_visit_t(visitor, this.m_stream)
gc.cell_visitor_visit_t(visitor, this.m_cancel_algorithm)
gc.cell_visitor_visit_t(visitor, this.m_pull_algorithm)