Intl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <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. ThrowCompletionOr<void> Intl::initialize(Realm& realm)
  32. {
  33. MUST_OR_THROW_OOM(Object::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"), 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. return {};
  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& realm = *vm.current_realm();
  56. auto locales = vm.argument(0);
  57. // 1. Let ll be ? CanonicalizeLocaleList(locales).
  58. auto locale_list = TRY(canonicalize_locale_list(vm, locales));
  59. MarkedVector<Value> marked_locale_list { vm.heap() };
  60. TRY_OR_THROW_OOM(vm, marked_locale_list.try_ensure_capacity(locale_list.size()));
  61. for (auto& locale : locale_list)
  62. marked_locale_list.unchecked_append(PrimitiveString::create(vm, move(locale)));
  63. // 2. Return CreateArrayFromList(ll).
  64. return Array::create_from(realm, marked_locale_list);
  65. }
  66. // 1.4.4 AvailableCanonicalTimeZones (), https://tc39.es/proposal-intl-enumeration/#sec-availablecanonicaltimezones
  67. static ThrowCompletionOr<Vector<StringView>> available_canonical_time_zones(VM& vm)
  68. {
  69. // 1. Let names be a List of all supported Zone and Link names in the IANA Time Zone Database.
  70. auto names = TimeZone::all_time_zones();
  71. // 2. Let result be a new empty List.
  72. Vector<StringView> result;
  73. // 3. For each element name of names, do
  74. for (auto name : names) {
  75. // a. Assert: IsValidTimeZoneName( name ) is true.
  76. // b. Let canonical be ! CanonicalizeTimeZoneName( name ).
  77. auto canonical = TimeZone::canonicalize_time_zone(name).value();
  78. // c. If result does not contain an element equal to canonical, then
  79. if (!result.contains_slow(canonical)) {
  80. // i. Append canonical to the end of result.
  81. TRY_OR_THROW_OOM(vm, result.try_append(canonical));
  82. }
  83. }
  84. // 4. Sort result in order as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn.
  85. quick_sort(result);
  86. // 5. Return result.
  87. return result;
  88. }
  89. // 2.2.2 Intl.supportedValuesOf ( key ), https://tc39.es/proposal-intl-enumeration/#sec-intl.supportedvaluesof
  90. JS_DEFINE_NATIVE_FUNCTION(Intl::supported_values_of)
  91. {
  92. auto& realm = *vm.current_realm();
  93. // 1. Let key be ? ToString(key).
  94. auto key = TRY(vm.argument(0).to_string(vm));
  95. Span<StringView const> list;
  96. // 2. If key is "calendar", then
  97. if (key == "calendar"sv) {
  98. // a. Let list be ! AvailableCanonicalCalendars( ).
  99. list = ::Locale::get_available_calendars();
  100. }
  101. // 3. Else if key is "collation", then
  102. else if (key == "collation"sv) {
  103. // a. Let list be ! AvailableCanonicalCollations( ).
  104. list = ::Locale::get_available_collation_types();
  105. }
  106. // 4. Else if key is "currency", then
  107. else if (key == "currency"sv) {
  108. // a. Let list be ! AvailableCanonicalCurrencies( ).
  109. list = ::Locale::get_available_currencies();
  110. }
  111. // 5. Else if key is "numberingSystem", then
  112. else if (key == "numberingSystem"sv) {
  113. // a. Let list be ! AvailableCanonicalNumberingSystems( ).
  114. list = ::Locale::get_available_number_systems();
  115. }
  116. // 6. Else if key is "timeZone", then
  117. else if (key == "timeZone"sv) {
  118. // a. Let list be ! AvailableCanonicalTimeZones( ).
  119. static auto time_zones = MUST_OR_THROW_OOM(available_canonical_time_zones(vm));
  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 ! AvailableCanonicalUnits( ).
  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>(ErrorType::IntlInvalidKey, key);
  132. }
  133. // 9. Return CreateArrayFromList( list ).
  134. return Array::create_from<StringView>(realm, list, [&](auto value) { return PrimitiveString::create(vm, value); });
  135. }
  136. }