LibJS: Move Console ownership from GlobalObject to ConsoleObject

GlobalObject is now a regular object with no special properties :^)
This commit is contained in:
Linus Groh 2022-08-28 14:20:06 +01:00
parent 78eca3ae64
commit 867ad03995
Notes: sideshowbarker 2024-07-17 10:39:39 +09:00
7 changed files with 53 additions and 31 deletions

View file

@ -14,6 +14,7 @@ namespace JS {
ConsoleObject::ConsoleObject(Realm& realm)
: Object(*realm.intrinsics().object_prototype())
, m_console(make<Console>(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();
}
}

View file

@ -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<Console> m_console;
};
}

View file

@ -11,7 +11,6 @@
#include <AK/UnicodeUtils.h>
#include <AK/Utf16View.h>
#include <AK/Utf8View.h>
#include <LibJS/Console.h>
#include <LibJS/Heap/DeferGC.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/AbstractOperations.h>
@ -85,7 +84,6 @@ namespace JS {
GlobalObject::GlobalObject(Realm& realm)
: Object(GlobalObjectTag::Tag, realm)
, m_console(make<Console>(realm.vm()))
{
}

View file

@ -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<Console> m_console;
};
inline GlobalObject* Shape::global_object() const

View file

@ -5,6 +5,7 @@
*/
#include <AK/String.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/WorkerWrapper.h>
@ -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;

View file

@ -15,6 +15,7 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Cookie/ParsedCookie.h>
#include <LibWeb/DOM/Document.h>
@ -389,9 +390,10 @@ void ConnectionFromClient::initialize_js_console(Badge<PageHost>)
if (m_interpreter.ptr() == interpreter.ptr())
return;
auto& console_object = *interpreter->realm().intrinsics().console_object();
m_interpreter = interpreter;
m_console_client = make<WebContentConsoleClient>(interpreter->realm().global_object().console(), interpreter, *this);
interpreter->realm().global_object().console().set_client(*m_console_client.ptr());
m_console_client = make<WebContentConsoleClient>(console_object.console(), interpreter, *this);
console_object.console().set_client(*m_console_client.ptr());
}
void ConnectionFromClient::js_console_input(String const& js_source)

View file

@ -27,6 +27,7 @@
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/AsyncGenerator.h>
#include <LibJS/Runtime/BooleanObject.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/DataView.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/DatePrototype.h>
@ -1559,8 +1560,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (evaluate_script.is_empty() && script_paths.is_empty()) {
s_print_last_result = true;
interpreter = JS::Interpreter::create<ReplObject>(*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<int> serenity_main(Main::Arguments arguments)
s_editor->save_history(s_history_path);
} else {
interpreter = JS::Interpreter::create<ScriptObject>(*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) {