dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / tests / web_test / worker_platform.lucb
#==============================================================================================
#
# worker_platform - the worker's platform: its event loop, plugins, fonts and VM
#
# DESCRIPTION:
# What WebContent's main (Services/WebContent/main.cpp) installs before it serves a page,
# for one test in layout-test mode:
# - the process's event loop: Core::EventLoop over a timer registry, a queue of
# deferred invocations and the Core::Notifiers of the in-process client's sockets,
# waited for together (LibCore's EventLoopImplementationUnix, minus signals and
# the wake pipe, which one thread does not need), installed behind the engine's
# Core seam (external/lib_core/event_loop.lucb), and the Platform::EventLoopPlugin
# over it;
# - the fonts: a PathFontProvider with the test fonts (resource://fonts in Ladybird:
# SerenitySans and Noto Emoji, here tests/libweb/fonts) and Platform::FontPlugin in
# layout-test mode, which maps every generic family to SerenitySans. Ladybird's
# FontPlugin also loads the system's font directories; tests that depend on them
# are not portable, so the worker loads only the test fonts (DESIGN.md ยง6.1);
# - the main thread VM (Bindings::initialize_main_thread_vm with a similar-origin
# window agent);
# - loading: the resource directory, the ImageCodecPlugin and the ResourceLoader with
# the engine's in-process RequestClient behind a hermetic one that refuses the
# network (worker_install_loading, worker_network).
#
# The loop lives in the worker's main frame (worker_main), so the conservative scan of
# the stack keeps the managed storage of its timer registry and deferred queue alive:
# a registered Core::Timer stays alive until it is unregistered (LibCore holds a weak
# pointer to its owner; here a timer whose owner was collected without stopping it
# cannot fire into freed memory).
#
# Copyright (c) 2018-2026, the Ladybird developers.
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: The event loop ==========================================================================
## A registered timer (LibCore's EventLoopTimer): fires `timer` at `fire_time` (time.now()
## nanoseconds), then every `interval_ms` while it stays registered.
struct LoopTimer:
var timer: web.CoreTimer*
var id: isize
var interval_ms: i32
var should_reload: bool
var fire_time: u64
## The worker's event loop (web.CoreEventLoopImplementation): one thread; its notifiers are the
## in-process client's sockets.
struct WorkerEventLoop: web.CoreEventLoopImplementation:
var timers: ak.Vector[LoopTimer]
var next_timer_id: isize
var deferred: ak.Vector[ak.Function0[unit]]
var notifiers: web.CoreEventLoopNotifiers
var exit_requested: bool
var exit_code: i32
## EventLoopImplementationUnix::pump: waits for events (in wait_for_events mode with nothing
## queued: until a notifier's descriptor is ready or the next timer is due; otherwise it
## only looks at the descriptors), then runs the deferred invocations queued so far, the
## notifiers whose descriptors are ready, and the timers that are due. Answers how many
## events were processed. With nothing queued, no timer and no notifier registered, nothing
## can ever happen in this process: the test is stalled, and the worker stops.
func pump(mode: web.CoreEventLoopPumpMode) -> usize:
let waiting = mode == web.CoreEventLoopPumpMode.wait_for_events and ak.vector_is_empty(&self.deferred)
let watching = web.core_event_loop_notifiers_count(&self.notifiers) > 0
if waiting and not watching:
if let next = self.next_fire_time():
let now = time.now()
if next > now:
thread.sleep((next - now + 999999) // 1000000)
else:
trap("web_test: stalled: the event loop has nothing left to run")
elif watching:
var deadline = net.Deadline.at(0)
if waiting:
deadline = net.Deadline()
if let next = self.next_fire_time():
deadline = net.Deadline.at(next)
_ = web.core_event_loop_notifiers_wait(&self.notifiers, deadline)
var processed: usize = 0
# Deferred invocations queued while these run wait for the next pump.
let pending = self.deferred
self.deferred = ak.Vector[ak.Function0[unit]]()
for index in 0..<ak.vector_size(&pending):
var function = *ak.vector_at_const(&pending, index)
ak.function0_call(&function)
processed += 1
# The notifiers the wait found ready.
processed += web.core_event_loop_notifiers_dispatch(&self.notifiers)
# The timers due now, by id; each is looked up again before it fires, since firing one
# may stop another.
let now = time.now()
var due: ak.Vector[isize]
for index in 0..<ak.vector_size(&self.timers):
let entry = ak.vector_at_const(&self.timers, index)
if entry.fire_time <= now:
ak.vector_append_t(&due, entry.id)
for index in 0..<ak.vector_size(&due):
let id = *ak.vector_at_const(&due, index)
let slot = self.find_timer(id) else continue
let entry = ak.vector_at(&self.timers, slot)
let timer = entry.timer
if entry.should_reload:
entry.fire_time = now + (u64)entry.interval_ms * 1000000
else:
_ = ak.vector_take(&self.timers, slot)
web.core_timer_timer_event(timer)
processed += 1
return processed
func register_notifier(notifier: web.CoreNotifier*):
web.core_event_loop_notifiers_register(&self.notifiers, notifier)
func unregister_notifier(notifier: web.CoreNotifier*):
web.core_event_loop_notifiers_unregister(&self.notifiers, notifier)
func quit(code: i32):
self.exit_requested = true
self.exit_code = code
func was_exit_requested() -> bool:
return self.exit_requested
func wake():
return # One thread: nothing waits while code runs.
func deferred_invoke(function: ak.Function0[unit]):
ak.vector_append_t(&self.deferred, function)
func register_timer(timer: web.CoreTimer*, milliseconds: i32, should_reload: bool) -> isize:
self.next_timer_id += 1
let interval = milliseconds if milliseconds > 0 else 0
ak.vector_append_t(&self.timers, LoopTimer(timer = timer, id = self.next_timer_id, interval_ms = interval,
should_reload = should_reload, fire_time = time.now() + (u64)interval * 1000000))
return self.next_timer_id
func unregister_timer(timer_id: isize):
if let slot = self.find_timer(timer_id):
_ = ak.vector_take(&self.timers, slot)
## The index of the registered timer `id`, if it is registered.
func find_timer(id: isize) -> usize?:
for index in 0..<ak.vector_size(&self.timers):
if ak.vector_at_const(&self.timers, index).id == id:
return index
return none
## When the next timer is due, if any is registered.
func next_fire_time() -> u64?:
var next: u64? = none
for index in 0..<ak.vector_size(&self.timers):
let fire_time = ak.vector_at_const(&self.timers, index).fire_time
if let earliest = next:
if fire_time < earliest:
next = fire_time
else:
next = fire_time
return next
## Whether anything is queued or due right now.
func has_pending_events() -> bool:
if not ak.vector_is_empty(&self.deferred):
return true
let now = time.now()
for index in 0..<ak.vector_size(&self.timers):
if ak.vector_at_const(&self.timers, index).fire_time <= now:
return true
return false
## Pumps `event_loop` until nothing is queued or due (timers due later are left alone).
func worker_drain(event_loop: WorkerEventLoop*):
while event_loop.has_pending_events():
_ = event_loop.pump(web.CoreEventLoopPumpMode.dont_wait_for_events)
## The EventLoopPlugin (a stateless object in the C heap, as WebContent's `new EventLoopPlugin`).
func worker_install_event_loop(event_loop: WorkerEventLoop*):
web.core_event_loop_install(event_loop)
let plugin = (new web.PlatformEventLoopPlugin in memory.heap) else trap("out of memory")
web.platform_event_loop_plugin_install(plugin)
# mark: Fonts ===================================================================================
## The test fonts in `directory` as the system fonts (WebContent's PathFontProvider over
## resource://fonts) and the FontPlugin in layout-test mode: Platform::FontPlugin's constructor
## (platform/font_plugin.lucb) without the system's font directories, in the C heap as it is.
func worker_install_fonts(directory: str):
let provider = web_fonts.path_font_provider_create()
_ = web_fonts.font_database_install_system_font_provider(web_fonts.font_database_the(), (web_fonts.SystemFontProvider*)provider)
var uri = ak.string_builder_new()
with memory.heap:
ak.string_builder_append(&uri, "file://")
ak.string_builder_append(&uri, directory)
let uri_view = ak.string_builder_string_view(&uri)
web_fonts.path_font_provider_load_all_fonts_from_uri(provider, uri_view)
let c_heap = web_fonts.font_allocators_c_heap()
let previous = web_fonts.font_allocators_enter(&c_heap)
defer web_fonts.font_allocators_leave(previous)
with memory.heap:
let plugin = (new web.PlatformFontPlugin(m_default_fixed_width_font = none, m_is_layout_test_mode = true)) else trap("out of memory")
web.platform_font_plugin_update_generic_fonts(plugin)
let default_fixed_width_font_name = web.platform_font_plugin_generic_font_name(plugin, web.PlatformGenericFont.ui_monospace, 400, 0)
plugin.m_default_fixed_width_font = web_fonts.font_database_get(web_fonts.font_database_the(), default_fixed_width_font_name, 12.0, 400, (u32)web_fonts.FontWidth.normal, 0)
assert(plugin.m_default_fixed_width_font != none, "the test fonts provide the default fixed-width font")
ak.vector_append_t(&plugin.m_symbol_font_names, ak.fly_string_from_str("Noto Emoji"))
web.platform_font_plugin_install(plugin)
# mark: The VM ==================================================================================
## Everything WebContent's main sets up before its first page: the strings, the event loop and
## its plugin, the fonts, the main thread VM, and what loads resources. Answers the VM.
func worker_initialize_platform(event_loop: WorkerEventLoop*, root: str) -> web.Vm*:
web.web_initialize_strings()
worker_install_event_loop(event_loop)
var fonts = ak.string_builder_new()
with memory.heap:
ak.string_builder_append(&fonts, root)
ak.string_builder_append(&fonts, "/fonts")
let fonts_view = ak.string_builder_string_view(&fonts)
worker_install_fonts(ak.string_view_text(fonts_view))
# test-web runs WebContent with --disable-scrollbar-painting (PaintViewportScrollbars::No,
# "Ensure tests are resilient to minor changes to the viewport scrollbar"), which main
# passes to Painting::set_paint_viewport_scrollbars: no viewport paints its scrollbars.
web.paint_set_paint_viewport_scrollbars(false)
web.bind_initialize_main_thread_vm(web.BindAgentType.similar_origin_window)
let vm = web.bind_main_thread_vm()
worker_install_loading(vm)
worker_stage("platform")
return vm
## What WebContent's main installs for loading: the resource directory (Ladybird's resources; the
## repository's data/res, from the working directory the driver runs in), the image decoder
## (ImageCodecPlugin: the engine's over luce-png and luce-jpeg) and the ResourceLoader with its
## RequestClient (the engine's in-process client: file:, data:, http(s):, kept off the network by
## worker_network's hermetic client). The plugin and the client live in the C heap for the process.
func worker_install_loading(vm: web.Vm*):
web.core_resource_implementation_install(web.core_resource_implementation_file_create(ak.sv("data/res")))
web.luce_image_codec_plugin_install()
let client = hermetic_request_client_create(web.vm_heap(vm))
web.resource_loader_initialize(web.vm_heap(vm), &client.client)