diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
index 4214fb6693..c53ea57ac3 100644
--- a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
+++ b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
@@ -234,12 +234,15 @@ void EnvironmentSettingsObject::notify_about_rejected_promises(Badge)
m_about_to_be_notified_rejected_promises_list.clear();
// 4. Let global be settings object's global object.
- auto& global = global_object();
+ // We need this as an event target for the unhandledrejection event below
+ auto& global = verify_cast(global_object());
// 5. Queue a global task on the DOM manipulation task source given global to run the following substep:
queue_global_task(Task::Source::DOMManipulation, global, [this, &global, list = move(list)] {
+ auto& realm = global.realm();
+
// 1. For each promise p in list:
- for (auto promise : list) {
+ for (auto const& promise : list) {
// 1. If p's [[PromiseIsHandled]] internal slot is true, continue to the next iteration of the loop.
if (promise->is_handled())
@@ -257,12 +260,10 @@ void EnvironmentSettingsObject::notify_about_rejected_promises(Badge)
/* .promise = */ JS::make_handle(*promise),
/* .reason = */ promise->result(),
};
- // FIXME: This currently assumes that global is a WindowObject.
- auto& window = verify_cast(global);
- auto promise_rejection_event = PromiseRejectionEvent::create(window.realm(), HTML::EventNames::unhandledrejection, event_init);
+ auto promise_rejection_event = PromiseRejectionEvent::create(realm, HTML::EventNames::unhandledrejection, event_init);
- bool not_handled = window.dispatch_event(*promise_rejection_event);
+ bool not_handled = global.dispatch_event(*promise_rejection_event);
// 3. If notHandled is false, then the promise rejection is handled. Otherwise, the promise rejection is not handled.
@@ -273,7 +274,7 @@ void EnvironmentSettingsObject::notify_about_rejected_promises(Badge)
// This algorithm results in promise rejections being marked as handled or not handled. These concepts parallel handled and not handled script errors.
// If a rejection is still not handled after this, then the rejection may be reported to a developer console.
if (not_handled)
- HTML::report_exception_to_console(promise->result(), realm(), ErrorInPromise::Yes);
+ HTML::report_exception_to_console(promise->result(), realm, ErrorInPromise::Yes);
}
});
}