LibJS: Convert canonicalize_locale_list() to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-09-18 19:24:28 +03:00
parent 5426901521
commit 3758e65293
8 changed files with 27 additions and 42 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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 {};

View file

@ -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());

View file

@ -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 {};

View file

@ -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);

View file

@ -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 {};

View file

@ -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;