Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / support.lucb

#==============================================================================================
#
#   support - cell allocation for the generated creation helpers
#
#   DESCRIPTION:
#       The allocation seam every generated realm_create_<class> helper calls (DESIGN.md §2.3, §3.2):
#       allocate a zeroed cell of a class from the realm's heap under DeferGC and set its vtable and
#       class id; then run the virtual initialize(realm). Region r14 ported these with the heap
#       (the WebIDL pending-exception slot of §2.10 is the VM's, external/lib_js/vm.lucb).
#       Part of the skeleton generator's output (Ladybird 47c82b38d0).
#
#==============================================================================================

## A pending WebIDL exception (`WebIDL::ExceptionOr<T>` is `T!`): the exception itself is stored in
## the VM's pending-exception slot before failing (DESIGN.md §2.10).
pub let webidl_exception: ErrorCode = ErrorCode.package(1)

## A pending JS completion (`JS::ThrowCompletionOr<T>` is `T!`), stored in the same slot (§2.10).
pub let js_exception: ErrorCode = ErrorCode.package(2)

## Offsets of the stateful mixins (secondary bases) inside a cell class; a class that does not
## mix one in leaves it none (DESIGN.md §2.5).
pub struct MixinOffsets:
    pub let animatable: usize?
    pub let aria_mixin: usize?
    pub let autocomplete_element: usize?
    pub let client: usize?
    pub let form_associated_element: usize?
    pub let form_associated_text_control_element: usize?
    pub let generated_css_style_properties: usize?
    pub let global_event_handlers: usize?
    pub let global_privacy_control_mixin: usize?
    pub let html_hyperlink_element_utils: usize?
    pub let image_provider: usize?
    pub let input_events_target: usize?
    pub let navigator_beacon_partial: usize?
    pub let navigator_encrypted_media_extensions_partial: usize?
    pub let navigator_gamepad_partial: usize?
    pub let navigator_storage: usize?
    pub let popover_target_attributes: usize?
    pub let serializable: usize?
    pub let slot: usize?
    pub let slottable_mixin: usize?
    pub let style_element_base: usize?
    pub let subresource: usize?
    pub let svg_fit_to_view_box: usize?
    pub let svg_maskable: usize?
    pub let universal_global_scope_mixin: usize?
    pub let viewport_client: usize?
    pub let window_event_handlers: usize?
    pub let window_global_mixin: usize?
    pub let window_or_worker_global_scope_mixin: usize?

## as_if<Web::Animations::Animatable>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_animatable[P: gc.IsCell](p: P*) -> AnimationsAnimatable*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.animatable else return none
    return (AnimationsAnimatable*)((u8*)p + offset)

## as_if<Web::ARIA::ARIAMixin>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_aria_mixin[P: gc.IsCell](p: P*) -> AriaMixin*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.aria_mixin else return none
    return (AriaMixin*)((u8*)p + offset)

## as_if<Web::HTML::AutocompleteElement>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_autocomplete_element[P: gc.IsCell](p: P*) -> HtmlAutocompleteElement*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.autocomplete_element else return none
    return (HtmlAutocompleteElement*)((u8*)p + offset)

## as_if<Web::CSS::ImageStyleValue::Client>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_client[P: gc.IsCell](p: P*) -> CssImageStyleValueClient*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.client else return none
    return (CssImageStyleValueClient*)((u8*)p + offset)

## as_if<Web::HTML::FormAssociatedElement>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_form_associated_element[P: gc.IsCell](p: P*) -> HtmlFormAssociatedElement*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.form_associated_element else return none
    return (HtmlFormAssociatedElement*)((u8*)p + offset)

## as_if<Web::HTML::FormAssociatedTextControlElement>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_form_associated_text_control_element[P: gc.IsCell](p: P*) -> HtmlFormAssociatedTextControlElement*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.form_associated_text_control_element else return none
    return (HtmlFormAssociatedTextControlElement*)((u8*)p + offset)

## as_if<Web::Bindings::GeneratedCSSStyleProperties>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_generated_css_style_properties[P: gc.IsCell](p: P*) -> BindGeneratedCssStyleProperties*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.generated_css_style_properties else return none
    return (BindGeneratedCssStyleProperties*)((u8*)p + offset)

## as_if<Web::HTML::GlobalEventHandlers>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_global_event_handlers[P: gc.IsCell](p: P*) -> HtmlGlobalEventHandlers*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.global_event_handlers else return none
    return (HtmlGlobalEventHandlers*)((u8*)p + offset)

