mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:17:35 +00:00
LibJS: Propagate OOM errors from all Intl.Locale *OfLocale AOs
This commit is contained in:
parent
f2527c8c81
commit
4548906e7b
3 changed files with 34 additions and 33 deletions
|
@ -51,7 +51,7 @@ Locale::Locale(Object& prototype)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1.1.1 CreateArrayFromListOrRestricted ( list , restricted )
|
// 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();
|
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 ).
|
// 2. Return ! CreateArrayFromList( list ).
|
||||||
return Array::create_from<StringView>(realm, list, [&vm](auto value) {
|
return Array::try_create_from<StringView>(vm, realm, list, [&vm](auto value) -> ThrowCompletionOr<Value> {
|
||||||
return PrimitiveString::create(vm, 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
|
// 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]].
|
// 1. Let restricted be loc.[[Calendar]].
|
||||||
Optional<String> restricted = locale_object.has_calendar() ? locale_object.calendar() : Optional<String> {};
|
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();
|
auto const& locale = locale_object.locale();
|
||||||
|
|
||||||
// 3. Assert: locale matches the unicode_locale_id production.
|
// 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.
|
// 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);
|
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
|
// 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]].
|
// 1. Let restricted be loc.[[Collation]].
|
||||||
Optional<String> restricted = locale_object.has_collation() ? locale_object.collation() : Optional<String> {};
|
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();
|
auto const& locale = locale_object.locale();
|
||||||
|
|
||||||
// 3. Assert: locale matches the unicode_locale_id production.
|
// 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.
|
// 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);
|
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
|
// 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]].
|
// 1. Let restricted be loc.[[HourCycle]].
|
||||||
Optional<String> restricted = locale_object.has_hour_cycle() ? locale_object.hour_cycle() : Optional<String> {};
|
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();
|
auto const& locale = locale_object.locale();
|
||||||
|
|
||||||
// 3. Assert: locale matches the unicode_locale_id production.
|
// 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.
|
// 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);
|
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
|
// 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]].
|
// 1. Let restricted be loc.[[NumberingSystem]].
|
||||||
Optional<String> restricted = locale_object.has_numbering_system() ? locale_object.numbering_system() : Optional<String> {};
|
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();
|
auto const& locale = locale_object.locale();
|
||||||
|
|
||||||
// 3. Assert: locale matches the unicode_locale_id production.
|
// 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.
|
// 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);
|
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
|
// 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.
|
// 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();
|
auto& realm = *vm.current_realm();
|
||||||
|
|
||||||
|
@ -158,19 +158,19 @@ Array* time_zones_of_locale(VM& vm, StringView region)
|
||||||
quick_sort(list);
|
quick_sort(list);
|
||||||
|
|
||||||
// 5. Return ! CreateArrayFromList( list ).
|
// 5. Return ! CreateArrayFromList( list ).
|
||||||
return Array::create_from<StringView>(realm, list, [&vm](auto value) {
|
return Array::try_create_from<StringView>(vm, realm, list, [&vm](auto value) -> ThrowCompletionOr<Value> {
|
||||||
return PrimitiveString::create(vm, 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
|
// 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]].
|
// 1. Let locale be loc.[[Locale]].
|
||||||
auto const& locale = locale_object.locale();
|
auto const& locale = locale_object.locale();
|
||||||
|
|
||||||
// 2. Assert: locale matches the unicode_locale_id production.
|
// 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".
|
// 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
|
// 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
|
// 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]].
|
// 1. Let locale be loc.[[Locale]].
|
||||||
auto const& locale = locale_object.locale();
|
auto const& locale = locale_object.locale();
|
||||||
|
|
||||||
// 2. Assert: locale matches the unicode_locale_id production.
|
// 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.
|
// 3. Return a record whose fields are defined by Table 1, with values based on locale.
|
||||||
WeekInfo week_info {};
|
WeekInfo week_info {};
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibJS/Heap/GCPtr.h>
|
||||||
#include <LibJS/Runtime/Completion.h>
|
#include <LibJS/Runtime/Completion.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
@ -81,12 +82,12 @@ struct WeekInfo {
|
||||||
Vector<u8> weekend; // [[Weekend]]
|
Vector<u8> weekend; // [[Weekend]]
|
||||||
};
|
};
|
||||||
|
|
||||||
Array* calendars_of_locale(VM&, Locale const&);
|
ThrowCompletionOr<NonnullGCPtr<Array>> calendars_of_locale(VM&, Locale const&);
|
||||||
Array* collations_of_locale(VM&, Locale const& locale);
|
ThrowCompletionOr<NonnullGCPtr<Array>> collations_of_locale(VM&, Locale const& locale);
|
||||||
Array* hour_cycles_of_locale(VM&, Locale const& locale);
|
ThrowCompletionOr<NonnullGCPtr<Array>> hour_cycles_of_locale(VM&, Locale const& locale);
|
||||||
Array* numbering_systems_of_locale(VM&, Locale const&);
|
ThrowCompletionOr<NonnullGCPtr<Array>> numbering_systems_of_locale(VM&, Locale const&);
|
||||||
Array* time_zones_of_locale(VM&, StringView region);
|
ThrowCompletionOr<NonnullGCPtr<Array>> time_zones_of_locale(VM&, StringView region);
|
||||||
StringView character_direction_of_locale(Locale const&);
|
ThrowCompletionOr<StringView> character_direction_of_locale(VM&, Locale const&);
|
||||||
WeekInfo week_info_of_locale(Locale const&);
|
ThrowCompletionOr<WeekInfo> week_info_of_locale(VM&, Locale const&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -225,7 +225,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::region)
|
||||||
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::keyword) \
|
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::keyword) \
|
||||||
{ \
|
{ \
|
||||||
auto* locale_object = TRY(typed_this_object(vm)); \
|
auto* locale_object = TRY(typed_this_object(vm)); \
|
||||||
return keyword##_of_locale(vm, *locale_object); \
|
return MUST_OR_THROW_OOM(keyword##_of_locale(vm, *locale_object)); \
|
||||||
}
|
}
|
||||||
JS_ENUMERATE_LOCALE_INFO_PROPERTIES
|
JS_ENUMERATE_LOCALE_INFO_PROPERTIES
|
||||||
#undef __JS_ENUMERATE
|
#undef __JS_ENUMERATE
|
||||||
|
@ -245,7 +245,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::time_zones)
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
|
|
||||||
// 5. Return ! TimeZonesOfLocale(loc).
|
// 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
|
// 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());
|
auto info = Object::create(realm, realm.intrinsics().object_prototype());
|
||||||
|
|
||||||
// 4. Let dir be ! CharacterDirectionOfLocale(loc).
|
// 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).
|
// 5. Perform ! CreateDataPropertyOrThrow(info, "direction", dir).
|
||||||
MUST(info->create_data_property_or_throw(vm.names.direction, PrimitiveString::create(vm, direction)));
|
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());
|
auto info = Object::create(realm, realm.intrinsics().object_prototype());
|
||||||
|
|
||||||
// 4. Let wi be ! WeekInfoOfLocale(loc).
|
// 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]] ).
|
// 5. Let we be ! CreateArrayFromList( wi.[[Weekend]] ).
|
||||||
auto weekend = Array::create_from<u8>(realm, week_info.weekend, [](auto day) { return Value(day); });
|
auto weekend = Array::create_from<u8>(realm, week_info.weekend, [](auto day) { return Value(day); });
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue