From 9e5055c29862259832332d6ef809fb5676a0ccc7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 30 Aug 2023 11:45:26 -0400 Subject: [PATCH] LibJS: Stop propagating small OOM errors from Intl.ListFormat --- .../LibJS/Runtime/Intl/DurationFormat.cpp | 2 +- .../LibJS/Runtime/Intl/ListFormat.cpp | 52 +++++++++---------- .../Libraries/LibJS/Runtime/Intl/ListFormat.h | 8 +-- .../Runtime/Intl/ListFormatPrototype.cpp | 4 +- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp index 8cebb36414..0c08a2394f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp @@ -565,7 +565,7 @@ Vector partition_duration_format_pattern(VM& vm, DurationForma } // 10. Set result to ! CreatePartsFromList(lf, result). - auto final_result = MUST(create_parts_from_list(vm, *list_format, string_result)); + auto final_result = create_parts_from_list(*list_format, string_result); // 11. Return result. return final_result; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp index fec8c21000..d71fbd1851 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp @@ -4,11 +4,11 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include #include -#include namespace JS::Intl { @@ -46,7 +46,7 @@ StringView ListFormat::type_string() const } // 13.5.1 DeconstructPattern ( pattern, placeables ), https://tc39.es/ecma402/#sec-deconstructpattern -ThrowCompletionOr> deconstruct_pattern(VM& vm, StringView pattern, Placeables placeables) +Vector deconstruct_pattern(StringView pattern, Placeables placeables) { // 1. Let patternParts be ! PartitionPattern(pattern). auto pattern_parts = partition_pattern(pattern); @@ -62,7 +62,7 @@ ThrowCompletionOr> deconstruct_pattern(VM& vm, StringVi // b. If part is "literal", then if (part == "literal"sv) { // i. Append Record { [[Type]]: "literal", [[Value]]: patternPart.[[Value]] } to result. - TRY_OR_THROW_OOM(vm, result.try_append({ part, move(pattern_part.value) })); + result.append({ part, move(pattern_part.value) }); } // c. Else, else { @@ -71,22 +71,18 @@ ThrowCompletionOr> deconstruct_pattern(VM& vm, StringVi auto subst = placeables.get(part); VERIFY(subst.has_value()); - MUST_OR_THROW_OOM(subst.release_value().visit( + subst.release_value().visit( // iii. If Type(subst) is List, then - [&](Vector& partition) -> ThrowCompletionOr { + [&](Vector& partition) { // 1. For each element s of subst, do - for (auto& element : partition) { - // a. Append s to result. - TRY_OR_THROW_OOM(vm, result.try_append(move(element))); - } - return {}; + // a. Append s to result. + result.extend(move(partition)); }, // iv. Else, - [&](PatternPartition& partition) -> ThrowCompletionOr { + [&](PatternPartition& partition) { // 1. Append subst to result. - TRY_OR_THROW_OOM(vm, result.try_append(move(partition))); - return {}; - })); + result.append(move(partition)); + }); } } @@ -95,11 +91,11 @@ ThrowCompletionOr> deconstruct_pattern(VM& vm, StringVi } // 13.5.2 CreatePartsFromList ( listFormat, list ), https://tc39.es/ecma402/#sec-createpartsfromlist -ThrowCompletionOr> create_parts_from_list(VM& vm, ListFormat const& list_format, Vector const& list) +Vector create_parts_from_list(ListFormat const& list_format, Vector const& list) { auto list_patterns = ::Locale::get_locale_list_patterns(list_format.locale(), list_format.type_string(), list_format.style()); if (!list_patterns.has_value()) - return Vector {}; + return {}; // 1. Let size be the number of elements of list. auto size = list.size(); @@ -107,7 +103,7 @@ ThrowCompletionOr> create_parts_from_list(VM& vm, ListF // 2. If size is 0, then if (size == 0) { // a. Return a new empty List. - return Vector {}; + return {}; } // 3. If size is 2, then @@ -128,7 +124,7 @@ ThrowCompletionOr> create_parts_from_list(VM& vm, ListF placeables.set("1"sv, move(second)); // f. Return ! DeconstructPattern(pattern, placeables). - return MUST_OR_THROW_OOM(deconstruct_pattern(vm, pattern, move(placeables))); + return deconstruct_pattern(pattern, move(placeables)); } // 4. Let last be a new Record { [[Type]]: "element", [[Value]]: list[size - 1] }. @@ -174,7 +170,7 @@ ThrowCompletionOr> create_parts_from_list(VM& vm, ListF placeables.set("1"sv, move(parts)); // g. Set parts to ! DeconstructPattern(pattern, placeables). - parts = MUST_OR_THROW_OOM(deconstruct_pattern(vm, pattern, move(placeables))); + parts = deconstruct_pattern(pattern, move(placeables)); // h. Decrement i by 1. } while (i-- != 0); @@ -184,31 +180,31 @@ ThrowCompletionOr> create_parts_from_list(VM& vm, ListF } // 13.5.3 FormatList ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlist -ThrowCompletionOr format_list(VM& vm, ListFormat const& list_format, Vector const& list) +String format_list(ListFormat const& list_format, Vector const& list) { // 1. Let parts be ! CreatePartsFromList(listFormat, list). - auto parts = MUST_OR_THROW_OOM(create_parts_from_list(vm, list_format, list)); + auto parts = create_parts_from_list(list_format, list); // 2. Let result be an empty String. - ThrowableStringBuilder result(vm); + StringBuilder result; // 3. For each Record { [[Type]], [[Value]] } part in parts, do for (auto& part : parts) { // a. Set result to the string-concatenation of result and part.[[Value]]. - TRY(result.append(part.value)); + result.append(part.value); } // 4. Return result. - return result.to_string(); + return MUST(result.to_string()); } // 13.5.4 FormatListToParts ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlisttoparts -ThrowCompletionOr format_list_to_parts(VM& vm, ListFormat const& list_format, Vector const& list) +NonnullGCPtr format_list_to_parts(VM& vm, ListFormat const& list_format, Vector const& list) { auto& realm = *vm.current_realm(); // 1. Let parts be ! CreatePartsFromList(listFormat, list). - auto parts = MUST_OR_THROW_OOM(create_parts_from_list(vm, list_format, list)); + auto parts = create_parts_from_list(list_format, list); // 2. Let result be ! ArrayCreate(0). auto result = MUST(Array::create(realm, 0)); @@ -235,7 +231,7 @@ ThrowCompletionOr format_list_to_parts(VM& vm, ListFormat const& list_fo } // 5. Return result. - return result.ptr(); + return result; } // 13.5.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable @@ -276,7 +272,7 @@ ThrowCompletionOr> string_list_from_iterable(VM& vm, Value iterab } // iii. Append nextValue to the end of the List list. - TRY_OR_THROW_OOM(vm, list.try_append(next_value.as_string().utf8_string())); + list.append(next_value.as_string().utf8_string()); } } while (next != nullptr); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h index 790e03c70e..7313781050 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h @@ -51,10 +51,10 @@ private: using Placeables = HashMap>>; -ThrowCompletionOr> deconstruct_pattern(VM&, StringView pattern, Placeables); -ThrowCompletionOr> create_parts_from_list(VM&, ListFormat const&, Vector const& list); -ThrowCompletionOr format_list(VM&, ListFormat const&, Vector const& list); -ThrowCompletionOr format_list_to_parts(VM&, ListFormat const&, Vector const& list); +Vector deconstruct_pattern(StringView pattern, Placeables); +Vector create_parts_from_list(ListFormat const&, Vector const& list); +String format_list(ListFormat const&, Vector const& list); +NonnullGCPtr format_list_to_parts(VM&, ListFormat const&, Vector const& list); ThrowCompletionOr> string_list_from_iterable(VM&, Value iterable); } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp index c9e620d9e2..6dcd248cb4 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.cpp @@ -46,7 +46,7 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format) auto string_list = TRY(string_list_from_iterable(vm, list)); // 4. Return ! FormatList(lf, stringList). - auto formatted = MUST_OR_THROW_OOM(format_list(vm, list_format, string_list)); + auto formatted = format_list(list_format, string_list); return PrimitiveString::create(vm, move(formatted)); } @@ -63,7 +63,7 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format_to_parts) auto string_list = TRY(string_list_from_iterable(vm, list)); // 4. Return ! FormatListToParts(lf, stringList). - return MUST_OR_THROW_OOM(format_list_to_parts(vm, list_format, string_list)); + return format_list_to_parts(vm, list_format, string_list); } // 13.3.5 Intl.ListFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.resolvedoptions