## as_if<Web::GlobalPrivacyControl::GlobalPrivacyControlMixin>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_global_privacy_control_mixin[P: gc.IsCell](p: P*) -> GlobalPrivacyControlGlobalPrivacyControlMixin*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.global_privacy_control_mixin else return none
    return (GlobalPrivacyControlGlobalPrivacyControlMixin*)((u8*)p + offset)

## as_if<Web::HTML::HTMLHyperlinkElementUtils>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_html_hyperlink_element_utils[P: gc.IsCell](p: P*) -> HtmlHyperlinkElementUtils*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.html_hyperlink_element_utils else return none
    return (HtmlHyperlinkElementUtils*)((u8*)p + offset)

## as_if<Web::Layout::ImageProvider>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_image_provider[P: gc.IsCell](p: P*) -> LayoutImageProvider*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.image_provider else return none
    return (LayoutImageProvider*)((u8*)p + offset)

## as_if<Web::InputEventsTarget>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_input_events_target[P: gc.IsCell](p: P*) -> InputEventsTarget*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.input_events_target else return none
    return (InputEventsTarget*)((u8*)p + offset)

## as_if<Web::HTML::NavigatorBeaconPartial>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_navigator_beacon_partial[P: gc.IsCell](p: P*) -> HtmlNavigatorBeaconPartial*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.navigator_beacon_partial else return none
    return (HtmlNavigatorBeaconPartial*)((u8*)p + offset)

## as_if<Web::EncryptedMediaExtensions::NavigatorEncryptedMediaExtensionsPartial>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_navigator_encrypted_media_extensions_partial[P: gc.IsCell](p: P*) -> EncryptedMediaExtensionsNavigatorEncryptedMediaExtensionsPartial*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.navigator_encrypted_media_extensions_partial else return none
    return (EncryptedMediaExtensionsNavigatorEncryptedMediaExtensionsPartial*)((u8*)p + offset)

## as_if<Web::Gamepad::NavigatorGamepadPartial>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_navigator_gamepad_partial[P: gc.IsCell](p: P*) -> GamepadNavigatorGamepadPartial*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.navigator_gamepad_partial else return none
    return (GamepadNavigatorGamepadPartial*)((u8*)p + offset)

## as_if<Web::StorageAPI::NavigatorStorage>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_navigator_storage[P: gc.IsCell](p: P*) -> StorageApiNavigatorStorage*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.navigator_storage else return none
    return (StorageApiNavigatorStorage*)((u8*)p + offset)

## as_if<Web::HTML::PopoverTargetAttributes>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_popover_target_attributes[P: gc.IsCell](p: P*) -> HtmlPopoverTargetAttributes*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.popover_target_attributes else return none
    return (HtmlPopoverTargetAttributes*)((u8*)p + offset)

## as_if<Web::Bindings::Serializable>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_serializable[P: gc.IsCell](p: P*) -> BindSerializable*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.serializable else return none
    return (BindSerializable*)((u8*)p + offset)

## as_if<Web::DOM::Slot>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_slot[P: gc.IsCell](p: P*) -> DomSlot*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.slot else return none
    return (DomSlot*)((u8*)p + offset)

## as_if<Web::DOM::SlottableMixin>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_slottable_mixin[P: gc.IsCell](p: P*) -> DomSlottableMixin*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.slottable_mixin else return none
    return (DomSlottableMixin*)((u8*)p + offset)

## as_if<Web::DOM::StyleElementBase>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_style_element_base[P: gc.IsCell](p: P*) -> DomStyleElementBase*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.style_element_base else return none
    return (DomStyleElementBase*)((u8*)p + offset)

## as_if<Web::CSS::CSSStyleSheet::Subresource>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_subresource[P: gc.IsCell](p: P*) -> CssStyleSheetSubresource*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.subresource else return none
    return (CssStyleSheetSubresource*)((u8*)p + offset)

## as_if<Web::SVG::SVGFitToViewBox>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_svg_fit_to_view_box[P: gc.IsCell](p: P*) -> SvgFitToViewBox*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.svg_fit_to_view_box else return none
    return (SvgFitToViewBox*)((u8*)p + offset)

## as_if<Web::Painting::SVGMaskable>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_svg_maskable[P: gc.IsCell](p: P*) -> PaintSvgMaskable*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.svg_maskable else return none
    return (PaintSvgMaskable*)((u8*)p + offset)

## as_if<Web::HTML::UniversalGlobalScopeMixin>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_universal_global_scope_mixin[P: gc.IsCell](p: P*) -> HtmlUniversalGlobalScopeMixin*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.universal_global_scope_mixin else return none
    return (HtmlUniversalGlobalScopeMixin*)((u8*)p + offset)

