1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:47:35 +00:00

LibJS+Everywhere: Remove VM::exception() and most related functions

This commit removes all exception related code:
Remove VM::exception(), VM::throw_exception() etc. Any leftover
throw_exception calls are moved to throw_completion.
The one method left is clear_exception() which is now a no-op. Most of
these calls are just to clear whatever exception might have been thrown
when handling a Completion. So to have a cleaner commit this will be
removed in a next commit.

It also removes the actual Exception and TemporaryClearException classes
since these are no longer used.

In any spot where the exception was actually used an attempt was made to
preserve that behavior. However since it is no longer tracked by the VM
we cannot access exceptions which were thrown in previous calls.
There are two such cases which might have different behavior:
- In Web::DOM::Document::interpreter() the on_call_stack_emptied hook
  used to print any uncaught exception but this is now no longer
  possible as the VM does not store uncaught exceptions.
- In js the code used to be interruptable by throwing an exception on
  the VM. This is no longer possible but was already somewhat fragile
  before as you could happen to throw an exception just before a VERIFY.
This commit is contained in:
davidot 2022-02-07 15:12:41 +01:00 committed by Linus Groh
parent 4ef1e8f226
commit 9264f9d24e
35 changed files with 145 additions and 349 deletions

View file

@ -194,6 +194,7 @@ set(SOURCES
HTML/Parser/ListOfActiveFormattingElements.cpp
HTML/Parser/StackOfOpenElements.cpp
HTML/Scripting/ClassicScript.cpp
HTML/Scripting/ExceptionReporter.cpp
HTML/Scripting/Script.cpp
HTML/SyntaxHighlighter/SyntaxHighlighter.cpp
HTML/TagNames.cpp

View file

@ -76,16 +76,10 @@ HTML::EventHandler AbortSignal::onabort()
// https://dom.spec.whatwg.org/#dom-abortsignal-throwifaborted
JS::ThrowCompletionOr<void> AbortSignal::throw_if_aborted() const
{
auto& global_object = wrapper()->global_object();
auto& vm = global_object.vm();
// The throwIfAborted() method steps are to throw thiss abort reason, if this is aborted.
if (!aborted())
return {};
// FIXME: Remove this once VM::exception() has been removed.
vm.throw_exception(global_object, m_abort_reason);
return JS::throw_completion(m_abort_reason);
}

View file

@ -53,6 +53,7 @@
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/HTML/HTMLTitleElement.h>
#include <LibWeb/HTML/MessageEvent.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/Layout/BlockFormattingContext.h>
#include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Layout/TreeBuilder.h>
@ -641,32 +642,6 @@ JS::Interpreter& Document::interpreter()
// FIXME: This isn't exactly the right place for this.
HTML::main_thread_event_loop().perform_a_microtask_checkpoint();
// Note: This is not an exception check for the promise jobs, they will just leave any
// exception that already exists intact and never throw a new one (without cleaning it
// up, that is). Taking care of any previous unhandled exception just happens to be the
// very last thing we want to do, even after running promise jobs.
if (auto* exception = vm.exception()) {
auto value = exception->value();
if (value.is_object()) {
auto& object = value.as_object();
auto name = object.get_without_side_effects(vm.names.name).value_or(JS::js_undefined());
auto message = object.get_without_side_effects(vm.names.message).value_or(JS::js_undefined());
if (name.is_accessor() || message.is_accessor()) {
// The result is not going to be useful, let's just print the value. This affects DOMExceptions, for example.
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", value);
} else {
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m [{}] {}", name, message);
}
} else {
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", value);
}
for (auto& traceback_frame : exception->traceback()) {
auto& function_name = traceback_frame.function_name;
auto& source_range = traceback_frame.source_range;
dbgln(" {} at {}:{}:{}", function_name, source_range.filename, source_range.start.line, source_range.start.column);
}
}
vm.finish_execution_generation();
};
}
@ -685,10 +660,10 @@ JS::Value Document::run_javascript(StringView source, StringView filename)
auto result = interpreter.run(script_or_error.value());
auto& vm = interpreter.vm();
if (result.is_error()) {
// FIXME: I'm sure the spec could tell us something about error propagation here!
vm.clear_exception();
HTML::report_exception(result);
return {};
}
return result.value();

View file

