From 412467cc70d66c02a9c6de44da019273ddb544a5 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Thu, 9 Oct 2025 16:58:17 -0500 Subject: [PATCH] LibWeb: Add a hook in EventLoop to call a task when reaching step 1 --- Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp | 13 +++++++++++++ Libraries/LibWeb/HTML/EventLoop/EventLoop.h | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 8801d05aed..73d00503ae 100644 --- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -49,6 +49,7 @@ EventLoop::~EventLoop() = default; void EventLoop::visit_edges(Visitor& visitor) { Base::visit_edges(visitor); + visitor.visit(m_reached_step_1_tasks); visitor.visit(m_task_queue); visitor.visit(m_microtask_queue); visitor.visit(m_currently_running_task); @@ -163,6 +164,12 @@ void EventLoop::process() GC::Ptr oldest_task; [[maybe_unused]] double task_start_time = 0; + // Some algorithms request that steps or states only occur once the event loop has reached step 1. + // Invoke a set of tasks that these algorithms request us to in order to achieve this. + auto reached_step_1_tasks = move(m_reached_step_1_tasks); + for (auto& reached_step_1_task : reached_step_1_tasks) + reached_step_1_task->function()(); + // 2. If the event loop has a task queue with at least one runnable task, then: if (m_task_queue->has_runnable_tasks()) { // 1. Let taskQueue be one such task queue, chosen in an implementation-defined manner. @@ -508,6 +515,12 @@ void EventLoop::update_the_rendering() } } +void run_when_event_loop_reaches_step_1(GC::Ref> steps) +{ + auto& event_loop = main_thread_event_loop(); + event_loop.run_upon_reaching_step_1(steps); +} + // https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task TaskID queue_a_task(HTML::Task::Source source, GC::Ptr event_loop, GC::Ptr document, GC::Ref> steps) { diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h index 5d41e7ecda..9de3237f44 100644 --- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h +++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h @@ -48,6 +48,8 @@ public: Type type() const { return m_type; } + void run_upon_reaching_step_1(GC::Ref> task) { m_reached_step_1_tasks.append(task); } + TaskQueue& task_queue() { return *m_task_queue; } TaskQueue const& task_queue() const { return *m_task_queue; } @@ -103,6 +105,8 @@ private: Type m_type { Type::Window }; + Vector>> m_reached_step_1_tasks; + GC::Ptr m_task_queue; GC::Ptr m_microtask_queue; @@ -142,6 +146,7 @@ private: }; WEB_API EventLoop& main_thread_event_loop(); +WEB_API void run_when_event_loop_reaches_step_1(GC::Ref> steps); WEB_API TaskID queue_a_task(HTML::Task::Source, GC::Ptr, GC::Ptr, GC::Ref> steps); WEB_API TaskID queue_global_task(HTML::Task::Source, JS::Object&, GC::Ref> steps); WEB_API void queue_a_microtask(DOM::Document const*, GC::Ref> steps);