1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:58:11 +00:00

LibJS: Remove unnecessary GlobalObject pointer from Environment

As it turns out, we didn't actually need this pointer. :^)
This commit is contained in:
Andreas Kling 2022-01-31 13:00:02 +01:00
parent 4a51165f5f
commit fc04465fa3
8 changed files with 17 additions and 32 deletions

View file

@ -511,7 +511,7 @@ void ContinuePendingUnwind::replace_references_impl(BasicBlock const& from, Basi
void PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const void PushDeclarativeEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
{ {
auto* environment = interpreter.vm().heap().allocate<DeclarativeEnvironment>(interpreter.global_object(), interpreter.vm().lexical_environment()); auto* environment = interpreter.vm().heap().allocate_without_global_object<DeclarativeEnvironment>(interpreter.vm().lexical_environment());
interpreter.vm().running_execution_context().lexical_environment = environment; interpreter.vm().running_execution_context().lexical_environment = environment;
interpreter.vm().running_execution_context().variable_environment = environment; interpreter.vm().running_execution_context().variable_environment = environment;
} }

View file

@ -419,24 +419,23 @@ ThrowCompletionOr<Object*> get_prototype_from_constructor(GlobalObject& global_o
// 9.1.2.2 NewDeclarativeEnvironment ( E ), https://tc39.es/ecma262/#sec-newdeclarativeenvironment // 9.1.2.2 NewDeclarativeEnvironment ( E ), https://tc39.es/ecma262/#sec-newdeclarativeenvironment
DeclarativeEnvironment* new_declarative_environment(Environment& environment) DeclarativeEnvironment* new_declarative_environment(Environment& environment)
{ {
auto& global_object = environment.global_object(); return environment.heap().allocate_without_global_object<DeclarativeEnvironment>(&environment);
return global_object.heap().allocate<DeclarativeEnvironment>(global_object, &environment);
} }
// 9.1.2.3 NewObjectEnvironment ( O, W, E ), https://tc39.es/ecma262/#sec-newobjectenvironment // 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) ObjectEnvironment* new_object_environment(Object& object, bool is_with_environment, Environment* environment)
{ {
auto& global_object = object.global_object(); auto& heap = object.heap();
return global_object.heap().allocate<ObjectEnvironment>(global_object, object, is_with_environment ? ObjectEnvironment::IsWithEnvironment::Yes : ObjectEnvironment::IsWithEnvironment::No, environment); return heap.allocate_without_global_object<ObjectEnvironment>(object, is_with_environment ? ObjectEnvironment::IsWithEnvironment::Yes : ObjectEnvironment::IsWithEnvironment::No, environment);
} }
// 9.1.2.4 NewFunctionEnvironment ( F, newTarget ), https://tc39.es/ecma262/#sec-newfunctionenvironment // 9.1.2.4 NewFunctionEnvironment ( F, newTarget ), https://tc39.es/ecma262/#sec-newfunctionenvironment
FunctionEnvironment* new_function_environment(ECMAScriptFunctionObject& function, Object* new_target) 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. // 1. Let env be a new function Environment Record containing no bindings.
auto* env = global_object.heap().allocate<FunctionEnvironment>(global_object, function.environment()); auto* env = heap.allocate_without_global_object<FunctionEnvironment>(function.environment());
// 2. Set env.[[FunctionObject]] to F. // 2. Set env.[[FunctionObject]] to F.
env->set_function_object(function); 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. // 1. Let names be a new empty List.
// 2. Return the PrivateEnvironment Record { [[OuterPrivateEnvironment]]: outerPrivEnv, [[Names]]: names }. // 2. Return the PrivateEnvironment Record { [[OuterPrivateEnvironment]]: outerPrivEnv, [[Names]]: names }.
return vm.heap().allocate<PrivateEnvironment>(vm.current_realm()->global_object(), outer); return vm.heap().allocate_without_global_object<PrivateEnvironment>(outer);
} }
// 9.4.3 GetThisEnvironment ( ), https://tc39.es/ecma262/#sec-getthisenvironment // 9.4.3 GetThisEnvironment ( ), https://tc39.es/ecma262/#sec-getthisenvironment

View file

