From 88814acbd39ba252167e48f48f5b938df142065b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 16 Feb 2023 14:09:11 -0500 Subject: LibJS+Everywhere: Convert JS::Error to String This includes an Error::create overload to create an Error from a UTF-8 StringView. If creating a String from that view fails, the factory will return an OOM InternalError instead. VM::throw_completion can also make use of this overload via its perfect forwarding. --- Tests/LibJS/test262-runner.cpp | 12 +++++++++--- Tests/LibWasm/test-wasm.cpp | 34 ++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 17 deletions(-) (limited to 'Tests') diff --git a/Tests/LibJS/test262-runner.cpp b/Tests/LibJS/test262-runner.cpp index a7fcba1e60..95d939520a 100644 --- a/Tests/LibJS/test262-runner.cpp +++ b/Tests/LibJS/test262-runner.cpp @@ -91,9 +91,15 @@ static Result run_program(InterpreterT& interpreter, ScriptOrMo return visitor->parse_node(); }); - auto unit_result = JS::Bytecode::Generator::generate(program_node); - if (unit_result.is_error()) { - result = JS::throw_completion(JS::InternalError::create(interpreter.realm(), DeprecatedString::formatted("TODO({})", unit_result.error().to_deprecated_string()))); + auto& vm = interpreter.vm(); + + if (auto unit_result = JS::Bytecode::Generator::generate(program_node); unit_result.is_error()) { + if (auto error_string = unit_result.error().to_string(); error_string.is_error()) + result = vm.template throw_completion(vm.error_message(JS::VM::ErrorMessage::OutOfMemory)); + else if (error_string = String::formatted("TODO({})", error_string.value()); error_string.is_error()) + result = vm.template throw_completion(vm.error_message(JS::VM::ErrorMessage::OutOfMemory)); + else + result = JS::throw_completion(JS::InternalError::create(interpreter.realm(), error_string.release_value())); } else { auto unit = unit_result.release_value(); auto optimization_level = s_enable_bytecode_optimizations ? JS::Bytecode::Interpreter::OptimizationLevel::Optimize : JS::Bytecode::Interpreter::OptimizationLevel::Default; diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index 29c7c70139..233f9f90f5 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -15,20 +15,26 @@ TEST_ROOT("Userland/Libraries/LibWasm/Tests"); TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile) { auto& realm = *vm.current_realm(); + + auto error_code_to_string = [](int code) { + auto const* error_string = strerror(code); + return StringView { error_string, strlen(error_string) }; + }; + auto filename = TRY(vm.argument(0).to_deprecated_string(vm)); auto file = Core::File::open(filename, Core::File::OpenMode::Read); if (file.is_error()) - return vm.throw_completion(strerror(file.error().code())); + return vm.throw_completion(error_code_to_string(file.error().code())); auto file_size = file.value()->size(); if (file_size.is_error()) - return vm.throw_completion(strerror(file_size.error().code())); + return vm.throw_completion(error_code_to_string(file_size.error().code())); auto array = TRY(JS::Uint8Array::create(realm, file_size.value())); auto read = file.value()->read(array->data()); if (read.is_error()) - return vm.throw_completion(strerror(read.error().code())); + return vm.throw_completion(error_code_to_string(read.error().code())); return JS::Value(array); } @@ -57,7 +63,7 @@ public: linker.link(spec_test_namespace()); auto link_result = linker.finish(); if (link_result.is_error()) - return vm.throw_completion("Link failed"); + return vm.throw_completion("Link failed"sv); auto result = machine().instantiate(*instance->m_module, link_result.release_value()); if (result.is_error()) return vm.throw_completion(result.release_error().error); @@ -103,7 +109,7 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) auto& realm = *vm.current_realm(); auto* object = TRY(vm.argument(0).to_object(vm)); if (!is(object)) - return vm.throw_completion("Expected a Uint8Array argument to parse_webassembly_module"); + return vm.throw_completion("Expected a Uint8Array argument to parse_webassembly_module"sv); auto& array = static_cast(*object); FixedMemoryStream stream { array.data() }; auto result = Wasm::Module::parse(stream); @@ -133,11 +139,11 @@ TESTJS_GLOBAL_FUNCTION(compare_typed_arrays, compareTypedArrays) { auto* lhs = TRY(vm.argument(0).to_object(vm)); if (!is(lhs)) - return vm.throw_completion("Expected a TypedArray"); + return vm.throw_completion("Expected a TypedArray"sv); auto& lhs_array = static_cast(*lhs); auto* rhs = TRY(vm.argument(1).to_object(vm)); if (!is(rhs)) - return vm.throw_completion("Expected a TypedArray"); + return vm.throw_completion("Expected a TypedArray"sv); auto& rhs_array = static_cast(*rhs); return JS::Value(lhs_array.viewed_array_buffer()->buffer() == rhs_array.viewed_array_buffer()->buffer()); } @@ -157,7 +163,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export) auto this_value = vm.this_value(); auto* object = TRY(this_value.to_object(vm)); if (!is(object)) - return vm.throw_completion("Not a WebAssemblyModule"); + return vm.throw_completion("Not a WebAssemblyModule"sv); auto instance = static_cast(object); for (auto& entry : instance->module_instance().exports()) { if (entry.name() == name) { @@ -175,10 +181,10 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export) [&](const auto& ref) -> JS::Value { return JS::Value(static_cast(ref.address.value())); }); }); } - return vm.throw_completion(DeprecatedString::formatted("'{}' does not refer to a function or a global", name)); + return vm.throw_completion(TRY_OR_THROW_OOM(vm, String::formatted("'{}' does not refer to a function or a global", name))); } } - return vm.throw_completion(DeprecatedString::formatted("'{}' could not be found", name)); + return vm.throw_completion(TRY_OR_THROW_OOM(vm, String::formatted("'{}' could not be found", name))); } JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) @@ -187,16 +193,16 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) Wasm::FunctionAddress function_address { address }; auto function_instance = WebAssemblyModule::machine().store().get(function_address); if (!function_instance) - return vm.throw_completion("Invalid function address"); + return vm.throw_completion("Invalid function address"sv); Wasm::FunctionType const* type { nullptr }; function_instance->visit([&](auto& value) { type = &value.type(); }); if (!type) - return vm.throw_completion("Invalid function found at given address"); + return vm.throw_completion("Invalid function found at given address"sv); Vector arguments; if (type->parameters().size() + 1 > vm.argument_count()) - return vm.throw_completion(DeprecatedString::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count())); + return vm.throw_completion(TRY_OR_THROW_OOM(vm, String::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count()))); size_t index = 1; for (auto& param : type->parameters()) { auto argument = vm.argument(index++); @@ -238,7 +244,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) auto result = WebAssemblyModule::machine().invoke(function_address, arguments); if (result.is_trap()) - return vm.throw_completion(DeprecatedString::formatted("Execution trapped: {}", result.trap().reason)); + return vm.throw_completion(TRY_OR_THROW_OOM(vm, String::formatted("Execution trapped: {}", result.trap().reason))); if (result.values().is_empty()) return JS::js_null(); -- cgit v1.2.3