From a6476ad61f8924d3f2632937d6a22d3e4df4adc8 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 31 Dec 2021 14:59:11 +0100 Subject: [PATCH] test-wasm: Convert WebAssemblyModule::create() to ThrowCompletionOr This exposed a missing exception check in parseWebAssemblyModule(), which could throw but still return a normal completion (which currently works as we check VM::exception() at the right point, but breaks when moving everything to exceptions). --- Tests/LibWasm/test-wasm.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index e748ae5f71..b25555ea75 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -38,22 +38,21 @@ public: Wasm::Module& module() { return *m_module; } Wasm::ModuleInstance& module_instance() { return *m_module_instance; } - static WebAssemblyModule* create(JS::GlobalObject& global_object, Wasm::Module module, HashMap const& imports) + static JS::ThrowCompletionOr create(JS::GlobalObject& global_object, Wasm::Module module, HashMap const& imports) { - auto instance = global_object.heap().allocate(global_object, *global_object.object_prototype()); + auto& vm = global_object.vm(); + auto* instance = global_object.heap().allocate(global_object, *global_object.object_prototype()); instance->m_module = move(module); Wasm::Linker linker(*instance->m_module); linker.link(imports); linker.link(spec_test_namespace()); auto link_result = linker.finish(); - if (link_result.is_error()) { - global_object.vm().throw_exception(global_object, "Link failed"); - } else { - if (auto result = machine().instantiate(*instance->m_module, link_result.release_value()); result.is_error()) - global_object.vm().throw_exception(global_object, result.release_error().error); - else - instance->m_module_instance = result.release_value(); - } + if (link_result.is_error()) + return vm.throw_completion(global_object, "Link failed"); + auto result = machine().instantiate(*instance->m_module, link_result.release_value()); + if (result.is_error()) + return vm.throw_completion(global_object, result.release_error().error); + instance->m_module_instance = result.release_value(); return instance; } void initialize(JS::GlobalObject&) override; @@ -125,7 +124,7 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) } } - return JS::Value(WebAssemblyModule::create(global_object, result.release_value(), imports)); + return JS::Value(TRY(WebAssemblyModule::create(global_object, result.release_value(), imports))); } TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)