1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 20:47:45 +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

@ -23,7 +23,7 @@ $262Object::$262Object(Realm& realm)
{
}
void $262Object::initialize(JS::Realm& realm)
void $262Object::initialize(Realm& realm)
{
Base::initialize(realm);
@ -63,7 +63,7 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::create_realm)
VERIFY(realm_global_object);
realm->set_global_object(realm_global_object, nullptr);
realm_global_object->set_associated_realm(*realm);
realm_global_object->initialize_global_object();
realm_global_object->initialize_global_object(*realm);
return Value(realm_global_object->$262());
}

View file

@ -14,11 +14,10 @@
namespace JS::Test262 {
void GlobalObject::initialize_global_object()
void GlobalObject::initialize_global_object(Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
auto& realm = *associated_realm();
m_$262 = vm().heap().allocate<$262Object>(realm, realm);
// https://github.com/tc39/test262/blob/master/INTERPRETING.md#host-defined-functions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -19,7 +19,7 @@ public:
: JS::GlobalObject(realm)
{
}
virtual void initialize_global_object() override;
virtual void initialize_global_object(Realm&) override;
virtual ~GlobalObject() override = default;
$262Object* $262() const { return m_$262; }

View file

@ -44,11 +44,13 @@ public:
VM::InterpreterExecutionScope scope(*interpreter);
GlobalObject* global_object { nullptr };
Realm* realm { nullptr };
interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
vm,
[&](Realm& realm) -> GlobalObject* {
global_object = interpreter->heap().allocate_without_realm<GlobalObjectType>(realm, forward<Args>(args)...);
[&](Realm& realm_) -> GlobalObject* {
global_object = interpreter->heap().allocate_without_realm<GlobalObjectType>(realm_, forward<Args>(args)...);
realm = &realm_;
return global_object;
},
nullptr));
@ -58,7 +60,7 @@ public:
interpreter->m_global_execution_context->function_name = global_execution_context_name;
interpreter->m_global_object = make_handle(global_object);
interpreter->m_realm = make_handle(global_object->associated_realm());
interpreter->m_realm = make_handle(realm);
return interpreter;
}

View file

