Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / platform / timer.lucb

#==============================================================================================
#
#   timer - Platform::Timer: a GC-managed timer over Core::Timer
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Platform/Timer.cpp (lines 1-111). The C++ destructor destroys
#       the owned Core::Timer, whose EventReceiver destructor unregisters it from the event
#       loop; without destructors that side effect is the cell's finalize (DESIGN.md §2.3).
#
#       Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## heap.allocate<Web::Platform::Timer>(): a cell from Timer's own allocator (GC_DEFINE_ALLOCATOR),
## constructed under DeferGC (LibGC's Heap::allocate, DESIGN.md §2.3).
pub func heap_allocate_platform_timer(heap: gc.Heap*) -> PlatformTimer*:
    if platform_timer_cell_allocator.allocator.m_cell_size == 0:
        gc.type_isolating_cell_allocator_construct(&platform_timer_cell_allocator, ak.sv("Timer"), false, true)
    let cell = gc.heap_allocate_cell[PlatformTimer](heap, &platform_timer_cell_allocator.allocator)
    gc.heap_defer_gc(heap)
    cell.vtable = (const gc.CellVTable*)&platform_timer_vtable
    cell.class_id = platform_timer_class.first_id
    let this = (PlatformTimer*)cell
    platform_timer_construct(this)
    gc.heap_undefer_gc(heap)
    return this

## The lambda Timer::Timer installs as the Core::Timer's on_timeout.
struct PlatformTimerOnTimeout:
    var this: PlatformTimer*

    static func call(context: void*?):
        let captures = (PlatformTimerOnTimeout*)(context else trap("Timer: no captures"))
        if let on_timeout = captures.this.on_timeout:
            ak.function0_call(&on_timeout.function)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:16 Web::Platform::Timer::Timer.
pub func platform_timer_construct(this: PlatformTimer*):
    this.m_timer = core_timer_create()
    let captures = new PlatformTimerOnTimeout(this = this) else trap("out of memory")
    this.m_timer.on_timeout = ak.Function0[unit](call = PlatformTimerOnTimeout.call, context = captures)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:25 Web::Platform::Timer::~Timer: destroying the
## owned Core::Timer unregisters it (Core::EventReceiver::~EventReceiver's stop_timer).
pub func platform_timer_finalize_impl(base: gc.Cell*):
    let this = (PlatformTimer*)base
    core_timer_stop_timer(this.m_timer)
    gc.cell_finalize_impl(base)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:27 Web::Platform::Timer::visit_edges (override).
pub func platform_timer_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (PlatformTimer*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t[gc.Function0[unit]](visitor, this.on_timeout)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:33 Web::Platform::Timer::create.
pub func platform_timer_create(heap: gc.Heap*) -> PlatformTimer*:
    return heap_allocate_platform_timer(heap)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:38 Web::Platform::Timer::create_repeating.
pub func platform_timer_create_repeating(heap: gc.Heap*, interval_ms: i32, timeout_handler: gc.Function0[unit]*?) -> PlatformTimer*:
    let timer = heap_allocate_platform_timer(heap)
    platform_timer_set_single_shot(timer, false)
    platform_timer_set_interval(timer, interval_ms)
    timer.on_timeout = timeout_handler
    return timer

## Ported from Libraries/LibWeb/Platform/Timer.cpp:47 Web::Platform::Timer::create_single_shot.
pub func platform_timer_create_single_shot(heap: gc.Heap*, interval_ms: i32, timeout_handler: gc.Function0[unit]*?) -> PlatformTimer*:
    let timer = heap_allocate_platform_timer(heap)
    platform_timer_set_single_shot(timer, true)
    platform_timer_set_interval(timer, interval_ms)
    timer.on_timeout = timeout_handler
    return timer

## Ported from Libraries/LibWeb/Platform/Timer.cpp:56 Web::Platform::Timer::start.
pub func platform_timer_start(this: PlatformTimer*):
    core_timer_start(this.m_timer)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:61 Web::Platform::Timer::start.
pub func platform_timer_start_i32(this: PlatformTimer*, interval_ms: i32):
    core_timer_start_i32(this.m_timer, interval_ms)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:66 Web::Platform::Timer::restart.
pub func platform_timer_restart(this: PlatformTimer*):
    core_timer_restart(this.m_timer)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:71 Web::Platform::Timer::restart.
pub func platform_timer_restart_i32(this: PlatformTimer*, interval_ms: i32):
    core_timer_restart_i32(this.m_timer, interval_ms)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:76 Web::Platform::Timer::stop.
pub func platform_timer_stop(this: PlatformTimer*):
    core_timer_stop(this.m_timer)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:81 Web::Platform::Timer::set_active.
pub func platform_timer_set_active(this: PlatformTimer*, active: bool):
    core_timer_set_active(this.m_timer, active)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:86 Web::Platform::Timer::is_active.
pub func platform_timer_is_active(this: PlatformTimer*) -> bool:
    return core_timer_is_active(this.m_timer)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:91 Web::Platform::Timer::interval.
pub func platform_timer_interval(this: PlatformTimer*) -> i32:
    return core_timer_interval(this.m_timer)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:96 Web::Platform::Timer::set_interval.
pub func platform_timer_set_interval(this: PlatformTimer*, interval_ms: i32):
    core_timer_set_interval(this.m_timer, interval_ms)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:101 Web::Platform::Timer::is_single_shot.
pub func platform_timer_is_single_shot(this: PlatformTimer*) -> bool:
    return core_timer_is_single_shot(this.m_timer)

## Ported from Libraries/LibWeb/Platform/Timer.cpp:106 Web::Platform::Timer::set_single_shot.
pub func platform_timer_set_single_shot(this: PlatformTimer*, single_shot: bool):
    core_timer_set_single_shot(this.m_timer, single_shot)