## as_if<Web::DOM::ViewportClient>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_viewport_client[P: gc.IsCell](p: P*) -> DomViewportClient*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.viewport_client else return none
    return (DomViewportClient*)((u8*)p + offset)

## as_if<Web::HTML::WindowEventHandlers>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_window_event_handlers[P: gc.IsCell](p: P*) -> HtmlWindowEventHandlers*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.window_event_handlers else return none
    return (HtmlWindowEventHandlers*)((u8*)p + offset)

## as_if<Web::Bindings::WindowGlobalMixin>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_window_global_mixin[P: gc.IsCell](p: P*) -> BindWindowGlobalMixin*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.window_global_mixin else return none
    return (BindWindowGlobalMixin*)((u8*)p + offset)

## as_if<Web::HTML::WindowOrWorkerGlobalScopeMixin>: the mixin inside a cell, found through its class's MixinOffsets.
pub func as_if_window_or_worker_global_scope_mixin[P: gc.IsCell](p: P*) -> HtmlWindowOrWorkerGlobalScopeMixin*?:
    let info = p.cell().vtable.class_info
    let mixins = (const MixinOffsets*)(info.mixins else return none)
    let offset = mixins.window_or_worker_global_scope_mixin else return none
    return (HtmlWindowOrWorkerGlobalScopeMixin*)((u8*)p + offset)

# mark: Cell allocation (region r14) ===========================================================

## The cell allocator of each class (GC_DECLARE_ALLOCATOR's TypeIsolatingCellAllocator), by the
## address of the class's ClassInfo. C++ reaches T::cell_allocator through the type; the
## creation helpers know only the ClassInfo, so the allocator is found here, created on first
## use with the class's size and name and with the finalize / must-survive flags its vtable
## implies (C++'s OVERRIDES_FINALIZE and OVERRIDES_MUST_SURVIVE_GARBAGE_COLLECTION). Like the
## C++ statics, the table and the allocators live on the C heap.
var web_cell_allocators: ak.HashMap[usize, gc.CellAllocator*, ak.UsizeTraits]

## The cell allocator of the class `info` (see web_cell_allocators).
pub func web_cell_allocator_for_class(info: const ak.ClassInfo*) -> gc.CellAllocator*:
    if let allocator = ak.hash_map_get_k(&web_cell_allocators, (usize)info):
        return *allocator
    let vtable = (const gc.CellVTable*)info.vtable
    let overrides_finalize = (void*)vtable.finalize != (void*)gc.cell_finalize_impl
    let overrides_must_survive_garbage_collection = (void*)vtable.must_survive_garbage_collection != (void*)gc.cell_must_survive_garbage_collection_impl
    with memory.heap:
        let allocator = (new gc.CellAllocator) else trap("out of memory")
        gc.cell_allocator_construct(allocator, info.size, ak.sv(info.name), overrides_must_survive_garbage_collection, overrides_finalize)
        ak.hash_map_set(&web_cell_allocators, (usize)info, allocator)
        return allocator

## `heap.allocate_cell<T>()` for the class `info`: a zeroed live cell from the class's allocator
## with its vtable and class id set (the C++ vptr of the most derived class, which the port sets
## before the constructors run, DESIGN.md §2.3). The caller constructs it under DeferGC.
pub func web_heap_allocate_cell(heap: gc.Heap*, info: const ak.ClassInfo*) -> gc.Cell*:
    let cell = gc.heap_allocate_cell_of_size(heap, info.size, web_cell_allocator_for_class(info))
    cell.vtable = (const gc.CellVTable*)info.vtable
    cell.class_id = info.first_id
    return cell

## The first half of `realm.create<T>(args...)` (JS::Realm::create, Realm.h:44, over
## GC::Heap::allocate): a zeroed cell of class `info` from `realm`'s heap, vtable and class id
## set, with garbage collection deferred while the caller runs the constructor. The caller then
## calls web_initialize_cell, which ends the deferral.
pub func web_allocate_cell(realm: Realm*, info: const ak.ClassInfo*) -> gc.Cell*:
    let heap = realm_heap(realm)
    let cell = web_heap_allocate_cell(heap, info)
    gc.heap_defer_gc(heap)
    return cell

## The second half of `realm.create<T>(args...)`: end the deferral web_allocate_cell began (the
## end of GC::Heap::allocate), then run the virtual JS::Cell::initialize(realm).
pub func web_initialize_cell(cell: gc.Cell*, realm: Realm*):
    gc.heap_undefer_gc(gc.cell_heap(cell))
    js_cell_initialize((JsCell*)cell, realm)