@ -23,6 +23,7 @@
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/UIEvents/MouseEvent.h>
namespace Web::DOM {
@ -89,11 +90,17 @@ bool EventDispatcher::inner_invoke(Event& event, Vector<EventTarget::EventListen
auto* this_value = Bindings::wrap(global, *event.current_target());
auto* wrapped_event = Bindings::wrap(global, event);
auto& vm = global.vm();
[[maybe_unused]] auto rc = JS::call(global, function, this_value, wrapped_event);
if (vm.exception()) {
vm.clear_exception();
// FIXME: Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently)
// 10. Call a user objects operation with listeners callback, "handleEvent", « event », and events currentTarget attribute value.
auto result = JS::call(global, function, this_value, wrapped_event);
// If this throws an exception, then:
if (result.is_error()) {
// 1. Report the exception.
VERIFY(result.throw_completion().value().has_value());
HTML::report_exception(*result.throw_completion().value());
// FIXME: 2. Set legacyOutputDidListenersThrowFlag if given. (Only used by IndexedDB currently)
}
event.set_in_passive_listener(false);

View file

@ -19,6 +19,7 @@
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/PageTransitionEvent.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HighResolutionTime/Performance.h>
#include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Page/Page.h>
@ -170,11 +171,9 @@ void Window::timer_did_fire(Badge<Timer>, Timer& timer)
HTML::queue_global_task(HTML::Task::Source::TimerTask, associated_document(), [this, strong_this = NonnullRefPtr(*this), strong_timer = NonnullRefPtr(timer)]() mutable {
// We should not be here if there's no JS wrapper for the Window object.
VERIFY(wrapper());
auto& vm = wrapper()->vm();
[[maybe_unused]] auto rc = JS::call(wrapper()->global_object(), strong_timer->callback(), wrapper());
if (vm.exception())
vm.clear_exception();
auto result = JS::call(wrapper()->global_object(), strong_timer->callback(), wrapper());
if (result.is_error())
HTML::report_exception(result);
});
}
@ -198,14 +197,18 @@ void Window::clear_interval(i32 timer_id)
m_timers.remove(timer_id);
}
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#run-the-animation-frame-callbacks
i32 Window::request_animation_frame(JS::FunctionObject& js_callback)
{
auto callback = request_animation_frame_driver().add([this, handle = JS::make_handle(&js_callback)](i32 id) mutable {
auto& function = *handle.cell();
auto& vm = function.vm();
(void)JS::call(function.global_object(), function, JS::js_undefined(), JS::Value(performance().now()));
if (vm.exception())
vm.clear_exception();
// 3. Invoke callback, passing now as the only argument,
auto result = JS::call(function.global_object(), function, JS::js_undefined(), JS::Value(performance().now()));
// and if an exception is thrown, report the exception.
if (result.is_error())
HTML::report_exception(result);
m_request_animation_frame_callbacks.remove(id);
});
m_request_animation_frame_callbacks.set(callback->id(), callback);
@ -399,11 +402,10 @@ void Window::queue_microtask(JS::FunctionObject& callback)
{
// The queueMicrotask(callback) method must queue a microtask to invoke callback,
HTML::queue_a_microtask(associated_document(), [&callback, handle = JS::make_handle(&callback)]() {
auto& vm = callback.vm();
[[maybe_unused]] auto rc = JS::call(callback.global_object(), callback, JS::js_null());
// FIXME: ...and if callback throws an exception, report the exception.
if (vm.exception())
vm.clear_exception();
auto result = JS::call(callback.global_object(), callback, JS::js_null());
// and if callback throws an exception, report the exception.
if (result.is_error())
HTML::report_exception(result);
});
}

View file

@ -8,6 +8,7 @@
#include <LibCore/ElapsedTimer.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
namespace Web::HTML {
@ -81,7 +82,7 @@ JS::Value ClassicScript::run(RethrowErrors rethrow_errors)
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
if (evaluation_status.is_error()) {
// FIXME: Propagate error according to the spec.
interpreter->vm().clear_exception();
report_exception(evaluation_status);
return {};
}
return evaluation_status.value();

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/#report-the-exception
void report_exception(JS::ThrowCompletionOr<JS::Value> const& result)
{
// FIXME: This is just old code, and does not strictly follow the spec of report an exception.
// FIXME: We should probably also report these exceptions to the JS console.
VERIFY(result.throw_completion().value().has_value());
auto thrown_value = *result.throw_completion().value();
if (thrown_value.is_object()) {
auto& object = thrown_value.as_object();
auto& vm = object.vm();
auto name = object.get_without_side_effects(vm.names.name).value_or(JS::js_undefined());
auto message = object.get_without_side_effects(vm.names.message).value_or(JS::js_undefined());
if (name.is_accessor() || message.is_accessor()) {
// The result is not going to be useful, let's just print the value. This affects DOMExceptions, for example.
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", thrown_value);
} else {
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m [{}] {}", name, message);
}
if (is<JS::Error>(object)) {
auto const& error_value = static_cast<JS::Error const&>(object);
for (auto const& traceback_frame : error_value.traceback()) {
auto const& function_name = traceback_frame.function_name;
auto const& source_range = traceback_frame.source_range;
dbgln(" {} at {}:{}:{}", function_name, source_range.filename, source_range.start.line, source_range.start.column);
}
}
} else {
dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", thrown_value);
}
}
}

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Forward.h>
namespace Web::HTML {
void report_exception(JS::ThrowCompletionOr<JS::Value> const& value);
}