From 01c87655197fd25362a8eebb40dd1365d91f865b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 29 Nov 2020 16:48:14 +0100 Subject: [PATCH] LibJS+LibWeb: Log JavaScript exceptions raised by web content Instead of hiding JS exceptions raised on the web, we now print them to the debug log. This will make it a bit easier to work out why some web pages aren't working right. :^) --- Libraries/LibJS/Runtime/VM.cpp | 5 ++--- Libraries/LibJS/Runtime/VM.h | 5 +++++ Libraries/LibWeb/DOM/Document.cpp | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp index 953aae1f519..21da1984033 100644 --- a/Libraries/LibJS/Runtime/VM.cpp +++ b/Libraries/LibJS/Runtime/VM.cpp @@ -264,8 +264,7 @@ Value VM::construct(Function& function, Function& new_target, Optionalvalue().is_object() && exception->value().as_object().is_error()) { + if (should_log_exceptions() && exception->value().is_object() && exception->value().as_object().is_error()) { auto& error = static_cast(exception->value().as_object()); dbgln("Throwing JavaScript Error: {}, {}", error.name(), error.message()); @@ -276,7 +275,7 @@ void VM::throw_exception(Exception* exception) dbgln(" {}", function_name); } } -#endif + m_exception = exception; unwind(ScopeType::Try); } diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.h index b2fc9b9ac75..5d20a77f87a 100644 --- a/Libraries/LibJS/Runtime/VM.h +++ b/Libraries/LibJS/Runtime/VM.h @@ -75,6 +75,9 @@ public: static NonnullRefPtr create(); ~VM(); + bool should_log_exceptions() const { return m_should_log_exceptions; } + void set_should_log_exceptions(bool b) { m_should_log_exceptions = b; } + Heap& heap() { return m_heap; } const Heap& heap() const { return m_heap; } @@ -275,6 +278,8 @@ private: #undef __JS_ENUMERATE Shape* m_scope_object_shape { nullptr }; + + bool m_should_log_exceptions { false }; }; template<> diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 3f13b3f6632..8016a402256 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -488,8 +488,10 @@ Color Document::visited_link_color() const static JS::VM& main_thread_vm() { static RefPtr vm; - if (!vm) + if (!vm) { vm = JS::VM::create(); + vm->set_should_log_exceptions(true); + } return *vm; }