Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / external / lib_core / notifier.lucb

#==============================================================================================
#
#   notifier - Core::Notifier: a callback when a file descriptor is ready
#
#   DESCRIPTION:
#       Port of Libraries/LibCore/Notifier.cpp (lines 1-67) with the inline members of
#       Notifier.h, over the embedder's event loop (event_loop.lucb): an enabled notifier is
#       registered with the loop, which watches its descriptor with the timers in one wait and
#       activates it (core_notifier_event) when the descriptor is ready for what its type
#       names. The engine's in-process RequestClient watches its sockets with notifiers, as
#       RequestServer's curl multi handle does through Core::Notifier.
#
#       Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibCore/Notifier.cpp:14 Core::Notifier::Notifier (Notifier::construct): a notifier of
## `fd` for `type`, enabled, in the current allocator.
pub func core_notifier_create(fd: i32, kind: CoreNotificationType) -> CoreNotifier*:
    let this = new CoreNotifier(m_fd = -1) else trap("out of memory")
    core_notifier_construct(this, fd, kind)
    return this

## Ported from Libraries/LibCore/Notifier.cpp:14 Core::Notifier::Notifier: makes `this` (storage its owner holds) a
## notifier of `fd` for `type`, and enables it.
pub func core_notifier_construct(this: CoreNotifier*, fd: i32, kind: CoreNotificationType):
    this.m_fd = fd
    this.m_type = kind
    this.m_is_enabled = false
    core_notifier_set_enabled(this, true)

## Ported from Libraries/LibCore/Notifier.cpp:21 Core::Notifier::~Notifier: unregisters the notifier (call it before
## its storage is released).
pub func core_notifier_destroy(this: CoreNotifier*):
    core_notifier_set_enabled(this, false)

## Ported from Libraries/LibCore/Notifier.cpp:26 Core::Notifier::set_enabled.
pub func core_notifier_set_enabled(this: CoreNotifier*, enabled: bool):
    if this.m_fd < 0:
        return
    if enabled == this.m_is_enabled:
        return
    this.m_is_enabled = enabled
    if enabled:
        core_event_loop_register_notifier(this)
    else:
        core_event_loop_unregister_notifier(this)

## Ported from Libraries/LibCore/Notifier.cpp:39 Core::Notifier::close.
pub func core_notifier_close(this: CoreNotifier*):
    if this.m_fd < 0:
        return
    core_notifier_set_enabled(this, false)
    this.m_fd = -1

## Ported from Libraries/LibCore/Notifier.h:39 Core::Notifier::fd.
pub func core_notifier_fd(this: const CoreNotifier*) -> i32:
    return this.m_fd

## Ported from Libraries/LibCore/Notifier.h:40 Core::Notifier::type.
pub func core_notifier_type(this: const CoreNotifier*) -> CoreNotificationType:
    return this.m_type

## Whether the notifier is registered with the event loop (Notifier::m_is_enabled).
pub func core_notifier_is_enabled(this: const CoreNotifier*) -> bool:
    return this.m_is_enabled

## Ported from Libraries/LibCore/Notifier.cpp:47 Core::Notifier::set_type.
pub func core_notifier_set_type(this: CoreNotifier*, kind: CoreNotificationType):
    if this.m_is_enabled:
        # FIXME: Directly communicate intent to the EventLoop.
        core_notifier_set_enabled(this, false)
        this.m_type = kind
        core_notifier_set_enabled(this, true)
    else:
        this.m_type = kind

## Ported from Libraries/LibCore/Notifier.cpp:59 Core::Notifier::event (Event::NotifierActivation): the event loop
## calls this when the descriptor is ready.
pub func core_notifier_event(this: CoreNotifier*):
    if ak.function0_is_set(&this.on_activation):
        ak.function0_call(&this.on_activation)

## Whether the flags `set` include every flag of `flag` (AK's has_flag).
pub func core_notification_type_has(set: CoreNotificationType, flag: CoreNotificationType) -> bool:
    return ((u8)set & (u8)flag) == (u8)flag

## The flags of `left` and `right` together.
pub func core_notification_type_union(left: CoreNotificationType, right: CoreNotificationType) -> CoreNotificationType:
    return (CoreNotificationType)((u8)left | (u8)right)

## The flags `left` and `right` share.
pub func core_notification_type_intersection(left: CoreNotificationType, right: CoreNotificationType) -> CoreNotificationType:
    return (CoreNotificationType)((u8)left & (u8)right)