Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / platform / event_loop_plugin.lucb

#==============================================================================================
#
#   event_loop_plugin - Platform::EventLoopPlugin: LibWeb's access to the process's event loop
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Platform/EventLoopPlugin.cpp (lines 1-50) over the Core event
#       loop seam (external/lib_core/event_loop.lucb), which the embedder implements.
#
#       Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Platform/EventLoopPlugin.cpp:15 Web::Platform::EventLoopPlugin::the.
pub func platform_event_loop_plugin_the() -> PlatformEventLoopPlugin*:
    return platform_s_the_2 else trap("VERIFY: no EventLoopPlugin installed")

## Ported from Libraries/LibWeb/Platform/EventLoopPlugin.cpp:21 Web::Platform::EventLoopPlugin::install.
pub func platform_event_loop_plugin_install(plugin: PlatformEventLoopPlugin*):
    assert(platform_s_the_2 == none)
    platform_s_the_2 = plugin

## The lambda EventLoopPlugin::spin_until passes to Core::EventLoop::spin_until.
struct PlatformEventLoopPluginSpinUntilGoal:
    var goal_condition: gc.Function0[bool]*

    static func call(context: void*?) -> bool:
        let captures = (PlatformEventLoopPluginSpinUntilGoal*)(context else trap("spin_until: no captures"))
        var event_loop = core_event_loop_current()
        if event_loop.was_exit_requested():
            os.exit(0)
        return ak.function0_call(&captures.goal_condition.function)

## Ported from Libraries/LibWeb/Platform/EventLoopPlugin.cpp:29 Web::Platform::EventLoopPlugin::spin_until.
pub func platform_event_loop_plugin_spin_until(this: PlatformEventLoopPlugin*, goal_condition: gc.Function0[bool]*?):
    var captures = PlatformEventLoopPluginSpinUntilGoal(goal_condition = goal_condition else trap("VERIFY: spin_until without a goal condition"))
    core_event_loop_spin_until(ak.Function0[bool](call = PlatformEventLoopPluginSpinUntilGoal.call, context = &captures))

## The lambda EventLoopPlugin::deferred_invoke passes to Core::deferred_invoke. It holds the
## GC::Root of the function: the embedder may keep it outside managed memory until it runs.
struct PlatformEventLoopPluginDeferredInvoke:
    var function: gc.Root[gc.Function0[unit]]

    static func call(context: void*?):
        let captures = (PlatformEventLoopPluginDeferredInvoke*)(context else trap("deferred_invoke: no captures"))
        let function = gc.root_cell(&captures.function) else trap("deferred_invoke: no function")
        gc.root_release(&captures.function)
        ak.function0_call(&function.function)

## Ported from Libraries/LibWeb/Platform/EventLoopPlugin.cpp:38 Web::Platform::EventLoopPlugin::deferred_invoke.
pub func platform_event_loop_plugin_deferred_invoke(this: PlatformEventLoopPlugin*, function: gc.Function0[unit]*?):
    let captures = new PlatformEventLoopPluginDeferredInvoke(function = gc.root_create(function)) else trap("out of memory")
    core_deferred_invoke(ak.Function0[unit](call = PlatformEventLoopPluginDeferredInvoke.call, context = captures))

## Ported from Libraries/LibWeb/Platform/EventLoopPlugin.cpp:45 Web::Platform::EventLoopPlugin::quit.
pub func platform_event_loop_plugin_quit(this: PlatformEventLoopPlugin*):
    var event_loop = core_event_loop_current()
    event_loop.quit(0)