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

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).
This commit is contained in:
Linus Groh 2021-12-31 14:59:11 +01:00
parent faad7a3ed1
commit a6476ad61f

View file

@ -38,22 +38,21 @@ public:
Wasm::Module& module() { return *m_module; } Wasm::Module& module() { return *m_module; }
Wasm::ModuleInstance& module_instance() { return *m_module_instance; } Wasm::ModuleInstance& module_instance() { return *m_module_instance; }
static WebAssemblyModule* create(JS::GlobalObject& global_object, Wasm::Module module, HashMap<Wasm::Linker::Name, Wasm::ExternValue> const& imports) static JS::ThrowCompletionOr<WebAssemblyModule*> create(JS::GlobalObject& global_object, Wasm::Module module, HashMap<Wasm::Linker::Name, Wasm::ExternValue> const& imports)
{ {
auto instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype()); auto& vm = global_object.vm();
auto* instance = global_object.heap().allocate<WebAssemblyModule>(global_object, *global_object.object_prototype());
instance->m_module = move(module); instance->m_module = move(module);
Wasm::Linker linker(*instance->m_module); Wasm::Linker linker(*instance->m_module);
linker.link(imports); linker.link(imports);
linker.link(spec_test_namespace()); linker.link(spec_test_namespace());
auto link_result = linker.finish(); auto link_result = linker.finish();
if (link_result.is_error()) { if (link_result.is_error())
global_object.vm().throw_exception<JS::TypeError>(global_object, "Link failed"); return vm.throw_completion<JS::TypeError>(global_object, "Link failed");
} else { auto result = machine().instantiate(*instance->m_module, link_result.release_value());
if (auto result = machine().instantiate(*instance->m_module, link_result.release_value()); result.is_error()) if (result.is_error())
global_object.vm().throw_exception<JS::TypeError>(global_object, result.release_error().error); return vm.throw_completion<JS::TypeError>(global_object, result.release_error().error);
else instance->m_module_instance = result.release_value();
instance->m_module_instance = result.release_value();
}
return instance; return instance;
} }
void initialize(JS::GlobalObject&) override; 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) TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays)