CollatorConstructor.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  10. #include <LibJS/Runtime/Intl/Collator.h>
  11. #include <LibJS/Runtime/Intl/CollatorConstructor.h>
  12. #include <LibUnicode/Locale.h>
  13. namespace JS::Intl {
  14. GC_DEFINE_ALLOCATOR(CollatorConstructor);
  15. // 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor
  16. CollatorConstructor::CollatorConstructor(Realm& realm)
  17. : NativeFunction(realm.vm().names.Collator.as_string(), realm.intrinsics().function_prototype())
  18. {
  19. }
  20. void CollatorConstructor::initialize(Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. auto& vm = this->vm();
  24. // 10.2.1 Intl.Collator.prototype, https://tc39.es/ecma402/#sec-intl.collator.prototype
  25. define_direct_property(vm.names.prototype, realm.intrinsics().intl_collator_prototype(), 0);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  29. }
  30. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  31. ThrowCompletionOr<Value> CollatorConstructor::call()
  32. {
  33. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget
  34. return TRY(construct(*this));
  35. }
  36. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  37. ThrowCompletionOr<GC::Ref<Object>> CollatorConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto locales_value = vm.argument(0);
  41. auto options_value = vm.argument(1);
  42. // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
  43. // 3. If %Intl.Collator%.[[RelevantExtensionKeys]] contains "kn", then
  44. // a. Append [[Numeric]] to internalSlotsList.
  45. // 4. If %Intl.Collator%.[[RelevantExtensionKeys]] contains "kf", then
  46. // a. Append [[CaseFirst]] to internalSlotsList.
  47. // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.Collator.prototype%", internalSlotsList).
  48. auto collator = TRY(ordinary_create_from_constructor<Collator>(vm, new_target, &Intrinsics::intl_collator_prototype));
  49. // 6. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  50. auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
  51. // 7. Set options to ? CoerceOptionsToObject(options).
  52. auto* options = TRY(coerce_options_to_object(vm, options_value));
  53. // 8. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort").
  54. auto usage = TRY(get_option(vm, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv));
  55. // 9. Set collator.[[Usage]] to usage.
  56. collator->set_usage(usage.as_string().utf8_string_view());
  57. // 10. If usage is "sort", then
  58. // a. Let localeData be %Intl.Collator%.[[SortLocaleData]].
  59. // 11. Else,
  60. // a. Let localeData be %Intl.Collator%.[[SearchLocaleData]].
  61. // 12. Let opt be a new Record.
  62. LocaleOptions opt {};
  63. // 13. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  64. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  65. // 14. Set opt.[[localeMatcher]] to matcher.
  66. opt.locale_matcher = matcher;
  67. // 15. Let collation be ? GetOption(options, "collation", string, empty, undefined).
  68. auto collation = TRY(get_option(vm, *options, vm.names.collation, OptionType::String, {}, Empty {}));
  69. // 16. If collation is not undefined, then
  70. if (!collation.is_undefined()) {
  71. // a. If collation cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception.
  72. if (!Unicode::is_type_identifier(collation.as_string().utf8_string_view()))
  73. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, collation, "collation"sv);
  74. }
  75. // 17. Set opt.[[co]] to collation.
  76. opt.co = locale_key_from_value(collation);
  77. // 18. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined).
  78. auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {}));
  79. // 19. If numeric is not undefined, then
  80. if (!numeric.is_undefined()) {
  81. // a. Set numeric to ! ToString(numeric).
  82. numeric = PrimitiveString::create(vm, MUST(numeric.to_string(vm)));
  83. }
  84. // 20. Set opt.[[kn]] to numeric.
  85. opt.kn = locale_key_from_value(numeric);
  86. // 21. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined).
  87. auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {}));
  88. // 22. Set opt.[[kf]] to caseFirst.
  89. opt.kf = locale_key_from_value(case_first);
  90. // 23. Let relevantExtensionKeys be %Intl.Collator%.[[RelevantExtensionKeys]].
  91. auto relevant_extension_keys = Collator::relevant_extension_keys();
  92. // 24. Let r be ResolveLocale(%Intl.Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
  93. auto result = resolve_locale(requested_locales, opt, relevant_extension_keys);
  94. // 25. Set collator.[[Locale]] to r.[[Locale]].
  95. collator->set_locale(move(result.locale));
  96. // 26. Set collation to r.[[co]].
  97. auto collation_value = move(result.co);
  98. // 27. If collation is null, set collation to "default".
  99. if (collation_value.has<Empty>())
  100. collation_value = "default"_string;
  101. // 28. Set collator.[[Collation]] to collation.
  102. collator->set_collation(move(collation_value.get<String>()));
  103. // 29. If relevantExtensionKeys contains "kn", then
  104. if (relevant_extension_keys.span().contains_slow("kn"sv)) {
  105. // a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
  106. collator->set_numeric(result.kn == "true"_string);
  107. }
  108. // 30. If relevantExtensionKeys contains "kf", then
  109. if (relevant_extension_keys.span().contains_slow("kf"sv)) {
  110. // a. Set collator.[[CaseFirst]] to r.[[kf]].
  111. if (auto* resolved_case_first = result.kf.get_pointer<String>())
  112. collator->set_case_first(*resolved_case_first);
  113. }
  114. // 31. Let resolvedLocaleData be r.[[LocaleData]].
  115. // 32. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined).
  116. auto sensitivity_value = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {}));
  117. // 33. If sensitivity is undefined, then
  118. if (sensitivity_value.is_undefined()) {
  119. // a. If usage is "sort", then
  120. if (collator->usage() == Unicode::Usage::Sort) {
  121. // i. Set sensitivity to "variant".
  122. sensitivity_value = PrimitiveString::create(vm, "variant"_string);
  123. }
  124. // b. Else,
  125. else {
  126. // i. Set sensitivity to resolvedLocaleData.[[sensitivity]].
  127. // NOTE: We do not acquire the default [[sensitivity]] here. Instead, we default the option to null,
  128. // and let LibUnicode fill in the default value if an override was not provided here.
  129. }
  130. }
  131. Optional<Unicode::Sensitivity> sensitivity;
  132. if (!sensitivity_value.is_undefined())
  133. sensitivity = Unicode::sensitivity_from_string(sensitivity_value.as_string().utf8_string_view());
  134. // 35. Let defaultIgnorePunctuation be resolvedLocaleData.[[ignorePunctuation]].
  135. // NOTE: We do not acquire the default [[ignorePunctuation]] here. Instead, we default the option to null,
  136. // and let LibUnicode fill in the default value if an override was not provided here.
  137. // 36. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, defaultIgnorePunctuation).
  138. auto ignore_punctuation_value = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, Empty {}));
  139. Optional<bool> ignore_punctuation;
  140. if (!ignore_punctuation_value.is_undefined())
  141. ignore_punctuation = ignore_punctuation_value.as_bool();
  142. // Non-standard, create an ICU collator for this Intl object.
  143. auto icu_collator = Unicode::Collator::create(
  144. collator->locale(),
  145. collator->usage(),
  146. collator->collation(),
  147. sensitivity,
  148. collator->case_first(),
  149. collator->numeric(),
  150. ignore_punctuation);
  151. collator->set_collator(move(icu_collator));
  152. // 34. Set collator.[[Sensitivity]] to sensitivity.
  153. collator->set_sensitivity(collator->collator().sensitivity());
  154. // 37. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
  155. collator->set_ignore_punctuation(collator->collator().ignore_punctuation());
  156. // 38. Return collator.
  157. return collator;
  158. }
  159. // 10.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.collator.supportedlocalesof
  160. JS_DEFINE_NATIVE_FUNCTION(CollatorConstructor::supported_locales_of)
  161. {
  162. auto locales = vm.argument(0);
  163. auto options = vm.argument(1);
  164. // 1. Let availableLocales be %Collator%.[[AvailableLocales]].
  165. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  166. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  167. // 3. Return ? FilterLocales(availableLocales, requestedLocales, options).
  168. return TRY(filter_locales(vm, requested_locales, options));
  169. }
  170. }