Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / fetch / fetching / pending_response.lucb

#==============================================================================================
#
#   pending_response - Fetch::Fetching::PendingResponse: a possibly pending response, and
#   RefCountedFlag
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp (lines 1-70) with the
#       inline members of PendingResponse.h, and of RefCountedFlag.cpp (lines 1-22) with
#       RefCountedFlag.h. PendingResponse is Ladybird's non-standard wrapper that fits the
#       asynchronous ResourceLoader into the Fetch spec's synchronous steps: "in parallel"
#       is a deferred invocation on the main thread, and a callback runs the steps that wait
#       for an HTTP load.
#
#       Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: PendingResponse =========================================================================

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp:17 Web::Fetch::Fetching::PendingResponse::create.
pub func fetch_pending_response_create(vm: Vm*, request: FetchRequest*) -> FetchPendingResponse*:
    # vm.heap().allocate<PendingResponse>(request)
    let heap = vm_heap(vm)
    let this = (FetchPendingResponse*)web_heap_allocate_cell(heap, &fetch_pending_response_class)
    gc.heap_defer_gc(heap)
    fetch_pending_response_construct(this, request)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp:22 Web::Fetch::Fetching::PendingResponse::create (with a
## response).
pub func fetch_pending_response_create_fetch_response(vm: Vm*, request: FetchRequest*, response: FetchResponse*) -> FetchPendingResponse*:
    # vm.heap().allocate<PendingResponse>(request, response)
    let heap = vm_heap(vm)
    let this = (FetchPendingResponse*)web_heap_allocate_cell(heap, &fetch_pending_response_class)
    gc.heap_defer_gc(heap)
    fetch_pending_response_construct(this, request, response)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp:27 Web::Fetch::Fetching::PendingResponse::PendingResponse (private).
func fetch_pending_response_construct(this: FetchPendingResponse*, request: FetchRequest*, response: FetchResponse*? = none):
    this.m_request = request
    this.m_response = response
    fetch_request_add_pending_response(this.m_request, this)

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.h:32 Web::Fetch::Fetching::PendingResponse::is_resolved.
pub func fetch_pending_response_is_resolved(this: FetchPendingResponse*) -> bool:
    return this.m_response != none

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp:34 Web::Fetch::Fetching::PendingResponse::visit_edges (override).
pub func fetch_pending_response_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (FetchPendingResponse*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_callback)
    gc.cell_visitor_visit_t_2(visitor, this.m_request)
    gc.cell_visitor_visit_t(visitor, this.m_response)

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp:42 Web::Fetch::Fetching::PendingResponse::when_loaded.
pub func fetch_pending_response_when_loaded(this: FetchPendingResponse*, callback: ak.Function1[FetchResponse*, unit]):
    assert(this.m_callback == none)
    this.m_callback = gc.create_function1(gc.cell_heap(this.cell()), callback)
    if this.m_response != none:
        fetch_pending_response_run_callback(this)

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp:50 Web::Fetch::Fetching::PendingResponse::resolve.
pub func fetch_pending_response_resolve(this: FetchPendingResponse*, response: FetchResponse*):
    assert(this.m_response == none)
    this.m_response = response
    if this.m_callback != none:
        fetch_pending_response_run_callback(this)

## The deferred invocation of run_callback, `[this] { ... }`: the callback given the response, then the request
## forgets this pending response.
func fetch_pending_response_run_callback_task(context: void*?):
    let this = (FetchPendingResponse*)(context else trap("run_callback: no pending response"))
    let callback = this.m_callback else trap("VERIFY: m_callback")
    let response = this.m_response else trap("VERIFY: m_response")
    ak.function1_call(&callback.function, response)
    fetch_request_remove_pending_response(this.m_request, this)

## Ported from Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp:58 Web::Fetch::Fetching::PendingResponse::run_callback (private).
func fetch_pending_response_run_callback(this: FetchPendingResponse*):
    assert(this.m_callback != none)
    assert(this.m_response != none)
    platform_event_loop_plugin_deferred_invoke(platform_event_loop_plugin_the(), gc.create_function0(gc.cell_heap(this.cell()), ak.Function0[unit](call = fetch_pending_response_run_callback_task, context = this)))

# mark: RefCountedFlag ==========================================================================

## Ported from Libraries/LibWeb/Fetch/Fetching/RefCountedFlag.cpp:12 Web::Fetch::Fetching::RefCountedFlag::create.
pub func fetch_ref_counted_flag_create(value: bool) -> FetchRefCountedFlag*:
    # adopt_ref(*new RefCountedFlag(value)): managed (DESIGN.md ยง3.3).
    let this = new FetchRefCountedFlag() else trap("out of memory")
    fetch_ref_counted_flag_construct(this, value)
    return this

## Ported from Libraries/LibWeb/Fetch/Fetching/RefCountedFlag.cpp:17 Web::Fetch::Fetching::RefCountedFlag::RefCountedFlag (private).
func fetch_ref_counted_flag_construct(this: FetchRefCountedFlag*, value: bool):
    this.m_value = value

## Ported from Libraries/LibWeb/Fetch/Fetching/RefCountedFlag.h:19 Web::Fetch::Fetching::RefCountedFlag::value.
pub func fetch_ref_counted_flag_value(this: const FetchRefCountedFlag*) -> bool:
    return this.m_value

## Ported from Libraries/LibWeb/Fetch/Fetching/RefCountedFlag.h:20 Web::Fetch::Fetching::RefCountedFlag::set_value.
pub func fetch_ref_counted_flag_set_value(this: FetchRefCountedFlag*, value: bool):
    this.m_value = value