From 3ec13fdb860fceb17d8e0ac8ecbe3d9cb734ddf4 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sat, 19 Nov 2022 16:08:16 +0000 Subject: [PATCH] WebContent: Rename $0 getter and use global object instead of `this` Using the global object works consistently in native accessors and native functions, so changing this for consistency. --- .../WebContent/ConsoleGlobalObject.cpp | 18 ++++++++++-------- .../Services/WebContent/ConsoleGlobalObject.h | 6 +++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.cpp b/Userland/Services/WebContent/ConsoleGlobalObject.cpp index 5a663a3583..9b76e42d44 100644 --- a/Userland/Services/WebContent/ConsoleGlobalObject.cpp +++ b/Userland/Services/WebContent/ConsoleGlobalObject.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -21,8 +21,7 @@ void ConsoleGlobalObject::initialize(JS::Realm& realm) { Base::initialize(realm); - // $0 magic variable - define_native_accessor(realm, "$0", inspected_node_getter, nullptr, 0); + define_native_accessor(realm, "$0", $0_getter, nullptr, 0); } void ConsoleGlobalObject::visit_edges(Visitor& visitor) @@ -92,14 +91,17 @@ JS::ThrowCompletionOr> ConsoleGlobalObject::internal return m_window_object->internal_own_property_keys(); } -JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::inspected_node_getter) +static JS::ThrowCompletionOr get_console(JS::VM& vm) { - auto* this_object = TRY(vm.this_value().to_object(vm)); - - if (!is(this_object)) + if (!is(vm.get_global_object())) return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "ConsoleGlobalObject"); - auto console_global_object = static_cast(this_object); + return static_cast(&vm.get_global_object()); +} + +JS_DEFINE_NATIVE_FUNCTION(ConsoleGlobalObject::$0_getter) +{ + auto* console_global_object = TRY(get_console(vm)); auto& window = *console_global_object->m_window_object; auto* inspected_node = window.associated_document().inspected_node(); if (!inspected_node) diff --git a/Userland/Services/WebContent/ConsoleGlobalObject.h b/Userland/Services/WebContent/ConsoleGlobalObject.h index 8a875ae5d4..1cb4758127 100644 --- a/Userland/Services/WebContent/ConsoleGlobalObject.h +++ b/Userland/Services/WebContent/ConsoleGlobalObject.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -36,8 +36,8 @@ public: private: virtual void visit_edges(Visitor&) override; - // Because $0 is not a nice C++ function name - JS_DECLARE_NATIVE_FUNCTION(inspected_node_getter); + // $0, the DOM node currently selected in the inspector + JS_DECLARE_NATIVE_FUNCTION($0_getter); Web::HTML::Window* m_window_object; };