1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:57:35 +00:00

LibJS+Everywhere: Make PrimitiveString and Utf16String fallible

This makes construction of Utf16String fallible in OOM conditions. The
immediate impact is that PrimitiveString must then be fallible as well,
as it may either transcode UTF-8 to UTF-16, or create a UTF-16 string
from ropes.

There are a couple of places where it is very non-trivial to propagate
the error further. A FIXME has been added to those locations.
This commit is contained in:
Timothy Flynn 2023-01-07 12:24:05 -05:00 committed by Linus Groh
parent d793262beb
commit 115baa7e32
57 changed files with 306 additions and 295 deletions

View file

@ -106,11 +106,11 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
// 7. If calendar is not undefined, then
if (!calendar.is_undefined()) {
// a. If calendar does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(calendar.as_string().deprecated_string()))
if (!::Locale::is_type_identifier(TRY(calendar.as_string().deprecated_string())))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, calendar, "calendar"sv);
// 8. Set opt.[[ca]] to calendar.
opt.ca = calendar.as_string().deprecated_string();
opt.ca = TRY(calendar.as_string().deprecated_string());
}
// 9. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
@ -119,11 +119,11 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
// 10. If numberingSystem is not undefined, then
if (!numbering_system.is_undefined()) {
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (!::Locale::is_type_identifier(numbering_system.as_string().deprecated_string()))
if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().deprecated_string())))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
// 11. Set opt.[[nu]] to numberingSystem.
opt.nu = numbering_system.as_string().deprecated_string();
opt.nu = TRY(numbering_system.as_string().deprecated_string());
}
// 12. Let hour12 be ? GetOption(options, "hour12", "boolean", undefined, undefined).
@ -140,11 +140,11 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
// 15. Set opt.[[hc]] to hourCycle.
if (!hour_cycle.is_nullish())
opt.hc = hour_cycle.as_string().deprecated_string();
opt.hc = TRY(hour_cycle.as_string().deprecated_string());
// 16. Let localeData be %DateTimeFormat%.[[LocaleData]].
// 17. Let r be ResolveLocale(%DateTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]], localeData).
auto result = resolve_locale(requested_locales, opt, DateTimeFormat::relevant_extension_keys());
auto result = TRY(resolve_locale(requested_locales, opt, DateTimeFormat::relevant_extension_keys()));
// 18. Set dateTimeFormat.[[Locale]] to r.[[locale]].
date_time_format.set_locale(move(result.locale));
@ -277,7 +277,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
// d. Set formatOptions.[[<prop>]] to value.
if (!value.is_undefined()) {
option = ::Locale::calendar_pattern_style_from_string(value.as_string().deprecated_string());
option = ::Locale::calendar_pattern_style_from_string(TRY(value.as_string().deprecated_string()));
// e. If value is not undefined, then
// i. Set hasExplicitFormatComponents to true.
@ -296,14 +296,14 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
// 39. Set dateTimeFormat.[[DateStyle]] to dateStyle.
if (!date_style.is_undefined())
date_time_format.set_date_style(date_style.as_string().deprecated_string());
date_time_format.set_date_style(TRY(date_style.as_string().deprecated_string()));
// 40. Let timeStyle be ? GetOption(options, "timeStyle", "string", « "full", "long", "medium", "short" », undefined).
auto time_style = TRY(get_option(vm, *options, vm.names.timeStyle, OptionType::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
// 41. Set dateTimeFormat.[[TimeStyle]] to timeStyle.
if (!time_style.is_undefined())
date_time_format.set_time_style(time_style.as_string().deprecated_string());
date_time_format.set_time_style(TRY(time_style.as_string().deprecated_string()));
Optional<::Locale::CalendarPattern> best_format {};
@ -325,7 +325,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
auto formats = ::Locale::get_calendar_available_formats(data_locale, date_time_format.calendar());
// b. If matcher is "basic", then
if (matcher.as_string().deprecated_string() == "basic"sv) {
if (TRY(matcher.as_string().deprecated_string()) == "basic"sv) {
// i. Let bestFormat be BasicFormatMatcher(formatOptions, formats).
best_format = basic_format_matcher(format_options, move(formats));
}