diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-02-16 14:09:11 -0500 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2023-02-17 09:14:23 -0500 |
commit | 88814acbd39ba252167e48f48f5b938df142065b (patch) | |
tree | c00f3aba6c299cb6d131204274fa4b4a3e532488 /Tests | |
parent | 153b7936381916aef185686d84e255380bb80f1d (diff) | |
download | serenity-88814acbd39ba252167e48f48f5b938df142065b.zip |
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.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/LibJS/test262-runner.cpp | 12 | ||||
-rw-r--r-- | Tests/LibWasm/test-wasm.cpp | 34 |
2 files changed, 29 insertions, 17 deletions
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<void, TestError> 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<JS::InternalError>(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<JS::InternalError>(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<JS::TypeError>(strerror(file.error().code())); + return vm.throw_completion<JS::TypeError>(error_code_to_string(file.error().code())); auto file_size = file.value()->size(); if (file_size.is_error()) - return vm.throw_completion<JS::TypeError>(strerror(file_size.error().code())); + return vm.throw_completion<JS::TypeError>(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<JS::TypeError>(strerror(read.error().code())); + return vm.throw_completion<JS::TypeError>(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<JS::TypeError>("Link failed"); + return vm.throw_completion<JS::TypeError>("Link failed"sv); auto result = machine().instantiate(*instance->m_module, link_result.release_value()); if (result.is_error()) return vm.throw_completion<JS::TypeError>(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<JS::Uint8Array>(object)) - return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module"); + return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module"sv); auto& array = static_cast<JS::Uint8Array&>(*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<JS::TypedArrayBase>(lhs)) - return vm.throw_completion<JS::TypeError>("Expected a TypedArray"); + return vm.throw_completion<JS::TypeError>("Expected a TypedArray"sv); auto& lhs_array = static_cast<JS::TypedArrayBase&>(*lhs); auto* rhs = TRY(vm.argument(1).to_object(vm)); if (!is<JS::TypedArrayBase>(rhs)) - return vm.throw_completion<JS::TypeError>("Expected a TypedArray"); + return vm.throw_completion<JS::TypeError>("Expected a TypedArray"sv); auto& rhs_array = static_cast<JS::TypedArrayBase&>(*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<WebAssemblyModule>(object)) - return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule"); + return vm.throw_completion<JS::TypeError>("Not a WebAssemblyModule"sv); auto instance = static_cast<WebAssemblyModule*>(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<double>(ref.address.value())); }); }); } - return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("'{}' does not refer to a function or a global", name)); + return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' does not refer to a function or a global", name))); } } - return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("'{}' could not be found", name)); + return vm.throw_completion<JS::TypeError>(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<JS::TypeError>("Invalid function address"); + return vm.throw_completion<JS::TypeError>("Invalid function address"sv); Wasm::FunctionType const* type { nullptr }; function_instance->visit([&](auto& value) { type = &value.type(); }); if (!type) - return vm.throw_completion<JS::TypeError>("Invalid function found at given address"); + return vm.throw_completion<JS::TypeError>("Invalid function found at given address"sv); Vector<Wasm::Value> arguments; if (type->parameters().size() + 1 > vm.argument_count()) - return vm.throw_completion<JS::TypeError>(DeprecatedString::formatted("Expected {} arguments for call, but found {}", type->parameters().size() + 1, vm.argument_count())); + return vm.throw_completion<JS::TypeError>(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<JS::TypeError>(DeprecatedString::formatted("Execution trapped: {}", result.trap().reason)); + return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("Execution trapped: {}", result.trap().reason))); if (result.values().is_empty()) return JS::js_null(); |