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

LibJS: Propagate OOM errors from Intl Abstract Operations

This excludes the PartitionPattern AO as that has a much larger foot-
print and will be handled separately.
This commit is contained in:
Timothy Flynn 2023-01-19 11:39:05 -05:00 committed by Linus Groh
parent ca62aeb6bd
commit bff0e25ebe
14 changed files with 71 additions and 67 deletions

View file

@ -47,7 +47,7 @@ static ThrowCompletionOr<DeprecatedString> apply_options_to_tag(VM& vm, StringVi
// 2. Assert: Type(options) is Object.
// 3. If ! IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception.
auto locale_id = is_structurally_valid_language_tag(tag);
auto locale_id = TRY(is_structurally_valid_language_tag(vm, tag));
if (!locale_id.has_value())
return vm.throw_completion<RangeError>(ErrorType::IntlInvalidLanguageTag, tag);
@ -67,7 +67,8 @@ static ThrowCompletionOr<DeprecatedString> apply_options_to_tag(VM& vm, StringVi
auto region = TRY(get_string_option(vm, options, vm.names.region, ::Locale::is_unicode_region_subtag));
// 10. Set tag to ! CanonicalizeUnicodeLocaleId(tag).
auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id);
// NOTE: We TRY this operation only to propagate OOM errors.
auto canonicalized_tag = TRY(JS::Intl::canonicalize_unicode_locale_id(vm, *locale_id));
// 11. Assert: tag matches the unicode_locale_id production.
locale_id = TRY_OR_THROW_OOM(vm, ::Locale::parse_unicode_locale_id(canonicalized_tag));
@ -102,11 +103,11 @@ static ThrowCompletionOr<DeprecatedString> apply_options_to_tag(VM& vm, StringVi
// 16. Set tag to tag with the substring corresponding to the unicode_language_id production replaced by the string languageId.
// 17. Return ! CanonicalizeUnicodeLocaleId(tag).
return JS::Intl::canonicalize_unicode_locale_id(*locale_id);
return JS::Intl::canonicalize_unicode_locale_id(vm, *locale_id);
}
// 14.1.3 ApplyUnicodeExtensionToTag ( tag, options, relevantExtensionKeys ), https://tc39.es/ecma402/#sec-apply-unicode-extension-to-tag
static LocaleAndKeys apply_unicode_extension_to_tag(StringView tag, LocaleAndKeys options, Span<StringView const> relevant_extension_keys)
static ThrowCompletionOr<LocaleAndKeys> apply_unicode_extension_to_tag(VM& vm, StringView tag, LocaleAndKeys options, Span<StringView const> relevant_extension_keys)
{
// 1. Assert: Type(tag) is String.
// 2. Assert: tag matches the unicode_locale_id production.
@ -207,7 +208,7 @@ static LocaleAndKeys apply_unicode_extension_to_tag(StringView tag, LocaleAndKey
// 9. If newExtension is not the empty String, then
if (!new_extension.attributes.is_empty() || !new_extension.keywords.is_empty()) {
// a. Let locale be ! InsertUnicodeExtensionAndCanonicalize(locale, newExtension).
locale = String::from_deprecated_string(insert_unicode_extension_and_canonicalize(locale_id.release_value(), move(new_extension))).release_value_but_fixme_should_propagate_errors();
locale = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(TRY(insert_unicode_extension_and_canonicalize(vm, locale_id.release_value(), move(new_extension)))));
}
// 10. Set result.[[locale]] to locale.
@ -323,7 +324,8 @@ ThrowCompletionOr<NonnullGCPtr<Object>> LocaleConstructor::construct(FunctionObj
opt.nu = TRY(get_string_option(vm, *options, vm.names.numberingSystem, ::Locale::is_type_identifier));
// 29. Let r be ! ApplyUnicodeExtensionToTag(tag, opt, relevantExtensionKeys).
auto result = apply_unicode_extension_to_tag(tag, move(opt), relevant_extension_keys);
// NOTE: We TRY this operation only to propagate OOM errors.
auto result = TRY(apply_unicode_extension_to_tag(vm, tag, move(opt), relevant_extension_keys));
// 30. Set locale.[[Locale]] to r.[[locale]].
locale->set_locale(result.locale.to_deprecated_string());