Intl.cpp 7.1 KB

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