Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / external / lib_core / event_loop.lucb

#==============================================================================================
#
#   event_loop - Core::EventLoop as LibWeb's platform code uses it, over the embedder's loop
#
#   DESCRIPTION:
#       LibCore is not ported (DESIGN.md ยง1.1): LibWeb's Platform plugins reach the process's
#       event loop through this seam instead. CoreEventLoopImplementation is LibCore's
#       EventLoopImplementation (pump, quit, was_exit_requested, deferred_invoke) together with
#       the timer half of its EventLoopManager (register_timer, unregister_timer); the embedder
#       (the web_test runner, later the browser's window loop) implements it and installs it
#       once, with the notifier half of the manager (register_notifier, unregister_notifier:
#       the loop watches the descriptors of the enabled Core::Notifiers with its timers in one
#       wait, as EventLoopManagerUnix::wait_for_events polls them; event_loop_notifiers.lucb
#       is that part, for embedders). The functions below are the parts of
#       Libraries/LibCore/EventLoop.cpp that LibWeb calls (lines 102-107 spin_until, 134-142
#       register_notifier and unregister_notifier, 159-167 deferred_invoke), ported over the
#       seam; there is one event loop per process, so EventLoopPusher's stack of loops is not
#       needed.
#
#       Copyright (c) 2018-2026, the Ladybird developers.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Core::EventLoopImplementation::PumpMode (LibCore/EventLoopImplementation.h).
pub enum CoreEventLoopPumpMode as i32:
    wait_for_events = 0
    dont_wait_for_events = 1

## The embedder's event loop: Core::EventLoopImplementation and the timer registry of
## Core::EventLoopManager. A registered timer fires by calling core_timer_timer_event.
pub interface CoreEventLoopImplementation:
    ## Processes the pending events, waiting for one first in `wait_for_events` mode; answers how
    ## many were processed.
    func pump(mode: CoreEventLoopPumpMode) -> usize

    ## Asks the loop to exit with `code`.
    func quit(code: i32)

    ## Whether quit was called.
    func was_exit_requested() -> bool

    ## Wakes the loop if it is waiting for events (added in region r29 for spin_until).
    func wake()

    ## Runs `function` on the loop once the current event has been handled.
    func deferred_invoke(function: ak.Function0[unit])

    ## Fires `timer` every `milliseconds` (once unless `should_reload`); answers a non-zero id.
    func register_timer(timer: CoreTimer*, milliseconds: i32, should_reload: bool) -> isize

    ## Stops the timer `timer_id` from firing.
    func unregister_timer(timer_id: isize)

    ## Watches the descriptor of `notifier` from now on, activating it (core_notifier_event)
    ## when the descriptor is ready for the notifier's type (EventLoopManager::register_notifier).
    func register_notifier(notifier: CoreNotifier*)

    ## Stops watching for `notifier` (EventLoopManager::unregister_notifier).
    func unregister_notifier(notifier: CoreNotifier*)

## The installed event loop (EventLoopManager::the() and the thread's current EventLoop).
var core_event_loop_installed: CoreEventLoopImplementation?

## Installs the embedder's event loop (Core::EventLoopManager::install and the main EventLoop).
pub func core_event_loop_install(implementation: CoreEventLoopImplementation):
    assert(core_event_loop_installed == none)
    core_event_loop_installed = implementation

## Removes the installed event loop, so another may be installed (an embedder's tests, each with its own loop).
pub func core_event_loop_uninstall():
    core_event_loop_installed = none

## Core::EventLoop::current(): the installed loop.
pub func core_event_loop_current() -> CoreEventLoopImplementation:
    return core_event_loop_installed else trap("VERIFY: no Core::EventLoop installed")

## Ported from Libraries/LibCore/EventLoop.cpp:102 Core::EventLoop::spin_until.
pub func core_event_loop_spin_until(goal_condition: ak.Function0[bool]):
    var event_loop = core_event_loop_current()
    while not ak.function0_call(&goal_condition):
        _ = event_loop.pump(CoreEventLoopPumpMode.wait_for_events)

## Core::EventLoop::wake (LibCore/EventLoop.h): wakes the installed loop (added in region r29).
pub func core_event_loop_wake():
    var event_loop = core_event_loop_current()
    event_loop.wake()

## Ported from Libraries/LibCore/EventLoop.cpp:164 Core::deferred_invoke.
pub func core_deferred_invoke(invokee: ak.Function0[unit]):
    var event_loop = core_event_loop_current()
    event_loop.deferred_invoke(invokee)

## Core::EventLoop::register_timer (LibCore/EventLoop.h:82).
pub func core_event_loop_register_timer(timer: CoreTimer*, milliseconds: i32, should_reload: bool) -> isize:
    var event_loop = core_event_loop_current()
    return event_loop.register_timer(timer, milliseconds, should_reload)

## Core::EventLoop::unregister_timer (LibCore/EventLoop.h:83).
pub func core_event_loop_unregister_timer(timer_id: isize):
    var event_loop = core_event_loop_current()
    event_loop.unregister_timer(timer_id)

## Ported from Libraries/LibCore/EventLoop.cpp:134 Core::EventLoop::register_notifier.
pub func core_event_loop_register_notifier(notifier: CoreNotifier*):
    var event_loop = core_event_loop_current()
    event_loop.register_notifier(notifier)

## Ported from Libraries/LibCore/EventLoop.cpp:139 Core::EventLoop::unregister_notifier.
pub func core_event_loop_unregister_notifier(notifier: CoreNotifier*):
    var event_loop = core_event_loop_current()
    event_loop.unregister_notifier(notifier)