1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:37:34 +00:00

LibJS: Propagate OOM errors from all Intl.Locale *OfLocale AOs

This commit is contained in:
Timothy Flynn 2023-01-21 22:45:02 -05:00 committed by Tim Flynn
parent f2527c8c81
commit 4548906e7b
3 changed files with 34 additions and 33 deletions

View file

@ -51,7 +51,7 @@ Locale::Locale(Object& prototype)
}
// 1.1.1 CreateArrayFromListOrRestricted ( list , restricted )
static Array* create_array_from_list_or_restricted(VM& vm, Vector<StringView> list, Optional<String> restricted)
static ThrowCompletionOr<NonnullGCPtr<Array>> create_array_from_list_or_restricted(VM& vm, Vector<StringView> list, Optional<String> restricted)
{
auto& realm = *vm.current_realm();
@ -62,13 +62,13 @@ static Array* create_array_from_list_or_restricted(VM& vm, Vector<StringView> li
}
// 2. Return ! CreateArrayFromList( list ).
return Array::create_from<StringView>(realm, list, [&vm](auto value) {
return PrimitiveString::create(vm, value);
return Array::try_create_from<StringView>(vm, realm, list, [&vm](auto value) -> ThrowCompletionOr<Value> {
return PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::from_utf8(value)));
});
}
// 1.1.2 CalendarsOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-calendars-of-locale
Array* calendars_of_locale(VM& vm, Locale const& locale_object)
ThrowCompletionOr<NonnullGCPtr<Array>> calendars_of_locale(VM& vm, Locale const& locale_object)
{
// 1. Let restricted be loc.[[Calendar]].
Optional<String> restricted = locale_object.has_calendar() ? locale_object.calendar() : Optional<String> {};
@ -77,7 +77,7 @@ Array* calendars_of_locale(VM& vm, Locale const& locale_object)
auto const& locale = locale_object.locale();
// 3. Assert: locale matches the unicode_locale_id production.
VERIFY(::Locale::parse_unicode_locale_id(locale).release_value_but_fixme_should_propagate_errors().has_value());
VERIFY(TRY_OR_THROW_OOM(vm, ::Locale::parse_unicode_locale_id(locale)).has_value());
// 4. Let list be a List of 1 or more unique canonical calendar identifiers, which must be lower case String values conforming to the type sequence from UTS 35 Unicode Locale Identifier, section 3.2, sorted in descending preference of those in common use for date and time formatting in locale.
auto list = ::Locale::get_keywords_for_locale(locale, "ca"sv);
@ -87,7 +87,7 @@ Array* calendars_of_locale(VM& vm, Locale const& locale_object)
}
// 1.1.3 CollationsOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-collations-of-locale
Array* collations_of_locale(VM& vm, Locale const& locale_object)
ThrowCompletionOr<NonnullGCPtr<Array>> collations_of_locale(VM& vm, Locale const& locale_object)
{
// 1. Let restricted be loc.[[Collation]].
Optional<String> restricted = locale_object.has_collation() ? locale_object.collation() : Optional<String> {};
@ -96,7 +96,7 @@ Array* collations_of_locale(VM& vm, Locale const& locale_object)
auto const& locale = locale_object.locale();
// 3. Assert: locale matches the unicode_locale_id production.
VERIFY(::Locale::parse_unicode_locale_id(locale).release_value_but_fixme_should_propagate_errors().has_value());
VERIFY(TRY_OR_THROW_OOM(vm, ::Locale::parse_unicode_locale_id(locale)).has_value());
// 4. Let list be a List of 1 or more unique canonical collation identifiers, which must be lower case String values conforming to the type sequence from UTS 35 Unicode Locale Identifier, section 3.2, ordered as if an Array of the same values had been sorted, using %Array.prototype.sort% using undefined as comparefn, of those in common use for string comparison in locale. The values "standard" and "search" must be excluded from list.
auto list = ::Locale::get_keywords_for_locale(locale, "co"sv);
@ -106,7 +106,7 @@ Array* collations_of_locale(VM& vm, Locale const& locale_object)
}
// 1.1.4 HourCyclesOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-hour-cycles-of-locale
Array* hour_cycles_of_locale(VM& vm, Locale const& locale_object)
ThrowCompletionOr<NonnullGCPtr<Array>> hour_cycles_of_locale(VM& vm, Locale const& locale_object)
{
// 1. Let restricted be loc.[[HourCycle]].
Optional<String> restricted = locale_object.has_hour_cycle() ? locale_object.hour_cycle() : Optional<String> {};
@ -115,7 +115,7 @@ Array* hour_cycles_of_locale(VM& vm, Locale const& locale_object)
auto const& locale = locale_object.locale();
// 3. Assert: locale matches the unicode_locale_id production.
VERIFY(::Locale::parse_unicode_locale_id(locale).release_value_but_fixme_should_propagate_errors().has_value());
VERIFY(TRY_OR_THROW_OOM(vm, ::Locale::parse_unicode_locale_id(locale)).has_value());
// 4. Let list be a List of 1 or more unique hour cycle identifiers, which must be lower case String values indicating either the 12-hour format ("h11", "h12") or the 24-hour format ("h23", "h24"), sorted in descending preference of those in common use for date and time formatting in locale.
auto list = ::Locale::get_keywords_for_locale(locale, "hc"sv);
@ -125,7 +125,7 @@ Array* hour_cycles_of_locale(VM& vm, Locale const& locale_object)
}
// 1.1.5 NumberingSystemsOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-numbering-systems-of-locale
Array* numbering_systems_of_locale(VM& vm, Locale const& locale_object)
ThrowCompletionOr<NonnullGCPtr<Array>> numbering_systems_of_locale(VM& vm, Locale const& locale_object)
{
// 1. Let restricted be loc.[[NumberingSystem]].
Optional<String> restricted = locale_object.has_numbering_system() ? locale_object.numbering_system() : Optional<String> {};
@ -134,7 +134,7 @@ Array* numbering_systems_of_locale(VM& vm, Locale const& locale_object)
auto const& locale = locale_object.locale();
// 3. Assert: locale matches the unicode_locale_id production.
VERIFY(::Locale::parse_unicode_locale_id(locale).release_value_but_fixme_should_propagate_errors().has_value());
VERIFY(TRY_OR_THROW_OOM(vm, ::Locale::parse_unicode_locale_id(locale)).has_value());
// 4. Let list be a List of 1 or more unique canonical numbering system identifiers, which must be lower case String values conforming to the type sequence from UTS 35 Unicode Locale Identifier, section 3.2, sorted in descending preference of those in common use for formatting numeric values in locale.
auto list = ::Locale::get_keywords_for_locale(locale, "nu"sv);
@ -145,7 +145,7 @@ Array* numbering_systems_of_locale(VM& vm, Locale const& locale_object)
// 1.1.6 TimeZonesOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-time-zones-of-locale
// NOTE: Our implementation takes a region rather than a Locale object to avoid needlessly parsing the locale twice.
Array* time_zones_of_locale(VM& vm, StringView region)
ThrowCompletionOr<NonnullGCPtr<Array>> time_zones_of_locale(VM& vm, StringView region)
{
auto& realm = *vm.current_realm();
@ -158,19 +158,19 @@ Array* time_zones_of_locale(VM& vm, StringView region)
quick_sort(list);
// 5. Return ! CreateArrayFromList( list ).
return Array::create_from<StringView>(realm, list, [&vm](auto value) {
return PrimitiveString::create(vm, value);
return Array::try_create_from<StringView>(vm, realm, list, [&vm](auto value) -> ThrowCompletionOr<Value> {
return PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::from_utf8(value)));
});
}
// 1.1.7 CharacterDirectionOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-character-direction-of-locale
StringView character_direction_of_locale(Locale const& locale_object)
ThrowCompletionOr<StringView> character_direction_of_locale(VM& vm, Locale const& locale_object)
{
// 1. Let locale be loc.[[Locale]].
auto const& locale = locale_object.locale();
// 2. Assert: locale matches the unicode_locale_id production.
VERIFY(::Locale::parse_unicode_locale_id(locale).release_value_but_fixme_should_propagate_errors().has_value());
VERIFY(TRY_OR_THROW_OOM(vm, ::Locale::parse_unicode_locale_id(locale)).has_value());
// 3. If the default general ordering of characters (characterOrder) within a line in locale is right-to-left, return "rtl".
// NOTE: LibUnicode handles both LTR and RTL character orders in this call, not just RTL. We then fallback to LTR
@ -225,13 +225,13 @@ static Vector<u8> weekend_of_locale(StringView locale)
}
// 1.1.8 WeekInfoOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-week-info-of-locale
WeekInfo week_info_of_locale(Locale const& locale_object)
ThrowCompletionOr<WeekInfo> week_info_of_locale(VM& vm, Locale const& locale_object)
{
// 1. Let locale be loc.[[Locale]].
auto const& locale = locale_object.locale();
// 2. Assert: locale matches the unicode_locale_id production.
VERIFY(::Locale::parse_unicode_locale_id(locale).release_value_but_fixme_should_propagate_errors().has_value());
VERIFY(TRY_OR_THROW_OOM(vm, ::Locale::parse_unicode_locale_id(locale)).has_value());
// 3. Return a record whose fields are defined by Table 1, with values based on locale.
WeekInfo week_info {};

View file

@ -10,6 +10,7 @@
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Value.h>
@ -81,12 +82,12 @@ struct WeekInfo {
Vector<u8> weekend; // [[Weekend]]
};
Array* calendars_of_locale(VM&, Locale const&);
Array* collations_of_locale(VM&, Locale const& locale);
Array* hour_cycles_of_locale(VM&, Locale const& locale);
Array* numbering_systems_of_locale(VM&, Locale const&);
Array* time_zones_of_locale(VM&, StringView region);
StringView character_direction_of_locale(Locale const&);
WeekInfo week_info_of_locale(Locale const&);
ThrowCompletionOr<NonnullGCPtr<Array>> calendars_of_locale(VM&, Locale const&);
ThrowCompletionOr<NonnullGCPtr<Array>> collations_of_locale(VM&, Locale const& locale);
ThrowCompletionOr<NonnullGCPtr<Array>> hour_cycles_of_locale(VM&, Locale const& locale);
ThrowCompletionOr<NonnullGCPtr<Array>> numbering_systems_of_locale(VM&, Locale const&);
ThrowCompletionOr<NonnullGCPtr<Array>> time_zones_of_locale(VM&, StringView region);
ThrowCompletionOr<StringView> character_direction_of_locale(VM&, Locale const&);
ThrowCompletionOr<WeekInfo> week_info_of_locale(VM&, Locale const&);
}

View file

@ -221,11 +221,11 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::region)
// 1.4.17 get Intl.Locale.prototype.collations, https://tc39.es/proposal-intl-locale-info/#sec-Intl.Locale.prototype.collations
// 1.4.18 get Intl.Locale.prototype.hourCycles, https://tc39.es/proposal-intl-locale-info/#sec-Intl.Locale.prototype.hourCycles
// 1.4.19 get Intl.Locale.prototype.numberingSystems, https://tc39.es/proposal-intl-locale-info/#sec-Intl.Locale.prototype.numberingSystems
#define __JS_ENUMERATE(keyword) \
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::keyword) \
{ \
auto* locale_object = TRY(typed_this_object(vm)); \
return keyword##_of_locale(vm, *locale_object); \
#define __JS_ENUMERATE(keyword) \
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::keyword) \
{ \
auto* locale_object = TRY(typed_this_object(vm)); \
return MUST_OR_THROW_OOM(keyword##_of_locale(vm, *locale_object)); \
}
JS_ENUMERATE_LOCALE_INFO_PROPERTIES
#undef __JS_ENUMERATE
@ -245,7 +245,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::time_zones)
return js_undefined();
// 5. Return ! TimeZonesOfLocale(loc).
return time_zones_of_locale(vm, locale->language_id.region.value());
return MUST_OR_THROW_OOM(time_zones_of_locale(vm, locale->language_id.region.value()));
}
// 1.4.21 get Intl.Locale.prototype.textInfo, https://tc39.es/proposal-intl-locale-info/#sec-Intl.Locale.prototype.textInfo
@ -261,7 +261,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::text_info)
auto info = Object::create(realm, realm.intrinsics().object_prototype());
// 4. Let dir be ! CharacterDirectionOfLocale(loc).
auto direction = character_direction_of_locale(*locale_object);
auto direction = MUST_OR_THROW_OOM(character_direction_of_locale(vm, *locale_object));
// 5. Perform ! CreateDataPropertyOrThrow(info, "direction", dir).
MUST(info->create_data_property_or_throw(vm.names.direction, PrimitiveString::create(vm, direction)));
@ -283,7 +283,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::week_info)
auto info = Object::create(realm, realm.intrinsics().object_prototype());
// 4. Let wi be ! WeekInfoOfLocale(loc).
auto week_info = week_info_of_locale(*locale_object);
auto week_info = MUST_OR_THROW_OOM(week_info_of_locale(vm, *locale_object));
// 5. Let we be ! CreateArrayFromList( wi.[[Weekend]] ).
auto weekend = Array::create_from<u8>(realm, week_info.weekend, [](auto day) { return Value(day); });