Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / streams / readable_stream_default_reader.lucb

#==============================================================================================
#
#   readable_stream_default_reader - Streams::ReadableStreamDefaultReader, its generic reader
#   mixin and ReadLoopReadRequest: reading a chunk and reading all bytes
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp (lines 41-248) and
#       ReadableStreamGenericReader.cpp (lines 17-47), as part of the ReadableStream stand-in of
#       DESIGN.md ยง3.6 (region p2s). The JS-facing read() (whose results are iterator result
#       objects), the constructor from JS and read_all_bytes_deprecated (FileAPI) are phase 4.
#
#       Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
#       Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: ReadableStreamDefaultReader =============================================================

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:41 Web::Streams::ReadableStreamDefaultReader::ReadableStreamDefaultReader.
pub func streams_readable_stream_default_reader_construct(this: StreamsReadableStreamDefaultReader*, realm: Realm*):
    bind_platform_object_construct_realm(this.bind_platform_object(), realm)
    streams_readable_stream_generic_reader_mixin_construct(&this.generic_reader, realm)

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:47 Web::Streams::ReadableStreamDefaultReader::initialize (override).
pub func streams_readable_stream_default_reader_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.readable_stream_default_reader)
    js_cell_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:53 Web::Streams::ReadableStreamDefaultReader::visit_edges (override).
