From fc04465fa3b772298aaa10f7fe4fa0e7ef6eff88 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 31 Jan 2022 13:00:02 +0100 Subject: [PATCH] LibJS: Remove unnecessary GlobalObject pointer from Environment As it turns out, we didn't actually need this pointer. :^) --- Userland/Libraries/LibJS/Bytecode/Op.cpp | 2 +- .../Libraries/LibJS/Runtime/AbstractOperations.cpp | 13 ++++++------- Userland/Libraries/LibJS/Runtime/Environment.cpp | 10 +--------- Userland/Libraries/LibJS/Runtime/Environment.h | 8 +------- .../Libraries/LibJS/Runtime/GlobalEnvironment.cpp | 10 +++++----- Userland/Libraries/LibJS/Runtime/Realm.cpp | 2 +- Userland/Libraries/LibJS/SourceTextModule.cpp | 2 +- Userland/Libraries/LibJS/SyntheticModule.cpp | 2 +- 8 files changed, 17 insertions(+), 32 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index aeb51288d4..eba4fe1790 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -511,7 +511,7 @@ void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, Basi void PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const { - auto* environment = interpreter.vm().heap().allocate(interpreter.global_object(), interpreter.vm().lexical_environment()); + auto* environment = interpreter.vm().heap().allocate_without_global_object(interpreter.vm().lexical_environment()); interpreter.vm().running_execution_context().lexical_environment = environment; interpreter.vm().running_execution_context().variable_environment = environment; } diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 569fe838bb..cc11b874fe 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -419,24 +419,23 @@ ThrowCompletionOr get_prototype_from_constructor(GlobalObject& global_o // 9.1.2.2 NewDeclarativeEnvironment ( E ), https://tc39.es/ecma262/#sec-newdeclarativeenvironment DeclarativeEnvironment* new_declarative_environment(Environment& environment) { - auto& global_object = environment.global_object(); - return global_object.heap().allocate(global_object, &environment); + return environment.heap().allocate_without_global_object(&environment); } // 9.1.2.3 NewObjectEnvironment ( O, W, E ), https://tc39.es/ecma262/#sec-newobjectenvironment ObjectEnvironment* new_object_environment(Object& object, bool is_with_environment, Environment* environment) { - auto& global_object = object.global_object(); - return global_object.heap().allocate(global_object, object, is_with_environment ? ObjectEnvironment::IsWithEnvironment::Yes : ObjectEnvironment::IsWithEnvironment::No, environment); + auto& heap = object.heap(); + return heap.allocate_without_global_object(object, is_with_environment ? ObjectEnvironment::IsWithEnvironment::Yes : ObjectEnvironment::IsWithEnvironment::No, environment); } // 9.1.2.4 NewFunctionEnvironment ( F, newTarget ), https://tc39.es/ecma262/#sec-newfunctionenvironment FunctionEnvironment* new_function_environment(ECMAScriptFunctionObject& function, Object* new_target) { - auto& global_object = function.global_object(); + auto& heap = function.heap(); // 1. Let env be a new function Environment Record containing no bindings. - auto* env = global_object.heap().allocate(global_object, function.environment()); + auto* env = heap.allocate_without_global_object(function.environment()); // 2. Set env.[[FunctionObject]] to F. env->set_function_object(function); @@ -463,7 +462,7 @@ PrivateEnvironment* new_private_environment(VM& vm, PrivateEnvironment* outer) { // 1. Let names be a new empty List. // 2. Return the PrivateEnvironment Record { [[OuterPrivateEnvironment]]: outerPrivEnv, [[Names]]: names }. - return vm.heap().allocate(vm.current_realm()->global_object(), outer); + return vm.heap().allocate_without_global_object(outer); } // 9.4.3 GetThisEnvironment ( ), https://tc39.es/ecma262/#sec-getthisenvironment diff --git a/Userland/Libraries/LibJS/Runtime/Environment.cpp b/Userland/Libraries/LibJS/Runtime/Environment.cpp index fa650d9f26..cfed895af0 100644 --- a/Userland/Libraries/LibJS/Runtime/Environment.cpp +++ b/Userland/Libraries/LibJS/Runtime/Environment.cpp @@ -1,12 +1,11 @@ /* - * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include -#include namespace JS { @@ -15,16 +14,9 @@ Environment::Environment(Environment* outer_environment) { } -void Environment::initialize(GlobalObject& global_object) -{ - m_global_object = &global_object; - Cell::initialize(global_object); -} - void Environment::visit_edges(Visitor& visitor) { Cell::visit_edges(visitor); - visitor.visit(m_global_object); visitor.visit(m_outer_environment); } diff --git a/Userland/Libraries/LibJS/Runtime/Environment.h b/Userland/Libraries/LibJS/Runtime/Environment.h index c671011616..f6254b7239 100644 --- a/Userland/Libraries/LibJS/Runtime/Environment.h +++ b/Userland/Libraries/LibJS/Runtime/Environment.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -23,11 +23,6 @@ public: \ class Environment : public Cell { public: - GlobalObject& global_object() { return *m_global_object; } - GlobalObject const& global_object() const { return *m_global_object; } - - virtual void initialize(GlobalObject&) override; - virtual bool has_this_binding() const { return false; } virtual ThrowCompletionOr get_this_binding(GlobalObject&) const { return Value {}; } @@ -69,7 +64,6 @@ private: bool m_permanently_screwed_by_eval { false }; - GlobalObject* m_global_object { nullptr }; Environment* m_outer_environment { nullptr }; }; diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp index 5031b6342d..a33fdf4ff1 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp @@ -19,8 +19,8 @@ GlobalEnvironment::GlobalEnvironment(GlobalObject& global_object, Object& this_v : Environment(nullptr) , m_global_this_value(&this_value) { - m_object_record = global_object.heap().allocate(global_object, global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr); - m_declarative_record = global_object.heap().allocate(global_object); + m_object_record = global_object.heap().allocate_without_global_object(global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr); + m_declarative_record = global_object.heap().allocate_without_global_object(); } void GlobalEnvironment::visit_edges(Cell::Visitor& visitor) @@ -245,7 +245,7 @@ ThrowCompletionOr GlobalEnvironment::create_global_var_binding(FlyString c { // 1. Let ObjRec be envRec.[[ObjectRecord]]. // 2. Let globalObject be ObjRec.[[BindingObject]]. - auto& global_object = m_object_record->binding_object(); + auto& global_object = verify_cast(m_object_record->binding_object()); // 3. Let hasProperty be ? HasOwnProperty(globalObject, N). auto has_property = TRY(global_object.has_own_property(name)); @@ -256,10 +256,10 @@ ThrowCompletionOr GlobalEnvironment::create_global_var_binding(FlyString c // 5. If hasProperty is false and extensible is true, then if (!has_property && extensible) { // a. Perform ? ObjRec.CreateMutableBinding(N, D). - TRY(m_object_record->create_mutable_binding(m_object_record->global_object(), name, can_be_deleted)); + TRY(m_object_record->create_mutable_binding(global_object, name, can_be_deleted)); // b. Perform ? ObjRec.InitializeBinding(N, undefined). - TRY(m_object_record->initialize_binding(m_object_record->global_object(), name, js_undefined())); + TRY(m_object_record->initialize_binding(global_object, name, js_undefined())); } // 6. Let varDeclaredNames be envRec.[[VarNames]]. diff --git a/Userland/Libraries/LibJS/Runtime/Realm.cpp b/Userland/Libraries/LibJS/Runtime/Realm.cpp index eb324077be..d269bb5f71 100644 --- a/Userland/Libraries/LibJS/Runtime/Realm.cpp +++ b/Userland/Libraries/LibJS/Runtime/Realm.cpp @@ -26,7 +26,7 @@ void Realm::set_global_object(GlobalObject& global_object, Object* this_value) // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue). // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv. - m_global_environment = global_object.heap().allocate(global_object, global_object, *this_value); + m_global_environment = global_object.heap().allocate_without_global_object(global_object, *this_value); // 7. Return realmRec. } diff --git a/Userland/Libraries/LibJS/SourceTextModule.cpp b/Userland/Libraries/LibJS/SourceTextModule.cpp index 7c6487f366..d9cf6e0fbb 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.cpp +++ b/Userland/Libraries/LibJS/SourceTextModule.cpp @@ -328,7 +328,7 @@ Completion SourceTextModule::initialize_environment(VM& vm) auto& global_object = realm().global_object(); // 5. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]). - auto* environment = vm.heap().allocate(global_object, &realm().global_environment()); + auto* environment = vm.heap().allocate_without_global_object(&realm().global_environment()); // 6. Set module.[[Environment]] to env. set_environment(environment); diff --git a/Userland/Libraries/LibJS/SyntheticModule.cpp b/Userland/Libraries/LibJS/SyntheticModule.cpp index 418fc8d650..99f693b8c2 100644 --- a/Userland/Libraries/LibJS/SyntheticModule.cpp +++ b/Userland/Libraries/LibJS/SyntheticModule.cpp @@ -53,7 +53,7 @@ ThrowCompletionOr JS::SyntheticModule::link(VM& vm) auto& global_object = realm().global_object(); // 3. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]). - auto* environment = vm.heap().allocate(global_object, &realm().global_environment()); + auto* environment = vm.heap().allocate_without_global_object(&realm().global_environment()); // 4. Set module.[[Environment]] to env. set_environment(environment);