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
Notes: sideshowbarker 2024-07-17 07:52:56 +09:00
18 changed files with 76 additions and 79 deletions

View file

@ -122,7 +122,7 @@ public:
TestRunnerGlobalObject(JS::Realm&);
virtual ~TestRunnerGlobalObject() override;
virtual void initialize_global_object() override;
virtual void initialize_global_object(JS::Realm&) override;
private:
JS_DECLARE_NATIVE_FUNCTION(fuzzilli);
@ -168,9 +168,9 @@ JS_DEFINE_NATIVE_FUNCTION(TestRunnerGlobalObject::fuzzilli)
return JS::js_undefined();
}
void TestRunnerGlobalObject::initialize_global_object()
void TestRunnerGlobalObject::initialize_global_object(JS::Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
define_direct_property("global", this, JS::Attribute::Enumerable);
define_native_function("fuzzilli", fuzzilli, 2, JS::default_attributes);
}

View file

@ -144,9 +144,9 @@ JS::ThrowCompletionOr<bool> SheetGlobalObject::internal_set(const JS::PropertyKe
return Base::internal_set(property_name, value, receiver);
}
void SheetGlobalObject::initialize_global_object()
void SheetGlobalObject::initialize_global_object(JS::Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
define_native_function("get_real_cell_contents", get_real_cell_contents, 1, attr);
define_native_function("set_real_cell_contents", set_real_cell_contents, 2, attr);

View file

@ -30,7 +30,7 @@ public:
virtual JS::ThrowCompletionOr<bool> internal_has_property(JS::PropertyKey const& name) const override;
virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override;
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
virtual void initialize_global_object() override;
virtual void initialize_global_object(JS::Realm&) override;
JS_DECLARE_NATIVE_FUNCTION(get_real_cell_contents);
JS_DECLARE_NATIVE_FUNCTION(set_real_cell_contents);

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

View file

@ -197,12 +197,12 @@ public:
virtual ~TestRunnerGlobalObject() override = default;
virtual void initialize_global_object() override;
virtual void initialize_global_object(JS::Realm&) override;
};
inline void TestRunnerGlobalObject::initialize_global_object()
inline void TestRunnerGlobalObject::initialize_global_object(JS::Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
define_direct_property("global", this, JS::Attribute::Enumerable);
for (auto& entry : s_exposed_global_functions) {
define_native_function(

View file

@ -57,14 +57,12 @@ WindowObject::WindowObject(JS::Realm& realm, HTML::Window& impl)
impl.set_wrapper({}, *this);
}
void WindowObject::initialize_global_object()
void WindowObject::initialize_global_object(JS::Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
Object::set_prototype(&ensure_web_prototype<WindowPrototype>("Window"));
auto& realm = *associated_realm();
// FIXME: These should be native accessors, not properties
define_direct_property("window", this, JS::Attribute::Enumerable);
define_direct_property("frames", this, JS::Attribute::Enumerable);

View file

@ -33,7 +33,7 @@ class WindowObject
public:
explicit WindowObject(JS::Realm&, HTML::Window&);
virtual void initialize_global_object() override;
virtual void initialize_global_object(JS::Realm&) override;
virtual ~WindowObject() override = default;
HTML::Window& impl() { return *m_impl; }

View file

@ -20,9 +20,9 @@ ConsoleGlobalObject::ConsoleGlobalObject(JS::Realm& realm, Web::Bindings::Window
{
}
void ConsoleGlobalObject::initialize_global_object()
void ConsoleGlobalObject::initialize_global_object(JS::Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
// $0 magic variable
define_native_accessor("$0", inspected_node_getter, nullptr, 0);

View file

@ -35,7 +35,7 @@ public:
virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const& name) override;
virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;
virtual void initialize_global_object() override;
virtual void initialize_global_object(JS::Realm&) override;
private:
virtual void visit_edges(Visitor&) override;

View file

@ -24,7 +24,8 @@ WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, WeakPtr<J
JS::DeferGC defer_gc(m_interpreter->heap());
auto& vm = m_interpreter->vm();
auto& global_object = m_interpreter->global_object();
auto& realm = m_interpreter->realm();
auto& global_object = realm.global_object();
auto console_global_object = m_interpreter->heap().allocate_without_realm<ConsoleGlobalObject>(m_interpreter->realm(), static_cast<Web::Bindings::WindowObject&>(global_object));
@ -32,8 +33,8 @@ WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, WeakPtr<J
// It gets removed immediately after creating the interpreter in Document::interpreter().
auto& eso = verify_cast<Web::HTML::EnvironmentSettingsObject>(*m_interpreter->realm().host_defined());
vm.push_execution_context(eso.realm_execution_context());
console_global_object->set_associated_realm(m_interpreter->realm());
console_global_object->initialize_global_object();
console_global_object->set_associated_realm(realm);
console_global_object->initialize_global_object(realm);
vm.pop_execution_context();
m_console_global_object = JS::make_handle(console_global_object);

View file

@ -96,7 +96,7 @@ public:
: GlobalObject(realm)
{
}
virtual void initialize_global_object() override;
virtual void initialize_global_object(JS::Realm&) override;
virtual ~ReplObject() override = default;
private:
@ -117,7 +117,7 @@ public:
: JS::GlobalObject(realm)
{
}
virtual void initialize_global_object() override;
virtual void initialize_global_object(JS::Realm&) override;
virtual ~ScriptObject() override = default;
private:
@ -1297,9 +1297,9 @@ static JS::ThrowCompletionOr<JS::Value> load_json_impl(JS::VM& vm)
return JS::JSONObject::parse_json_value(vm, json.value());
}
void ReplObject::initialize_global_object()
void ReplObject::initialize_global_object(JS::Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
define_direct_property("global", this, JS::Attribute::Enumerable);
u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
define_native_function("exit", exit_interpreter, 0, attr);
@ -1375,9 +1375,9 @@ JS_DEFINE_NATIVE_FUNCTION(ReplObject::print)
return JS::js_undefined();
}
void ScriptObject::initialize_global_object()
void ScriptObject::initialize_global_object(JS::Realm& realm)
{
Base::initialize_global_object();
Base::initialize_global_object(realm);
define_direct_property("global", this, JS::Attribute::Enumerable);
u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
define_native_function("loadINI", load_ini, 1, attr);