LibWeb: Use JS::SafeFunction in the HTML task queues

This means that HTML tasks automatically protect anything in their
capture lists, and we no longer need to jump through hoops with
JS::Handle etc.
This commit is contained in:
Andreas Kling 2022-09-24 12:04:06 +02:00
parent 131c3f50de
commit d505192014
Notes: sideshowbarker 2024-07-17 06:41:15 +09:00
4 changed files with 13 additions and 11 deletions

View File

@ -231,13 +231,13 @@ void EventLoop::process()
// FIXME: This is here to paper over an issue in the HTML parser where it'll create new interpreters (and thus ESOs) on temporary documents created for innerHTML if it uses Document::realm() to get the global object.
// Use queue_global_task instead.
void old_queue_global_task_with_document(HTML::Task::Source source, DOM::Document& document, Function<void()> steps)
void old_queue_global_task_with_document(HTML::Task::Source source, DOM::Document& document, JS::SafeFunction<void()> steps)
{
main_thread_event_loop().task_queue().add(HTML::Task::create(source, &document, move(steps)));
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task
void queue_global_task(HTML::Task::Source source, JS::Object& global_object, Function<void()> steps)
void queue_global_task(HTML::Task::Source source, JS::Object& global_object, JS::SafeFunction<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());
@ -255,7 +255,7 @@ void queue_global_task(HTML::Task::Source source, JS::Object& global_object, Fun
}
// https://html.spec.whatwg.org/#queue-a-microtask
void queue_a_microtask(DOM::Document* document, Function<void()> steps)
void queue_a_microtask(DOM::Document* document, JS::SafeFunction<void()> steps)
{
// 1. If event loop was not given, set event loop to the implied event loop.
auto& event_loop = HTML::main_thread_event_loop();

View File

@ -10,6 +10,7 @@
#include <AK/WeakPtr.h>
#include <LibCore/Forward.h>
#include <LibJS/Forward.h>
#include <LibJS/SafeFunction.h>
#include <LibWeb/HTML/EventLoop/TaskQueue.h>
namespace Web::HTML {
@ -107,9 +108,9 @@ private:
};
EventLoop& main_thread_event_loop();
void old_queue_global_task_with_document(HTML::Task::Source, DOM::Document&, Function<void()> steps);
void queue_global_task(HTML::Task::Source, JS::Object&, Function<void()> steps);
void queue_a_microtask(DOM::Document*, Function<void()> steps);
void old_queue_global_task_with_document(HTML::Task::Source, DOM::Document&, JS::SafeFunction<void()> steps);
void queue_global_task(HTML::Task::Source, JS::Object&, JS::SafeFunction<void()> steps);
void queue_a_microtask(DOM::Document*, JS::SafeFunction<void()> steps);
void perform_a_microtask_checkpoint();
}

View File

@ -9,7 +9,7 @@
namespace Web::HTML {
Task::Task(Source source, DOM::Document* document, Function<void()> steps)
Task::Task(Source source, DOM::Document* document, JS::SafeFunction<void()> steps)
: m_source(source)
, m_steps(move(steps))
, m_document(JS::make_handle(document))

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,6 +10,7 @@
#include <AK/NonnullOwnPtr.h>
#include <AK/RefPtr.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/SafeFunction.h>
#include <LibWeb/Forward.h>
namespace Web::HTML {
@ -30,7 +31,7 @@ public:
JavaScriptEngine,
};
static NonnullOwnPtr<Task> create(Source source, DOM::Document* document, Function<void()> steps)
static NonnullOwnPtr<Task> create(Source source, DOM::Document* document, JS::SafeFunction<void()> steps)
{
return adopt_own(*new Task(source, document, move(steps)));
}
@ -45,10 +46,10 @@ public:
bool is_runnable() const;
private:
Task(Source, DOM::Document*, Function<void()> steps);
Task(Source, DOM::Document*, JS::SafeFunction<void()> steps);
Source m_source { Source::Unspecified };
Function<void()> m_steps;
JS::SafeFunction<void()> m_steps;
JS::Handle<DOM::Document> m_document;
};