LibWeb: Only take runnable tasks from the HTML task queue

We were previously willing to execute tasks before they had become
runnable.
This commit is contained in:
Andreas Kling 2021-10-03 15:38:11 +02:00
parent 6283c098ad
commit bbfde63f79
Notes: sideshowbarker 2024-07-18 03:08:55 +09:00
5 changed files with 23 additions and 6 deletions

View file

@ -90,9 +90,8 @@ void EventLoop::process()
// 1. Let taskQueue be one of the event loop's task queues, chosen in an implementation-defined manner, with the constraint that the chosen task queue must contain at least one runnable task. If there is no such task queue, then jump to the microtasks step below.
auto& task_queue = m_task_queue;
if (!task_queue.is_empty()) {
if (auto oldest_task = task_queue.take_first_runnable()) {
// 2. Let oldestTask be the first runnable task in taskQueue, and remove it from taskQueue.
auto oldest_task = task_queue.take_first_runnable();
// 3. Set the event loop's currently running task to oldestTask.
m_currently_running_task = oldest_task.ptr();

View file

@ -25,4 +25,11 @@ void Task::execute()
m_steps();
}
// https://html.spec.whatwg.org/#concept-task-runnable
bool Task::is_runnable() const
{
// A task is runnable if its document is either null or fully active.
return !m_document || m_document->is_fully_active();
}
}

View file

@ -40,6 +40,8 @@ public:
DOM::Document* document() { return m_document; }
DOM::Document const* document() const { return m_document; }
bool is_runnable() const;
private:
Task(Source, DOM::Document*, Function<void()> steps);

View file

@ -20,8 +20,17 @@ TaskQueue::~TaskQueue()
void TaskQueue::add(NonnullOwnPtr<Task> task)
{
m_tasks.enqueue(move(task));
m_tasks.append(move(task));
m_event_loop.schedule();
}
OwnPtr<Task> TaskQueue::take_first_runnable()
{
for (size_t i = 0; i < m_tasks.size(); ++i) {
if (m_tasks[i]->is_runnable())
return m_tasks.take(i);
}
return nullptr;
}
}

View file

@ -19,20 +19,20 @@ public:
bool is_empty() const { return m_tasks.is_empty(); }
void add(NonnullOwnPtr<HTML::Task>);
OwnPtr<HTML::Task> take_first_runnable() { return m_tasks.dequeue(); }
OwnPtr<HTML::Task> take_first_runnable();
void enqueue(NonnullOwnPtr<HTML::Task> task) { add(move(task)); }
OwnPtr<HTML::Task> dequeue()
{
if (m_tasks.is_empty())
return {};
return m_tasks.dequeue();
return m_tasks.take_first();
}
private:
HTML::EventLoop& m_event_loop;
Queue<NonnullOwnPtr<HTML::Task>> m_tasks;
Vector<NonnullOwnPtr<HTML::Task>> m_tasks;
};
}