Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / fetch / infrastructure / incremental_read_loop_read_request.lucb

#==============================================================================================
#
#   incremental_read_loop_read_request - Fetch::Infrastructure::IncrementalReadLoopReadRequest:
#   the read request of a body's incrementally-read loop
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp
#       (lines 15-85). Its chunk, close and error steps queue fetch tasks; the reader and a
#       chunk's Uint8Array are the stand-ins of DESIGN.md ยง3.6 (region p2s).
#
#       Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Chunk steps =============================================================================

## The captures of on_chunk's continueAlgorithm for a chunk that is not a Uint8Array,
## `[&realm, process_body_error = m_process_body_error]`.
struct FetchIncrementalReadLoopReadRequestOnChunkError:
    var realm: Realm*
    var process_body_error: gc.Function1[JsValue, unit]*

## on_chunk's continueAlgorithm for a chunk that is not a Uint8Array: run processBodyError given a TypeError.
func fetch_incremental_read_loop_read_request_on_chunk_error(context: void*?):
    let captures = (FetchIncrementalReadLoopReadRequestOnChunkError*)(context else trap("on_chunk: no captures"))
    ak.function1_call(&captures.process_body_error.function, js_type_error_create(captures.realm, ak.string_from_str("Chunk data is not Uint8Array")))

## The captures of on_chunk's continueAlgorithm for a Uint8Array chunk, `[bytes = move(bytes), body = m_body,
## reader = m_reader, task_destination = m_task_destination, process_body_chunk = m_process_body_chunk,
## process_end_of_body = m_process_end_of_body, process_body_error = m_process_body_error]`.
struct FetchIncrementalReadLoopReadRequestOnChunkContinue:
    var bytes: ak.ByteBuffer
    var body: FetchBody*
    var reader: StreamsReadableStreamDefaultReader*
    var task_destination: FetchTaskDestination
    var process_body_chunk: gc.Function1[ak.ByteBuffer, unit]*
    var process_end_of_body: gc.Function0[unit]*
    var process_body_error: gc.Function1[JsValue, unit]*

## on_chunk's continueAlgorithm for a Uint8Array chunk: process the bytes, then read the next chunk.
func fetch_incremental_read_loop_read_request_on_chunk_continue(context: void*?):
    let captures = (FetchIncrementalReadLoopReadRequestOnChunkContinue*)(context else trap("on_chunk: no captures"))
    var execution_context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&execution_context, streams_readable_stream_default_reader_realm(captures.reader), HtmlTemporaryExecutionContextCallbacksEnabled.yes)
    defer html_temporary_execution_context_destruct(&execution_context)

    # 1. Run processBodyChunk given bytes.
    ak.function1_call(&captures.process_body_chunk.function, captures.bytes)

    # 2. Perform the incrementally-read loop given reader, taskDestination, processBodyChunk, processEndOfBody, and processBodyError.
    fetch_body_incrementally_read_loop(captures.body, captures.reader, captures.task_destination, captures.process_body_chunk, captures.process_end_of_body, captures.process_body_error)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp:15 Web::Fetch::Infrastructure::IncrementalReadLoopReadRequest::on_chunk (override).
pub func fetch_incremental_read_loop_read_request_on_chunk_impl(base: StreamsReadRequest*, chunk: JsValue):
    let this = (FetchIncrementalReadLoopReadRequest*)base
    let realm = streams_readable_stream_default_reader_realm(this.m_reader)
    # 1. Let continueAlgorithm be null.
    var continue_algorithm: gc.Function0[unit]*? = none

    # 2. If chunk is not a Uint8Array object, then set continueAlgorithm to this step: run processBodyError given a TypeError.
    let uint8_array = js_value_as_if_uint8_array_data(chunk)

    if uint8_array == none:
        let captures = new FetchIncrementalReadLoopReadRequestOnChunkError(realm = realm, process_body_error = this.m_process_body_error) else trap("out of memory")
        continue_algorithm = gc.create_function0(realm_heap(realm), ak.Function0[unit](call = fetch_incremental_read_loop_read_request_on_chunk_error, context = captures))
    # 3. Otherwise:
    else:
        # 1. Let bytes be a copy of chunk.
        # NOTE: Implementations are strongly encouraged to use an implementation strategy that avoids this copy where possible.
        let data = uint8_array else trap("unreachable: checked above")
        let bytes = ak.byte_buffer_copy_span_u8(data) else trap("MUST: ByteBuffer::copy")
        # 2. Set continueAlgorithm to these steps:
        let captures = new FetchIncrementalReadLoopReadRequestOnChunkContinue(bytes = bytes, body = this.m_body, reader = this.m_reader, task_destination = this.m_task_destination, process_body_chunk = this.m_process_body_chunk, process_end_of_body = this.m_process_end_of_body, process_body_error = this.m_process_body_error) else trap("out of memory")
        continue_algorithm = gc.create_function0(realm_heap(realm), ak.Function0[unit](call = fetch_incremental_read_loop_read_request_on_chunk_continue, context = captures))

    # 4. Queue a fetch task given continueAlgorithm and taskDestination.
    _ = fetch_queue_fetch_task_fetch_task_destination(this.m_task_destination, continue_algorithm else trap("VERIFY: continueAlgorithm is set"))