@ -145,15 +145,13 @@ GlobalObject::GlobalObject(Realm& realm)
{
}
void GlobalObject::initialize_global_object()
void GlobalObject::initialize_global_object(Realm& realm)
{
auto& vm = this->vm();
ensure_shape_is_unique();
// These are done first since other prototypes depend on their presence.
VERIFY(associated_realm());
auto& realm = *associated_realm();
m_empty_object_shape = heap().allocate_without_realm<Shape>(realm);
m_object_prototype = heap().allocate_without_realm<ObjectPrototype>(realm);
m_function_prototype = heap().allocate_without_realm<FunctionPrototype>(realm);
@ -200,7 +198,7 @@ void GlobalObject::initialize_global_object()
// Must be allocated before `Intl::Intl` below.
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
initialize_constructor(vm.names.ClassName, m_intl_##snake_name##_constructor, m_intl_##snake_name##_prototype);
initialize_constructor(realm, vm.names.ClassName, m_intl_##snake_name##_constructor, m_intl_##snake_name##_prototype);
JS_ENUMERATE_INTL_OBJECTS
#undef __JS_ENUMERATE
@ -212,7 +210,7 @@ void GlobalObject::initialize_global_object()
// Must be allocated before `Temporal::Temporal` below.
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
initialize_constructor(vm.names.ClassName, m_temporal_##snake_name##_constructor, m_temporal_##snake_name##_prototype);
initialize_constructor(realm, vm.names.ClassName, m_temporal_##snake_name##_constructor, m_temporal_##snake_name##_prototype);
JS_ENUMERATE_TEMPORAL_OBJECTS
#undef __JS_ENUMERATE
@ -259,44 +257,44 @@ void GlobalObject::initialize_global_object()
define_direct_property(vm.names.Temporal, heap().allocate<Temporal::Temporal>(realm, realm), attr);
// This must be initialized before allocating AggregateErrorConstructor, which uses ErrorConstructor as its prototype.
initialize_constructor(vm.names.Error, m_error_constructor, m_error_prototype);
initialize_constructor(realm, vm.names.Error, m_error_constructor, m_error_prototype);
add_constructor(vm.names.AggregateError, m_aggregate_error_constructor, m_aggregate_error_prototype);
add_constructor(vm.names.Array, m_array_constructor, m_array_prototype);
add_constructor(vm.names.ArrayBuffer, m_array_buffer_constructor, m_array_buffer_prototype);
add_constructor(vm.names.BigInt, m_bigint_constructor, m_bigint_prototype);
add_constructor(vm.names.Boolean, m_boolean_constructor, m_boolean_prototype);
add_constructor(vm.names.DataView, m_data_view_constructor, m_data_view_prototype);
add_constructor(vm.names.Date, m_date_constructor, m_date_prototype);
add_constructor(vm.names.Error, m_error_constructor, m_error_prototype);
add_constructor(vm.names.FinalizationRegistry, m_finalization_registry_constructor, m_finalization_registry_prototype);
add_constructor(vm.names.Function, m_function_constructor, m_function_prototype);
add_constructor(vm.names.Map, m_map_constructor, m_map_prototype);
add_constructor(vm.names.Number, m_number_constructor, m_number_prototype);
add_constructor(vm.names.Object, m_object_constructor, m_object_prototype);
add_constructor(vm.names.Promise, m_promise_constructor, m_promise_prototype);
add_constructor(vm.names.Proxy, m_proxy_constructor, nullptr);
add_constructor(vm.names.RegExp, m_regexp_constructor, m_regexp_prototype);
add_constructor(vm.names.Set, m_set_constructor, m_set_prototype);
add_constructor(vm.names.ShadowRealm, m_shadow_realm_constructor, m_shadow_realm_prototype);
add_constructor(vm.names.String, m_string_constructor, m_string_prototype);
add_constructor(vm.names.Symbol, m_symbol_constructor, m_symbol_prototype);
add_constructor(vm.names.WeakMap, m_weak_map_constructor, m_weak_map_prototype);
add_constructor(vm.names.WeakRef, m_weak_ref_constructor, m_weak_ref_prototype);
add_constructor(vm.names.WeakSet, m_weak_set_constructor, m_weak_set_prototype);
add_constructor(realm, vm.names.AggregateError, m_aggregate_error_constructor, m_aggregate_error_prototype);
add_constructor(realm, vm.names.Array, m_array_constructor, m_array_prototype);
add_constructor(realm, vm.names.ArrayBuffer, m_array_buffer_constructor, m_array_buffer_prototype);
add_constructor(realm, vm.names.BigInt, m_bigint_constructor, m_bigint_prototype);
add_constructor(realm, vm.names.Boolean, m_boolean_constructor, m_boolean_prototype);
add_constructor(realm, vm.names.DataView, m_data_view_constructor, m_data_view_prototype);
add_constructor(realm, vm.names.Date, m_date_constructor, m_date_prototype);
add_constructor(realm, vm.names.Error, m_error_constructor, m_error_prototype);
add_constructor(realm, vm.names.FinalizationRegistry, m_finalization_registry_constructor, m_finalization_registry_prototype);
add_constructor(realm, vm.names.Function, m_function_constructor, m_function_prototype);
add_constructor(realm, vm.names.Map, m_map_constructor, m_map_prototype);
add_constructor(realm, vm.names.Number, m_number_constructor, m_number_prototype);
add_constructor(realm, vm.names.Object, m_object_constructor, m_object_prototype);
add_constructor(realm, vm.names.Promise, m_promise_constructor, m_promise_prototype);
add_constructor(realm, vm.names.Proxy, m_proxy_constructor, nullptr);
add_constructor(realm, vm.names.RegExp, m_regexp_constructor, m_regexp_prototype);
add_constructor(realm, vm.names.Set, m_set_constructor, m_set_prototype);
add_constructor(realm, vm.names.ShadowRealm, m_shadow_realm_constructor, m_shadow_realm_prototype);
add_constructor(realm, vm.names.String, m_string_constructor, m_string_prototype);
add_constructor(realm, vm.names.Symbol, m_symbol_constructor, m_symbol_prototype);
add_constructor(realm, vm.names.WeakMap, m_weak_map_constructor, m_weak_map_prototype);
add_constructor(realm, vm.names.WeakRef, m_weak_ref_constructor, m_weak_ref_prototype);
add_constructor(realm, vm.names.WeakSet, m_weak_set_constructor, m_weak_set_prototype);
initialize_constructor(vm.names.TypedArray, m_typed_array_constructor, m_typed_array_prototype);
initialize_constructor(realm, vm.names.TypedArray, m_typed_array_constructor, m_typed_array_prototype);
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
add_constructor(vm.names.ClassName, m_##snake_name##_constructor, m_##snake_name##_prototype);
add_constructor(realm, vm.names.ClassName, m_##snake_name##_constructor, m_##snake_name##_prototype);
JS_ENUMERATE_NATIVE_ERRORS
JS_ENUMERATE_TYPED_ARRAYS
#undef __JS_ENUMERATE
// NOTE: These constructors cannot be initialized with add_constructor as they have no global binding.
initialize_constructor(vm.names.GeneratorFunction, m_generator_function_constructor, m_generator_function_prototype, Attribute::Configurable);
initialize_constructor(vm.names.AsyncGeneratorFunction, m_async_generator_function_constructor, m_async_generator_function_prototype, Attribute::Configurable);
initialize_constructor(vm.names.AsyncFunction, m_async_function_constructor, m_async_function_prototype, Attribute::Configurable);
initialize_constructor(realm, vm.names.GeneratorFunction, m_generator_function_constructor, m_generator_function_prototype, Attribute::Configurable);
initialize_constructor(realm, vm.names.AsyncGeneratorFunction, m_async_generator_function_constructor, m_async_generator_function_prototype, Attribute::Configurable);
initialize_constructor(realm, vm.names.AsyncFunction, m_async_function_constructor, m_async_function_prototype, Attribute::Configurable);
// 27.5.1.1 Generator.prototype.constructor, https://tc39.es/ecma262/#sec-generator.prototype.constructor
m_generator_prototype->define_direct_property(vm.names.constructor, m_generator_function_prototype, Attribute::Configurable);

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);
}

View file

@ -64,7 +64,7 @@ ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_define
// 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
// 11. Create any host-defined global object properties on globalObj.
realm->global_object().initialize_global_object();
realm->global_object().initialize_global_object(*realm);
// 12. Return unused.
return new_context;

View file

@ -64,7 +64,7 @@ ThrowCompletionOr<Object*> ShadowRealmConstructor::construct(FunctionObject& new
// 10. Perform ? SetRealmGlobalObject(realmRec, undefined, undefined).
auto* new_global_object = vm.heap().allocate_without_realm<GlobalObject>(*realm);
realm->set_global_object(new_global_object, nullptr);
new_global_object->initialize_global_object();
new_global_object->initialize_global_object(*realm);
// TODO: I don't think we should have these exactly like this, that doesn't work well with how
// we create global objects. Still, it should be possible to make a ShadowRealm with a