From 69dd158f91480551a4b7e1205c3fab77c507fc1a Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 21 Sep 2022 13:59:08 +0100 Subject: LibJS: Create Console using a Realm instead of a VM --- Userland/Libraries/LibJS/Console.cpp | 34 ++++++++++++---------- Userland/Libraries/LibJS/Console.h | 6 ++-- Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) (limited to 'Userland/Libraries/LibJS') diff --git a/Userland/Libraries/LibJS/Console.cpp b/Userland/Libraries/LibJS/Console.cpp index e7270d5681..b8bdd161e7 100644 --- a/Userland/Libraries/LibJS/Console.cpp +++ b/Userland/Libraries/LibJS/Console.cpp @@ -13,8 +13,8 @@ namespace JS { -Console::Console(VM& vm) - : m_vm(vm) +Console::Console(Realm& realm) + : m_realm(realm) { } @@ -91,9 +91,11 @@ ThrowCompletionOr Console::trace() if (!m_client) return js_undefined(); + auto& vm = realm().vm(); + // 1. Let trace be some implementation-specific, potentially-interactive representation of the callstack from where this function was called. Console::Trace trace; - auto& execution_context_stack = vm().execution_context_stack(); + auto& execution_context_stack = vm.execution_context_stack(); // NOTE: -2 to skip the console.trace() execution context for (ssize_t i = execution_context_stack.size() - 2; i >= 0; --i) { auto& function_name = execution_context_stack[i]->function_name; @@ -101,7 +103,7 @@ ThrowCompletionOr Console::trace() } // 2. Optionally, let formattedData be the result of Formatter(data), and incorporate formattedData as a label for trace. - if (vm().argument_count() > 0) { + if (vm.argument_count() > 0) { StringBuilder builder; auto data = vm_arguments(); auto formatted_data = TRY(m_client->formatter(data)); @@ -115,7 +117,7 @@ ThrowCompletionOr Console::trace() // 1.2.1. count(label), https://console.spec.whatwg.org/#count ThrowCompletionOr Console::count() { - auto& vm = this->vm(); + auto& vm = realm().vm(); // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-count auto label = vm.argument_count() ? TRY(vm.argument(0).to_string(vm)) : "default"; @@ -146,7 +148,7 @@ ThrowCompletionOr Console::count() // 1.2.2. countReset(label), https://console.spec.whatwg.org/#countreset ThrowCompletionOr Console::count_reset() { - auto& vm = this->vm(); + auto& vm = realm().vm(); // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-countreset auto label = vm.argument_count() ? TRY(vm.argument(0).to_string(vm)) : "default"; @@ -176,7 +178,7 @@ ThrowCompletionOr Console::count_reset() // 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert ThrowCompletionOr Console::assert_() { - auto& vm = this->vm(); + auto& vm = realm().vm(); // 1. If condition is true, return. auto condition = vm.argument(0).to_boolean(); @@ -307,7 +309,7 @@ ThrowCompletionOr Console::group_end() // 1.4.1. time(label), https://console.spec.whatwg.org/#time ThrowCompletionOr Console::time() { - auto& vm = this->vm(); + auto& vm = realm().vm(); // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-time auto label = vm.argument_count() ? TRY(vm.argument(0).to_string(vm)) : "default"; @@ -331,7 +333,7 @@ ThrowCompletionOr Console::time() // 1.4.2. timeLog(label, ...data), https://console.spec.whatwg.org/#timelog ThrowCompletionOr Console::time_log() { - auto& vm = this->vm(); + auto& vm = realm().vm(); // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-timelog auto label = vm.argument_count() ? TRY(vm.argument(0).to_string(vm)) : "default"; @@ -374,7 +376,7 @@ ThrowCompletionOr Console::time_log() // 1.4.3. timeEnd(label), https://console.spec.whatwg.org/#timeend ThrowCompletionOr Console::time_end() { - auto& vm = this->vm(); + auto& vm = realm().vm(); // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-timeend auto label = vm.argument_count() ? TRY(vm.argument(0).to_string(vm)) : "default"; @@ -415,7 +417,7 @@ ThrowCompletionOr Console::time_end() MarkedVector Console::vm_arguments() { - auto& vm = this->vm(); + auto& vm = realm().vm(); MarkedVector arguments { vm.heap() }; arguments.ensure_capacity(vm.argument_count()); @@ -453,7 +455,7 @@ void Console::output_debug_message([[maybe_unused]] LogLevel log_level, [[maybe_ ThrowCompletionOr Console::value_vector_to_string(MarkedVector const& values) { - auto& vm = this->vm(); + auto& vm = realm().vm(); StringBuilder builder; for (auto const& item : values) { if (!builder.is_empty()) @@ -465,7 +467,7 @@ ThrowCompletionOr Console::value_vector_to_string(MarkedVector co ThrowCompletionOr Console::format_time_since(Core::ElapsedTimer timer) { - auto& vm = this->vm(); + auto& vm = realm().vm(); auto elapsed_ms = timer.elapsed_time().to_milliseconds(); auto duration = TRY(Temporal::balance_duration(vm, 0, 0, 0, 0, elapsed_ms, 0, "0"_sbigint, "year")); @@ -493,7 +495,7 @@ ThrowCompletionOr Console::format_time_since(Core::ElapsedTimer timer) // 2.1. Logger(logLevel, args), https://console.spec.whatwg.org/#logger ThrowCompletionOr ConsoleClient::logger(Console::LogLevel log_level, MarkedVector const& args) { - auto& vm = m_console.vm(); + auto& vm = m_console.realm().vm(); // 1. If args is empty, return. if (args.is_empty()) @@ -525,8 +527,8 @@ ThrowCompletionOr ConsoleClient::logger(Console::LogLevel log_level, Mark // 2.2. Formatter(args), https://console.spec.whatwg.org/#formatter ThrowCompletionOr> ConsoleClient::formatter(MarkedVector const& args) { - auto& vm = m_console.vm(); - auto& realm = *vm.current_realm(); + auto& realm = m_console.realm(); + auto& vm = realm.vm(); // 1. If args’s size is 1, return args. if (args.size() == 1) diff --git a/Userland/Libraries/LibJS/Console.h b/Userland/Libraries/LibJS/Console.h index fcb20f043b..86dc68c60e 100644 --- a/Userland/Libraries/LibJS/Console.h +++ b/Userland/Libraries/LibJS/Console.h @@ -53,11 +53,11 @@ public: Vector stack; }; - explicit Console(VM&); + explicit Console(Realm&); void set_client(ConsoleClient& client) { m_client = &client; } - VM& vm() const { return m_vm; } + Realm& realm() const { return m_realm; } MarkedVector vm_arguments(); @@ -87,7 +87,7 @@ private: ThrowCompletionOr value_vector_to_string(MarkedVector const&); ThrowCompletionOr format_time_since(Core::ElapsedTimer timer); - VM& m_vm; + Realm& m_realm; ConsoleClient* m_client { nullptr }; HashMap m_counters; diff --git a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp index f3cb46dea8..1d17f2a7eb 100644 --- a/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -14,7 +14,7 @@ namespace JS { ConsoleObject::ConsoleObject(Realm& realm) : Object(*realm.intrinsics().object_prototype()) - , m_console(make(realm.vm())) + , m_console(make(realm)) { } -- cgit v1.2.3