From 0090b916dda783d175c868e2f432bff4eeb246f2 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Fri, 5 Apr 2024 00:39:53 +0200 Subject: [PATCH] LibJS: Make ParserError::to_string infallible --- Meta/Lagom/Wasm/js_repl.cpp | 4 ++-- Userland/Applications/Spreadsheet/Spreadsheet.cpp | 2 +- Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp | 2 +- Userland/Libraries/LibJS/ParserError.cpp | 6 +++--- Userland/Libraries/LibJS/ParserError.h | 2 +- Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp | 2 +- Userland/Libraries/LibJS/Runtime/FunctionConstructor.cpp | 6 +++--- Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp | 2 +- Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp | 2 +- Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp | 2 +- Userland/Utilities/js.cpp | 4 ++-- 11 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Meta/Lagom/Wasm/js_repl.cpp b/Meta/Lagom/Wasm/js_repl.cpp index 0eab1a3f62d..77ad4735eb9 100644 --- a/Meta/Lagom/Wasm/js_repl.cpp +++ b/Meta/Lagom/Wasm/js_repl.cpp @@ -133,7 +133,7 @@ static ErrorOr parse_and_run(JS::Realm& realm, StringView source, StringVi if (!hint.is_empty()) displayln("{}", hint); - auto error_string = TRY(error.to_string()); + auto error_string = error.to_string(); displayln("{}", error_string); result = g_vm->throw_completion(move(error_string)); } else { @@ -149,7 +149,7 @@ static ErrorOr parse_and_run(JS::Realm& realm, StringView source, StringVi if (!hint.is_empty()) displayln("{}", hint); - auto error_string = TRY(error.to_string()); + auto error_string = error.to_string(); displayln("{}", error_string); result = g_vm->throw_completion(move(error_string)); } else { diff --git a/Userland/Applications/Spreadsheet/Spreadsheet.cpp b/Userland/Applications/Spreadsheet/Spreadsheet.cpp index 70e6ef97d6a..c01ad03e8c5 100644 --- a/Userland/Applications/Spreadsheet/Spreadsheet.cpp +++ b/Userland/Applications/Spreadsheet/Spreadsheet.cpp @@ -167,7 +167,7 @@ JS::ThrowCompletionOr Sheet::evaluate(StringView source, Cell* on_beh name); if (script_or_error.is_error()) - return vm().throw_completion(TRY_OR_THROW_OOM(vm(), script_or_error.error().first().to_string())); + return vm().throw_completion(script_or_error.error().first().to_string()); return vm().bytecode_interpreter().run(script_or_error.value()); } diff --git a/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp b/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp index adfced67cc2..5fc7a3f17cb 100644 --- a/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp +++ b/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp @@ -100,7 +100,7 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script) auto& error = script_or_error.error()[0]; // b. Return Completion { [[Type]]: throw, [[Value]]: error, [[Target]]: empty }. - return vm.throw_completion(TRY_OR_THROW_OOM(vm, error.to_string())); + return vm.throw_completion(error.to_string()); } // 5. Let status be ScriptEvaluation(s). diff --git a/Userland/Libraries/LibJS/ParserError.cpp b/Userland/Libraries/LibJS/ParserError.cpp index 66217f1b309..b47ab1584df 100644 --- a/Userland/Libraries/LibJS/ParserError.cpp +++ b/Userland/Libraries/LibJS/ParserError.cpp @@ -12,11 +12,11 @@ namespace JS { -ErrorOr ParserError::to_string() const +String ParserError::to_string() const { if (!position.has_value()) - return String::from_byte_string(message); - return String::formatted("{} (line: {}, column: {})", message, position.value().line, position.value().column); + return MUST(String::from_byte_string(message)); + return MUST(String::formatted("{} (line: {}, column: {})", message, position.value().line, position.value().column)); } ByteString ParserError::to_byte_string() const diff --git a/Userland/Libraries/LibJS/ParserError.h b/Userland/Libraries/LibJS/ParserError.h index 48cae7992b4..143b1121d26 100644 --- a/Userland/Libraries/LibJS/ParserError.h +++ b/Userland/Libraries/LibJS/ParserError.h @@ -19,7 +19,7 @@ struct ParserError { ByteString message; Optional position; - ErrorOr to_string() const; + String to_string() const; ByteString to_byte_string() const; ByteString source_location_hint(StringView source, char const spacer = ' ', char const indicator = '^') const; }; diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 3eee9d53eb8..d1aef644669 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -586,7 +586,7 @@ ThrowCompletionOr perform_eval(VM& vm, Value x, CallerMode strict_caller, // b. If script is a List of errors, throw a SyntaxError exception. if (parser.has_errors()) { auto& error = parser.errors()[0]; - return vm.throw_completion(TRY_OR_THROW_OOM(vm, error.to_string())); + return vm.throw_completion(error.to_string()); } bool strict_eval = false; diff --git a/Userland/Libraries/LibJS/Runtime/FunctionConstructor.cpp b/Userland/Libraries/LibJS/Runtime/FunctionConstructor.cpp index 9e6ab208224..f53d61c19a6 100644 --- a/Userland/Libraries/LibJS/Runtime/FunctionConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/FunctionConstructor.cpp @@ -178,7 +178,7 @@ ThrowCompletionOr FunctionConstructor::create_dynamic // 17. If parameters is a List of errors, throw a SyntaxError exception. if (parameters_parser.has_errors()) { auto error = parameters_parser.errors()[0]; - return vm.throw_completion(TRY_OR_THROW_OOM(vm, error.to_string())); + return vm.throw_completion(error.to_string()); } // 18. Let body be ParseText(StringToCodePoints(bodyString), bodySym). @@ -188,7 +188,7 @@ ThrowCompletionOr FunctionConstructor::create_dynamic // 19. If body is a List of errors, throw a SyntaxError exception. if (body_parser.has_errors()) { auto error = body_parser.errors()[0]; - return vm.throw_completion(TRY_OR_THROW_OOM(vm, error.to_string())); + return vm.throw_completion(error.to_string()); } // 20. NOTE: The parameters and body are parsed separately to ensure that each is valid alone. For example, new Function("/*", "*/ ) {") is not legal. @@ -202,7 +202,7 @@ ThrowCompletionOr FunctionConstructor::create_dynamic // 23. If expr is a List of errors, throw a SyntaxError exception. if (source_parser.has_errors()) { auto error = source_parser.errors()[0]; - return vm.throw_completion(TRY_OR_THROW_OOM(vm, error.to_string())); + return vm.throw_completion(error.to_string()); } // 24. Let proto be ? GetPrototypeFromConstructor(newTarget, fallbackProto). diff --git a/Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp b/Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp index 8ff2a0e8502..297d17dac92 100644 --- a/Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp +++ b/Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp @@ -110,7 +110,7 @@ ThrowCompletionOr perform_shadow_realm_eval(VM& vm, StringView source_tex // b. If script is a List of errors, throw a SyntaxError exception. if (parser.has_errors()) { auto& error = parser.errors()[0]; - return vm.throw_completion(TRY_OR_THROW_OOM(vm, error.to_string())); + return vm.throw_completion(error.to_string()); } // c. If script Contains ScriptBody is false, return undefined. diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp index 6b89a9c0bdc..dc47865afc9 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp @@ -59,7 +59,7 @@ JS::NonnullGCPtr ClassicScript::create(ByteString filename, Strin dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to parse: {}", parse_error.to_byte_string()); // 1. Set script's parse error and its error to rethrow to result[0]. - script->set_parse_error(JS::SyntaxError::create(environment_settings_object.realm(), parse_error.to_string().release_value_but_fixme_should_propagate_errors())); + script->set_parse_error(JS::SyntaxError::create(environment_settings_object.realm(), parse_error.to_string())); script->set_error_to_rethrow(script->parse_error()); // 2. Return script. diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp b/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp index 111f75142ba..afbc24cda44 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp +++ b/Userland/Libraries/LibWeb/HTML/Scripting/ModuleScript.cpp @@ -62,7 +62,7 @@ WebIDL::ExceptionOr> JavaScriptModuleScript::c dbgln("JavaScriptModuleScript: Failed to parse: {}", parse_error.to_byte_string()); // 1. Set script's parse error to result[0]. - script->set_parse_error(JS::SyntaxError::create(settings_object.realm(), parse_error.to_string().release_value_but_fixme_should_propagate_errors())); + script->set_parse_error(JS::SyntaxError::create(settings_object.realm(), parse_error.to_string())); // 2. Return script. return script; diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 2aa475b1b65..8f428110385 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -222,7 +222,7 @@ static ErrorOr parse_and_run(JS::Realm& realm, StringView source, StringVi if (!hint.is_empty()) outln("{}", hint); - auto error_string = TRY(error.to_string()); + auto error_string = error.to_string(); outln("{}", error_string); result = vm.throw_completion(move(error_string)); } else { @@ -236,7 +236,7 @@ static ErrorOr parse_and_run(JS::Realm& realm, StringView source, StringVi if (!hint.is_empty()) outln("{}", hint); - auto error_string = TRY(error.to_string()); + auto error_string = error.to_string(); outln("{}", error_string); result = vm.throw_completion(move(error_string)); } else {