dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / scripting / similar_origin_window_agent.lucb
#==============================================================================================
#
# similar_origin_window_agent - HTML::SimilarOriginWindowAgent: the agent of window realms
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/Scripting/SimilarOriginWindowAgent.cpp (lines 1-40) and
# SimilarOriginWindowAgent.h (lines 1-57). The agent is owned by the VM and lives on the
# C heap, as the VM does (Bindings::create_agent allocates it there). Its GC::Root and
# GC::RootVector members keep their cells alive in C++; here one GC::ConservativeVector
# registered with the heap (the `roots` addition) reports the agent's own words (which
# hold the event loop and the managed storage of its vectors and map) and every element
# of the root vectors and every registry of the constructor map, so they are roots
# wherever their storage lives (DESIGN.md ยง3.3 rule 4).
#
# Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
# Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## The vtable of the agent's roots (GC::ConservativeVector's possible_values).
let html_similar_origin_window_agent_roots_vtable: gc.ConservativeVectorBaseVTable = gc.ConservativeVectorBaseVTable(class_info = &gc.conservative_vector_base_class, possible_values = html_similar_origin_window_agent_roots_possible_values_impl)
## Ported from Libraries/LibWeb/HTML/Scripting/SimilarOriginWindowAgent.cpp:17 Web::HTML::SimilarOriginWindowAgent::create.
## The agent is allocated on the C heap (NonnullOwnPtr owned by the VM).
pub func html_similar_origin_window_agent_create(heap: gc.Heap*) -> HtmlSimilarOriginWindowAgent*:
# See 'creating an agent' step in: https://html.spec.whatwg.org/multipage/webappapis.html#obtain-similar-origin-window-agent
let storage = (new HtmlSimilarOriginWindowAgent[1] --- in memory.heap) else trap("out of memory")
memory.set(u8[]((u8*)storage.data, memory.size_of(HtmlSimilarOriginWindowAgent)), 0)
let agent = (HtmlSimilarOriginWindowAgent*)storage.data
html_similar_origin_window_agent_construct(agent, heap, JsAgentCanBlock.no)
# The event loop and what its constructor allocates (captures, the task queue) are managed
# memory, whatever allocator the caller has current.
let allocator: memory.Allocator = heap
with allocator:
agent.agent.event_loop = heap_allocate_html_event_loop(heap, HtmlEventLoopType.window)
return agent
## Ported from Libraries/LibWeb/HTML/Scripting/SimilarOriginWindowAgent.cpp:26 Web::HTML::relevant_similar_origin_window_agent.
## https://html.spec.whatwg.org/multipage/webappapis.html#relevant-agent
pub func html_relevant_similar_origin_window_agent(object: JsObject*) -> HtmlSimilarOriginWindowAgent*:
# The relevant agent for a platform object platformObject is platformObject's relevant Realm's agent.
# Spec Note: This pointer is not yet defined in the JavaScript specification; see tc39/ecma262#1357.
let agent = vm_agent(realm_vm(html_relevant_realm(object))) else trap("VERIFY: the VM has an agent")
return as_html_similar_origin_window_agent(agent)
## Ported from Libraries/LibWeb/HTML/Scripting/SimilarOriginWindowAgent.cpp:33 Web::HTML::SimilarOriginWindowAgent::SimilarOriginWindowAgent.
## Agent(can_block): JS::Agent's CanBlock is not kept (nothing in LibWeb reads it before
## workers). pending_mutation_observers(heap) and signal_slots(heap) register the root vectors
## with the heap: here the `roots` conservative vector.
pub func html_similar_origin_window_agent_construct(this: HtmlSimilarOriginWindowAgent*, heap: gc.Heap*, can_block: JsAgentCanBlock):
this.agent.vtable = (const HtmlAgentVTable*)&html_similar_origin_window_agent_vtable
this.agent.class_id = html_similar_origin_window_agent_class.first_id
this.roots.vtable = &html_similar_origin_window_agent_roots_vtable
gc.conservative_vector_base_construct(&this.roots, heap)
## Addition: SimilarOriginWindowAgent's destructor (the VM never destroys the main thread agent;
## a test VM does): the root vectors unregister from the heap and the storage goes back.
pub func html_similar_origin_window_agent_destroy(this: HtmlSimilarOriginWindowAgent*):
gc.conservative_vector_base_destroy(&this.roots)
with memory.heap:
ak.vector_clear(&this.root_values)
free(u8[]((u8*)this, memory.size_of(HtmlSimilarOriginWindowAgent))) in memory.heap
## The words the heap scans for the agent's roots: the agent itself, the elements of its root
## vectors and the registries of its constructor map.
func html_similar_origin_window_agent_roots_possible_values_impl(base: gc.ConservativeVectorBase*) -> const u64[]:
let this = (HtmlSimilarOriginWindowAgent*)((u8*)base - memory.offset_of(HtmlSimilarOriginWindowAgent, roots))
with memory.heap:
ak.vector_clear_with_capacity(&this.root_values)
let words = (const u64*)this
for i in 0..<(memory.size_of(HtmlSimilarOriginWindowAgent) // memory.size_of(u64)):
ak.vector_append_t(&this.root_values, words[i])
for observer in ak.vector_span(&this.pending_mutation_observers):
ak.vector_append_t(&this.root_values, (u64)(usize)observer)
for slot in ak.vector_span(&this.signal_slots):
ak.vector_append_t(&this.root_values, (u64)(usize)slot)
for entry in ak.hash_map_begin(&this.active_custom_element_constructor_map):
if let registry = entry.value:
ak.vector_append_t(&this.root_values, (u64)(usize)registry)
return ak.vector_span_const(&this.root_values)
## Ported from Libraries/LibWeb/HTML/Scripting/SimilarOriginWindowAgent.h:43 Web::HTML::SimilarOriginWindowAgent::current_element_queue.
## https://html.spec.whatwg.org/multipage/custom-elements.html#current-element-queue
pub func html_similar_origin_window_agent_current_element_queue(this: HtmlSimilarOriginWindowAgent*) -> ak.Vector[gc.Weak[DomElement]]*:
return ak.vector_last(&this.custom_element_reactions_stack.element_queue_stack)
## Ported from Libraries/LibWeb/HTML/Scripting/SimilarOriginWindowAgent.h:44 Web::HTML::SimilarOriginWindowAgent::current_element_queue (const).
pub func html_similar_origin_window_agent_current_element_queue_const(this: HtmlSimilarOriginWindowAgent*) -> ak.Vector[gc.Weak[DomElement]]:
return *ak.vector_last(&this.custom_element_reactions_stack.element_queue_stack)