mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:27:43 +00:00
LibJS: Convert Locale AOs to ThrowCompletionOr
This commit is contained in:
parent
09b08d8e35
commit
3426285738
1 changed files with 17 additions and 40 deletions
|
@ -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.
|
||||
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 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())
|
||||
return {};
|
||||
return Optional<String> {};
|
||||
|
||||
if (validator && !validator(option.as_string().string())) {
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, option, property);
|
||||
return {};
|
||||
}
|
||||
if (validator && !validator(option.as_string().string()))
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, option, property);
|
||||
|
||||
return option.as_string().string();
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
|
@ -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.
|
||||
auto locale_id = is_structurally_valid_language_tag(tag);
|
||||
if (!locale_id.has_value()) {
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, tag);
|
||||
return {};
|
||||
}
|
||||
if (!locale_id.has_value())
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, tag);
|
||||
|
||||
// 4. Let language be ? GetOption(options, "language", "string", undefined, undefined).
|
||||
// 5. If language is not undefined, then
|
||||
// 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);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto language = TRY(get_string_option(global_object, options, vm.names.language, Unicode::is_unicode_language_subtag));
|
||||
|
||||
// 6. Let script be ? GetOption(options, "script", "string", undefined, undefined).
|
||||
// 7. If script is not undefined, then
|
||||
// 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);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto script = TRY(get_string_option(global_object, options, vm.names.script, Unicode::is_unicode_script_subtag));
|
||||
|
||||
// 8. Let region be ? GetOption(options, "region", "string", undefined, undefined).
|
||||
// 9. If region is not undefined, then
|
||||
// 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);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto region = TRY(get_string_option(global_object, options, vm.names.region, Unicode::is_unicode_region_subtag));
|
||||
|
||||
// 10. Set tag to CanonicalizeUnicodeLocaleId(tag).
|
||||
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));
|
||||
|
||||
// 11. Set tag to ? ApplyOptionsToTag(tag, options).
|
||||
auto applied_tag = apply_options_to_tag(global_object, tag, *options);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
tag = applied_tag.release_value();
|
||||
tag = TRY(apply_options_to_tag(global_object, tag, *options));
|
||||
|
||||
// 12. Let opt be a new Record.
|
||||
LocaleAndKeys opt {};
|
||||
|
@ -308,29 +295,21 @@ ThrowCompletionOr<Object*> LocaleConstructor::construct(FunctionObject& new_targ
|
|||
// 14. If calendar is not undefined, then
|
||||
// a. If calendar does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
|
||||
// 15. Set opt.[[ca]] to calendar.
|
||||
opt.ca = get_string_option(global_object, *options, vm.names.calendar, Unicode::is_type_identifier);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
opt.ca = TRY(get_string_option(global_object, *options, vm.names.calendar, Unicode::is_type_identifier));
|
||||
|
||||
// 16. Let collation be ? GetOption(options, "collation", "string", undefined, undefined).
|
||||
// 17. If collation is not undefined, then
|
||||
// a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
|
||||
// 18. Set opt.[[co]] to collation.
|
||||
opt.co = get_string_option(global_object, *options, vm.names.collation, Unicode::is_type_identifier);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
opt.co = TRY(get_string_option(global_object, *options, vm.names.collation, Unicode::is_type_identifier));
|
||||
|
||||
// 19. Let hc be ? GetOption(options, "hourCycle", "string", « "h11", "h12", "h23", "h24" », undefined).
|
||||
// 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 });
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
opt.hc = TRY(get_string_option(global_object, *options, vm.names.hourCycle, nullptr, { "h11"sv, "h12"sv, "h23"sv, "h24"sv }));
|
||||
|
||||
// 21. Let kf be ? GetOption(options, "caseFirst", "string", « "upper", "lower", "false" », undefined).
|
||||
// 22. Set opt.[[kf]] to kf.
|
||||
opt.kf = 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());
|
||||
opt.kf = TRY(get_string_option(global_object, *options, vm.names.caseFirst, nullptr, { "upper"sv, "lower"sv, "false"sv }));
|
||||
|
||||
// 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 {}));
|
||||
|
@ -344,9 +323,7 @@ ThrowCompletionOr<Object*> LocaleConstructor::construct(FunctionObject& new_targ
|
|||
// 27. If numberingSystem is not undefined, then
|
||||
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
|
||||
// 28. Set opt.[[nu]] to numberingSystem.
|
||||
opt.nu = get_string_option(global_object, *options, vm.names.numberingSystem, Unicode::is_type_identifier);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
opt.nu = TRY(get_string_option(global_object, *options, vm.names.numberingSystem, Unicode::is_type_identifier));
|
||||
|
||||
// 29. Let r be ! ApplyUnicodeExtensionToTag(tag, opt, relevantExtensionKeys).
|
||||
auto result = apply_unicode_extension_to_tag(tag, move(opt), relevant_extension_keys);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue