CollatorConstructor.cpp 8.9 KB

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