diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-07-01 11:19:45 +0430 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-07-02 04:53:01 +0430 |
commit | bfb3d9e9d03aeac338577fdcf72230c479602ca2 (patch) | |
tree | a2dde944867810080c853beb519472b6eebec63a /Userland/Libraries/LibWeb/WebAssembly | |
parent | a4897ed7b2fb15d637b3ddfaf896ca7c0cb6b4d2 (diff) | |
download | serenity-bfb3d9e9d03aeac338577fdcf72230c479602ca2.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibWeb/WebAssembly')
-rw-r--r-- | Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp | 66 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.h | 39 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.cpp (renamed from Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObjectPrototype.cpp) | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h (renamed from Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObjectPrototype.h) | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp | 51 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h | 26 |
6 files changed, 114 insertions, 76 deletions
diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp new file mode 100644 index 0000000000..d0382e0a9a --- /dev/null +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp @@ -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); +} + +} diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.h b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.h new file mode 100644 index 0000000000..4d342603fa --- /dev/null +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.h @@ -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 }; +}; + +} diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObjectPrototype.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.cpp index 978c2dcad9..3de1657f39 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObjectPrototype.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.cpp @@ -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 { diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObjectPrototype.h b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h index b65759598b..d312345a9e 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObjectPrototype.h +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h @@ -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) { } diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp index e593564458..f33b78d24d 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp @@ -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) diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h index 1229545e53..0a51085a69 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h @@ -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); |