diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-09-18 19:24:28 +0300 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2021-09-18 22:21:15 +0300 |
commit | 3758e6529373eea03393dcc8d30ab4e04bac7d5e (patch) | |
tree | 68c55065052fa3f12e368a4da0405a580c10ca0c | |
parent | 5426901521ef50172f8a4754e542f3a1fe7e78dc (diff) | |
download | serenity-3758e6529373eea03393dcc8d30ab4e04bac7d5e.zip |
LibJS: Convert canonicalize_locale_list() to ThrowCompletionOr
8 files changed, 27 insertions, 42 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index 61743443dc..b59ecb60db 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -186,14 +186,14 @@ bool is_well_formed_unit_identifier(StringView unit_identifier) } // 9.2.1 CanonicalizeLocaleList ( locales ), https://tc39.es/ecma402/#sec-canonicalizelocalelist -Vector<String> canonicalize_locale_list(GlobalObject& global_object, Value locales) +ThrowCompletionOr<Vector<String>> canonicalize_locale_list(GlobalObject& global_object, Value locales) { auto& vm = global_object.vm(); // 1. If locales is undefined, then if (locales.is_undefined()) { // a. Return a new empty List. - return {}; + return Vector<String> {}; } // 2. Let seen be a new empty List. @@ -209,17 +209,17 @@ Vector<String> canonicalize_locale_list(GlobalObject& global_object, Value local else { // a. Let O be ? ToObject(locales). object = locales.to_object(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); } // 5. Let len be ? ToLength(? Get(O, "length")). auto length_value = object->get(vm.names.length); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); auto length = length_value.to_length(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // 6. Let k be 0. // 7. Repeat, while k < len, @@ -229,21 +229,19 @@ Vector<String> canonicalize_locale_list(GlobalObject& global_object, Value local // b. Let kPresent be ? HasProperty(O, Pk). auto key_present = object->has_property(property_key); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // c. If kPresent is true, then if (key_present) { // i. Let kValue be ? Get(O, Pk). auto key_value = object->get(property_key); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); // ii. If Type(kValue) is not String or Object, throw a TypeError exception. - if (!key_value.is_string() && !key_value.is_object()) { - vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObjectOrString, key_value.to_string_without_side_effects()); - return {}; - } + if (!key_value.is_string() && !key_value.is_object()) + return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObjectOrString, key_value.to_string_without_side_effects()); String tag; @@ -256,16 +254,14 @@ Vector<String> canonicalize_locale_list(GlobalObject& global_object, Value local else { // 1. Let tag be ? ToString(kValue). tag = key_value.to_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); } // v. If IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception. auto locale_id = is_structurally_valid_language_tag(tag); - if (!locale_id.has_value()) { - vm.throw_exception<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, tag); - return {}; - } + if (!locale_id.has_value()) + return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, tag); // vi. Let canonicalizedTag be CanonicalizeUnicodeLocaleId(tag). auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h index c2484bcc30..7513e9580a 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h @@ -38,7 +38,7 @@ Optional<Unicode::LocaleID> is_structurally_valid_language_tag(StringView locale String canonicalize_unicode_locale_id(Unicode::LocaleID& locale); bool is_well_formed_currency_code(StringView currency); bool is_well_formed_unit_identifier(StringView unit_identifier); -Vector<String> canonicalize_locale_list(GlobalObject&, Value locales); +ThrowCompletionOr<Vector<String>> canonicalize_locale_list(GlobalObject&, Value locales); Optional<String> best_available_locale(StringView const& locale); String insert_unicode_extension_and_canonicalize(Unicode::LocaleID locale_id, Unicode::LocaleExtension extension); LocaleResult resolve_locale(Vector<String> const& requested_locales, LocaleOptions const& options, Vector<StringView> const& relevant_extension_keys); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp index a4ef855b18..5824d44577 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesConstructor.cpp @@ -57,9 +57,7 @@ Value DisplayNamesConstructor::construct(FunctionObject& new_target) auto* display_names = TRY_OR_DISCARD(ordinary_create_from_constructor<DisplayNames>(global_object, new_target, &GlobalObject::intl_display_names_prototype)); // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = canonicalize_locale_list(global_object, locale_value); - if (vm.exception()) - return {}; + auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locale_value)); // 4. If options is undefined, throw a TypeError exception. if (options_value.is_undefined()) { @@ -136,7 +134,7 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesConstructor::supported_locales_of) // No-op, availability of each requested locale is checked via Unicode::is_locale_available() // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = canonicalize_locale_list(global_object, locales); + auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales)); if (vm.exception()) return {}; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp index 800dcbcaf6..70bb64ef24 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp @@ -45,9 +45,7 @@ JS_DEFINE_NATIVE_FUNCTION(Intl::get_canonical_locales) auto locales = vm.argument(0); // 1. Let ll be ? CanonicalizeLocaleList(locales). - auto locale_list = canonicalize_locale_list(global_object, locales); - if (vm.exception()) - return {}; + auto locale_list = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales)); MarkedValueList marked_locale_list { vm.heap() }; marked_locale_list.ensure_capacity(locale_list.size()); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp index 390f1399e1..f1c4eaf7c6 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp @@ -56,9 +56,7 @@ Value ListFormatConstructor::construct(FunctionObject& new_target) auto* list_format = TRY_OR_DISCARD(ordinary_create_from_constructor<ListFormat>(global_object, new_target, &GlobalObject::intl_list_format_prototype)); // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = canonicalize_locale_list(global_object, locale_value); - if (vm.exception()) - return {}; + auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locale_value)); // 4. Set options to ? GetOptionsObject(options). auto* options = TRY_OR_DISCARD(Temporal::get_options_object(global_object, options_value)); @@ -113,7 +111,7 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatConstructor::supported_locales_of) // 1. Let availableLocales be %ListFormat%.[[AvailableLocales]]. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = canonicalize_locale_list(global_object, locales); + auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales)); if (vm.exception()) return {}; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp index deaa7c451f..2b722aeb83 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp @@ -357,9 +357,7 @@ NumberFormat* initialize_number_format(GlobalObject& global_object, NumberFormat auto& vm = global_object.vm(); // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = canonicalize_locale_list(global_object, locales_value); - if (vm.exception()) - return {}; + auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales_value)); // 2. Set options to ? CoerceOptionsToObject(options). auto* options = coerce_options_to_object(global_object, options_value); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp index f86b763d46..2496a3739b 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp @@ -75,7 +75,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of) // 1. Let availableLocales be %NumberFormat%.[[AvailableLocales]]. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = canonicalize_locale_list(global_object, locales); + auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales)); if (vm.exception()) return {}; diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index aafcfd3b91..ef3a040299 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -373,12 +373,9 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of) static Optional<String> resolve_best_locale(GlobalObject& global_object, Value locales) { // For details on these steps, see https://tc39.es/ecma402/#sup-string.prototype.tolocalelowercase - auto& vm = global_object.vm(); // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales). - auto requested_locales = Intl::canonicalize_locale_list(global_object, locales); - if (vm.exception()) - return {}; + auto requested_locales = TRY_OR_DISCARD(Intl::canonicalize_locale_list(global_object, locales)); Optional<Unicode::LocaleID> requested_locale; |