LibJS: Migrate Intl.DisplayNames to use ResolveOptions

This is an editorial change in the ECMA-402 spec. See:
5eafacd
This commit is contained in:
Timothy Flynn 2025-04-07 17:19:51 -04:00 committed by Tim Flynn
commit 3f55240a8e
Notes: github-actions[bot] 2025-04-08 10:52:56 +00:00

View file

@ -49,70 +49,53 @@ ThrowCompletionOr<GC::Ref<Object>> DisplayNamesConstructor::construct(FunctionOb
{
auto& vm = this->vm();
auto locale_value = vm.argument(0);
auto locales_value = vm.argument(0);
auto options_value = vm.argument(1);
// 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%Intl.DisplayNames.prototype%", « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[LanguageDisplay]], [[Fields]] »).
auto display_names = TRY(ordinary_create_from_constructor<DisplayNames>(vm, new_target, &Intrinsics::intl_display_names_prototype));
// 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
auto requested_locales = TRY(canonicalize_locale_list(vm, locale_value));
// 3. Let optionsResolution be ? ResolveOptions(%Intl.DisplayNames%, %Intl.DisplayNames%.[[LocaleData]], locales, options, « REQUIRE-OPTIONS »).
// 4. Set options to optionsResolution.[[Options]].
// 5. Let r be optionsResolution.[[ResolvedLocale]].
auto [options, result, _] = TRY(resolve_options(vm, display_names, locales_value, options_value, SpecialBehaviors::RequireOptions));
// 4. If options is undefined, throw a TypeError exception.
if (options_value.is_undefined())
return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "options"sv);
// 5. Set options to ? GetOptionsObject(options).
auto options = TRY(get_options_object(vm, options_value));
// 6. Let opt be a new Record.
LocaleOptions opt {};
// 7. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
// 8. Set opt.[[localeMatcher]] to matcher.
opt.locale_matcher = matcher;
// 9. Let r be ResolveLocale(%Intl.DisplayNames%.[[AvailableLocales]], requestedLocales, opt, %Intl.DisplayNames%.[[RelevantExtensionKeys]], %Intl.DisplayNames%.[[LocaleData]]).
auto result = resolve_locale(requested_locales, opt, display_names->relevant_extension_keys());
// 10. Let style be ? GetOption(options, "style", string, « "narrow", "short", "long" », "long").
// 6. Let style be ? GetOption(options, "style", string, « "narrow", "short", "long" », "long").
auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv));
// 11. Set displayNames.[[Style]] to style.
// 7. Set displayNames.[[Style]] to style.
display_names->set_style(style.as_string().utf8_string_view());
// 12. Let type be ? GetOption(options, "type", string, « "language", "region", "script", "currency", "calendar", "dateTimeField" », undefined).
// 8. Let type be ? GetOption(options, "type", string, « "language", "region", "script", "currency", "calendar", "dateTimeField" », undefined).
auto type = TRY(get_option(vm, *options, vm.names.type, OptionType::String, { "language"sv, "region"sv, "script"sv, "currency"sv, "calendar"sv, "dateTimeField"sv }, Empty {}));
// 13. If type is undefined, throw a TypeError exception.
// 9. If type is undefined, throw a TypeError exception.
if (type.is_undefined())
return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "options.type"sv);
// 14. Set displayNames.[[Type]] to type.
// 10. Set displayNames.[[Type]] to type.
display_names->set_type(type.as_string().utf8_string_view());
// 15. Let fallback be ? GetOption(options, "fallback", string, « "code", "none" », "code").
// 11. Let fallback be ? GetOption(options, "fallback", string, « "code", "none" », "code").
auto fallback = TRY(get_option(vm, *options, vm.names.fallback, OptionType::String, { "code"sv, "none"sv }, "code"sv));
// 16. Set displayNames.[[Fallback]] to fallback.
// 12. Set displayNames.[[Fallback]] to fallback.
display_names->set_fallback(fallback.as_string().utf8_string_view());
// 17. Set displayNames.[[Locale]] to r.[[Locale]].
// 13. Set displayNames.[[Locale]] to r.[[Locale]].
display_names->set_locale(move(result.locale));
// 18. Let resolvedLocaleData be r.[[LocaleData]].
// 19. Let types be resolvedLocaleData.[[types]].
// 20. Assert: types is a Record (see 12.2.3).
// 14. Let resolvedLocaleData be r.[[LocaleData]].
// 15. Let types be resolvedLocaleData.[[types]].
// 16. Assert: types is a Record (see 12.2.3).
// 21. Let languageDisplay be ? GetOption(options, "languageDisplay", string, « "dialect", "standard" », "dialect").
// 17. Let languageDisplay be ? GetOption(options, "languageDisplay", string, « "dialect", "standard" », "dialect").
auto language_display = TRY(get_option(vm, *options, vm.names.languageDisplay, OptionType::String, { "dialect"sv, "standard"sv }, "dialect"sv));
// 22. Let typeFields be types.[[<type>]].
// 23. Assert: typeFields is a Record (see 12.2.3).
// 18. Let typeFields be types.[[<type>]].
// 19. Assert: typeFields is a Record (see 12.2.3).
// 24. If type is "language", then
// 20. If type is "language", then
if (display_names->type() == DisplayNames::Type::Language) {
// a. Set displayNames.[[LanguageDisplay]] to languageDisplay.
display_names->set_language_display(language_display.as_string().utf8_string_view());
@ -121,11 +104,11 @@ ThrowCompletionOr<GC::Ref<Object>> DisplayNamesConstructor::construct(FunctionOb
// c. Assert: typeFields is a Record (see 12.2.3).
}
// 25. Let styleFields be typeFields.[[<style>]].
// 26. Assert: styleFields is a Record (see 12.2.3).
// 27. Set displayNames.[[Fields]] to styleFields.
// 21. Let styleFields be typeFields.[[<style>]].
// 22. Assert: styleFields is a Record (see 12.2.3).
// 23. Set displayNames.[[Fields]] to styleFields.
// 28. Return displayNames.
// 24. Return displayNames.
return display_names;
}