LibWeb: Treat global as EventTarget in notify_about_rejected_promises

We only need to know that the Global Object of the environment is an
event target in order to dispatch an event on it. This resolves a FIXME
where we assumed that the only type of Global in LibWeb is HTML::Window.
This commit is contained in:
Andrew Kaster 2024-02-20 18:25:11 -07:00 committed by Andreas Kling
parent 9d7e234439
commit 0dde88d714
Notes: sideshowbarker 2024-07-17 09:49:33 +09:00

View File

@ -234,12 +234,15 @@ void EnvironmentSettingsObject::notify_about_rejected_promises(Badge<EventLoop>)
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<DOM::EventTarget>(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<EventLoop>)
/* .promise = */ JS::make_handle(*promise),
/* .reason = */ promise->result(),
};
// FIXME: This currently assumes that global is a WindowObject.
auto& window = verify_cast<HTML::Window>(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<EventLoop>)
// 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);
}
});
}