Intl.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2021-2022, 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 <LibUnicode/CurrencyCode.h>
  23. #include <LibUnicode/DateTimeFormat.h>
  24. #include <LibUnicode/Locale.h>
  25. #include <LibUnicode/NumberFormat.h>
  26. namespace JS::Intl {
  27. // 8 The Intl Object, https://tc39.es/ecma402/#intl-object
  28. Intl::Intl(GlobalObject& global_object)
  29. : Object(*global_object.object_prototype())
  30. {
  31. }
  32. void Intl::initialize(GlobalObject& global_object)
  33. {
  34. Object::initialize(global_object);
  35. auto& vm = this->vm();
  36. // 8.1.1 Intl[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl-toStringTag
  37. define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl"), Attribute::Configurable);
  38. u8 attr = Attribute::Writable | Attribute::Configurable;
  39. define_direct_property(vm.names.Collator, global_object.intl_collator_constructor(), attr);
  40. define_direct_property(vm.names.DateTimeFormat, global_object.intl_date_time_format_constructor(), attr);
  41. define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
  42. define_direct_property(vm.names.DurationFormat, global_object.intl_duration_format_constructor(), attr);
  43. define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
  44. define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);
  45. define_direct_property(vm.names.NumberFormat, global_object.intl_number_format_constructor(), attr);
  46. define_direct_property(vm.names.PluralRules, global_object.intl_plural_rules_constructor(), attr);
  47. define_direct_property(vm.names.RelativeTimeFormat, global_object.intl_relative_time_format_constructor(), attr);
  48. define_direct_property(vm.names.Segmenter, global_object.intl_segmenter_constructor(), attr);
  49. define_native_function(vm.names.getCanonicalLocales, get_canonical_locales, 1, attr);
  50. define_native_function(vm.names.supportedValuesOf, supported_values_of, 1, attr);
  51. }
  52. // 8.3.1 Intl.getCanonicalLocales ( locales ), https://tc39.es/ecma402/#sec-intl.getcanonicallocales
  53. JS_DEFINE_NATIVE_FUNCTION(Intl::get_canonical_locales)
  54. {
  55. auto locales = vm.argument(0);
  56. // 1. Let ll be ? CanonicalizeLocaleList(locales).
  57. auto locale_list = TRY(canonicalize_locale_list(global_object, 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.append(js_string(vm, move(locale)));
  62. // 2. Return CreateArrayFromList(ll).
  63. return Array::create_from(global_object, marked_locale_list);
  64. }
  65. // 1.4.4 AvailableTimeZones (), https://tc39.es/proposal-intl-enumeration/#sec-availablecurrencies
  66. static Vector<StringView> available_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 name : names) {
  74. // a. Assert: IsValidTimeZoneName( name ) is true.
  75. // b. Let canonical be ! CanonicalizeTimeZoneName( name ).
  76. auto canonical = TimeZone::canonicalize_time_zone(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. // 2.2.2 Intl.supportedValuesOf ( key ), https://tc39.es/proposal-intl-enumeration/#sec-intl.supportedvaluesof
  89. JS_DEFINE_NATIVE_FUNCTION(Intl::supported_values_of)
  90. {
  91. // 1. Let key be ? ToString(key).
  92. auto key = TRY(vm.argument(0).to_string(global_object));
  93. Span<StringView const> list;
  94. // 2. If key is "calendar", then
  95. if (key == "calendar"sv) {
  96. // a. Let list be ! AvailableCalendars( ).
  97. list = Unicode::get_available_calendars();
  98. }
  99. // 3. Else if key is "collation", then
  100. else if (key == "collation"sv) {
  101. // a. Let list be ! AvailableCollations( ).
  102. // FIXME: We don't yet parse any collation data, but "default" is allowed. This matches Intl.Locale.prototype.collations.
  103. static constexpr auto collations = AK::Array { "default"sv };
  104. list = collations.span();
  105. }
  106. // 4. Else if key is "currency", then
  107. else if (key == "currency"sv) {
  108. // a. Let list be ! AvailableCurrencies( ).
  109. list = Unicode::get_available_currencies();
  110. }
  111. // 5. Else if key is "numberingSystem", then
  112. else if (key == "numberingSystem"sv) {
  113. // a. Let list be ! AvailableNumberingSystems( ).
  114. list = Unicode::get_available_number_systems();
  115. }
  116. // 6. Else if key is "timeZone", then
  117. else if (key == "timeZone"sv) {
  118. // a. Let list be ! AvailableTimeZones( ).
  119. static auto time_zones = available_time_zones();
  120. list = time_zones.span();
  121. }
  122. // 7. Else if key is "unit", then
  123. else if (key == "unit"sv) {
  124. // a. Let list be ! AvailableUnits( ).
  125. static auto units = sanctioned_single_unit_identifiers();
  126. list = units.span();
  127. }
  128. // 8. Else,
  129. else {
  130. // a. Throw a RangeError exception.
  131. return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidKey, key);
  132. }
  133. // 9. Return CreateArrayFromList( list ).
  134. return Array::create_from<StringView>(global_object, list, [&](auto value) { return js_string(vm, value); });
  135. }
  136. }