LibWeb: Introduce the Environment Settings Object

The environment settings object is effectively the context a piece of
script is running under, for example, it contains the origin,
responsible document, realm, global object and event loop for the
current context. This effectively replaces ScriptExecutionContext, but
it cannot be removed in this commit as EventTarget still depends on it.

https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
This commit is contained in:
Luke Wilde 2021-10-14 16:12:53 +01:00 committed by Linus Groh
parent 4db5406d62
commit f71f404e0c
Notes: sideshowbarker 2024-07-17 19:09:28 +09:00
29 changed files with 883 additions and 86 deletions

View file

@ -12,6 +12,7 @@
#include <LibWeb/DOM/Window.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HighResolutionTime/Performance.h>
namespace Web::HTML {
@ -207,15 +208,33 @@ void EventLoop::process()
schedule();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task
void queue_global_task(HTML::Task::Source source, DOM::Document& document, Function<void()> steps)
// 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)
{
// FIXME: This should take a global object as input and find the relevant document for it.
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::GlobalObject& global_object, Function<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());
auto& event_loop = global_custom_data.event_loop;
// 2. Let document be global's associated Document, if global is a Window object; otherwise null.
DOM::Document* document { nullptr };
if (is<Bindings::WindowObject>(global_object)) {
auto& window_object = verify_cast<Bindings::WindowObject>(global_object);
document = &window_object.impl().associated_document();
}
// 3. Queue a task given source, event loop, document, and steps.
event_loop.task_queue().add(HTML::Task::create(source, document, move(steps)));
}
// 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, Function<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();
@ -226,7 +245,7 @@ void queue_a_microtask(DOM::Document& document, Function<void()> steps)
// 4. Set microtask's steps to steps.
// 5. Set microtask's source to the microtask task source.
// 6. Set microtask's document to document.
auto microtask = HTML::Task::create(HTML::Task::Source::Microtask, &document, move(steps));
auto microtask = HTML::Task::create(HTML::Task::Source::Microtask, document, move(steps));
// FIXME: 7. Set microtask's script evaluation environment settings object set to an empty set.
@ -259,7 +278,9 @@ void EventLoop::perform_a_microtask_checkpoint()
m_currently_running_task = nullptr;
}
// FIXME: 4. For each environment settings object whose responsible event loop is this event loop, notify about rejected promises on that environment settings object.
// 4. For each environment settings object whose responsible event loop is this event loop, notify about rejected promises on that environment settings object.
for (auto& environment_settings_object : m_related_environment_settings_objects)
environment_settings_object.notify_about_rejected_promises({});
// FIXME: 5. Cleanup Indexed Database transactions.
@ -290,4 +311,30 @@ void EventLoop::unregister_document(Badge<DOM::Document>, DOM::Document& documen
VERIFY(did_remove);
}
void EventLoop::push_onto_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object)
{
m_backup_incumbent_settings_object_stack.append(environment_settings_object);
}
void EventLoop::pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>)
{
m_backup_incumbent_settings_object_stack.take_last();
}
EnvironmentSettingsObject& EventLoop::top_of_backup_incumbent_settings_object_stack()
{
return m_backup_incumbent_settings_object_stack.last();
}
void EventLoop::register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object)
{
m_related_environment_settings_objects.append(environment_settings_object);
}
void EventLoop::unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object)
{
bool did_remove = m_related_environment_settings_objects.remove_first_matching([&](auto& entry) { return &entry == &environment_settings_object; });
VERIFY(did_remove);
}
}