CollatorConstructor.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2022-2023, 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 <LibLocale/Locale.h>
  13. namespace JS::Intl {
  14. JS_DEFINE_ALLOCATOR(CollatorConstructor);
  15. // 10.1.2 InitializeCollator ( collator, locales, options ), https://tc39.es/ecma402/#sec-initializecollator
  16. static ThrowCompletionOr<NonnullGCPtr<Collator>> initialize_collator(VM& vm, Collator& collator, Value locales_value, Value options_value)
  17. {
  18. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  19. auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
  20. // 2. Set options to ? CoerceOptionsToObject(options).
  21. auto* options = TRY(coerce_options_to_object(vm, options_value));
  22. // 3. Let usage be ? GetOption(options, "usage", string, « "sort", "search" », "sort").
  23. auto usage = TRY(get_option(vm, *options, vm.names.usage, OptionType::String, { "sort"sv, "search"sv }, "sort"sv));
  24. // 4. Set collator.[[Usage]] to usage.
  25. collator.set_usage(usage.as_string().utf8_string_view());
  26. // 5. If usage is "sort", then
  27. // a. Let localeData be %Collator%.[[SortLocaleData]].
  28. // 6. Else,
  29. // a. Let localeData be %Collator%.[[SearchLocaleData]].
  30. // 7. Let opt be a new Record.
  31. LocaleOptions opt {};
  32. // 8. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  33. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  34. // 9. Set opt.[[localeMatcher]] to matcher.
  35. opt.locale_matcher = matcher;
  36. // 10. Let collation be ? GetOption(options, "collation", string, empty, undefined).
  37. auto collation = TRY(get_option(vm, *options, vm.names.collation, OptionType::String, {}, Empty {}));
  38. // 11. If collation is not undefined, then
  39. if (!collation.is_undefined()) {
  40. // a. If collation does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  41. if (!::Locale::is_type_identifier(collation.as_string().utf8_string_view()))
  42. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, collation, "collation"sv);
  43. // 12. Set opt.[[co]] to collation.
  44. opt.co = collation.as_string().utf8_string();
  45. }
  46. // 13. Let numeric be ? GetOption(options, "numeric", boolean, empty, undefined).
  47. auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::Boolean, {}, Empty {}));
  48. // 14. If numeric is not undefined, then
  49. if (!numeric.is_undefined()) {
  50. // a. Let numeric be ! ToString(numeric).
  51. numeric = PrimitiveString::create(vm, MUST(numeric.to_string(vm)));
  52. }
  53. // 15. Set opt.[[kn]] to numeric.
  54. opt.kn = locale_key_from_value(numeric);
  55. // 16. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined).
  56. auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {}));
  57. // 17. Set opt.[[kf]] to caseFirst.
  58. opt.kf = locale_key_from_value(case_first);
  59. // 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]].
  60. auto relevant_extension_keys = Collator::relevant_extension_keys();
  61. // 19. Let r be ResolveLocale(%Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
  62. auto result = resolve_locale(requested_locales, opt, relevant_extension_keys);
  63. // 20. Set collator.[[Locale]] to r.[[locale]].
  64. collator.set_locale(move(result.locale));
  65. // 21. Let collation be r.[[co]].
  66. auto& collation_value = result.co;
  67. // 22. If collation is null, let collation be "default".
  68. if (collation_value.has<Empty>())
  69. collation_value = "default"_string;
  70. // 23. Set collator.[[Collation]] to collation.
  71. collator.set_collation(move(collation_value.get<String>()));
  72. // 24. If relevantExtensionKeys contains "kn", then
  73. if (relevant_extension_keys.span().contains_slow("kn"sv)) {
  74. // a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
  75. collator.set_numeric(result.kn == "true"_string);
  76. }
  77. // 25. If relevantExtensionKeys contains "kf", then
  78. if (relevant_extension_keys.span().contains_slow("kf"sv)) {
  79. // a. Set collator.[[CaseFirst]] to r.[[kf]].
  80. if (auto* resolved_case_first = result.kf.get_pointer<String>())
  81. collator.set_case_first(*resolved_case_first);
  82. }
  83. // 26. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined).
  84. auto sensitivity = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {}));
  85. // 27. If sensitivity is undefined, then
  86. if (sensitivity.is_undefined()) {
  87. // a. If usage is "sort", then
  88. if (collator.usage() == Collator::Usage::Sort) {
  89. // i. Let sensitivity be "variant".
  90. sensitivity = PrimitiveString::create(vm, "variant"_string);
  91. }
  92. // b. Else,
  93. else {
  94. // i. Let dataLocale be r.[[dataLocale]].
  95. // ii. Let dataLocaleData be localeData.[[<dataLocale>]].
  96. // iii. Let sensitivity be dataLocaleData.[[sensitivity]].
  97. sensitivity = PrimitiveString::create(vm, "base"_string);
  98. }
  99. }
  100. // 28. Set collator.[[Sensitivity]] to sensitivity.
  101. collator.set_sensitivity(sensitivity.as_string().utf8_string_view());
  102. // 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, false).
  103. auto ignore_punctuation = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, false));
  104. // 30. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
  105. collator.set_ignore_punctuation(ignore_punctuation.as_bool());
  106. // 31. Return collator.
  107. return collator;
  108. }
  109. // 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor
  110. CollatorConstructor::CollatorConstructor(Realm& realm)
  111. : NativeFunction(realm.vm().names.Collator.as_string(), realm.intrinsics().function_prototype())
  112. {
  113. }
  114. void CollatorConstructor::initialize(Realm& realm)
  115. {
  116. Base::initialize(realm);
  117. auto& vm = this->vm();
  118. // 10.2.1 Intl.Collator.prototype, https://tc39.es/ecma402/#sec-intl.collator.prototype
  119. define_direct_property(vm.names.prototype, realm.intrinsics().intl_collator_prototype(), 0);
  120. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  121. u8 attr = Attribute::Writable | Attribute::Configurable;
  122. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  123. }
  124. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  125. ThrowCompletionOr<Value> CollatorConstructor::call()
  126. {
  127. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget
  128. return TRY(construct(*this));
  129. }
  130. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  131. ThrowCompletionOr<NonnullGCPtr<Object>> CollatorConstructor::construct(FunctionObject& new_target)
  132. {
  133. auto& vm = this->vm();
  134. auto locales = vm.argument(0);
  135. auto options = vm.argument(1);
  136. // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
  137. // 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then
  138. // a. Append [[Numeric]] as the last element of internalSlotsList.
  139. // 4. If %Collator%.[[RelevantExtensionKeys]] contains "kf", then
  140. // a. Append [[CaseFirst]] as the last element of internalSlotsList.
  141. // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Collator.prototype%", internalSlotsList).
  142. auto collator = TRY(ordinary_create_from_constructor<Collator>(vm, new_target, &Intrinsics::intl_collator_prototype));
  143. // 6. Return ? InitializeCollator(collator, locales, options).
  144. return TRY(initialize_collator(vm, collator, locales, options));
  145. }
  146. // 10.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.collator.supportedlocalesof
  147. JS_DEFINE_NATIVE_FUNCTION(CollatorConstructor::supported_locales_of)
  148. {
  149. auto locales = vm.argument(0);
  150. auto options = vm.argument(1);
  151. // 1. Let availableLocales be %Collator%.[[AvailableLocales]].
  152. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  153. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  154. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  155. return TRY(supported_locales(vm, requested_locales, options));
  156. }
  157. }