From 636e2143264ef3e4a8d4886987bb263de08e299b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 7 Apr 2025 17:13:24 -0400 Subject: [PATCH] LibJS: Migrate Intl.Collator to use ResolveOptions This is an editorial change in the ECMA-402 spec. See: https://github.com/tc39/ecma402/commit/f822dc1 --- .../Runtime/Intl/CollatorConstructor.cpp | 94 ++++++------------- 1 file changed, 30 insertions(+), 64 deletions(-) diff --git a/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp b/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp index 17e05331706..86f3363106a 100644 --- a/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp +++ b/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp @@ -47,6 +47,7 @@ ThrowCompletionOr CollatorConstructor::call() ThrowCompletionOr> CollatorConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); + auto& realm = *vm.current_realm(); auto locales_value = vm.argument(0); auto options_value = vm.argument(1); @@ -55,105 +56,70 @@ ThrowCompletionOr> CollatorConstructor::construct(FunctionObject // 3. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.Collator.prototype%", internalSlotsList). auto collator = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::intl_collator_prototype)); - // 4. Let requestedLocales be ? CanonicalizeLocaleList(locales). + // 4. NOTE: The source of locale data for ResolveOptions depends upon the "usage" property of options, but the following + // two steps must observably precede that lookup (and must not observably repeat inside ResolveOptions). + + // 5. Let requestedLocales be ? CanonicalizeLocaleList(locales). auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value)); - // 5. Set options to ? CoerceOptionsToObject(options). + // 6. Set options to ? CoerceOptionsToObject(options). auto* options = TRY(coerce_options_to_object(vm, options_value)); - // 6. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort"). + // 7. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort"). auto usage = TRY(get_option(vm, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv)); - // 7. Set collator.[[Usage]] to usage. + // 8. Set collator.[[Usage]] to usage. collator->set_usage(usage.as_string().utf8_string_view()); - // 8. If usage is "sort", then + // 9. If usage is "sort", then // a. Let localeData be %Intl.Collator%.[[SortLocaleData]]. - // 9. Else, + // 10. Else, // a. Let localeData be %Intl.Collator%.[[SearchLocaleData]]. - // 10. Let opt be a new Record. - LocaleOptions opt {}; + // 11. Let optionsResolution be ? ResolveOptions(%Intl.Collator%, localeData, CreateArrayFromList(requestedLocales), options). + auto requested_locales_array = Array::create_from(realm, requested_locales, [&](auto& locale) { return PrimitiveString::create(vm, move(locale)); }); + auto options_resolution = TRY(resolve_options(vm, collator, requested_locales_array, options_value)); - // 11. 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)); + // 12. Let r be optionsResolution.[[ResolvedLocale]]. + auto result = move(options_resolution.resolved_locale); - // 12. Set opt.[[localeMatcher]] to matcher. - opt.locale_matcher = matcher; - - // 13. Let collation be ? GetOption(options, "collation", string, empty, undefined). - auto collation = TRY(get_option(vm, *options, vm.names.collation, OptionType::String, {}, Empty {})); - - // 14. If collation is not undefined, then - if (!collation.is_undefined()) { - // a. If collation cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception. - if (!Unicode::is_type_identifier(collation.as_string().utf8_string_view())) - return vm.throw_completion(ErrorType::OptionIsNotValidValue, collation, "collation"sv); - } - - // 15. Set opt.[[co]] to collation. - opt.co = locale_key_from_value(collation); - - // 16. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined). - auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {})); - - // 17. If numeric is not undefined, then - if (!numeric.is_undefined()) { - // a. Set numeric to ! ToString(numeric). - numeric = PrimitiveString::create(vm, MUST(numeric.to_string(vm))); - } - - // 18. Set opt.[[kn]] to numeric. - opt.kn = locale_key_from_value(numeric); - - // 19. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined). - auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {})); - - // 20. Set opt.[[kf]] to caseFirst. - opt.kf = locale_key_from_value(case_first); - - // 21. Let r be ResolveLocale(%Intl.Collator%.[[AvailableLocales]], requestedLocales, opt, %Intl.Collator%.[[RelevantExtensionKeys]], localeData). - auto result = resolve_locale(requested_locales, opt, collator->relevant_extension_keys()); - - // 22. Set collator.[[Locale]] to r.[[Locale]]. + // 13. Set collator.[[Locale]] to r.[[Locale]]. collator->set_locale(move(result.locale)); - // 23. Set collation to r.[[co]]. - auto collation_value = move(result.co); + // 14. If r.[[co]] is null, let collation be "default". Otherwise, let collation be r.[[co]]. + auto collation = result.co.has() + ? "default"_string + : move(result.co.get()); - // 24. If collation is null, set collation to "default". - if (collation_value.has()) - collation_value = "default"_string; + // 15. Set collator.[[Collation]] to collation. + collator->set_collation(move(collation)); - // 25. Set collator.[[Collation]] to collation. - collator->set_collation(move(collation_value.get())); - - // 26. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true"). + // 16. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true"). collator->set_numeric(result.kn == "true"_string); - // 27. Set collator.[[CaseFirst]] to r.[[kf]]. + // 17. Set collator.[[CaseFirst]] to r.[[kf]]. if (auto const* resolved_case_first = result.kf.get_pointer()) collator->set_case_first(*resolved_case_first); - // 28. Let resolvedLocaleData be r.[[LocaleData]]. + // 18. Let resolvedLocaleData be r.[[LocaleData]]. - // 29. If usage is "sort", let defaultSensitivity be "variant". Otherwise, let defaultSensitivity be resolvedLocaleData.[[sensitivity]]. + // 19. If usage is "sort", let defaultSensitivity be "variant". Otherwise, let defaultSensitivity be resolvedLocaleData.[[sensitivity]]. // NOTE: We do not acquire resolvedLocaleData.[[sensitivity]] here. Instead, we let LibUnicode fill in the // default value if an override was not provided here. auto default_sensitivity = collator->usage() == Unicode::Usage::Sort ? "variant"sv : OptionDefault {}; - // 30. Set collator.[[Sensitivity]] to ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », defaultSensitivity). + // 20. Set collator.[[Sensitivity]] to ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », defaultSensitivity). auto sensitivity_value = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, default_sensitivity)); Optional sensitivity; if (!sensitivity_value.is_undefined()) sensitivity = Unicode::sensitivity_from_string(sensitivity_value.as_string().utf8_string_view()); - // 31. Let defaultIgnorePunctuation be resolvedLocaleData.[[ignorePunctuation]]. + // 21. Let defaultIgnorePunctuation be resolvedLocaleData.[[ignorePunctuation]]. // NOTE: We do not acquire resolvedLocaleData.[[ignorePunctuation]] here. Instead, we let LibUnicode fill in the // default value if an override was not provided here. - // 32. Set collator.[[IgnorePunctuation]] to ? GetOption(options, "ignorePunctuation", boolean, empty, defaultIgnorePunctuation). + // 22. Set collator.[[IgnorePunctuation]] to ? GetOption(options, "ignorePunctuation", boolean, empty, defaultIgnorePunctuation). auto ignore_punctuation_value = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, Empty {})); Optional ignore_punctuation; @@ -174,7 +140,7 @@ ThrowCompletionOr> CollatorConstructor::construct(FunctionObject collator->set_sensitivity(collator->collator().sensitivity()); collator->set_ignore_punctuation(collator->collator().ignore_punctuation()); - // 33. Return collator. + // 23. Return collator. return collator; }