From e4bda2e1e70efed986745431e132b6608586b991 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 29 Sep 2020 21:15:06 +0200 Subject: [PATCH] LibJS: Move Console from Interpreter to GlobalObject Each JS global object has its own "console", so it makes more sense to store it in GlobalObject. We'll need some smartness later to bundle up console messages from all the different frames that make up a page later, but this works for now. --- Applications/Browser/BrowserConsoleClient.cpp | 17 +++++++------ Applications/Browser/ConsoleWidget.cpp | 4 ++-- Libraries/LibJS/Console.cpp | 13 ++++++---- Libraries/LibJS/Console.h | 14 ++++++----- Libraries/LibJS/Forward.h | 1 + Libraries/LibJS/Interpreter.cpp | 1 - Libraries/LibJS/Interpreter.h | 6 ----- Libraries/LibJS/Runtime/ConsoleObject.cpp | 19 +++++++-------- Libraries/LibJS/Runtime/GlobalObject.cpp | 2 ++ Libraries/LibJS/Runtime/GlobalObject.h | 4 ++++ Userland/js.cpp | 24 +++++++++---------- 11 files changed, 55 insertions(+), 50 deletions(-) diff --git a/Applications/Browser/BrowserConsoleClient.cpp b/Applications/Browser/BrowserConsoleClient.cpp index 8380216122..10c54af22d 100644 --- a/Applications/Browser/BrowserConsoleClient.cpp +++ b/Applications/Browser/BrowserConsoleClient.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -41,7 +40,7 @@ namespace Browser { JS::Value BrowserConsoleClient::log() { - m_console_widget.print_html(interpreter().vm().join_arguments()); + m_console_widget.print_html(vm().join_arguments()); return JS::js_undefined(); } @@ -50,7 +49,7 @@ JS::Value BrowserConsoleClient::info() StringBuilder html; html.append(""); html.append("(i) "); - html.append(interpreter().vm().join_arguments()); + html.append(vm().join_arguments()); html.append(""); m_console_widget.print_html(html.string_view()); return JS::js_undefined(); @@ -61,7 +60,7 @@ JS::Value BrowserConsoleClient::debug() StringBuilder html; html.append(""); html.append("(d) "); - html.append(interpreter().vm().join_arguments()); + html.append(vm().join_arguments()); html.append(""); m_console_widget.print_html(html.string_view()); return JS::js_undefined(); @@ -72,7 +71,7 @@ JS::Value BrowserConsoleClient::warn() StringBuilder html; html.append(""); html.append("(w) "); - html.append(interpreter().vm().join_arguments()); + html.append(vm().join_arguments()); html.append(""); m_console_widget.print_html(html.string_view()); return JS::js_undefined(); @@ -83,7 +82,7 @@ JS::Value BrowserConsoleClient::error() StringBuilder html; html.append(""); html.append("(e) "); - html.append(interpreter().vm().join_arguments()); + html.append(vm().join_arguments()); html.append(""); m_console_widget.print_html(html.string_view()); return JS::js_undefined(); @@ -98,7 +97,7 @@ JS::Value BrowserConsoleClient::clear() JS::Value BrowserConsoleClient::trace() { StringBuilder html; - html.append(interpreter().vm().join_arguments()); + html.append(vm().join_arguments()); auto trace = get_trace(); for (auto& function_name : trace) { if (function_name.is_empty()) @@ -111,7 +110,7 @@ JS::Value BrowserConsoleClient::trace() JS::Value BrowserConsoleClient::count() { - auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default"; + auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default"; auto counter_value = m_console.counter_increment(label); m_console_widget.print_html(String::format("%s: %u", label.characters(), counter_value)); return JS::js_undefined(); @@ -119,7 +118,7 @@ JS::Value BrowserConsoleClient::count() JS::Value BrowserConsoleClient::count_reset() { - auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default"; + auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default"; if (m_console.counter_reset(label)) { m_console_widget.print_html(String::format("%s: 0", label.characters())); } else { diff --git a/Applications/Browser/ConsoleWidget.cpp b/Applications/Browser/ConsoleWidget.cpp index 5ef3e545a7..5e6b34cc90 100644 --- a/Applications/Browser/ConsoleWidget.cpp +++ b/Applications/Browser/ConsoleWidget.cpp @@ -129,8 +129,8 @@ void ConsoleWidget::set_interpreter(WeakPtr interpreter) return; m_interpreter = interpreter; - m_console_client = adopt_own(*new BrowserConsoleClient(interpreter->console(), *this)); - interpreter->console().set_client(*m_console_client.ptr()); + m_console_client = make(interpreter->global_object().console(), *this); + interpreter->global_object().console().set_client(*m_console_client.ptr()); clear_output(); } diff --git a/Libraries/LibJS/Console.cpp b/Libraries/LibJS/Console.cpp index eff5bec2e9..4bddbcdfab 100644 --- a/Libraries/LibJS/Console.cpp +++ b/Libraries/LibJS/Console.cpp @@ -26,12 +26,12 @@ */ #include -#include +#include namespace JS { -Console::Console(Interpreter& interpreter) - : m_interpreter(interpreter) +Console::Console(GlobalObject& global_object) + : m_global_object(global_object) { } @@ -120,10 +120,15 @@ bool Console::counter_reset(String label) return true; } +VM& ConsoleClient::vm() +{ + return global_object().vm(); +} + Vector ConsoleClient::get_trace() const { Vector trace; - auto& call_stack = m_console.interpreter().vm().call_stack(); + auto& call_stack = m_console.global_object().vm().call_stack(); // -2 to skip the console.trace() call frame for (ssize_t i = call_stack.size() - 2; i >= 0; --i) trace.append(call_stack[i].function_name); diff --git a/Libraries/LibJS/Console.h b/Libraries/LibJS/Console.h index 7465f1c32d..d8d96be85c 100644 --- a/Libraries/LibJS/Console.h +++ b/Libraries/LibJS/Console.h @@ -40,12 +40,12 @@ class Console { AK_MAKE_NONMOVABLE(Console); public: - Console(Interpreter&); + explicit Console(GlobalObject&); void set_client(ConsoleClient& client) { m_client = &client; } - Interpreter& interpreter() { return m_interpreter; } - const Interpreter& interpreter() const { return m_interpreter; } + GlobalObject& global_object() { return m_global_object; } + const GlobalObject& global_object() const { return m_global_object; } HashMap& counters() { return m_counters; } const HashMap& counters() const { return m_counters; } @@ -67,7 +67,7 @@ public: bool counter_reset(String label); private: - Interpreter& m_interpreter; + GlobalObject& m_global_object; ConsoleClient* m_client { nullptr }; HashMap m_counters; @@ -91,8 +91,10 @@ public: virtual Value count_reset() = 0; protected: - Interpreter& interpreter() { return m_console.interpreter(); } - const Interpreter& interpreter() const { return m_console.interpreter(); } + VM& vm(); + + GlobalObject& global_object() { return m_console.global_object(); } + const GlobalObject& global_object() const { return m_console.global_object(); } Vector get_trace() const; diff --git a/Libraries/LibJS/Forward.h b/Libraries/LibJS/Forward.h index 9426c0f9d9..8d02d77d6b 100644 --- a/Libraries/LibJS/Forward.h +++ b/Libraries/LibJS/Forward.h @@ -98,6 +98,7 @@ class ASTNode; class BigInt; class BoundFunction; class Cell; +class Console; class DeferGC; class Error; class Exception; diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 119919acc0..2f4e80be37 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -54,7 +54,6 @@ NonnullOwnPtr Interpreter::create_with_existing_global_object(Globa Interpreter::Interpreter(VM& vm) : m_vm(vm) - , m_console(*this) { } diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index e8e1c93b31..afdc9a667a 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -87,9 +86,6 @@ public: Heap& heap() { return vm().heap(); } Exception* exception() { return vm().exception(); } - Console& console() { return m_console; } - const Console& console() const { return m_console; } - bool in_strict_mode() const { // FIXME: This implementation is bogus; strict mode is per-file or per-function, not per-block! @@ -121,8 +117,6 @@ private: Handle m_global_object; - Console m_console; - Vector m_scope_stack; }; diff --git a/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Libraries/LibJS/Runtime/ConsoleObject.cpp index 3543d32827..49cf76491c 100644 --- a/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include @@ -60,47 +59,47 @@ ConsoleObject::~ConsoleObject() JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::log) { - return vm.interpreter().console().log(); + return global_object.console().log(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::debug) { - return vm.interpreter().console().debug(); + return global_object.console().debug(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::info) { - return vm.interpreter().console().info(); + return global_object.console().info(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn) { - return vm.interpreter().console().warn(); + return global_object.console().warn(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::error) { - return vm.interpreter().console().error(); + return global_object.console().error(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::trace) { - return vm.interpreter().console().trace(); + return global_object.console().trace(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count) { - return vm.interpreter().console().count(); + return global_object.console().count(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count_reset) { - return vm.interpreter().console().count_reset(); + return global_object.console().count_reset(); } JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::clear) { - return vm.interpreter().console().clear(); + return global_object.console().clear(); } } diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 56f2f6ca34..db8bda0962 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -26,6 +26,7 @@ */ #include +#include #include #include #include @@ -68,6 +69,7 @@ namespace JS { GlobalObject::GlobalObject() : Object(GlobalObjectTag::Tag) + , m_console(make(*this)) { } diff --git a/Libraries/LibJS/Runtime/GlobalObject.h b/Libraries/LibJS/Runtime/GlobalObject.h index 2c256afc78..7c0c8892e0 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Libraries/LibJS/Runtime/GlobalObject.h @@ -41,6 +41,8 @@ public: virtual ~GlobalObject() override; + Console& console() { return *m_console; } + Shape* empty_object_shape() { return m_empty_object_shape; } #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ @@ -66,6 +68,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(is_finite); JS_DECLARE_NATIVE_FUNCTION(parse_float); + NonnullOwnPtr m_console; + Shape* m_empty_object_shape { nullptr }; #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ diff --git a/Userland/js.cpp b/Userland/js.cpp index c455ead699..97605d10f1 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -470,32 +470,32 @@ public: virtual JS::Value log() override { - puts(interpreter().vm().join_arguments().characters()); + puts(vm().join_arguments().characters()); return JS::js_undefined(); } virtual JS::Value info() override { - printf("(i) %s\n", interpreter().vm().join_arguments().characters()); + printf("(i) %s\n", vm().join_arguments().characters()); return JS::js_undefined(); } virtual JS::Value debug() override { printf("\033[36;1m"); - puts(interpreter().vm().join_arguments().characters()); + puts(vm().join_arguments().characters()); printf("\033[0m"); return JS::js_undefined(); } virtual JS::Value warn() override { printf("\033[33;1m"); - puts(interpreter().vm().join_arguments().characters()); + puts(vm().join_arguments().characters()); printf("\033[0m"); return JS::js_undefined(); } virtual JS::Value error() override { printf("\033[31;1m"); - puts(interpreter().vm().join_arguments().characters()); + puts(vm().join_arguments().characters()); printf("\033[0m"); return JS::js_undefined(); } @@ -507,7 +507,7 @@ public: } virtual JS::Value trace() override { - puts(interpreter().vm().join_arguments().characters()); + puts(vm().join_arguments().characters()); auto trace = get_trace(); for (auto& function_name : trace) { if (function_name.is_empty()) @@ -518,14 +518,14 @@ public: } virtual JS::Value count() override { - auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default"; + auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default"; auto counter_value = m_console.counter_increment(label); printf("%s: %u\n", label.characters(), counter_value); return JS::js_undefined(); } virtual JS::Value count_reset() override { - auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default"; + auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default"; if (m_console.counter_reset(label)) { printf("%s: 0\n", label.characters()); } else { @@ -564,8 +564,8 @@ int main(int argc, char** argv) if (script_path == nullptr) { s_print_last_result = true; interpreter = JS::Interpreter::create(*vm); - ReplConsoleClient console_client(interpreter->console()); - interpreter->console().set_client(console_client); + ReplConsoleClient console_client(interpreter->global_object().console()); + interpreter->global_object().console().set_client(console_client); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); interpreter->vm().set_underscore_is_last_value(true); @@ -846,8 +846,8 @@ int main(int argc, char** argv) repl(*interpreter); } else { interpreter = JS::Interpreter::create(*vm); - ReplConsoleClient console_client(interpreter->console()); - interpreter->console().set_client(console_client); + ReplConsoleClient console_client(interpreter->global_object().console()); + interpreter->global_object().console().set_client(console_client); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); signal(SIGINT, [](int) {