diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-05-10 15:40:49 +0430 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-21 00:15:23 +0100 |
commit | efb106069bb434e96aac3391fd2d81ca68007000 (patch) | |
tree | 5076e43293a5d78343dd2647ca5c0c9efe01ecc0 /Tests | |
parent | 24b2a6c93a6fcb8d539d80dca23a68f8e44528b8 (diff) | |
download | serenity-efb106069bb434e96aac3391fd2d81ca68007000.zip |
LibWasm: Decouple ModuleInstance from the AbstractMachine
This fixes a FIXME and will allow linking only select modules together,
instead of linking every instantiated module into a big mess of exported
entities :P
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/LibWasm/test-wasm.cpp | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index ac2eea0b06..14fff623f2 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -31,18 +31,23 @@ class WebAssemblyModule final : public JS::Object { JS_OBJECT(WebAssemblyModule, JS::Object); public: - // FIXME: This should only contain an instantiated module, not the entire abstract machine! explicit WebAssemblyModule(JS::Object& prototype) : JS::Object(prototype) { } + static Wasm::AbstractMachine& machine() { return m_machine; } + Wasm::Module& module() { return *m_module; } + Wasm::ModuleInstance& module_instance() { return *m_module_instance; } + static WebAssemblyModule* create(JS::GlobalObject& global_object, Wasm::Module module) { auto instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype()); instance->m_module = move(module); - if (auto result = instance->m_machine.instantiate(*instance->m_module, {}); result.is_error()) + if (auto result = machine().instantiate(*instance->m_module, {}); result.is_error()) global_object.vm().throw_exception<JS::TypeError>(global_object, result.release_error().error); + else + instance->m_module_instance = result.release_value(); return instance; } void initialize(JS::GlobalObject&) override; @@ -53,10 +58,13 @@ private: JS_DECLARE_NATIVE_FUNCTION(get_export); JS_DECLARE_NATIVE_FUNCTION(wasm_invoke); - Wasm::AbstractMachine m_machine; + static Wasm::AbstractMachine m_machine; Optional<Wasm::Module> m_module; + Optional<Wasm::ModuleInstance> m_module_instance; }; +Wasm::AbstractMachine WebAssemblyModule::m_machine; + TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) { auto object = vm.argument(0).to_object(global_object); @@ -123,7 +131,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export) return {}; } auto instance = static_cast<WebAssemblyModule*>(object); - for (auto& entry : instance->m_machine.module_instance().exports()) { + for (auto& entry : instance->module_instance().exports()) { if (entry.name() == name) { auto& value = entry.value(); if (auto ptr = value.get_pointer<Wasm::FunctionAddress>()) @@ -151,7 +159,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) } auto instance = static_cast<WebAssemblyModule*>(object); Wasm::FunctionAddress function_address { address }; - auto function_instance = instance->m_machine.store().get(function_address); + auto function_instance = WebAssemblyModule::machine().store().get(function_address); if (!function_instance) { vm.throw_exception<JS::TypeError>(global_object, "Invalid function address"); return {}; @@ -194,7 +202,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) } } - auto result = instance->m_machine.invoke(function_address, arguments); + auto result = WebAssemblyModule::machine().invoke(function_address, arguments); if (result.is_trap()) { vm.throw_exception<JS::TypeError>(global_object, "Execution trapped"); return {}; |