From 8670526f2af3392f2809f2729882ba48aadbbda4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 2 Feb 2023 21:37:08 -0500 Subject: [PATCH] LibJS+LibLocale: Propagate OOM from CLDR RelativeTime Vector operations --- .../LibLocale/GenerateRelativeTimeFormatData.cpp | 6 +++--- .../LibJS/Runtime/Intl/RelativeTimeFormat.cpp | 10 +++++----- Userland/Libraries/LibLocale/RelativeTimeFormat.cpp | 4 ++-- Userland/Libraries/LibLocale/RelativeTimeFormat.h | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateRelativeTimeFormatData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateRelativeTimeFormatData.cpp index c4cb741117..0ec821c8a6 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateRelativeTimeFormatData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateRelativeTimeFormatData.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Tim Flynn + * Copyright (c) 2022-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -247,7 +247,7 @@ static constexpr Array<@relative_time_format_index_type@, @size@> @name@ { {)~~~ generate_mapping(generator, cldr.locales, cldr.unique_formats.type_that_fits(), "s_locale_relative_time_formats"sv, "s_number_systems_digits_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_list(name, value.time_units); }); generator.append(R"~~~( -Vector get_relative_time_format_patterns(StringView locale, TimeUnit time_unit, StringView tense_or_number, Style style) +ErrorOr> get_relative_time_format_patterns(StringView locale, TimeUnit time_unit, StringView tense_or_number, Style style) { Vector formats; @@ -268,7 +268,7 @@ Vector get_relative_time_format_patterns(StringView locale, if (decode_string(locale_format.tense_or_number) != tense_or_number) continue; - formats.append(locale_format.to_relative_time_format()); + TRY(formats.try_append(locale_format.to_relative_time_format())); } return formats; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp index 53df2d341c..7f5999ee32 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormat.cpp @@ -117,20 +117,20 @@ ThrowCompletionOr> partition_relative_time_patt // then filtering the large set of locale data down to the pattern we are looking for. Instead, // LibUnicode expects the individual options as enumeration values, and returns the couple of // patterns that match those options. - auto find_patterns_for_tense_or_number = [&](StringView tense_or_number) { + auto find_patterns_for_tense_or_number = [&](StringView tense_or_number) -> ThrowCompletionOr> { // 10. If style is equal to "short", then // a. Let entry be the string-concatenation of unit and "-short". // 11. Else if style is equal to "narrow", then // a. Let entry be the string-concatenation of unit and "-narrow". // 12. Else, // a. Let entry be unit. - auto patterns = ::Locale::get_relative_time_format_patterns(data_locale, time_unit, tense_or_number, style); + auto patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_relative_time_format_patterns(data_locale, time_unit, tense_or_number, style)); // 13. If fields doesn't have a field [[]], then if (patterns.is_empty()) { // a. Let entry be unit. // NOTE: In the CLDR, the lack of "short" or "narrow" in the key implies "long". - patterns = ::Locale::get_relative_time_format_patterns(data_locale, time_unit, tense_or_number, ::Locale::Style::Long); + patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_relative_time_format_patterns(data_locale, time_unit, tense_or_number, ::Locale::Style::Long)); } // 14. Let patterns be fields.[[]]. @@ -144,7 +144,7 @@ ThrowCompletionOr> partition_relative_time_patt auto value_string = MUST(Value(value).to_string(vm)); // b. If patterns has a field [[]], then - if (auto patterns = find_patterns_for_tense_or_number(value_string); !patterns.is_empty()) { + if (auto patterns = MUST_OR_THROW_OOM(find_patterns_for_tense_or_number(value_string)); !patterns.is_empty()) { VERIFY(patterns.size() == 1); // i. Let result be patterns.[[]]. @@ -173,7 +173,7 @@ ThrowCompletionOr> partition_relative_time_patt } // 19. Let po be patterns.[[]]. - auto patterns = find_patterns_for_tense_or_number(tense); + auto patterns = MUST_OR_THROW_OOM(find_patterns_for_tense_or_number(tense)); // 20. Let fv be ! PartitionNumberPattern(relativeTimeFormat.[[NumberFormat]], value). auto value_partitions = MUST_OR_THROW_OOM(partition_number_pattern(vm, relative_time_format.number_format(), Value(value))); diff --git a/Userland/Libraries/LibLocale/RelativeTimeFormat.cpp b/Userland/Libraries/LibLocale/RelativeTimeFormat.cpp index 7f5e24d13e..dcf5ac856b 100644 --- a/Userland/Libraries/LibLocale/RelativeTimeFormat.cpp +++ b/Userland/Libraries/LibLocale/RelativeTimeFormat.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Tim Flynn + * Copyright (c) 2022-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -53,6 +53,6 @@ StringView time_unit_to_string(TimeUnit time_unit) } } -Vector __attribute__((weak)) get_relative_time_format_patterns(StringView, TimeUnit, StringView, Style) { return {}; } +ErrorOr> __attribute__((weak)) get_relative_time_format_patterns(StringView, TimeUnit, StringView, Style) { return Vector {}; } } diff --git a/Userland/Libraries/LibLocale/RelativeTimeFormat.h b/Userland/Libraries/LibLocale/RelativeTimeFormat.h index 1a1914328b..437c560c61 100644 --- a/Userland/Libraries/LibLocale/RelativeTimeFormat.h +++ b/Userland/Libraries/LibLocale/RelativeTimeFormat.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Tim Flynn + * Copyright (c) 2022-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -34,6 +34,6 @@ struct RelativeTimeFormat { Optional time_unit_from_string(StringView time_unit); StringView time_unit_to_string(TimeUnit time_unit); -Vector get_relative_time_format_patterns(StringView locale, TimeUnit time_unit, StringView tense_or_number, Style style); +ErrorOr> get_relative_time_format_patterns(StringView locale, TimeUnit time_unit, StringView tense_or_number, Style style); }