1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 01:07:44 +00:00

LibJS: Pass Realm to GlobalObject::initialize_global_object()

Global object initialization is tightly coupled to realm creation, so
simply pass it to the function instead of relying on the non-standard
'associated realm' concept, which I'd like to remove later.

This works essentially the same way as regular Object::initialize() now.

Additionally this allows us to forward the realm to GlobalObject's
add_constructor() / initialize_constructor() helpers, so they set the
correct realm on the allocated constructor function object.
This commit is contained in:
Linus Groh 2022-08-22 18:56:16 +01:00
parent b465f46e00
commit 7c468b5a77
18 changed files with 76 additions and 79 deletions

View file

@ -18,7 +18,7 @@ class GlobalObject : public Object {
public:
explicit GlobalObject(Realm&);
virtual void initialize_global_object();
virtual void initialize_global_object(Realm&);
virtual ~GlobalObject() override;
@ -103,9 +103,9 @@ protected:
virtual void visit_edges(Visitor&) override;
template<typename ConstructorType>
void initialize_constructor(PropertyKey const&, ConstructorType*&, Object* prototype, PropertyAttributes = Attribute::Writable | Attribute::Configurable);
void initialize_constructor(Realm&, PropertyKey const&, ConstructorType*&, Object* prototype, PropertyAttributes = Attribute::Writable | Attribute::Configurable);
template<typename ConstructorType>
void add_constructor(PropertyKey const&, ConstructorType*&, Object* prototype);
void add_constructor(Realm&, PropertyKey const&, ConstructorType*&, Object* prototype);
private:
virtual bool is_global_object() const final { return true; }
@ -174,10 +174,9 @@ private:
};
template<typename ConstructorType>
inline void GlobalObject::initialize_constructor(PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype, PropertyAttributes attributes)
inline void GlobalObject::initialize_constructor(Realm& realm, PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype, PropertyAttributes attributes)
{
auto& vm = this->vm();
auto& realm = *associated_realm();
constructor = heap().allocate<ConstructorType>(realm, realm);
constructor->define_direct_property(vm.names.name, js_string(heap(), property_key.as_string()), Attribute::Configurable);
if (prototype)
@ -185,11 +184,11 @@ inline void GlobalObject::initialize_constructor(PropertyKey const& property_key
}
template<typename ConstructorType>
inline void GlobalObject::add_constructor(PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype)
inline void GlobalObject::add_constructor(Realm& realm, PropertyKey const& property_key, ConstructorType*& constructor, Object* prototype)
{
// Some constructors are pre-initialized separately.
if (!constructor)
initialize_constructor(property_key, constructor, prototype);
initialize_constructor(realm, property_key, constructor, prototype);
define_direct_property(property_key, constructor, Attribute::Writable | Attribute::Configurable);
}