Intl.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2021, 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/Intl.h>
  14. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  15. #include <LibJS/Runtime/Intl/LocaleConstructor.h>
  16. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  17. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  18. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  19. #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
  20. #include <LibJS/Runtime/Temporal/TimeZone.h>
  21. #include <LibUnicode/CurrencyCode.h>
  22. #include <LibUnicode/DateTimeFormat.h>
  23. #include <LibUnicode/Locale.h>
  24. #include <LibUnicode/NumberFormat.h>
  25. namespace JS::Intl {
  26. // 8 The Intl Object, https://tc39.es/ecma402/#intl-object
  27. Intl::Intl(GlobalObject& global_object)
  28. : Object(*global_object.object_prototype())
  29. {
  30. }
  31. void Intl::initialize(GlobalObject& global_object)
  32. {
  33. Object::initialize(global_object);
  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(), js_string(vm, "Intl"), Attribute::Configurable);
  37. u8 attr = Attribute::Writable | Attribute::Configurable;
  38. define_direct_property(vm.names.Collator, global_object.intl_collator_constructor(), attr);
  39. define_direct_property(vm.names.DateTimeFormat, global_object.intl_date_time_format_constructor(), attr);
  40. define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
  41. define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
  42. define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);
  43. define_direct_property(vm.names.NumberFormat, global_object.intl_number_format_constructor(), attr);
  44. define_direct_property(vm.names.PluralRules, global_object.intl_plural_rules_constructor(), attr);
  45. define_direct_property(vm.names.RelativeTimeFormat, global_object.intl_relative_time_format_constructor(), attr);
  46. define_direct_property(vm.names.Segmenter, global_object.intl_segmenter_constructor(), attr);
  47. define_native_function(vm.names.getCanonicalLocales, get_canonical_locales, 1, attr);
  48. define_native_function(vm.names.supportedValuesOf, supported_values_of, 1, attr);
  49. }
  50. // 8.3.1 Intl.getCanonicalLocales ( locales ), https://tc39.es/ecma402/#sec-intl.getcanonicallocales
  51. JS_DEFINE_NATIVE_FUNCTION(Intl::get_canonical_locales)
  52. {
  53. auto locales = vm.argument(0);
  54. // 1. Let ll be ? CanonicalizeLocaleList(locales).
  55. auto locale_list = TRY(canonicalize_locale_list(global_object, locales));
  56. MarkedVector<Value> marked_locale_list { vm.heap() };
  57. marked_locale_list.ensure_capacity(locale_list.size());
  58. for (auto& locale : locale_list)
  59. marked_locale_list.append(js_string(vm, move(locale)));
  60. // 2. Return CreateArrayFromList(ll).
  61. return Array::create_from(global_object, marked_locale_list);
  62. }
  63. // 1.4.4 AvailableTimeZones (), https://tc39.es/proposal-intl-enumeration/#sec-availablecurrencies
  64. static Vector<StringView> available_time_zones()
  65. {
  66. // 1. Let names be a List of all supported Zone and Link names in the IANA Time Zone Database.
  67. auto names = TimeZone::all_time_zones();
  68. // 2. Let result be a new empty List.
  69. Vector<StringView> result;
  70. // 3. For each element name of names, do
  71. for (auto name : names) {
  72. // a. Assert: ! IsValidTimeZoneName( name ) is true.
  73. // b. Let canonical be ! CanonicalizeTimeZoneName( name ).
  74. auto canonical = TimeZone::canonicalize_time_zone(name).value();
  75. // c. If result does not contain an element equal to canonical, then
  76. if (!result.contains_slow(canonical)) {
  77. // i. Append canonical to the end of result.
  78. result.append(canonical);
  79. }
  80. }
  81. // 4. Sort result in order as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn.
  82. quick_sort(result);
  83. // 5. Return result.
  84. return result;
  85. }
  86. // 2.2.2 Intl.supportedValuesOf ( key ), https://tc39.es/proposal-intl-enumeration/#sec-intl.supportedvaluesof
  87. JS_DEFINE_NATIVE_FUNCTION(Intl::supported_values_of)
  88. {
  89. // 1. Let key be ? ToString(key).
  90. auto key = TRY(vm.argument(0).to_string(global_object));
  91. Span<StringView const> list;
  92. // 2. If key is "calendar", then
  93. if (key == "calendar"sv) {
  94. // a. Let list be ! AvailableCalendars( ).
  95. list = Unicode::get_available_calendars();
  96. }
  97. // 3. Else if key is "collation", then
  98. else if (key == "collation"sv) {
  99. // a. Let list be ! AvailableCollations( ).
  100. // NOTE: We don't yet parse any collation data, but "default" is allowed.
  101. static constexpr auto collations = AK::Array { "default"sv };
  102. list = collations.span();
  103. }
  104. // 4. Else if key is "currency", then
  105. else if (key == "currency"sv) {
  106. // a. Let list be ! AvailableCurrencies( ).
  107. list = Unicode::get_available_currencies();
  108. }
  109. // 5. Else if key is "numberingSystem", then
  110. else if (key == "numberingSystem"sv) {
  111. // a. Let list be ! AvailableNumberingSystems( ).
  112. list = Unicode::get_available_number_systems();
  113. }
  114. // 6. Else if key is "timeZone", then
  115. else if (key == "timeZone"sv) {
  116. // a. Let list be ! AvailableTimeZones( ).
  117. static auto time_zones = available_time_zones();
  118. list = time_zones.span();
  119. }
  120. // 7. Else if key is "unit", then
  121. else if (key == "unit"sv) {
  122. // a. Let list be ! AvailableUnits( ).
  123. static auto units = sanctioned_simple_unit_identifiers();
  124. list = units.span();
  125. }
  126. // 8. Else,
  127. else {
  128. // a. Throw a RangeError exception.
  129. return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidKey, key);
  130. }
  131. // 9. Return ! CreateArrayFromList( list ).
  132. return Array::create_from<StringView>(global_object, list, [&](auto value) { return js_string(vm, value); });
  133. }
  134. }