1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:27:43 +00:00

LibJS+LibWeb: Replace GlobalObject with Realm in object constructors

No functional changes - we can still very easily get to the global
object via `Realm::global_object()`. This is in preparation of moving
the intrinsics to the realm and no longer having to pass a global
object when allocating any object.
In a few (now, and many more in subsequent commits) places we get a
realm using `GlobalObject::associated_realm()`, this is intended to be
temporary. For example, create() functions will later receive the same
treatment and are passed a realm instead of a global object.
This commit is contained in:
Linus Groh 2022-08-16 00:20:49 +01:00
parent 4c300cc5e8
commit ecd163bdf1
315 changed files with 592 additions and 554 deletions

View file

@ -14,8 +14,8 @@
namespace Web::Bindings {
WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(JS::GlobalObject& global_object)
: NativeFunction(*global_object.function_prototype())
WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(JS::Realm& realm)
: NativeFunction(*realm.global_object().function_prototype())
{
}
@ -30,12 +30,14 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyInstanceConstructor::construct(Fun
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto& realm = *global_object.associated_realm();
auto* module_argument = TRY(vm.argument(0).to_object(global_object));
if (!is<WebAssemblyModuleObject>(module_argument))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument);
auto result = TRY(WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object));
return heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result);
return heap().allocate<WebAssemblyInstanceObject>(global_object, realm, result);
}
void WebAssemblyInstanceConstructor::initialize(JS::GlobalObject& global_object)

View file

