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

LibWeb: Split the WebAssemblyInstance object logic into multiple files

Now that we're adding a constructor to it, let's split it up like the
rest of LibWeb does.
This commit is contained in:
Ali Mohammad Pur 2021-07-01 11:19:45 +04:30 committed by Ali Mohammad Pur
parent a4897ed7b2
commit bfb3d9e9d0
7 changed files with 116 additions and 77 deletions

View file

@ -222,10 +222,11 @@ set(SOURCES
UIEvents/EventNames.cpp
UIEvents/MouseEvent.cpp
URLEncoder.cpp
WebAssembly/WebAssemblyInstanceObject.cpp
WebAssembly/WebAssemblyInstanceObjectPrototype.cpp
WebAssembly/WebAssemblyMemoryConstructor.cpp
WebAssembly/WebAssemblyMemoryPrototype.cpp
WebAssembly/WebAssemblyObject.cpp
WebAssembly/WebAssemblyObjectPrototype.cpp
WebContentClient.cpp
XHR/EventNames.cpp
XHR/XMLHttpRequest.cpp

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ScopeGuard.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibWasm/AbstractMachine/Interpreter.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
#include <LibWeb/WebAssembly/WebAssemblyMemoryPrototype.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
namespace Web::Bindings {
WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::GlobalObject& global_object, size_t index)
: Object(static_cast<Web::Bindings::WindowObject&>(global_object).ensure_web_prototype<WebAssemblyInstancePrototype>(class_name()))
, m_index(index)
{
}
void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
{
Object::initialize(global_object);
VERIFY(!m_exports_object);
m_exports_object = create(global_object, nullptr);
auto& instance = this->instance();
auto& cache = this->cache();
for (auto& export_ : instance.exports()) {
export_.value().visit(
[&](const Wasm::FunctionAddress& address) {
auto object = cache.function_instances.get(address);
if (!object.has_value()) {
object = create_native_function(address, export_.name(), global_object);
cache.function_instances.set(address, *object);
}
m_exports_object->define_property(export_.name(), *object);
},
[&](const Wasm::MemoryAddress& address) {
auto object = cache.memory_instances.get(address);
if (!object.has_value()) {
object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(global_object, global_object, address);
cache.memory_instances.set(address, *object);
}
m_exports_object->define_property(export_.name(), *object);
},
[&](const auto&) {
// FIXME: Implement other exports!
});
}
m_exports_object->set_integrity_level(IntegrityLevel::Frozen);
}
void WebAssemblyInstanceObject::visit_edges(Visitor& visitor)
{
Object::visit_edges(visitor);
visitor.visit(m_exports_object);
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/VM.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
namespace Web::Bindings {
class WebAssemblyInstanceObject final : public JS::Object {
JS_OBJECT(WebAssemblyInstanceObject, Object);
public:
explicit WebAssemblyInstanceObject(JS::GlobalObject&, size_t index);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyInstanceObject() override = default;
size_t index() const { return m_index; }
Wasm::ModuleInstance& instance() const { return WebAssemblyObject::s_instantiated_modules.at(m_index); }
auto& cache() { return WebAssemblyObject::s_module_caches.at(m_index); }
void visit_edges(Visitor&) override;
friend class WebAssemblyInstancePrototype;
private:
size_t m_index { 0 };
Object* m_exports_object { nullptr };
};
}

View file

@ -4,8 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
#include <LibWeb/WebAssembly/WebAssemblyObjectPrototype.h>
#include "WebAssemblyInstanceObject.h"
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
namespace Web::Bindings {

View file

@ -14,11 +14,11 @@
namespace Web::Bindings {
class WebAssemblyInstancePrototype final : public JS::Object {
JS_OBJECT(WebAssemblyInstancePrototype, JS::Object);
JS_OBJECT(WebAssemblyInstancePrototype, Object);
public:
explicit WebAssemblyInstancePrototype(JS::GlobalObject& global_object)
: JS::Object(global_object)
: Object(global_object)
{
}

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "WebAssemblyInstanceObject.h"
#include "WebAssemblyMemoryPrototype.h"
#include <AK/ScopeGuard.h>
#include <LibJS/Runtime/Array.h>
@ -16,10 +17,6 @@
namespace Web::Bindings {
static JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String name, JS::GlobalObject& global_object);
static JS::Value to_js_value(Wasm::Value& wasm_value, JS::GlobalObject& global_object);
static Optional<Wasm::Value> to_webassembly_value(JS::Value value, const Wasm::ValueType& type, JS::GlobalObject& global_object);
WebAssemblyObject::WebAssemblyObject(JS::GlobalObject& global_object)
: Object(*global_object.object_prototype())
{
@ -307,12 +304,6 @@ WebAssemblyModuleObject::WebAssemblyModuleObject(JS::GlobalObject& global_object
{
}
WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::GlobalObject& global_object, size_t index)
: Object(static_cast<WindowObject&>(global_object).ensure_web_prototype<WebAssemblyInstancePrototype>(class_name()))
, m_index(index)
{
}
JS::Value to_js_value(Wasm::Value& wasm_value, JS::GlobalObject& global_object)
{
switch (wasm_value.type().kind()) {
@ -428,46 +419,6 @@ JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String
return function;
}
void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
{
Object::initialize(global_object);
VERIFY(!m_exports_object);
m_exports_object = JS::Object::create(global_object, nullptr);
auto& instance = this->instance();
auto& cache = this->cache();
for (auto& export_ : instance.exports()) {
export_.value().visit(
[&](const Wasm::FunctionAddress& address) {
auto object = cache.function_instances.get(address);
if (!object.has_value()) {
object = create_native_function(address, export_.name(), global_object);
cache.function_instances.set(address, *object);
}
m_exports_object->define_property(export_.name(), *object);
},
[&](const Wasm::MemoryAddress& address) {
auto object = cache.memory_instances.get(address);
if (!object.has_value()) {
object = heap().allocate<WebAssemblyMemoryObject>(global_object, global_object, address);
cache.memory_instances.set(address, *object);
}
m_exports_object->define_property(export_.name(), *object);
},
[&](const auto&) {
// FIXME: Implement other exports!
});
}
m_exports_object->set_integrity_level(IntegrityLevel::Frozen);
}
void WebAssemblyInstanceObject::visit_edges(Cell::Visitor& visitor)
{
Object::visit_edges(visitor);
visitor.visit(m_exports_object);
}
WebAssemblyMemoryObject::WebAssemblyMemoryObject(JS::GlobalObject& global_object, Wasm::MemoryAddress address)
: Object(static_cast<WindowObject&>(global_object).ensure_web_prototype<WebAssemblyMemoryPrototype>(class_name()))
, m_address(address)

View file

@ -9,11 +9,14 @@
#include <LibJS/Runtime/Object.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebAssembly/WebAssemblyObjectPrototype.h>
#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
namespace Web::Bindings {
class WebAssemblyMemoryObject;
JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String name, JS::GlobalObject& global_object);
JS::Value to_js_value(Wasm::Value& wasm_value, JS::GlobalObject& global_object);
Optional<Wasm::Value> to_webassembly_value(JS::Value value, const Wasm::ValueType& type, JS::GlobalObject& global_object);
class WebAssemblyObject final : public JS::Object {
JS_OBJECT(WebAssemblyObject, JS::Object);
@ -73,27 +76,6 @@ private:
size_t m_index { 0 };
};
class WebAssemblyInstanceObject final : public JS::Object {
JS_OBJECT(WebAssemblyInstanceObject, JS::Object);
public:
explicit WebAssemblyInstanceObject(JS::GlobalObject&, size_t index);
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyInstanceObject() override = default;
size_t index() const { return m_index; }
Wasm::ModuleInstance& instance() const { return WebAssemblyObject::s_instantiated_modules.at(m_index); }
auto& cache() { return WebAssemblyObject::s_module_caches.at(m_index); }
void visit_edges(Cell::Visitor&) override;
friend class WebAssemblyInstancePrototype;
private:
size_t m_index { 0 };
JS::Object* m_exports_object { nullptr };
};
class WebAssemblyMemoryObject final : public JS::Object {
JS_OBJECT(WebAssemblyMemoryObject, JS::Object);