# mark: Close and error steps ===================================================================

## on_close's task, `[this] { m_process_end_of_body->function()(); }`.
func fetch_incremental_read_loop_read_request_on_close_task(context: void*?):
    let this = (FetchIncrementalReadLoopReadRequest*)(context else trap("on_close: no captures"))
    ak.function0_call(&this.m_process_end_of_body.function)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp:49 Web::Fetch::Infrastructure::IncrementalReadLoopReadRequest::on_close (override).
pub func fetch_incremental_read_loop_read_request_on_close_impl(base: StreamsReadRequest*):
    let this = (FetchIncrementalReadLoopReadRequest*)base
    # 1. Queue a fetch task given processEndOfBody and taskDestination.
    _ = fetch_queue_fetch_task_fetch_task_destination(this.m_task_destination, gc.create_function0(gc.cell_heap(this.m_reader.cell()), ak.Function0[unit](call = fetch_incremental_read_loop_read_request_on_close_task, context = this)))

## The captures of on_error's task, `[this, error = move(error)]`.
struct FetchIncrementalReadLoopReadRequestOnError:
    var this: FetchIncrementalReadLoopReadRequest*
    var error_: JsValue

## on_error's task: run processBodyError given e.
func fetch_incremental_read_loop_read_request_on_error_task(context: void*?):
    let captures = (FetchIncrementalReadLoopReadRequestOnError*)(context else trap("on_error: no captures"))
    ak.function1_call(&captures.this.m_process_body_error.function, captures.error_)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp:57 Web::Fetch::Infrastructure::IncrementalReadLoopReadRequest::on_error (override).
pub func fetch_incremental_read_loop_read_request_on_error_impl(base: StreamsReadRequest*, error_: JsValue):
    let this = (FetchIncrementalReadLoopReadRequest*)base
    # 1. Queue a fetch task to run processBodyError given e, with taskDestination.
    let captures = new FetchIncrementalReadLoopReadRequestOnError(this = this, error_ = error_) else trap("out of memory")
    _ = fetch_queue_fetch_task_fetch_task_destination(this.m_task_destination, gc.create_function0(gc.cell_heap(this.m_reader.cell()), ak.Function0[unit](call = fetch_incremental_read_loop_read_request_on_error_task, context = captures)))

# mark: Construction ============================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp:65 Web::Fetch::Infrastructure::IncrementalReadLoopReadRequest::IncrementalReadLoopReadRequest.
pub func fetch_incremental_read_loop_read_request_construct(this: FetchIncrementalReadLoopReadRequest*, body: FetchBody*, reader: StreamsReadableStreamDefaultReader*, task_destination: FetchTaskDestination, process_body_chunk: gc.Function1[ak.ByteBuffer, unit]*, process_end_of_body: gc.Function0[unit]*, process_body_error: gc.Function1[JsValue, unit]*):
    this.m_body = body
    this.m_reader = reader
    this.m_task_destination = task_destination
    this.m_process_body_chunk = process_body_chunk
    this.m_process_end_of_body = process_end_of_body
    this.m_process_body_error = process_body_error

## Ported from Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp:75 Web::Fetch::Infrastructure::IncrementalReadLoopReadRequest::visit_edges (override).
pub func fetch_incremental_read_loop_read_request_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (FetchIncrementalReadLoopReadRequest*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_body)
    gc.cell_visitor_visit_t_2(visitor, this.m_reader)
    match this.m_task_destination:
        .js_object(task_destination_object): js_visit_object(visitor, task_destination_object)
        _: _ = 0
    gc.cell_visitor_visit_t(visitor, this.m_process_body_chunk)
    gc.cell_visitor_visit_t(visitor, this.m_process_end_of_body)
    gc.cell_visitor_visit_t(visitor, this.m_process_body_error)