From 579a289d3db849657987c3310e7b1d71d290b566 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Thu, 26 Sep 2024 15:40:23 +0100 Subject: [PATCH] LibWeb: Fire error event when script has an execution error We now use the "report an exception" AO when a script has an execution error. This has mostly replaced the older "report the exception" AO in various specifications. Using this newer AO ensures that `window.onerror` is invoked when a script has an execution error. --- .../LibWeb/Text/expected/HTML/Window-onerror.txt | 1 + Tests/LibWeb/Text/input/HTML/Window-onerror.html | 16 ++++++++++++++++ .../Libraries/LibWeb/DOM/EventDispatcher.cpp | 7 +++++-- .../LibWeb/HTML/Scripting/ClassicScript.cpp | 7 +++++-- .../LibWeb/HTML/WindowOrWorkerGlobalScope.cpp | 7 +++++++ .../LibWeb/HTML/WindowOrWorkerGlobalScope.h | 1 + 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/HTML/Window-onerror.txt create mode 100644 Tests/LibWeb/Text/input/HTML/Window-onerror.html diff --git a/Tests/LibWeb/Text/expected/HTML/Window-onerror.txt b/Tests/LibWeb/Text/expected/HTML/Window-onerror.txt new file mode 100644 index 00000000000..81ce1a1e6d2 --- /dev/null +++ b/Tests/LibWeb/Text/expected/HTML/Window-onerror.txt @@ -0,0 +1 @@ +onerror event fired: Error: Uncaught error diff --git a/Tests/LibWeb/Text/input/HTML/Window-onerror.html b/Tests/LibWeb/Text/input/HTML/Window-onerror.html new file mode 100644 index 00000000000..525f1f70a61 --- /dev/null +++ b/Tests/LibWeb/Text/input/HTML/Window-onerror.html @@ -0,0 +1,16 @@ + + + diff --git a/Userland/Libraries/LibWeb/DOM/EventDispatcher.cpp b/Userland/Libraries/LibWeb/DOM/EventDispatcher.cpp index 2ffa115579d..73da452c665 100644 --- a/Userland/Libraries/LibWeb/DOM/EventDispatcher.cpp +++ b/Userland/Libraries/LibWeb/DOM/EventDispatcher.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -91,8 +92,10 @@ bool EventDispatcher::inner_invoke(Event& event, Vector(&global); + VERIFY(window_or_worker); + window_or_worker->report_an_exception(*result.release_error().value()); // FIXME: 2. Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently) } diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp index f4e590048a2..69f6586075d 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace Web::HTML { @@ -126,8 +127,10 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, JS::GCPtr(&settings.global_object()); + VERIFY(window_or_worker); + window_or_worker->report_an_exception(*evaluation_status.value()); // 2. Clean up after running script with settings. settings.clean_up_after_running_script(); diff --git a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp index 82bce7125a5..512657240ce 100644 --- a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp +++ b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp @@ -746,6 +746,13 @@ JS::NonnullGCPtr WindowOrWorkerGlobalScopeMixin::supported_entry_typ // https://html.spec.whatwg.org/multipage/webappapis.html#dom-reporterror void WindowOrWorkerGlobalScopeMixin::report_error(JS::Value e) +{ + // The reportError(e) method steps are to report an exception e for this. + report_an_exception(e); +} + +// https://html.spec.whatwg.org/multipage/webappapis.html#report-an-exception +void WindowOrWorkerGlobalScopeMixin::report_an_exception(JS::Value const& e) { auto& target = static_cast(this_impl()); auto& realm = relevant_realm(target); diff --git a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h index 88635bc66c6..5aa68db9e95 100644 --- a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h +++ b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h @@ -77,6 +77,7 @@ public: JS::NonnullGCPtr indexed_db(); void report_error(JS::Value e); + void report_an_exception(JS::Value const& e); [[nodiscard]] JS::NonnullGCPtr crypto();