1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:37:43 +00:00

LibJS: Stop propagating small OOM errors from Intl.RelativeTimeFormat

This commit is contained in:
Timothy Flynn 2023-08-30 12:41:11 -04:00 committed by Andreas Kling
parent b3694653a7
commit b6835d2c40
4 changed files with 18 additions and 20 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/StringBuilder.h>
#include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/GlobalObject.h>
@ -11,7 +12,6 @@
#include <LibJS/Runtime/Intl/NumberFormatConstructor.h> #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
#include <LibJS/Runtime/Intl/PluralRules.h> #include <LibJS/Runtime/Intl/PluralRules.h>
#include <LibJS/Runtime/Intl/RelativeTimeFormat.h> #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
namespace JS::Intl { namespace JS::Intl {
@ -148,12 +148,10 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_patt
VERIFY(patterns.size() == 1); VERIFY(patterns.size() == 1);
// i. Let result be patterns.[[<valueString>]]. // i. Let result be patterns.[[<valueString>]].
auto result = TRY_OR_THROW_OOM(vm, String::from_utf8(patterns[0].pattern)); auto result = MUST(String::from_utf8(patterns[0].pattern));
// ii. Return a List containing the Record { [[Type]]: "literal", [[Value]]: result }. // ii. Return a List containing the Record { [[Type]]: "literal", [[Value]]: result }.
Vector<PatternPartitionWithUnit> result_list; return Vector<PatternPartitionWithUnit> { { "literal"sv, move(result) } };
TRY_OR_THROW_OOM(vm, result_list.try_empend("literal"sv, move(result)));
return result_list;
} }
} }
@ -187,11 +185,11 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_patt
return Vector<PatternPartitionWithUnit> {}; return Vector<PatternPartitionWithUnit> {};
// 23. Return ! MakePartsList(pattern, unit, fv). // 23. Return ! MakePartsList(pattern, unit, fv).
return MUST_OR_THROW_OOM(make_parts_list(vm, pattern->pattern, ::Locale::time_unit_to_string(time_unit), move(value_partitions))); return make_parts_list(pattern->pattern, ::Locale::time_unit_to_string(time_unit), move(value_partitions));
} }
// 17.5.3 MakePartsList ( pattern, unit, parts ), https://tc39.es/ecma402/#sec-makepartslist // 17.5.3 MakePartsList ( pattern, unit, parts ), https://tc39.es/ecma402/#sec-makepartslist
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> make_parts_list(VM& vm, StringView pattern, StringView unit, Vector<PatternPartition> parts) Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts)
{ {
// 1. Let patternParts be PartitionPattern(pattern). // 1. Let patternParts be PartitionPattern(pattern).
auto pattern_parts = partition_pattern(pattern); auto pattern_parts = partition_pattern(pattern);
@ -204,7 +202,7 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> make_parts_list(VM& vm, Stri
// a. If patternPart.[[Type]] is "literal", then // a. If patternPart.[[Type]] is "literal", then
if (pattern_part.type == "literal"sv) { if (pattern_part.type == "literal"sv) {
// i. Append Record { [[Type]]: "literal", [[Value]]: patternPart.[[Value]], [[Unit]]: empty } to result. // i. Append Record { [[Type]]: "literal", [[Value]]: patternPart.[[Value]], [[Unit]]: empty } to result.
TRY_OR_THROW_OOM(vm, result.try_empend("literal"sv, move(pattern_part.value))); result.empend("literal"sv, move(pattern_part.value));
} }
// b. Else, // b. Else,
else { else {
@ -214,7 +212,7 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> make_parts_list(VM& vm, Stri
// ii. For each Record { [[Type]], [[Value]] } part in parts, do // ii. For each Record { [[Type]], [[Value]] } part in parts, do
for (auto& part : parts) { for (auto& part : parts) {
// 1. Append Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: unit } to result. // 1. Append Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: unit } to result.
TRY_OR_THROW_OOM(vm, result.try_empend(part.type, move(part.value), unit)); result.empend(part.type, move(part.value), unit);
} }
} }
} }
@ -230,20 +228,20 @@ ThrowCompletionOr<String> format_relative_time(VM& vm, RelativeTimeFormat& relat
auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit)); auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit));
// 2. Let result be an empty String. // 2. Let result be an empty String.
ThrowableStringBuilder result(vm); StringBuilder result;
// 3. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do // 3. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
for (auto& part : parts) { for (auto& part : parts) {
// a. Set result to the string-concatenation of result and part.[[Value]]. // a. Set result to the string-concatenation of result and part.[[Value]].
MUST_OR_THROW_OOM(result.append(part.value)); result.append(part.value);
} }
// 4. Return result. // 4. Return result.
return result.to_string(); return MUST(result.to_string());
} }
// 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts // 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts
ThrowCompletionOr<Array*> format_relative_time_to_parts(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit) ThrowCompletionOr<NonnullGCPtr<Array>> format_relative_time_to_parts(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{ {
auto& realm = *vm.current_realm(); auto& realm = *vm.current_realm();
@ -281,7 +279,7 @@ ThrowCompletionOr<Array*> format_relative_time_to_parts(VM& vm, RelativeTimeForm
} }
// 5. Return result. // 5. Return result.
return result.ptr(); return result;
} }
} }

View file

@ -84,8 +84,8 @@ struct PatternPartitionWithUnit : public PatternPartition {
ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM&, StringView unit); ThrowCompletionOr<::Locale::TimeUnit> singular_relative_time_unit(VM&, StringView unit);
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit); ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit);
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> make_parts_list(VM&, StringView pattern, StringView unit, Vector<PatternPartition> parts); Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts);
ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit); ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
ThrowCompletionOr<Array*> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit); ThrowCompletionOr<NonnullGCPtr<Array>> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit);
} }

View file

@ -57,7 +57,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> RelativeTimeFormatConstructor::construct
auto relative_time_format = TRY(ordinary_create_from_constructor<RelativeTimeFormat>(vm, new_target, &Intrinsics::intl_relative_time_format_prototype)); auto relative_time_format = TRY(ordinary_create_from_constructor<RelativeTimeFormat>(vm, new_target, &Intrinsics::intl_relative_time_format_prototype));
// 3. Return ? InitializeRelativeTimeFormat(relativeTimeFormat, locales, options). // 3. Return ? InitializeRelativeTimeFormat(relativeTimeFormat, locales, options).
return *TRY(initialize_relative_time_format(vm, relative_time_format, locales, options)); return TRY(initialize_relative_time_format(vm, relative_time_format, locales, options));
} }
// 17.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf // 17.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf
@ -76,7 +76,7 @@ JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of)
} }
// 17.1.2 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat // 17.1.2 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat
ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value) ThrowCompletionOr<NonnullGCPtr<RelativeTimeFormat>> initialize_relative_time_format(VM& vm, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value)
{ {
auto& realm = *vm.current_realm(); auto& realm = *vm.current_realm();
@ -146,7 +146,7 @@ ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, R
relative_time_format.set_plural_rules(static_cast<PluralRules*>(plural_rules.ptr())); relative_time_format.set_plural_rules(static_cast<PluralRules*>(plural_rules.ptr()));
// 21. Return relativeTimeFormat. // 21. Return relativeTimeFormat.
return &relative_time_format; return relative_time_format;
} }
} }

View file

@ -28,6 +28,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(supported_locales_of); JS_DECLARE_NATIVE_FUNCTION(supported_locales_of);
}; };
ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value); ThrowCompletionOr<NonnullGCPtr<RelativeTimeFormat>> initialize_relative_time_format(VM& vm, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value);
} }