dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / event_loop / task.lucb
#==============================================================================================
#
# task - HTML::Task, UniqueTaskSource and ParallelQueue: the units of work of an event loop
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/EventLoop/Task.cpp (lines 1-90) with the inline functions
# of Task.h (lines 1-139). A task is a cell holding its steps (a GC::Function), its source
# and its document; a task is runnable when its document is null or fully active. The
# unique task sources come from a process-wide IDAllocator above the generic sources.
#
# Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Task ====================================================================================
## Whether html_s_unique_task_source_allocator has been constructed (a C++ static with a
## non-constant initializer, DESIGN.md §2.6).
var html_s_unique_task_source_allocator_initialized: bool
## The static s_unique_task_source_allocator, constructed on first use: an IDAllocator whose
## ids start at Task::Source::UniqueTaskSourceStart. It is a module global, which the collector
## does not scan, so its table lives on the C heap (DESIGN.md §3.3 rule 4).
func html_unique_task_source_allocator() -> ak.IdAllocator*:
if not html_s_unique_task_source_allocator_initialized:
ak.id_allocator_init_fields(&html_s_unique_task_source_allocator)
ak.id_allocator_construct(&html_s_unique_task_source_allocator, (i32)HtmlTaskSource.unique_task_source_start)
html_s_unique_task_source_allocator_initialized = true
return &html_s_unique_task_source_allocator
## The static next_task_id of allocate_task_id.
var html_next_task_id: u64 = 1
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:17 Web::HTML::allocate_task_id.
func html_allocate_task_id() -> u64:
let id = html_next_task_id
html_next_task_id += 1
return id
## heap.allocate<HTML::Task>(source, document, steps): a Task cell constructed under DeferGC.
pub func heap_allocate_html_task(heap: gc.Heap*, source: HtmlTaskSource, document: DomDocument*?, steps: gc.Function0[unit]*) -> HtmlTask*:
let this = (HtmlTask*)web_heap_allocate_cell(heap, &html_task_class)
gc.heap_defer_gc(heap)
html_task_construct(this, source, document, steps)
gc.heap_undefer_gc(heap)
return this
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:23 Web::HTML::Task::create.
pub func html_task_create(vm: Vm*, source: HtmlTaskSource, document: DomDocument*?, steps: gc.Function0[unit]*) -> HtmlTask*:
return heap_allocate_html_task(vm_heap(vm), source, document, steps)
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:28 Web::HTML::Task::Task.
pub func html_task_construct(this: HtmlTask*, source: HtmlTaskSource, document: DomDocument*?, steps: gc.Function0[unit]*):
this.m_id = html_allocate_task_id()
this.m_source = source
this.m_steps = steps
this.m_document = document
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:38 Web::HTML::Task::visit_edges (override).
pub func html_task_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlTask*)base
gc.cell_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t_2(visitor, this.m_steps)
gc.cell_visitor_visit_t(visitor, this.m_document)
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:45 Web::HTML::Task::execute.
pub func html_task_execute(this: HtmlTask*):
ak.function0_call(&this.m_steps.function)
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:51 Web::HTML::Task::is_runnable.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-task-runnable
pub func html_task_is_runnable(this: HtmlTask*) -> bool:
# A task is runnable if its document is either null or fully active.
let document = this.m_document else return true
return dom_document_is_fully_active(document)
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:57 Web::HTML::Task::is_permanently_unrunnable.
pub func html_task_is_permanently_unrunnable(this: HtmlTask*) -> bool:
let document = this.m_document else return false
return dom_document_has_been_destroyed(document)
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:62 Web::HTML::Task::document.
pub func html_task_document(this: HtmlTask*) -> DomDocument*?:
return this.m_document
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.h:103 Web::HTML::Task::id.
pub func html_task_id(this: HtmlTask*) -> u64:
return this.m_id
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.h:104 Web::HTML::Task::source.
pub func html_task_source(this: HtmlTask*) -> HtmlTaskSource:
return this.m_source
# mark: UniqueTaskSource ========================================================================
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:67 Web::HTML::UniqueTaskSource::UniqueTaskSource.
pub func html_unique_task_source_construct(this: HtmlUniqueTaskSource*):
with memory.heap:
this.source = (HtmlTaskSource)ak.id_allocator_allocate(html_unique_task_source_allocator())
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:72 Web::HTML::UniqueTaskSource::~UniqueTaskSource:
## the source goes back to the allocator (C++ runs it when the owner is destroyed; an owner
## with a unique task source calls it from its own destruction or finalize).
pub func html_unique_task_source_destruct(this: HtmlUniqueTaskSource*):
with memory.heap:
ak.id_allocator_deallocate(html_unique_task_source_allocator(), (i32)this.source)
# mark: ParallelQueue ===========================================================================
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:77 Web::HTML::ParallelQueue::create.
## (RefCounted in C++: managed memory, DESIGN.md §3.3.)
pub func html_parallel_queue_create() -> HtmlParallelQueue*:
let queue = (new HtmlParallelQueue(m_task_source = HtmlUniqueTaskSource(source = HtmlTaskSource.unspecified))) else trap("out of memory")
html_unique_task_source_construct(&queue.m_task_source)
return queue
## Ported from Libraries/LibWeb/HTML/EventLoop/Task.cpp:82 Web::HTML::ParallelQueue::enqueue.
pub func html_parallel_queue_enqueue(this: HtmlParallelQueue*, algorithm: gc.Function0[unit]*) -> u64:
let event_loop = html_main_thread_event_loop()
let task = html_task_create(js_cell_vm(event_loop.js_cell()), this.m_task_source.source, none, algorithm)
html_task_queue_add(html_event_loop_task_queue(event_loop), task)
return html_task_id(task)