Intl.cpp 6.8 KB

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