1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:37:35 +00:00

LibJS+LibWeb: Replace GlobalObject with Realm in initialize() functions

This is a continuation of the previous commit.

Calling initialize() is the first thing that's done after allocating a
cell on the JS heap - and in the common case of allocating an object,
that's where properties are assigned and intrinsics occasionally
accessed.
Since those are supposed to live on the realm eventually, this is
another step into that direction.
This commit is contained in:
Linus Groh 2022-08-16 00:20:49 +01:00
parent ecd163bdf1
commit 5dd5896588
304 changed files with 608 additions and 603 deletions

View file

@ -40,12 +40,12 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyInstanceConstructor::construct(Fun
return heap().allocate<WebAssemblyInstanceObject>(global_object, realm, result);
}
void WebAssemblyInstanceConstructor::initialize(JS::GlobalObject& global_object)
void WebAssemblyInstanceConstructor::initialize(JS::Realm& realm)
{
auto& vm = this->vm();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(realm.global_object());
NativeFunction::initialize(global_object);
NativeFunction::initialize(realm);
define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype"), 0);
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
}

View file

@ -15,7 +15,7 @@ class WebAssemblyInstanceConstructor : public JS::NativeFunction {
public:
explicit WebAssemblyInstanceConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual void initialize(JS::Realm&) override;
virtual ~WebAssemblyInstanceConstructor() override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;

View file

@ -23,14 +23,12 @@ WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::Realm& realm, size_t in
{
}
void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
void WebAssemblyInstanceObject::initialize(JS::Realm& realm)
{
Object::initialize(global_object);
auto& realm = *global_object.associated_realm();
Object::initialize(realm);
VERIFY(!m_exports_object);
m_exports_object = create(global_object, nullptr);
m_exports_object = create(realm.global_object(), nullptr);
auto& instance = this->instance();
auto& cache = this->cache();
for (auto& export_ : instance.exports()) {
@ -38,7 +36,7 @@ void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
[&](Wasm::FunctionAddress const& address) {
Optional<JS::FunctionObject*> object = cache.function_instances.get(address);
if (!object.has_value()) {
object = create_native_function(global_object, address, export_.name());
object = create_native_function(realm.global_object(), address, export_.name());
cache.function_instances.set(address, *object);
}
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
@ -46,7 +44,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, realm, address);
object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(realm.global_object(), realm, address);
cache.memory_instances.set(address, *object);
}
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);

View file

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

View file

@ -10,9 +10,9 @@
namespace Web::Bindings {
void WebAssemblyInstancePrototype::initialize(JS::GlobalObject& global_object)
void WebAssemblyInstancePrototype::initialize(JS::Realm& realm)
{
Object::initialize(global_object);
Object::initialize(realm);
define_native_accessor("exports", exports_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
}

View file

@ -22,7 +22,7 @@ public:
{
}
virtual void initialize(JS::GlobalObject&) override;
virtual void initialize(JS::Realm&) override;
private:
JS_DECLARE_NATIVE_FUNCTION(exports_getter);

View file

@ -54,12 +54,12 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyMemoryConstructor::construct(Funct
return vm.heap().allocate<WebAssemblyMemoryObject>(global_object, realm, *address);
}
void WebAssemblyMemoryConstructor::initialize(JS::GlobalObject& global_object)
void WebAssemblyMemoryConstructor::initialize(JS::Realm& realm)
{
auto& vm = this->vm();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(realm.global_object());
NativeFunction::initialize(global_object);
NativeFunction::initialize(realm);
define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyMemoryPrototype>("WebAssemblyMemoryPrototype"), 0);
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
}

View file

@ -15,7 +15,7 @@ class WebAssemblyMemoryConstructor : public JS::NativeFunction {
public:
explicit WebAssemblyMemoryConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual void initialize(JS::Realm&) override;
virtual ~WebAssemblyMemoryConstructor() override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;

View file

@ -10,9 +10,9 @@
namespace Web::Bindings {
void WebAssemblyMemoryPrototype::initialize(JS::GlobalObject& global_object)
void WebAssemblyMemoryPrototype::initialize(JS::Realm& realm)
{
Object::initialize(global_object);
Object::initialize(realm);
define_native_accessor("buffer", buffer_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_function("grow", grow, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
}

View file

@ -24,7 +24,7 @@ public:
{
}
virtual void initialize(JS::GlobalObject&) override;
virtual void initialize(JS::Realm&) override;
private:
JS_DECLARE_NATIVE_FUNCTION(grow);

View file

@ -38,12 +38,12 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyModuleConstructor::construct(Funct
return heap().allocate<WebAssemblyModuleObject>(global_object, realm, result);
}
void WebAssemblyModuleConstructor::initialize(JS::GlobalObject& global_object)
void WebAssemblyModuleConstructor::initialize(JS::Realm& realm)
{
auto& vm = this->vm();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(realm.global_object());
NativeFunction::initialize(global_object);
NativeFunction::initialize(realm);
define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyModulePrototype>("WebAssemblyModulePrototype"), 0);
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
}

View file

@ -15,7 +15,7 @@ class WebAssemblyModuleConstructor : public JS::NativeFunction {
public:
explicit WebAssemblyModuleConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual void initialize(JS::Realm&) override;
virtual ~WebAssemblyModuleConstructor() override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;

View file

@ -31,18 +31,18 @@ WebAssemblyObject::WebAssemblyObject(JS::Realm& realm)
s_abstract_machine.enable_instruction_count_limit();
}
void WebAssemblyObject::initialize(JS::GlobalObject& global_object)
void WebAssemblyObject::initialize(JS::Realm& realm)
{
Object::initialize(global_object);
Object::initialize(realm);
u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
define_native_function("validate", validate, 1, attr);
define_native_function("compile", compile, 1, attr);
define_native_function("instantiate", instantiate, 1, attr);
auto& vm = global_object.vm();
auto& vm = this->vm();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(realm.global_object());
auto& memory_constructor = window.ensure_web_constructor<WebAssemblyMemoryConstructor>("WebAssembly.Memory");
memory_constructor.define_direct_property(vm.names.name, js_string(vm, "WebAssembly.Memory"), JS::Attribute::Configurable);
auto& memory_prototype = window.ensure_web_prototype<WebAssemblyMemoryPrototype>("WebAssemblyMemoryPrototype");

View file

@ -24,7 +24,7 @@ class WebAssemblyObject final : public JS::Object {
public:
explicit WebAssemblyObject(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual void initialize(JS::Realm&) override;
virtual ~WebAssemblyObject() override = default;
virtual void visit_edges(Cell::Visitor&) override;

View file

@ -81,12 +81,12 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
return vm.heap().allocate<WebAssemblyTableObject>(global_object, realm, *address);
}
void WebAssemblyTableConstructor::initialize(JS::GlobalObject& global_object)
void WebAssemblyTableConstructor::initialize(JS::Realm& realm)
{
auto& vm = this->vm();
auto& window = static_cast<WindowObject&>(global_object);
auto& window = static_cast<WindowObject&>(realm.global_object());
NativeFunction::initialize(global_object);
NativeFunction::initialize(realm);
define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyTablePrototype>("WebAssemblyTablePrototype"), 0);
define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
}

View file

@ -15,7 +15,7 @@ class WebAssemblyTableConstructor : public JS::NativeFunction {
public:
explicit WebAssemblyTableConstructor(JS::Realm&);
virtual void initialize(JS::GlobalObject&) override;
virtual void initialize(JS::Realm&) override;
virtual ~WebAssemblyTableConstructor() override;
virtual JS::ThrowCompletionOr<JS::Value> call() override;

View file

@ -10,9 +10,9 @@
namespace Web::Bindings {
void WebAssemblyTablePrototype::initialize(JS::GlobalObject& global_object)
void WebAssemblyTablePrototype::initialize(JS::Realm& realm)
{
Object::initialize(global_object);
Object::initialize(realm);
define_native_accessor("length", length_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_function("grow", grow, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_function("get", get, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);

View file

@ -24,7 +24,7 @@ public:
{
}
virtual void initialize(JS::GlobalObject& global_object) override;
virtual void initialize(JS::Realm&) override;
private:
JS_DECLARE_NATIVE_FUNCTION(grow);