@ -14,7 +14,7 @@ class WebAssemblyInstanceConstructor : public JS::NativeFunction {
JS_OBJECT(WebAssemblyInstanceConstructor, JS::NativeFunction);
public:
explicit WebAssemblyInstanceConstructor(JS::GlobalObject&);
explicit WebAssemblyInstanceConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyInstanceConstructor() override;

View file

@ -17,8 +17,8 @@
namespace Web::Bindings {
WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::GlobalObject& global_object, size_t index)
: Object(static_cast<Web::Bindings::WindowObject&>(global_object).ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype"))
WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t index)
: Object(static_cast<Web::Bindings::WindowObject&>(realm.global_object()).ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype"))
, m_index(index)
{
}
@ -27,6 +27,8 @@ void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
{
Object::initialize(global_object);
auto& realm = *global_object.associated_realm();
VERIFY(!m_exports_object);
m_exports_object = create(global_object, nullptr);
auto& instance = this->instance();
@ -44,7 +46,7 @@ void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
[&](Wasm::MemoryAddress const& address) {
Optional<WebAssemblyMemoryObject*> object = cache.memory_instances.get(address);
if (!object.has_value()) {
object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(global_object, global_object, address);
object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(global_object, realm, address);
cache.memory_instances.set(address, *object);
}
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);

View file

@ -19,7 +19,7 @@ class WebAssemblyInstanceObject final : public JS::Object {
JS_OBJECT(WebAssemblyInstanceObject, Object);
public:
explicit WebAssemblyInstanceObject(JS::GlobalObject&, size_t index);
explicit WebAssemblyInstanceObject(JS::Realm&, size_t index);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyInstanceObject() override = default;

View file

@ -17,8 +17,8 @@ class WebAssemblyInstancePrototype final : public JS::Object {
JS_OBJECT(WebAssemblyInstancePrototype, Object);
public:
explicit WebAssemblyInstancePrototype(JS::GlobalObject& global_object)
: JS::Object(*global_object.object_prototype())
explicit WebAssemblyInstancePrototype(JS::Realm& realm)
: JS::Object(*realm.global_object().object_prototype())
{
}

View file

@ -12,8 +12,8 @@
namespace Web::Bindings {
WebAssemblyMemoryConstructor::WebAssemblyMemoryConstructor(JS::GlobalObject& global_object)
: NativeFunction(*global_object.function_prototype())
WebAssemblyMemoryConstructor::WebAssemblyMemoryConstructor(JS::Realm& realm)
: NativeFunction(*realm.global_object().function_prototype())
{
}
@ -28,6 +28,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(Funct
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto& realm = *global_object.associated_realm();
auto descriptor = TRY(vm.argument(0).to_object(global_object));
auto initial_value = TRY(descriptor->get("initial"));
@ -50,7 +51,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(Funct
if (!WebAssemblyObject::s_abstract_machine.store().get(*address)->grow(initial))
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("Wasm Memory grow failed: {}", initial));
return vm.heap().allocate<WebAssemblyMemoryObject>(global_object, global_object, *address);
return vm.heap().allocate<WebAssemblyMemoryObject>(global_object, realm, *address);
}
void WebAssemblyMemoryConstructor::initialize(JS::GlobalObject& global_object)

View file

@ -14,7 +14,7 @@ class WebAssemblyMemoryConstructor : public JS::NativeFunction {
JS_OBJECT(WebAssemblyMemoryConstructor, JS::NativeFunction);
public:
explicit WebAssemblyMemoryConstructor(JS::GlobalObject&);
explicit WebAssemblyMemoryConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyMemoryConstructor() override;

View file

@ -19,8 +19,8 @@ class WebAssemblyMemoryPrototype final : public JS::Object {
JS_OBJECT(WebAssemblyMemoryPrototype, JS::Object);
public:
explicit WebAssemblyMemoryPrototype(JS::GlobalObject& global_object)
: JS::Object(*global_object.object_prototype())
explicit WebAssemblyMemoryPrototype(JS::Realm& realm)
: JS::Object(*realm.global_object().object_prototype())
{
}

View file

@ -14,8 +14,8 @@
namespace Web::Bindings {
WebAssemblyModuleConstructor::WebAssemblyModuleConstructor(JS::GlobalObject& global_object)
: NativeFunction(*global_object.function_prototype())
WebAssemblyModuleConstructor::WebAssemblyModuleConstructor(JS::Realm& realm)
: NativeFunction(*realm.global_object().function_prototype())
{
}
@ -30,11 +30,12 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyModuleConstructor::construct(Funct
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto& realm = *global_object.associated_realm();
auto* buffer_object = TRY(vm.argument(0).to_object(global_object));
auto result = TRY(parse_module(global_object, buffer_object));
return heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result);
return heap().allocate<WebAssemblyModuleObject>(global_object, realm, result);
}
void WebAssemblyModuleConstructor::initialize(JS::GlobalObject& global_object)

View file

@ -14,7 +14,7 @@ class WebAssemblyModuleConstructor : public JS::NativeFunction {
JS_OBJECT(WebAssemblyModuleConstructor, JS::NativeFunction);
public:
explicit WebAssemblyModuleConstructor(JS::GlobalObject&);
explicit WebAssemblyModuleConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyModuleConstructor() override;

View file

@ -9,8 +9,8 @@
namespace Web::Bindings {
WebAssemblyModuleObject::WebAssemblyModuleObject(JS::GlobalObject& global_object, size_t index)
: Object(static_cast<WindowObject&>(global_object).ensure_web_prototype<WebAssemblyModulePrototype>("WebAssemblyModulePrototype"))
WebAssemblyModuleObject::WebAssemblyModuleObject(JS::Realm& realm, size_t index)
: Object(static_cast<WindowObject&>(realm.global_object()).ensure_web_prototype<WebAssemblyModulePrototype>("WebAssemblyModulePrototype"))
, m_index(index)
{
}

View file

@ -18,7 +18,7 @@ class WebAssemblyModuleObject final : public JS::Object {
JS_OBJECT(WebAssemblyModuleObject, Object);
public:
explicit WebAssemblyModuleObject(JS::GlobalObject&, size_t index);
explicit WebAssemblyModuleObject(JS::Realm&, size_t index);
virtual ~WebAssemblyModuleObject() override = default;
size_t index() const { return m_index; }

View file

@ -19,8 +19,8 @@ class WebAssemblyModulePrototype final : public JS::Object {
JS_OBJECT(WebAssemblyModulePrototype, JS::Object);
public:
explicit WebAssemblyModulePrototype(JS::GlobalObject& global_object)
: JS::Object(*global_object.object_prototype())
explicit WebAssemblyModulePrototype(JS::Realm& realm)
: JS::Object(*realm.global_object().object_prototype())
{
}
};

View file

@ -25,8 +25,8 @@
namespace Web::Bindings {
WebAssemblyObject::WebAssemblyObject(JS::GlobalObject& global_object)
: Object(*global_object.object_prototype())
WebAssemblyObject::WebAssemblyObject(JS::Realm& realm)
: Object(*realm.global_object().object_prototype())
{
s_abstract_machine.enable_instruction_count_limit();
}
@ -156,6 +156,8 @@ JS::ThrowCompletionOr<size_t> parse_module(JS::GlobalObject& global_object, JS::
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile)
{
auto& realm = *global_object.associated_realm();
// FIXME: This shouldn't block!
auto buffer_or_error = vm.argument(0).to_object(global_object);
JS::Value rejection_value;
@ -172,7 +174,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile)
if (result.is_error())
promise->reject(*result.release_error().value());
else
promise->fulfill(vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.release_value()));
promise->fulfill(vm.heap().allocate<WebAssemblyModuleObject>(global_object, realm, result.release_value()));
return promise;
}
@ -317,6 +319,8 @@ JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
{
auto& realm = *global_object.associated_realm();
// FIXME: This shouldn't block!
auto buffer_or_error = vm.argument(0).to_object(global_object);
auto promise = JS::Promise::create(global_object);
@ -350,10 +354,10 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
if (result.is_error()) {
promise->reject(*result.release_error().value());
} else {
auto instance_object = vm.heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.release_value());
auto instance_object = vm.heap().allocate<WebAssemblyInstanceObject>(global_object, realm, result.release_value());
if (should_return_module) {
auto object = JS::Object::create(global_object, nullptr);
object->define_direct_property("module", vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, s_compiled_modules.size() - 1), JS::default_attributes);
object->define_direct_property("module", vm.heap().allocate<WebAssemblyModuleObject>(global_object, realm, s_compiled_modules.size() - 1), JS::default_attributes);
object->define_direct_property("instance", instance_object, JS::default_attributes);
promise->fulfill(object);
} else {
@ -477,8 +481,8 @@ JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm
return function;
}
WebAssemblyMemoryObject::WebAssemblyMemoryObject(JS::GlobalObject& global_object, Wasm::MemoryAddress address)
: Object(static_cast<WindowObject&>(global_object).ensure_web_prototype<WebAssemblyMemoryPrototype>("WebAssemblyMemoryPrototype"))
WebAssemblyMemoryObject::WebAssemblyMemoryObject(JS::Realm& realm, Wasm::MemoryAddress address)
: Object(static_cast<WindowObject&>(realm.global_object()).ensure_web_prototype<WebAssemblyMemoryPrototype>("WebAssemblyMemoryPrototype"))
, m_address(address)
{
}

View file

@ -23,7 +23,7 @@ class WebAssemblyObject final : public JS::Object {
JS_OBJECT(WebAssemblyObject, JS::Object);
public:
explicit WebAssemblyObject(JS::GlobalObject&);
explicit WebAssemblyObject(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyObject() override = default;
@ -69,7 +69,7 @@ class WebAssemblyMemoryObject final : public JS::Object {
JS_OBJECT(WebAssemblyMemoryObject, JS::Object);
public:
explicit WebAssemblyMemoryObject(JS::GlobalObject&, Wasm::MemoryAddress);
WebAssemblyMemoryObject(JS::Realm&, Wasm::MemoryAddress);
virtual ~WebAssemblyMemoryObject() override = default;
auto address() const { return m_address; }

View file

@ -14,8 +14,8 @@
namespace Web::Bindings {
WebAssemblyTableConstructor::WebAssemblyTableConstructor(JS::GlobalObject& global_object)
: NativeFunction(*global_object.function_prototype())
WebAssemblyTableConstructor::WebAssemblyTableConstructor(JS::Realm& realm)
: NativeFunction(*realm.global_object().function_prototype())
{
}
@ -30,6 +30,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
{
auto& vm = this->vm();
auto& global_object = this->global_object();
auto& realm = *global_object.associated_realm();
auto descriptor = TRY(vm.argument(0).to_object(global_object));
auto element_value = TRY(descriptor->get("element"));
@ -77,7 +78,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
for (auto& element : table.elements())
element = reference;
return vm.heap().allocate<WebAssemblyTableObject>(global_object, global_object, *address);
return vm.heap().allocate<WebAssemblyTableObject>(global_object, realm, *address);
}
void WebAssemblyTableConstructor::initialize(JS::GlobalObject& global_object)

View file

@ -14,7 +14,7 @@ class WebAssemblyTableConstructor : public JS::NativeFunction {
JS_OBJECT(WebAssemblyTableConstructor, JS::NativeFunction);
public:
explicit WebAssemblyTableConstructor(JS::GlobalObject&);
explicit WebAssemblyTableConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyTableConstructor() override;

View file

@ -9,8 +9,8 @@
namespace Web::Bindings {
WebAssemblyTableObject::WebAssemblyTableObject(JS::GlobalObject& global_object, Wasm::TableAddress address)
: Object(static_cast<WindowObject&>(global_object).ensure_web_prototype<WebAssemblyTablePrototype>("WebAssemblyTablePrototype"))
WebAssemblyTableObject::WebAssemblyTableObject(JS::Realm& realm, Wasm::TableAddress address)
: Object(static_cast<WindowObject&>(realm.global_object()).ensure_web_prototype<WebAssemblyTablePrototype>("WebAssemblyTablePrototype"))
, m_address(address)
{
}

View file

@ -18,7 +18,7 @@ class WebAssemblyTableObject final : public JS::Object {
JS_OBJECT(WebAssemblyTableObject, Object);
public:
explicit WebAssemblyTableObject(JS::GlobalObject&, Wasm::TableAddress);
WebAssemblyTableObject(JS::Realm&, Wasm::TableAddress);
virtual ~WebAssemblyTableObject() override = default;
Wasm::TableAddress address() const { return m_address; }

View file

@ -19,8 +19,8 @@ class WebAssemblyTablePrototype final : public JS::Object {
JS_OBJECT(WebAssemblyTablePrototype, JS::Object);
public:
explicit WebAssemblyTablePrototype(JS::GlobalObject& global_object)
: JS::Object(*global_object.object_prototype())
explicit WebAssemblyTablePrototype(JS::Realm& realm)
: JS::Object(*realm.global_object().object_prototype())
{
}