diff --git a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp index 184b66b0a02..f3cb46dea81 100644 --- a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -14,6 +14,7 @@ namespace JS { ConsoleObject::ConsoleObject(Realm& realm) : Object(*realm.intrinsics().object_prototype()) + , m_console(make(realm.vm())) { } @@ -43,97 +44,113 @@ void ConsoleObject::initialize(Realm& realm) // 1.1.6. log(...data), https://console.spec.whatwg.org/#log JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::log) { - return vm.current_realm()->global_object().console().log(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().log(); } // 1.1.3. debug(...data), https://console.spec.whatwg.org/#debug JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::debug) { - return vm.current_realm()->global_object().console().debug(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().debug(); } // 1.1.5. info(...data), https://console.spec.whatwg.org/#info JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::info) { - return vm.current_realm()->global_object().console().info(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().info(); } // 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn) { - return vm.current_realm()->global_object().console().warn(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().warn(); } // 1.1.4. error(...data), https://console.spec.whatwg.org/#error JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::error) { - return vm.current_realm()->global_object().console().error(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().error(); } // 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::trace) { - return vm.current_realm()->global_object().console().trace(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().trace(); } // 1.2.1. count(label), https://console.spec.whatwg.org/#count JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count) { - return vm.current_realm()->global_object().console().count(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().count(); } // 1.2.2. countReset(label), https://console.spec.whatwg.org/#countreset JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count_reset) { - return vm.current_realm()->global_object().console().count_reset(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().count_reset(); } // 1.1.2. clear(), https://console.spec.whatwg.org/#clear JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::clear) { - return vm.current_realm()->global_object().console().clear(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().clear(); } // 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::assert_) { - return vm.current_realm()->global_object().console().assert_(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().assert_(); } // 1.3.1. group(...data), https://console.spec.whatwg.org/#group JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::group) { - return vm.current_realm()->global_object().console().group(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().group(); } // 1.3.2. groupCollapsed(...data), https://console.spec.whatwg.org/#groupcollapsed JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::group_collapsed) { - return vm.current_realm()->global_object().console().group_collapsed(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().group_collapsed(); } // 1.3.3. groupEnd(), https://console.spec.whatwg.org/#groupend JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::group_end) { - return vm.current_realm()->global_object().console().group_end(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().group_end(); } // 1.4.1. time(label), https://console.spec.whatwg.org/#time JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::time) { - return vm.current_realm()->global_object().console().time(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().time(); } // 1.4.2. timeLog(label, ...data), https://console.spec.whatwg.org/#timelog JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::time_log) { - return vm.current_realm()->global_object().console().time_log(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().time_log(); } // 1.4.3. timeEnd(label), https://console.spec.whatwg.org/#timeend JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::time_end) { - return vm.current_realm()->global_object().console().time_end(); + auto& console_object = *vm.current_realm()->intrinsics().console_object(); + return console_object.console().time_end(); } } diff --git a/Userland/Libraries/LibJS/Runtime/ConsoleObject.h b/Userland/Libraries/LibJS/Runtime/ConsoleObject.h index 438896b9c00..405f02bf568 100644 --- a/Userland/Libraries/LibJS/Runtime/ConsoleObject.h +++ b/Userland/Libraries/LibJS/Runtime/ConsoleObject.h @@ -18,6 +18,8 @@ public: virtual void initialize(Realm&) override; virtual ~ConsoleObject() override = default; + Console& console() { return *m_console; } + private: JS_DECLARE_NATIVE_FUNCTION(log); JS_DECLARE_NATIVE_FUNCTION(debug); @@ -35,6 +37,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(time); JS_DECLARE_NATIVE_FUNCTION(time_log); JS_DECLARE_NATIVE_FUNCTION(time_end); + + NonnullOwnPtr m_console; }; } diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index 30ff62a45b9..3464b6b34a2 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -85,7 +84,6 @@ namespace JS { GlobalObject::GlobalObject(Realm& realm) : Object(GlobalObjectTag::Tag, realm) - , m_console(make(realm.vm())) { } diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.h b/Userland/Libraries/LibJS/Runtime/GlobalObject.h index 49496bae8e8..e1704b9c70e 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.h @@ -22,8 +22,6 @@ public: virtual ~GlobalObject() override; - Console& console() { return *m_console; } - private: virtual bool is_global_object() const final { return true; } @@ -39,8 +37,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(decode_uri_component); JS_DECLARE_NATIVE_FUNCTION(escape); JS_DECLARE_NATIVE_FUNCTION(unescape); - - NonnullOwnPtr m_console; }; inline GlobalObject* Shape::global_object() const diff --git a/Userland/Libraries/LibWeb/HTML/Worker.cpp b/Userland/Libraries/LibWeb/HTML/Worker.cpp index 387a0c78417..b84aba47cf8 100644 --- a/Userland/Libraries/LibWeb/HTML/Worker.cpp +++ b/Userland/Libraries/LibWeb/HTML/Worker.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -114,10 +115,11 @@ void Worker::run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_setti return m_worker_scope; }, nullptr); - m_worker_realm = realm_execution_context->realm; - m_console = adopt_ref(*new WorkerDebugConsoleClient(m_worker_scope->console())); - m_worker_scope->console().set_client(*m_console); + auto& console_object = *realm_execution_context->realm->intrinsics().console_object(); + m_worker_realm = realm_execution_context->realm; + m_console = adopt_ref(*new WorkerDebugConsoleClient(console_object.console())); + console_object.console().set_client(*m_console); // FIXME: This should be done with IDL u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable; diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 90bb582e09e..901f56bd33e 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -389,9 +390,10 @@ void ConnectionFromClient::initialize_js_console(Badge) if (m_interpreter.ptr() == interpreter.ptr()) return; + auto& console_object = *interpreter->realm().intrinsics().console_object(); m_interpreter = interpreter; - m_console_client = make(interpreter->realm().global_object().console(), interpreter, *this); - interpreter->realm().global_object().console().set_client(*m_console_client.ptr()); + m_console_client = make(console_object.console(), interpreter, *this); + console_object.console().set_client(*m_console_client.ptr()); } void ConnectionFromClient::js_console_input(String const& js_source) diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 4d51a76b7c8..39ee42c0eaf 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -1559,8 +1560,9 @@ ErrorOr serenity_main(Main::Arguments arguments) if (evaluate_script.is_empty() && script_paths.is_empty()) { s_print_last_result = true; interpreter = JS::Interpreter::create(*g_vm); - ReplConsoleClient console_client(interpreter->realm().global_object().console()); - interpreter->realm().global_object().console().set_client(console_client); + auto& console_object = *interpreter->realm().intrinsics().console_object(); + ReplConsoleClient console_client(console_object.console()); + console_object.console().set_client(console_client); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); auto& global_environment = interpreter->realm().global_environment(); @@ -1769,8 +1771,9 @@ ErrorOr serenity_main(Main::Arguments arguments) s_editor->save_history(s_history_path); } else { interpreter = JS::Interpreter::create(*g_vm); - ReplConsoleClient console_client(interpreter->realm().global_object().console()); - interpreter->realm().global_object().console().set_client(console_client); + auto& console_object = *interpreter->realm().intrinsics().console_object(); + ReplConsoleClient console_client(console_object.console()); + console_object.console().set_client(console_client); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); signal(SIGINT, [](int) {