Intl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.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/CollatorConstructor.h>
  11. #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
  12. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  13. #include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
  14. #include <LibJS/Runtime/Intl/Intl.h>
  15. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  16. #include <LibJS/Runtime/Intl/LocaleConstructor.h>
  17. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  18. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  19. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  20. #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
  21. #include <LibJS/Runtime/Temporal/TimeZone.h>
  22. #include <LibLocale/DateTimeFormat.h>
  23. #include <LibLocale/Locale.h>
  24. #include <LibLocale/NumberFormat.h>
  25. #include <LibLocale/UnicodeKeywords.h>
  26. namespace JS::Intl {
  27. JS_DEFINE_ALLOCATOR(Intl);
  28. // 8 The Intl Object, https://tc39.es/ecma402/#intl-object
  29. Intl::Intl(Realm& realm)
  30. : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
  31. {
  32. }
  33. void Intl::initialize(Realm& realm)
  34. {
  35. Base::initialize(realm);
  36. auto& vm = this->vm();
  37. // 8.1.1 Intl[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl-toStringTag
  38. define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Intl"_string), Attribute::Configurable);
  39. u8 attr = Attribute::Writable | Attribute::Configurable;
  40. define_intrinsic_accessor(vm.names.Collator, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_collator_constructor(); });
  41. define_intrinsic_accessor(vm.names.DateTimeFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_date_time_format_constructor(); });
  42. define_intrinsic_accessor(vm.names.DisplayNames, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_display_names_constructor(); });
  43. define_intrinsic_accessor(vm.names.DurationFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_duration_format_constructor(); });
  44. define_intrinsic_accessor(vm.names.ListFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_list_format_constructor(); });
  45. define_intrinsic_accessor(vm.names.Locale, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_locale_constructor(); });
  46. define_intrinsic_accessor(vm.names.NumberFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_number_format_constructor(); });
  47. define_intrinsic_accessor(vm.names.PluralRules, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_plural_rules_constructor(); });
  48. define_intrinsic_accessor(vm.names.RelativeTimeFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_relative_time_format_constructor(); });
  49. define_intrinsic_accessor(vm.names.Segmenter, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_segmenter_constructor(); });
  50. define_native_function(realm, vm.names.getCanonicalLocales, get_canonical_locales, 1, attr);
  51. define_native_function(realm, vm.names.supportedValuesOf, supported_values_of, 1, attr);
  52. }
  53. // 8.3.1 Intl.getCanonicalLocales ( locales ), https://tc39.es/ecma402/#sec-intl.getcanonicallocales
  54. JS_DEFINE_NATIVE_FUNCTION(Intl::get_canonical_locales)
  55. {
  56. auto& realm = *vm.current_realm();
  57. auto locales = vm.argument(0);
  58. // 1. Let ll be ? CanonicalizeLocaleList(locales).
  59. auto locale_list = TRY(canonicalize_locale_list(vm, locales));
  60. MarkedVector<Value> marked_locale_list { vm.heap() };
  61. marked_locale_list.ensure_capacity(locale_list.size());
  62. for (auto& locale : locale_list)
  63. marked_locale_list.unchecked_append(PrimitiveString::create(vm, move(locale)));
  64. // 2. Return CreateArrayFromList(ll).
  65. return Array::create_from(realm, marked_locale_list);
  66. }
  67. // 6.5.4 AvailableCanonicalTimeZones ( ), https://tc39.es/ecma402/#sec-availablecanonicaltimezones
  68. static Vector<StringView> available_canonical_time_zones()
  69. {
  70. // 1. Let names be a List of all supported Zone and Link names in the IANA Time Zone Database.
  71. auto names = TimeZone::all_time_zones();
  72. // 2. Let result be a new empty List.
  73. Vector<StringView> result;
  74. // 3. For each element name of names, do
  75. for (auto const& name : names) {
  76. // a. Assert: IsValidTimeZoneName( name ) is true.
  77. // b. Let canonical be ! CanonicalizeTimeZoneName( name ).
  78. auto canonical = TimeZone::canonicalize_time_zone(name.name).value();
  79. // c. If result does not contain an element equal to canonical, then
  80. if (!result.contains_slow(canonical)) {
  81. // i. Append canonical to the end of result.
  82. result.append(canonical);
  83. }
  84. }
  85. // 4. Sort result in order as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn.
  86. quick_sort(result);
  87. // 5. Return result.
  88. return result;
  89. }
  90. // 8.3.2 Intl.supportedValuesOf ( key ), https://tc39.es/ecma402/#sec-intl.supportedvaluesof
  91. JS_DEFINE_NATIVE_FUNCTION(Intl::supported_values_of)
  92. {
  93. auto& realm = *vm.current_realm();
  94. // 1. Let key be ? ToString(key).
  95. auto key = TRY(vm.argument(0).to_string(vm));
  96. Optional<Variant<ReadonlySpan<StringView>, ReadonlySpan<String>>> list;
  97. // 2. If key is "calendar", then
  98. if (key == "calendar"sv) {
  99. // a. Let list be ! AvailableCanonicalCalendars( ).
  100. list = ::Locale::available_calendars().span();
  101. }
  102. // 3. Else if key is "collation", then
  103. else if (key == "collation"sv) {
  104. // a. Let list be ! AvailableCanonicalCollations( ).
  105. list = ::Locale::available_collations().span();
  106. }
  107. // 4. Else if key is "currency", then
  108. else if (key == "currency"sv) {
  109. // a. Let list be ! AvailableCanonicalCurrencies( ).
  110. list = ::Locale::available_currencies().span();
  111. }
  112. // 5. Else if key is "numberingSystem", then
  113. else if (key == "numberingSystem"sv) {
  114. // a. Let list be ! AvailableCanonicalNumberingSystems( ).
  115. list = ::Locale::available_number_systems().span();
  116. }
  117. // 6. Else if key is "timeZone", then
  118. else if (key == "timeZone"sv) {
  119. // a. Let list be ! AvailableCanonicalTimeZones( ).
  120. static auto const time_zones = available_canonical_time_zones();
  121. list = time_zones.span();
  122. }
  123. // 7. Else if key is "unit", then
  124. else if (key == "unit"sv) {
  125. // a. Let list be ! AvailableCanonicalUnits( ).
  126. static auto const units = sanctioned_single_unit_identifiers();
  127. list = units.span();
  128. }
  129. // 8. Else,
  130. else {
  131. // a. Throw a RangeError exception.
  132. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidKey, key);
  133. }
  134. // 9. Return CreateArrayFromList( list ).
  135. return list->visit([&]<typename T>(ReadonlySpan<T> list) {
  136. return Array::create_from<T>(realm, list, [&](auto value) {
  137. return PrimitiveString::create(vm, value);
  138. });
  139. });
  140. }
  141. }