LibWeb: Make EventLoop, TaskQueue, and Task GC-allocated

...and use HeapFunction instead of SafeFunction for task steps.

Since there is only one EventLoop per process, it lives as a global
handle in the VM custom data.

This makes it much easier to reason about lifetimes of tasks, task
steps, and random stuff captured by them.
This commit is contained in:
Andreas Kling 2024-04-04 12:06:50 +02:00
commit 2ef37c0b06
Notes: sideshowbarker 2024-07-17 02:14:39 +09:00
20 changed files with 167 additions and 124 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,25 +10,41 @@
namespace Web::HTML {
JS_DEFINE_ALLOCATOR(Task);
static IDAllocator s_unique_task_source_allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) };
static IDAllocator s_task_id_allocator;
Task::Task(Source source, DOM::Document const* document, JS::SafeFunction<void()> steps)
JS::NonnullGCPtr<Task> Task::create(JS::VM& vm, Source source, JS::GCPtr<DOM::Document const> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
{
return vm.heap().allocate_without_realm<Task>(source, document, move(steps));
}
Task::Task(Source source, JS::GCPtr<DOM::Document const> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
: m_id(s_task_id_allocator.allocate())
, m_source(source)
, m_steps(move(steps))
, m_document(JS::make_handle(document))
, m_steps(steps)
, m_document(document)
{
}
Task::~Task()
Task::~Task() = default;
void Task::finalize()
{
s_unique_task_source_allocator.deallocate(m_id);
}
void Task::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_steps);
visitor.visit(m_document);
}
void Task::execute()
{
m_steps();
m_steps->function()();
}
// https://html.spec.whatwg.org/#concept-task-runnable