pub func streams_readable_stream_default_reader_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (StreamsReadableStreamDefaultReader*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    streams_readable_stream_generic_reader_mixin_visit_edges(&this.generic_reader, visitor)
    for request in ak.singly_linked_list_begin(&this.m_read_requests):
        gc.cell_visitor_visit_t_2(visitor, *request)
    gc.cell_visitor_visit_t(visitor, this.m_readable_stream_pipe_to_operation)

## `reader.realm()` (Bindings::PlatformObject::realm) of a Streams::ReadableStreamDefaultReader.
pub func streams_readable_stream_default_reader_realm(this: StreamsReadableStreamDefaultReader*) -> Realm*:
    return bind_platform_object_realm(this.bind_platform_object())

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.h:101 Web::Streams::ReadableStreamDefaultReader::read_requests.
pub func streams_readable_stream_default_reader_read_requests(this: StreamsReadableStreamDefaultReader*) -> ak.SinglyLinkedList[StreamsReadRequest*, ak.DefaultSizeCalculationPolicy[StreamsReadRequest*]]*:
    return &this.m_read_requests

# mark: ReadLoopReadRequest =====================================================================

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:63 Web::Streams::ReadLoopReadRequest::ReadLoopReadRequest.
## https://streams.spec.whatwg.org/#read-loop
func streams_read_loop_read_request_construct(this: StreamsReadLoopReadRequest*, realm: Realm*, reader: StreamsReadableStreamDefaultReader*, success_steps: gc.Function1[ak.ByteBuffer, unit]*, failure_steps: gc.Function1[JsValue, unit]*, chunk_steps: gc.Function1[ak.ByteBuffer, unit]*? = none):
    this.m_realm = realm
    this.m_reader = reader
    this.m_success_steps = success_steps
    this.m_failure_steps = failure_steps
    this.m_chunk_steps = chunk_steps

## `heap().allocate<ReadLoopReadRequest>(realm, reader, success_steps, failure_steps)`.
func heap_allocate_streams_read_loop_read_request(heap: gc.Heap*, realm: Realm*, reader: StreamsReadableStreamDefaultReader*, success_steps: gc.Function1[ak.ByteBuffer, unit]*, failure_steps: gc.Function1[JsValue, unit]*) -> StreamsReadLoopReadRequest*:
    let this = (StreamsReadLoopReadRequest*)web_heap_allocate_cell(heap, &streams_read_loop_read_request_class)
    gc.heap_defer_gc(heap)
    streams_read_loop_read_request_construct(this, realm, reader, success_steps, failure_steps)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:72 Web::Streams::ReadLoopReadRequest::visit_edges (override).
pub func streams_read_loop_read_request_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (StreamsReadLoopReadRequest*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_realm)
    gc.cell_visitor_visit_t_2(visitor, this.m_reader)
    gc.cell_visitor_visit_t_2(visitor, this.m_success_steps)
    gc.cell_visitor_visit_t_2(visitor, this.m_failure_steps)
    gc.cell_visitor_visit_t(visitor, this.m_chunk_steps)

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:83 Web::Streams::ReadLoopReadRequest::on_chunk (override):
## chunk steps, given chunk.
pub func streams_read_loop_read_request_on_chunk_impl(base: StreamsReadRequest*, chunk: JsValue):
    let this = (StreamsReadLoopReadRequest*)base
    # 1. If chunk is not a Uint8Array object, call failureSteps with a TypeError and abort these steps.
    if not js_value_is_object(chunk) or not is_js_uint8_array((gc.Cell*)js_value_as_object(chunk)):
        ak.function1_call(&this.m_failure_steps.function, js_type_error_create(this.m_realm, ak.string_from_str("Chunk data is not Uint8Array")))
        return

    let array = (JsTypedArrayBase*)js_value_as_object(chunk)
    let buffer = js_array_buffer_buffer(js_typed_array_base_viewed_array_buffer(array))

    # 2. Append the bytes represented by chunk to bytes.
    # (C++ appends the whole viewed buffer, not the view's range.)
    ak.byte_buffer_append_span_u8(&this.m_bytes, ak.byte_buffer_bytes_const(buffer))

    if let chunk_steps = this.m_chunk_steps:
        # FIXME: Can we move the buffer out of the `chunk`? Unclear if that is safe.
        ak.function1_call(&chunk_steps.function, ak.byte_buffer_copy_span_u8(ak.byte_buffer_bytes_const(buffer)) else trap("MUST: ByteBuffer::copy"))

    # FIXME: As the spec suggests, implement this non-recursively - instead of directly. It is not too big of a deal currently
    #        as we enqueue the entire blob buffer in one go, meaning that we only recurse a single time. Once we begin queuing
    #        up more than one chunk at a time, we may run into stack overflow problems.
    #
    # 3. Read-loop given reader, bytes, successSteps, and failureSteps.
    streams_readable_stream_default_reader_read_2(this.m_reader, this.streams_read_request())

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:111 Web::Streams::ReadLoopReadRequest::on_close (override):
## close steps.
pub func streams_read_loop_read_request_on_close_impl(base: StreamsReadRequest*):
    let this = (StreamsReadLoopReadRequest*)base
    # 1. Call successSteps with bytes.
    var bytes = ak.ByteBuffer()
    ak.byte_buffer_move_from(&bytes, &this.m_bytes)
    ak.function1_call(&this.m_success_steps.function, bytes)

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:118 Web::Streams::ReadLoopReadRequest::on_error (override):
## error steps, given e.
pub func streams_read_loop_read_request_on_error_impl(base: StreamsReadRequest*, error_: JsValue):
    let this = (StreamsReadLoopReadRequest*)base
    # 1. Call failureSteps with e.
    ak.function1_call(&this.m_failure_steps.function, error_)

# mark: Reading =================================================================================

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:194 Web::Streams::ReadableStreamDefaultReader::read_a_chunk.
## https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-a-chunk
pub func streams_readable_stream_default_reader_read_a_chunk(this: StreamsReadableStreamDefaultReader*, read_request: FetchIncrementalReadLoopReadRequest*):
    # To read a chunk from a ReadableStreamDefaultReader reader, given a read request readRequest,
    # perform ! ReadableStreamDefaultReaderRead(reader, readRequest).
    streams_readable_stream_default_reader_read_2(this, read_request.streams_read_request())

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:202 Web::Streams::ReadableStreamDefaultReader::read_all_bytes.
## https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes
pub func streams_readable_stream_default_reader_read_all_bytes(this: StreamsReadableStreamDefaultReader*, success_steps: gc.Function1[ak.ByteBuffer, unit]*, failure_steps: gc.Function1[JsValue, unit]*):
    let realm = streams_readable_stream_default_reader_realm(this)

    # 1. Let readRequest be a new read request with the following items:
    #    NOTE: items and steps in ReadLoopReadRequest.
    let read_request = heap_allocate_streams_read_loop_read_request(gc.cell_heap(this.cell()), realm, this, success_steps, failure_steps)

    # 2. Perform ! ReadableStreamDefaultReaderRead(this, readRequest).
    streams_readable_stream_default_reader_read_2(this, read_request.streams_read_request())

## Ported from Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp:238 Web::Streams::ReadableStreamDefaultReader::release_lock.
## https://streams.spec.whatwg.org/#default-reader-release-lock
pub func streams_readable_stream_default_reader_release_lock(this: StreamsReadableStreamDefaultReader*):
    # 1. If this.[[stream]] is undefined, return.
    if this.generic_reader.m_stream == none:
        return

    # 2. Perform ! ReadableStreamDefaultReaderRelease(this).
    streams_readable_stream_default_reader_release(this)

# mark: ReadableStreamGenericReaderMixin ========================================================

## Ported from Libraries/LibWeb/Streams/ReadableStreamGenericReader.cpp:17 Web::Streams::ReadableStreamGenericReaderMixin::closed.
## https://streams.spec.whatwg.org/#generic-reader-closed
pub func streams_readable_stream_generic_reader_mixin_closed(this: StreamsReadableStreamGenericReaderMixin*) -> JsPromiseCapability*?:
    # 1. Return this.[[closedPromise]].
    return this.m_closed_promise

## Ported from Libraries/LibWeb/Streams/ReadableStreamGenericReader.cpp:24 Web::Streams::ReadableStreamGenericReaderMixin::cancel.
## https://streams.spec.whatwg.org/#generic-reader-cancel
pub func streams_readable_stream_generic_reader_mixin_cancel(this: StreamsReadableStreamGenericReaderMixin*, reason: JsValue) -> JsPromiseCapability*:
    # 1. If this.[[stream]] is undefined, return a promise rejected with a TypeError exception.
    if this.m_stream == none:
        let exception = IdlSimpleException(type_ = IdlSimpleExceptionType.type_error, message = .string_view(ak.sv("No stream present to cancel")))
        return idl_create_rejected_promise_from_exception(this.m_realm, .idl_simple_exception(exception))

    # 2. Return ! ReadableStreamReaderGenericCancel(this, reason).
    return streams_readable_stream_reader_generic_cancel(this, reason)

## Ported from Libraries/LibWeb/Streams/ReadableStreamGenericReader.cpp:36 Web::Streams::ReadableStreamGenericReaderMixin::ReadableStreamGenericReaderMixin.
pub func streams_readable_stream_generic_reader_mixin_construct(this: StreamsReadableStreamGenericReaderMixin*, realm: Realm*):
    this.m_realm = realm

## Ported from Libraries/LibWeb/Streams/ReadableStreamGenericReader.cpp:41 Web::Streams::ReadableStreamGenericReaderMixin::visit_edges.
pub func streams_readable_stream_generic_reader_mixin_visit_edges(this: StreamsReadableStreamGenericReaderMixin*, visitor: gc.CellVisitor*):
    gc.cell_visitor_visit_t(visitor, this.m_closed_promise)
    gc.cell_visitor_visit_t(visitor, this.m_stream)
    gc.cell_visitor_visit_t_2(visitor, this.m_realm)