LibWeb: Make HTML::Task IDs a sequential, distinct numeric type

This also fixes a bug where task IDs were being deallocated from the
wrong IDAllocator. I don't know if it was actually possible to cause any
real trouble with that mistake, nor do I know how to write a test for
it, but this makes the bug go away.
This commit is contained in:
Andreas Kling 2024-08-04 17:10:49 +02:00 committed by Andreas Kling
commit 08d60d7521
Notes: github-actions[bot] 2024-08-05 07:12:59 +00:00
12 changed files with 33 additions and 29 deletions

View file

@ -384,7 +384,7 @@ void EventLoop::process()
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task
int queue_a_task(HTML::Task::Source source, JS::GCPtr<EventLoop> event_loop, JS::GCPtr<DOM::Document> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
TaskID queue_a_task(HTML::Task::Source source, JS::GCPtr<EventLoop> event_loop, JS::GCPtr<DOM::Document> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
{
// 1. If event loop was not given, set event loop to the implied event loop.
if (!event_loop)
@ -409,7 +409,7 @@ int queue_a_task(HTML::Task::Source source, JS::GCPtr<EventLoop> event_loop, JS:
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task
int queue_global_task(HTML::Task::Source source, JS::Object& global_object, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
TaskID queue_global_task(HTML::Task::Source source, JS::Object& global_object, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
{
// 1. Let event loop be global's relevant agent's event loop.
auto& global_custom_data = verify_cast<Bindings::WebEngineCustomData>(*global_object.vm().custom_data());

View file

@ -116,8 +116,8 @@ private:
};
EventLoop& main_thread_event_loop();
int queue_a_task(HTML::Task::Source, JS::GCPtr<EventLoop>, JS::GCPtr<DOM::Document>, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
int queue_global_task(HTML::Task::Source, JS::Object&, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
TaskID queue_a_task(HTML::Task::Source, JS::GCPtr<EventLoop>, JS::GCPtr<DOM::Document>, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
TaskID queue_global_task(HTML::Task::Source, JS::Object&, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
void queue_a_microtask(DOM::Document const*, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
void perform_a_microtask_checkpoint();

View file

@ -13,7 +13,12 @@ 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;
[[nodiscard]] static TaskID allocate_task_id()
{
static u64 next_task_id = 1;
return next_task_id++;
}
JS::NonnullGCPtr<Task> Task::create(JS::VM& vm, Source source, JS::GCPtr<DOM::Document const> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
{
@ -21,7 +26,7 @@ JS::NonnullGCPtr<Task> Task::create(JS::VM& vm, Source source, JS::GCPtr<DOM::Do
}
Task::Task(Source source, JS::GCPtr<DOM::Document const> document, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps)
: m_id(s_task_id_allocator.allocate())
: m_id(allocate_task_id())
, m_source(source)
, m_steps(steps)
, m_document(document)
@ -30,11 +35,6 @@ Task::Task(Source source, JS::GCPtr<DOM::Document const> document, JS::NonnullGC
Task::~Task() = default;
void Task::finalize()
{
s_unique_task_source_allocator.deallocate(m_id);
}
void Task::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/DistinctNumeric.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/CellAllocator.h>
#include <LibJS/SafeFunction.h>
@ -15,6 +16,8 @@ namespace Web::HTML {
struct UniqueTaskSource;
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, TaskID, Comparison);
class Task final : public JS::Cell {
JS_CELL(Task, JS::Cell);
JS_DECLARE_ALLOCATOR(Task);
@ -69,9 +72,8 @@ public:
static JS::NonnullGCPtr<Task> create(JS::VM&, Source, JS::GCPtr<DOM::Document const>, JS::NonnullGCPtr<JS::HeapFunction<void()>> steps);
virtual ~Task() override;
virtual void finalize() override;
int id() const { return m_id; }
[[nodiscard]] TaskID id() const { return m_id; }
Source source() const { return m_source; }
void execute();
@ -84,7 +86,7 @@ private:
virtual void visit_edges(Visitor&) override;
int m_id { 0 };
TaskID m_id {};
Source m_source { Source::Unspecified };
JS::NonnullGCPtr<JS::HeapFunction<void()>> m_steps;
JS::GCPtr<DOM::Document const> m_document;