diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index 14fff623f28..0ecd294ec6c 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -60,7 +60,7 @@ private: static Wasm::AbstractMachine m_machine; Optional m_module; - Optional m_module_instance; + OwnPtr m_module_instance; }; Wasm::AbstractMachine WebAssemblyModule::m_machine; @@ -149,15 +149,6 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) auto address = static_cast(vm.argument(0).to_double(global_object)); if (vm.exception()) return {}; - auto this_value = vm.this_value(global_object); - auto object = this_value.to_object(global_object); - if (vm.exception()) - return {}; - if (!object || !is(object)) { - vm.throw_exception(global_object, "Not a WebAssemblyModule"); - return {}; - } - auto instance = static_cast(object); Wasm::FunctionAddress function_address { address }; auto function_instance = WebAssemblyModule::machine().store().get(function_address); if (!function_instance) { diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp index 9383927c9a4..6179e1b3b20 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.cpp @@ -85,7 +85,8 @@ GlobalInstance* Store::get(GlobalAddress address) InstantiationResult AbstractMachine::instantiate(const Module& module, Vector externs) { - ModuleInstance main_module_instance; + auto main_module_instance_pointer = make(); + auto& main_module_instance = *main_module_instance_pointer; Optional instantiation_result; module.for_each_section_of_type([&](const TypeSection& section) { @@ -181,7 +182,10 @@ InstantiationResult AbstractMachine::instantiate(const Module& module, Vector AbstractMachine::allocate_all(const Module& module, ModuleInstance& module_instance, Vector& externs, Vector& global_values) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index 1f06b76f795..aa480ae70d4 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -219,7 +219,7 @@ public: auto& code() const { return m_code; } private: - const FunctionType& m_type; + FunctionType m_type; const ModuleInstance& m_module; const Module::Function& m_code; }; @@ -237,7 +237,7 @@ public: private: FlatPtr m_ptr { 0 }; - const FunctionType& m_type; + FunctionType m_type; }; using FunctionInstance = Variant; @@ -418,7 +418,7 @@ private: Vector m_data; }; -using InstantiationResult = AK::Result; +using InstantiationResult = AK::Result, InstantiationError>; class AbstractMachine { public: diff --git a/Userland/Libraries/LibWasm/Types.h b/Userland/Libraries/LibWasm/Types.h index 4b834d74655..99060357152 100644 --- a/Userland/Libraries/LibWasm/Types.h +++ b/Userland/Libraries/LibWasm/Types.h @@ -955,10 +955,10 @@ class Module { public: class Function { public: - explicit Function(TypeIndex type, Vector local_types, const Expression& body) + explicit Function(TypeIndex type, Vector local_types, Expression body) : m_type(type) , m_local_types(move(local_types)) - , m_body(body) + , m_body(move(body)) { } @@ -969,7 +969,7 @@ public: private: TypeIndex m_type; Vector m_local_types; - const Expression& m_body; + Expression m_body; }; using AnySection = Variant< diff --git a/Userland/Utilities/wasm.cpp b/Userland/Utilities/wasm.cpp index eed9f3ccd09..2100be47a89 100644 --- a/Userland/Utilities/wasm.cpp +++ b/Userland/Utilities/wasm.cpp @@ -91,7 +91,7 @@ int main(int argc, char* argv[]) }; if (print) { // Now, let's dump the functions! - for (auto& address : module_instance.functions()) { + for (auto& address : module_instance->functions()) { print_func(address); } } @@ -99,7 +99,7 @@ int main(int argc, char* argv[]) if (!exported_function_to_execute.is_empty()) { Optional run_address; Vector values; - for (auto& entry : module_instance.exports()) { + for (auto& entry : module_instance->exports()) { if (entry.name() == exported_function_to_execute) { if (auto addr = entry.value().get_pointer()) run_address = *addr;