CollatorConstructor.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // a. Let numeric be ! ToString(numeric).
  50. // 15. Set opt.[[kn]] to numeric.
  51. if (!numeric.is_undefined())
  52. opt.kn = MUST(numeric.to_string(vm));
  53. // 16. Let caseFirst be ? GetOption(options, "caseFirst", string, « "upper", "lower", "false" », undefined).
  54. // 17. Set opt.[[kf]] to caseFirst.
  55. auto case_first = TRY(get_option(vm, *options, vm.names.caseFirst, OptionType::String, { "upper"sv, "lower"sv, "false"sv }, Empty {}));
  56. if (!case_first.is_undefined())
  57. opt.kf = case_first.as_string().utf8_string();
  58. // 18. Let relevantExtensionKeys be %Collator%.[[RelevantExtensionKeys]].
  59. auto relevant_extension_keys = Collator::relevant_extension_keys();
  60. // 19. Let r be ResolveLocale(%Collator%.[[AvailableLocales]], requestedLocales, opt, relevantExtensionKeys, localeData).
  61. auto result = resolve_locale(requested_locales, opt, relevant_extension_keys);
  62. // 20. Set collator.[[Locale]] to r.[[locale]].
  63. collator.set_locale(move(result.locale));
  64. // 21. Let collation be r.[[co]].
  65. // 22. If collation is null, let collation be "default".
  66. // 23. Set collator.[[Collation]] to collation.
  67. collator.set_collation(result.co.has_value() ? result.co.release_value() : "default"_string);
  68. // 24. If relevantExtensionKeys contains "kn", then
  69. if (relevant_extension_keys.span().contains_slow("kn"sv) && result.kn.has_value()) {
  70. // a. Set collator.[[Numeric]] to SameValue(r.[[kn]], "true").
  71. collator.set_numeric(same_value(PrimitiveString::create(vm, result.kn.release_value()), PrimitiveString::create(vm, "true"_string)));
  72. }
  73. // 25. If relevantExtensionKeys contains "kf", then
  74. if (relevant_extension_keys.span().contains_slow("kf"sv) && result.kf.has_value()) {
  75. // a. Set collator.[[CaseFirst]] to r.[[kf]].
  76. collator.set_case_first(result.kf.release_value());
  77. }
  78. // 26. Let sensitivity be ? GetOption(options, "sensitivity", string, « "base", "accent", "case", "variant" », undefined).
  79. auto sensitivity = TRY(get_option(vm, *options, vm.names.sensitivity, OptionType::String, { "base"sv, "accent"sv, "case"sv, "variant"sv }, Empty {}));
  80. // 27. If sensitivity is undefined, then
  81. if (sensitivity.is_undefined()) {
  82. // a. If usage is "sort", then
  83. if (collator.usage() == Collator::Usage::Sort) {
  84. // i. Let sensitivity be "variant".
  85. sensitivity = PrimitiveString::create(vm, "variant"_string);
  86. }
  87. // b. Else,
  88. else {
  89. // i. Let dataLocale be r.[[dataLocale]].
  90. // ii. Let dataLocaleData be localeData.[[<dataLocale>]].
  91. // iii. Let sensitivity be dataLocaleData.[[sensitivity]].
  92. sensitivity = PrimitiveString::create(vm, "base"_string);
  93. }
  94. }
  95. // 28. Set collator.[[Sensitivity]] to sensitivity.
  96. collator.set_sensitivity(sensitivity.as_string().utf8_string_view());
  97. // 29. Let ignorePunctuation be ? GetOption(options, "ignorePunctuation", boolean, empty, false).
  98. auto ignore_punctuation = TRY(get_option(vm, *options, vm.names.ignorePunctuation, OptionType::Boolean, {}, false));
  99. // 30. Set collator.[[IgnorePunctuation]] to ignorePunctuation.
  100. collator.set_ignore_punctuation(ignore_punctuation.as_bool());
  101. // 31. Return collator.
  102. return collator;
  103. }
  104. // 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor
  105. CollatorConstructor::CollatorConstructor(Realm& realm)
  106. : NativeFunction(realm.vm().names.Collator.as_string(), realm.intrinsics().function_prototype())
  107. {
  108. }
  109. void CollatorConstructor::initialize(Realm& realm)
  110. {
  111. Base::initialize(realm);
  112. auto& vm = this->vm();
  113. // 10.2.1 Intl.Collator.prototype, https://tc39.es/ecma402/#sec-intl.collator.prototype
  114. define_direct_property(vm.names.prototype, realm.intrinsics().intl_collator_prototype(), 0);
  115. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  116. u8 attr = Attribute::Writable | Attribute::Configurable;
  117. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  118. }
  119. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  120. ThrowCompletionOr<Value> CollatorConstructor::call()
  121. {
  122. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget
  123. return TRY(construct(*this));
  124. }
  125. // 10.1.1 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator
  126. ThrowCompletionOr<NonnullGCPtr<Object>> CollatorConstructor::construct(FunctionObject& new_target)
  127. {
  128. auto& vm = this->vm();
  129. auto locales = vm.argument(0);
  130. auto options = vm.argument(1);
  131. // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]], [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
  132. // 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then
  133. // a. Append [[Numeric]] as the last element of internalSlotsList.
  134. // 4. If %Collator%.[[RelevantExtensionKeys]] contains "kf", then
  135. // a. Append [[CaseFirst]] as the last element of internalSlotsList.
  136. // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%Collator.prototype%", internalSlotsList).
  137. auto collator = TRY(ordinary_create_from_constructor<Collator>(vm, new_target, &Intrinsics::intl_collator_prototype));
  138. // 6. Return ? InitializeCollator(collator, locales, options).
  139. return TRY(initialize_collator(vm, collator, locales, options));
  140. }
  141. // 10.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.collator.supportedlocalesof
  142. JS_DEFINE_NATIVE_FUNCTION(CollatorConstructor::supported_locales_of)
  143. {
  144. auto locales = vm.argument(0);
  145. auto options = vm.argument(1);
  146. // 1. Let availableLocales be %Collator%.[[AvailableLocales]].
  147. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  148. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  149. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  150. return TRY(supported_locales(vm, requested_locales, options));
  151. }
  152. }