From 62491cda0b9e8aa55067aa794d58a79b965f6f46 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Mon, 27 Jun 2022 19:53:30 +0100 Subject: [PATCH] LibWeb: Use CSO if running script is null in HostPromiseRejectionTracker --- Base/res/html/misc/unhandledpromisetest.html | 29 +++++++++++++++++++ .../LibWeb/Bindings/MainThreadVM.cpp | 23 +++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 Base/res/html/misc/unhandledpromisetest.html diff --git a/Base/res/html/misc/unhandledpromisetest.html b/Base/res/html/misc/unhandledpromisetest.html new file mode 100644 index 00000000000..3838ff17dcc --- /dev/null +++ b/Base/res/html/misc/unhandledpromisetest.html @@ -0,0 +1,29 @@ + + + + Unhandled Promise rejection with no [[ScriptOrModule]] for the current execution context + + + + + + + diff --git a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp index e1209646bb0..9329b77ea66 100644 --- a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp +++ b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp @@ -55,8 +55,7 @@ JS::VM& main_thread_vm() vm->host_promise_rejection_tracker = [](JS::Promise& promise, JS::Promise::RejectionOperation operation) { // 1. Let script be the running script. // The running script is the script in the [[HostDefined]] field in the ScriptOrModule component of the running JavaScript execution context. - // FIXME: This currently assumes there's only a ClassicScript. - HTML::ClassicScript* script { nullptr }; + HTML::Script* script { nullptr }; vm->running_execution_context().script_or_module.visit( [&script](WeakPtr& js_script) { script = verify_cast(js_script->host_defined()); @@ -67,19 +66,17 @@ JS::VM& main_thread_vm() [](Empty) { }); - // If there's no script, we're out of luck. Return. - // FIXME: This can happen from JS::NativeFunction, which makes its callee contexts [[ScriptOrModule]] null. - if (!script) { - dbgln("FIXME: Unable to process unhandled promise rejection in host_promise_rejection_tracker as the running script is null."); - return; + // 2. If script is a classic script and script's muted errors is true, then return. + // NOTE: is() returns false if nullptr is passed. + if (is(script)) { + auto const& classic_script = static_cast(*script); + if (classic_script.muted_errors() == HTML::ClassicScript::MutedErrors::Yes) + return; } - // 2. If script's muted errors is true, terminate these steps. - if (script->muted_errors() == HTML::ClassicScript::MutedErrors::Yes) - return; - - // 3. Let settings object be script's settings object. - auto& settings_object = script->settings_object(); + // 3. Let settings object be the current settings object. + // 4. If script is not null, then set settings object to script's settings object. + auto& settings_object = script ? script->settings_object() : HTML::current_settings_object(); switch (operation) { case JS::Promise::RejectionOperation::Reject: