LibJS: Ensure Intl.Collator instances have [[Numeric]] and [[CaseFirst]]

This is an editorial change in the ECMA-402 spec. See:
243ec38
This commit is contained in:
Timothy Flynn 2025-04-07 14:09:35 -04:00 committed by Tim Flynn
commit b81d0d3261
Notes: github-actions[bot] 2025-04-08 10:53:51 +00:00

View file

@ -51,110 +51,96 @@ ThrowCompletionOr<GC::Ref<Object>> CollatorConstructor::construct(FunctionObject
auto locales_value = vm.argument(0); auto locales_value = vm.argument(0);
auto options_value = vm.argument(1); auto options_value = vm.argument(1);
// 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ». // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Collation]], [[Numeric]], [[CaseFirst]], [[Sensitivity]], [[IgnorePunctuation]], [[BoundCompare]] ».
// 3. If %Intl.Collator%.[[RelevantExtensionKeys]] contains "kn", then // 3. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.Collator.prototype%", internalSlotsList).
// a. Append [[Numeric]] to internalSlotsList.
// 4. If %Intl.Collator%.[[RelevantExtensionKeys]] contains "kf", then
// a. Append [[CaseFirst]] to internalSlotsList.
// 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.Collator.prototype%", internalSlotsList).
auto collator = TRY(ordinary_create_from_constructor<Collator>(vm, new_target, &Intrinsics::intl_collator_prototype)); auto collator = TRY(ordinary_create_from_constructor<Collator>(vm, new_target, &Intrinsics::intl_collator_prototype));
// 6. Let requestedLocales be ? CanonicalizeLocaleList(locales). // 4. Let requestedLocales be ? CanonicalizeLocaleList(locales).
auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value)); auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
// 7. Set options to ? CoerceOptionsToObject(options). // 5. Set options to ? CoerceOptionsToObject(options).
auto* options = TRY(coerce_options_to_object(vm, options_value)); auto* options = TRY(coerce_options_to_object(vm, options_value));
// 8. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort"). // 6. 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)); auto usage = TRY(get_option(vm, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv));
// 9. Set collator.[[Usage]] to usage. // 7. Set collator.[[Usage]] to usage.
collator->set_usage(usage.as_string().utf8_string_view()); collator->set_usage(usage.as_string().utf8_string_view());
// 10. If usage is "sort", then // 8. If usage is "sort", then
// a. Let localeData be %Intl.Collator%.[[SortLocaleData]]. // a. Let localeData be %Intl.Collator%.[[SortLocaleData]].
// 11. Else, // 9. Else,
// a. Let localeData be %Intl.Collator%.[[SearchLocaleData]]. // a. Let localeData be %Intl.Collator%.[[SearchLocaleData]].
// 12. Let opt be a new Record. // 10. Let opt be a new Record.
LocaleOptions opt {}; LocaleOptions opt {};
// 13. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit"). // 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)); auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
// 14. Set opt.[[localeMatcher]] to matcher. // 12. Set opt.[[localeMatcher]] to matcher.
opt.locale_matcher = matcher; opt.locale_matcher = matcher;
// 15. Let collation be ? GetOption(options, "collation", string, empty, undefined). // 13. Let collation be ? GetOption(options, "collation", string, empty, undefined).
auto collation = TRY(get_option(vm, *options, vm.names.collation, OptionType::String, {}, Empty {})); auto collation = TRY(get_option(vm, *options, vm.names.collation, OptionType::String, {}, Empty {}));
// 16. If collation is not undefined, then // 14. If collation is not undefined, then
if (!collation.is_undefined()) { if (!collation.is_undefined()) {
// a. If collation cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception. // 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())) if (!Unicode::is_type_identifier(collation.as_string().utf8_string_view()))
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, collation, "collation"sv); return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, collation, "collation"sv);
} }
// 17. Set opt.[[co]] to collation. // 15. Set opt.[[co]] to collation.
opt.co = locale_key_from_value(collation); opt.co = locale_key_from_value(collation);
// 18. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined). // 16. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined).
auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {})); auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {}));
// 19. If numeric is not undefined, then // 17. If numeric is not undefined, then
if (!numeric.is_undefined()) { if (!numeric.is_undefined()) {
// a. Set numeric to ! ToString(numeric). // a. Set numeric to ! ToString(numeric).
numeric = PrimitiveString::create(vm, MUST(numeric.to_string(vm))); numeric = PrimitiveString::create(vm, MUST(numeric.to_string(vm)));
} }
// 20. Set opt.[[kn]] to numeric. // 18. Set opt.[[kn]] to numeric.
opt.kn = locale_key_from_value(numeric); opt.kn = locale_key_from_value(numeric);
// 21. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined). // 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 {})); auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {}));
// 22. Set opt.[[kf]] to caseFirst. // 20. Set opt.[[kf]] to caseFirst.
opt.kf = locale_key_from_value(case_first); opt.kf = locale_key_from_value(case_first);
// 23. Let relevantExtensionKeys be %Intl.Collator%.[[RelevantExtensionKeys]]. // 21. Let r be ResolveLocale(%Intl.Collator%.[[AvailableLocales]], requestedLocales, opt, %Intl.Collator%.[[RelevantExtensionKeys]], localeData).
auto relevant_extension_keys = Collator::relevant_extension_keys(); auto result = resolve_locale(requested_locales, opt, Collator::relevant_extension_keys());
// 24. Let r be ResolveLocale(%Intl.Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData). // 22. Set collator.[[Locale]] to r.[[Locale]].
auto result = resolve_locale(requested_locales, opt, relevant_extension_keys);
// 25. Set collator.[[Locale]] to r.[[Locale]].
collator->set_locale(move(result.locale)); collator->set_locale(move(result.locale));
// 26. Set collation to r.[[co]]. // 23. Set collation to r.[[co]].
auto collation_value = move(result.co); auto collation_value = move(result.co);
// 27. If collation is null, set collation to "default". // 24. If collation is null, set collation to "default".
if (collation_value.has<Empty>()) if (collation_value.has<Empty>())
collation_value = "default"_string; collation_value = "default"_string;
// 28. Set collator.[[Collation]] to collation. // 25. Set collator.[[Collation]] to collation.
collator->set_collation(move(collation_value.get<String>())); collator->set_collation(move(collation_value.get<String>()));
// 29. If relevantExtensionKeys contains "kn", then // 26. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
if (relevant_extension_keys.span().contains_slow("kn"sv)) {
// a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
collator->set_numeric(result.kn == "true"_string); collator->set_numeric(result.kn == "true"_string);
}
// 30. If relevantExtensionKeys contains "kf", then // 27. Set collator.[[CaseFirst]] to r.[[kf]].
if (relevant_extension_keys.span().contains_slow("kf"sv)) { if (auto const* resolved_case_first = result.kf.get_pointer<String>())
// a. Set collator.[[CaseFirst]] to r.[[kf]].
if (auto* resolved_case_first = result.kf.get_pointer<String>())
collator->set_case_first(*resolved_case_first); collator->set_case_first(*resolved_case_first);
}
// 31. Let resolvedLocaleData be r.[[LocaleData]]. // 28. Let resolvedLocaleData be r.[[LocaleData]].
// 32. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined). // 29. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined).
auto sensitivity_value = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {})); auto sensitivity_value = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {}));
// 33. If sensitivity is undefined, then // 30. If sensitivity is undefined, then
if (sensitivity_value.is_undefined()) { if (sensitivity_value.is_undefined()) {
// a. If usage is "sort", then // a. If usage is "sort", then
if (collator->usage() == Unicode::Usage::Sort) { if (collator->usage() == Unicode::Usage::Sort) {
@ -173,11 +159,11 @@ ThrowCompletionOr<GC::Ref<Object>> CollatorConstructor::construct(FunctionObject
if (!sensitivity_value.is_undefined()) if (!sensitivity_value.is_undefined())
sensitivity = Unicode::sensitivity_from_string(sensitivity_value.as_string().utf8_string_view()); sensitivity = Unicode::sensitivity_from_string(sensitivity_value.as_string().utf8_string_view());
// 35. Let defaultIgnorePunctuation be resolvedLocaleData.[[ignorePunctuation]]. // 32. Let defaultIgnorePunctuation be resolvedLocaleData.[[ignorePunctuation]].
// NOTE: We do not acquire the default [[ignorePunctuation]] here. Instead, we default the option to null, // NOTE: We do not acquire the default [[ignorePunctuation]] here. Instead, we default the option to null,
// and let LibUnicode fill in the default value if an override was not provided here. // and let LibUnicode fill in the default value if an override was not provided here.
// 36. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, defaultIgnorePunctuation). // 33. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, defaultIgnorePunctuation).
auto ignore_punctuation_value = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, Empty {})); auto ignore_punctuation_value = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, Empty {}));
Optional<bool> ignore_punctuation; Optional<bool> ignore_punctuation;
@ -195,13 +181,13 @@ ThrowCompletionOr<GC::Ref<Object>> CollatorConstructor::construct(FunctionObject
ignore_punctuation); ignore_punctuation);
collator->set_collator(move(icu_collator)); collator->set_collator(move(icu_collator));
// 34. Set collator.[[Sensitivity]] to sensitivity. // 31. Set collator.[[Sensitivity]] to sensitivity.
collator->set_sensitivity(collator->collator().sensitivity()); collator->set_sensitivity(collator->collator().sensitivity());
// 37. Set collator.[[IgnorePunctuation]] to ignorePunctuation. // 34. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
collator->set_ignore_punctuation(collator->collator().ignore_punctuation()); collator->set_ignore_punctuation(collator->collator().ignore_punctuation());
// 38. Return collator. // 35. Return collator.
return collator; return collator;
} }