@ -1,12 +1,11 @@
/* /*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibJS/Runtime/Environment.h> #include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/VM.h>
namespace JS { 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) void Environment::visit_edges(Visitor& visitor)
{ {
Cell::visit_edges(visitor); Cell::visit_edges(visitor);
visitor.visit(m_global_object);
visitor.visit(m_outer_environment); visitor.visit(m_outer_environment);
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -23,11 +23,6 @@ public: \
class Environment : public Cell { class Environment : public Cell {
public: 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 bool has_this_binding() const { return false; }
virtual ThrowCompletionOr<Value> get_this_binding(GlobalObject&) const { return Value {}; } virtual ThrowCompletionOr<Value> get_this_binding(GlobalObject&) const { return Value {}; }
@ -69,7 +64,6 @@ private:
bool m_permanently_screwed_by_eval { false }; bool m_permanently_screwed_by_eval { false };
GlobalObject* m_global_object { nullptr };
Environment* m_outer_environment { nullptr }; Environment* m_outer_environment { nullptr };
}; };

View file

@ -19,8 +19,8 @@ GlobalEnvironment::GlobalEnvironment(GlobalObject& global_object, Object& this_v
: Environment(nullptr) : Environment(nullptr)
, m_global_this_value(&this_value) , m_global_this_value(&this_value)
{ {
m_object_record = global_object.heap().allocate<ObjectEnvironment>(global_object, global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr); m_object_record = global_object.heap().allocate_without_global_object<ObjectEnvironment>(global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
m_declarative_record = global_object.heap().allocate<DeclarativeEnvironment>(global_object); m_declarative_record = global_object.heap().allocate_without_global_object<DeclarativeEnvironment>();
} }
void GlobalEnvironment::visit_edges(Cell::Visitor& visitor) void GlobalEnvironment::visit_edges(Cell::Visitor& visitor)
@ -245,7 +245,7 @@ ThrowCompletionOr<void> GlobalEnvironment::create_global_var_binding(FlyString c
{ {
// 1. Let ObjRec be envRec.[[ObjectRecord]]. // 1. Let ObjRec be envRec.[[ObjectRecord]].
// 2. Let globalObject be ObjRec.[[BindingObject]]. // 2. Let globalObject be ObjRec.[[BindingObject]].
auto& global_object = m_object_record->binding_object(); auto& global_object = verify_cast<GlobalObject>(m_object_record->binding_object());
// 3. Let hasProperty be ? HasOwnProperty(globalObject, N). // 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
auto has_property = TRY(global_object.has_own_property(name)); auto has_property = TRY(global_object.has_own_property(name));
@ -256,10 +256,10 @@ ThrowCompletionOr<void> GlobalEnvironment::create_global_var_binding(FlyString c
// 5. If hasProperty is false and extensible is true, then // 5. If hasProperty is false and extensible is true, then
if (!has_property && extensible) { if (!has_property && extensible) {
// a. Perform ? ObjRec.CreateMutableBinding(N, D). // 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). // 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]]. // 6. Let varDeclaredNames be envRec.[[VarNames]].

View file

@ -26,7 +26,7 @@ void Realm::set_global_object(GlobalObject& global_object, Object* this_value)
// 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue). // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
// 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv. // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
m_global_environment = global_object.heap().allocate<GlobalEnvironment>(global_object, global_object, *this_value); m_global_environment = global_object.heap().allocate_without_global_object<GlobalEnvironment>(global_object, *this_value);
// 7. Return realmRec. // 7. Return realmRec.
} }

View file

@ -328,7 +328,7 @@ Completion SourceTextModule::initialize_environment(VM& vm)
auto& global_object = realm().global_object(); auto& global_object = realm().global_object();
// 5. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]). // 5. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]).
auto* environment = vm.heap().allocate<ModuleEnvironment>(global_object, &realm().global_environment()); auto* environment = vm.heap().allocate_without_global_object<ModuleEnvironment>(&realm().global_environment());
// 6. Set module.[[Environment]] to env. // 6. Set module.[[Environment]] to env.
set_environment(environment); set_environment(environment);

View file

@ -53,7 +53,7 @@ ThrowCompletionOr<void> JS::SyntheticModule::link(VM& vm)
auto& global_object = realm().global_object(); auto& global_object = realm().global_object();
// 3. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]). // 3. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]).
auto* environment = vm.heap().allocate<ModuleEnvironment>(global_object, &realm().global_environment()); auto* environment = vm.heap().allocate_without_global_object<ModuleEnvironment>(&realm().global_environment());
// 4. Set module.[[Environment]] to env. // 4. Set module.[[Environment]] to env.
set_environment(environment); set_environment(environment);