diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-09-18 19:41:25 +0300 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2021-09-18 22:59:15 +0300 |
commit | e65aeee67d6ac291e8b3346199ea32e7d502462a (patch) | |
tree | 9414ff61f029fd1b6823333e3ba5b59bd3d534aa /Userland/Libraries/LibJS/Runtime/Intl | |
parent | 768009e0050da2b55e4b730e9114fb56002c1e52 (diff) | |
download | serenity-e65aeee67d6ac291e8b3346199ea32e7d502462a.zip |
LibJS: Convert DisplayNames AOs to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Intl')
3 files changed, 17 insertions, 29 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp index 876d00d271..c720533c02 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp @@ -99,69 +99,59 @@ StringView DisplayNames::fallback_string() const } // 12.1.1 CanonicalCodeForDisplayNames ( type, code ), https://tc39.es/ecma402/#sec-canonicalcodefordisplaynames -Value canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code) +ThrowCompletionOr<Value> canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code) { auto& vm = global_object.vm(); // 1. If type is "language", then if (type == DisplayNames::Type::Language) { // a. If code does not match the unicode_language_id production, throw a RangeError exception. - if (!Unicode::parse_unicode_language_id(code).has_value()) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "language"sv); - return {}; - } + if (!Unicode::parse_unicode_language_id(code).has_value()) + return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "language"sv); // b. If IsStructurallyValidLanguageTag(code) is false, throw a RangeError exception. auto locale_id = is_structurally_valid_language_tag(code); - if (!locale_id.has_value()) { - vm.throw_exception<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, code); - return {}; - } + if (!locale_id.has_value()) + return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, code); // c. Set code to CanonicalizeUnicodeLocaleId(code). // d. Return code. auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id); - return js_string(vm, move(canonicalized_tag)); + return { js_string(vm, move(canonicalized_tag)) }; } // 2. If type is "region", then if (type == DisplayNames::Type::Region) { // a. If code does not match the unicode_region_subtag production, throw a RangeError exception. - if (!Unicode::is_unicode_region_subtag(code)) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "region"sv); - return {}; - } + if (!Unicode::is_unicode_region_subtag(code)) + return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "region"sv); // b. Let code be the result of mapping code to upper case as described in 6.1. // c. Return code. - return js_string(vm, code.to_uppercase_string()); + return { js_string(vm, code.to_uppercase_string()) }; } // 3. If type is "script", then if (type == DisplayNames::Type::Script) { // a. If code does not match the unicode_script_subtag production, throw a RangeError exception. - if (!Unicode::is_unicode_script_subtag(code)) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "script"sv); - return {}; - } + if (!Unicode::is_unicode_script_subtag(code)) + return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "script"sv); // b. Let code be the result of mapping the first character in code to upper case, and mapping the second, third, and fourth character in code to lower case, as described in 6.1. // c. Return code. - return js_string(vm, code.to_titlecase_string()); + return { js_string(vm, code.to_titlecase_string()) }; } // 4. Assert: type is "currency". VERIFY(type == DisplayNames::Type::Currency); // 5. If ! IsWellFormedCurrencyCode(code) is false, throw a RangeError exception. - if (!is_well_formed_currency_code(code)) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "currency"sv); - return {}; - } + if (!is_well_formed_currency_code(code)) + return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "currency"sv); // 6. Let code be the result of mapping code to upper case as described in 6.1. // 7. Return code. - return js_string(vm, code.to_uppercase_string()); + return { js_string(vm, code.to_uppercase_string()) }; } } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h index 17fef5af76..10e62bc522 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h @@ -62,6 +62,6 @@ private: Fallback m_fallback { Fallback::Invalid }; // [[Fallback]] }; -Value canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code); +ThrowCompletionOr<Value> canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code); } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp index ed0c94b8f1..cdcac329a4 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp @@ -50,9 +50,7 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::of) code = js_string(vm, move(code_string)); // 4. Let code be ? CanonicalCodeForDisplayNames(displayNames.[[Type]], code). - code = canonical_code_for_display_names(global_object, display_names->type(), code.as_string().string()); - if (vm.exception()) - return {}; + code = TRY_OR_DISCARD(canonical_code_for_display_names(global_object, display_names->type(), code.as_string().string())); // 5. Let fields be displayNames.[[Fields]]. // 6. If fields has a field [[<code>]], return fields.[[<code>]]. |