LibWeb: Return a representation of an 'Agent' in 'relevant agent'

This makes it more convenient to use the 'relvant agent' concept,
instead of the awkward dynamic casts we needed to do for every call
site.

mutation_observers is also changed to hold a GC::Root instead of raw
GC::Ptr. Somehow this was not causing problems before, but trips up CI
after these changes.
This commit is contained in:
Shannon Booth 2025-01-04 23:12:55 +13:00 committed by Tim Flynn
commit 64eeda6450
Notes: github-actions[bot] 2025-01-11 15:40:39 +00:00
15 changed files with 140 additions and 98 deletions

View file

@ -118,6 +118,7 @@
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/PopStateEvent.h>
#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
@ -3949,7 +3950,7 @@ void Document::unload(GC::Ptr<Document>)
auto intend_to_store_in_bfcache = false;
// 6. Let eventLoop be oldDocument's relevant agent's event loop.
auto& event_loop = *verify_cast<Bindings::WebEngineCustomData>(*HTML::relevant_agent(*this).custom_data()).event_loop;
auto& event_loop = *HTML::relevant_agent(*this).event_loop;
// 7. Increase eventLoop's termination nesting level by 1.
event_loop.increment_termination_nesting_level();
@ -6096,7 +6097,7 @@ Document::StepsToFireBeforeunloadResult Document::steps_to_fire_beforeunload(boo
m_unload_counter++;
// 3. Increase document's relevant agent's event loop's termination nesting level by 1.
auto& event_loop = *verify_cast<Bindings::WebEngineCustomData>(*HTML::relevant_agent(*this).custom_data()).event_loop;
auto& event_loop = *HTML::relevant_agent(*this).event_loop;
event_loop.increment_termination_nesting_level();
// 4. Let eventFiringResult be the result of firing an event named beforeunload at document's relevant global object,

View file

@ -54,6 +54,7 @@
#include <LibWeb/HTML/HTMLTextAreaElement.h>
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/TraversableNavigable.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/CharacterTypes.h>
@ -2088,8 +2089,7 @@ void Element::enqueue_an_element_on_the_appropriate_element_queue()
{
// 1. Let reactionsStack be element's relevant agent's custom element reactions stack.
auto& relevant_agent = HTML::relevant_agent(*this);
auto* custom_data = verify_cast<Bindings::WebEngineCustomData>(relevant_agent.custom_data());
auto& reactions_stack = custom_data->custom_element_reactions_stack;
auto& reactions_stack = relevant_agent.custom_element_reactions_stack;
// 2. If reactionsStack is empty, then:
if (reactions_stack.element_queue_stack.is_empty()) {
@ -2105,10 +2105,8 @@ void Element::enqueue_an_element_on_the_appropriate_element_queue()
// 4. Queue a microtask to perform the following steps:
// NOTE: `this` is protected by GC::Function
HTML::queue_a_microtask(&document(), GC::create_function(relevant_agent.heap(), [this]() {
auto& relevant_agent = HTML::relevant_agent(*this);
auto* custom_data = verify_cast<Bindings::WebEngineCustomData>(relevant_agent.custom_data());
auto& reactions_stack = custom_data->custom_element_reactions_stack;
HTML::queue_a_microtask(&document(), GC::create_function(heap(), [this]() {
auto& reactions_stack = HTML::relevant_agent(*this).custom_element_reactions_stack;
// 1. Invoke custom element reactions in reactionsStack's backup element queue.
Bindings::invoke_custom_element_reactions(reactions_stack.backup_element_queue);
@ -2121,7 +2119,7 @@ void Element::enqueue_an_element_on_the_appropriate_element_queue()
}
// 3. Otherwise, add element to element's relevant agent's current element queue.
custom_data->current_element_queue().append(*this);
relevant_agent.current_element_queue().append(*this);
}
// https://html.spec.whatwg.org/multipage/custom-elements.html#enqueue-a-custom-element-upgrade-reaction

View file

@ -9,6 +9,7 @@
#include <LibWeb/Bindings/MutationObserverPrototype.h>
#include <LibWeb/DOM/MutationObserver.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/HTML/Scripting/Agent.h>
namespace Web::DOM {
@ -29,14 +30,12 @@ MutationObserver::MutationObserver(JS::Realm& realm, GC::Ptr<WebIDL::CallbackTyp
// 1. Set thiss callback to callback.
// 2. Append this to thiss relevant agents mutation observers.
auto* agent_custom_data = verify_cast<Bindings::WebEngineCustomData>(realm.vm().custom_data());
agent_custom_data->mutation_observers.append(*this);
HTML::relevant_agent(*this).mutation_observers.append(*this);
}
MutationObserver::~MutationObserver()
{
auto* agent_custom_data = verify_cast<Bindings::WebEngineCustomData>(vm().custom_data());
agent_custom_data->mutation_observers.remove_all_matching([this](auto& observer) {
HTML::relevant_agent(*this).mutation_observers.remove_all_matching([this](auto& observer) {
return observer.ptr() == this;
});
}