1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:07:45 +00:00

LibJS: Convert Locale AOs to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-22 01:23:16 +03:00
parent 09b08d8e35
commit 3426285738

View file

@ -26,24 +26,22 @@ struct LocaleAndKeys {
}; };
// Note: This is not an AO in the spec. This just serves to abstract very similar steps in ApplyOptionsToTag and the Intl.Locale constructor. // Note: This is not an AO in the spec. This just serves to abstract very similar steps in ApplyOptionsToTag and the Intl.Locale constructor.
static Optional<String> get_string_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Function<bool(StringView)> validator, Vector<StringView> const& values = {}) static ThrowCompletionOr<Optional<String>> get_string_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Function<bool(StringView)> validator, Vector<StringView> const& values = {})
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto option = TRY_OR_DISCARD(get_option(global_object, options, property, Value::Type::String, values, Empty {})); auto option = TRY(get_option(global_object, options, property, Value::Type::String, values, Empty {}));
if (option.is_undefined()) if (option.is_undefined())
return {}; return Optional<String> {};
if (validator && !validator(option.as_string().string())) { if (validator && !validator(option.as_string().string()))
vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, option, property); return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, option, property);
return {};
}
return option.as_string().string(); return option.as_string().string();
} }
// 14.1.1 ApplyOptionsToTag ( tag, options ), https://tc39.es/ecma402/#sec-apply-options-to-tag // 14.1.1 ApplyOptionsToTag ( tag, options ), https://tc39.es/ecma402/#sec-apply-options-to-tag
static Optional<String> apply_options_to_tag(GlobalObject& global_object, StringView tag, Object const& options) static ThrowCompletionOr<String> apply_options_to_tag(GlobalObject& global_object, StringView tag, Object const& options)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
@ -52,31 +50,23 @@ static Optional<String> apply_options_to_tag(GlobalObject& global_object, String
// 3. If IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception. // 3. If IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception.
auto locale_id = is_structurally_valid_language_tag(tag); auto locale_id = is_structurally_valid_language_tag(tag);
if (!locale_id.has_value()) { if (!locale_id.has_value())
vm.throw_exception<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, tag); return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, tag);
return {};
}
// 4. Let language be ? GetOption(options, "language", "string", undefined, undefined). // 4. Let language be ? GetOption(options, "language", "string", undefined, undefined).
// 5. If language is not undefined, then // 5. If language is not undefined, then
// a. If language does not match the unicode_language_subtag production, throw a RangeError exception. // a. If language does not match the unicode_language_subtag production, throw a RangeError exception.
auto language = get_string_option(global_object, options, vm.names.language, Unicode::is_unicode_language_subtag); auto language = TRY(get_string_option(global_object, options, vm.names.language, Unicode::is_unicode_language_subtag));
if (vm.exception())
return {};
// 6. Let script be ? GetOption(options, "script", "string", undefined, undefined). // 6. Let script be ? GetOption(options, "script", "string", undefined, undefined).
// 7. If script is not undefined, then // 7. If script is not undefined, then
// a. If script does not match the unicode_script_subtag production, throw a RangeError exception. // a. If script does not match the unicode_script_subtag production, throw a RangeError exception.
auto script = get_string_option(global_object, options, vm.names.script, Unicode::is_unicode_script_subtag); auto script = TRY(get_string_option(global_object, options, vm.names.script, Unicode::is_unicode_script_subtag));
if (vm.exception())
return {};
// 8. Let region be ? GetOption(options, "region", "string", undefined, undefined). // 8. Let region be ? GetOption(options, "region", "string", undefined, undefined).
// 9. If region is not undefined, then // 9. If region is not undefined, then
// a. If region does not match the unicode_region_subtag production, throw a RangeError exception. // a. If region does not match the unicode_region_subtag production, throw a RangeError exception.
auto region = get_string_option(global_object, options, vm.names.region, Unicode::is_unicode_region_subtag); auto region = TRY(get_string_option(global_object, options, vm.names.region, Unicode::is_unicode_region_subtag));
if (vm.exception())
return {};
// 10. Set tag to CanonicalizeUnicodeLocaleId(tag). // 10. Set tag to CanonicalizeUnicodeLocaleId(tag).
auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id); auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id);
@ -296,10 +286,7 @@ ThrowCompletionOr<Object*> LocaleConstructor::construct(FunctionObject& new_targ
auto* options = TRY(coerce_options_to_object(global_object, options_value)); auto* options = TRY(coerce_options_to_object(global_object, options_value));
// 11. Set tag to ? ApplyOptionsToTag(tag, options). // 11. Set tag to ? ApplyOptionsToTag(tag, options).
auto applied_tag = apply_options_to_tag(global_object, tag, *options); tag = TRY(apply_options_to_tag(global_object, tag, *options));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
tag = applied_tag.release_value();
// 12. Let opt be a new Record. // 12. Let opt be a new Record.
LocaleAndKeys opt {}; LocaleAndKeys opt {};
@ -308,29 +295,21 @@ ThrowCompletionOr<Object*> LocaleConstructor::construct(FunctionObject& new_targ
// 14. If calendar is not undefined, then // 14. If calendar is not undefined, then
// a. If calendar does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. // a. If calendar does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
// 15. Set opt.[[ca]] to calendar. // 15. Set opt.[[ca]] to calendar.
opt.ca = get_string_option(global_object, *options, vm.names.calendar, Unicode::is_type_identifier); opt.ca = TRY(get_string_option(global_object, *options, vm.names.calendar, Unicode::is_type_identifier));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 16. Let collation be ? GetOption(options, "collation", "string", undefined, undefined). // 16. Let collation be ? GetOption(options, "collation", "string", undefined, undefined).
// 17. If collation is not undefined, then // 17. If collation is not undefined, then
// a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. // a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
// 18. Set opt.[[co]] to collation. // 18. Set opt.[[co]] to collation.
opt.co = get_string_option(global_object, *options, vm.names.collation, Unicode::is_type_identifier); opt.co = TRY(get_string_option(global_object, *options, vm.names.collation, Unicode::is_type_identifier));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 19. Let hc be ? GetOption(options, "hourCycle", "string", « "h11", "h12", "h23", "h24" », undefined). // 19. Let hc be ? GetOption(options, "hourCycle", "string", « "h11", "h12", "h23", "h24" », undefined).
// 20. Set opt.[[hc]] to hc. // 20. Set opt.[[hc]] to hc.
opt.hc = get_string_option(global_object, *options, vm.names.hourCycle, nullptr, { "h11"sv, "h12"sv, "h23"sv, "h24"sv }); opt.hc = TRY(get_string_option(global_object, *options, vm.names.hourCycle, nullptr, { "h11"sv, "h12"sv, "h23"sv, "h24"sv }));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 21. Let kf be ? GetOption(options, "caseFirst", "string", « "upper", "lower", "false" », undefined). // 21. Let kf be ? GetOption(options, "caseFirst", "string", « "upper", "lower", "false" », undefined).
// 22. Set opt.[[kf]] to kf. // 22. Set opt.[[kf]] to kf.
opt.kf = get_string_option(global_object, *options, vm.names.caseFirst, nullptr, { "upper"sv, "lower"sv, "false"sv }); opt.kf = TRY(get_string_option(global_object, *options, vm.names.caseFirst, nullptr, { "upper"sv, "lower"sv, "false"sv }));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 23. Let kn be ? GetOption(options, "numeric", "boolean", undefined, undefined). // 23. Let kn be ? GetOption(options, "numeric", "boolean", undefined, undefined).
auto kn = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::Boolean, {}, Empty {})); auto kn = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::Boolean, {}, Empty {}));
@ -344,9 +323,7 @@ ThrowCompletionOr<Object*> LocaleConstructor::construct(FunctionObject& new_targ
// 27. If numberingSystem is not undefined, then // 27. If numberingSystem is not undefined, then
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception. // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
// 28. Set opt.[[nu]] to numberingSystem. // 28. Set opt.[[nu]] to numberingSystem.
opt.nu = get_string_option(global_object, *options, vm.names.numberingSystem, Unicode::is_type_identifier); opt.nu = TRY(get_string_option(global_object, *options, vm.names.numberingSystem, Unicode::is_type_identifier));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 29. Let r be ! ApplyUnicodeExtensionToTag(tag, opt, relevantExtensionKeys). // 29. Let r be ! ApplyUnicodeExtensionToTag(tag, opt, relevantExtensionKeys).
auto result = apply_unicode_extension_to_tag(tag, move(opt), relevant_extension_keys); auto result = apply_unicode_extension_to_tag(tag, move(opt), relevant_extension_keys);