Intl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/QuickSort.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/Date.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  12. #include <LibJS/Runtime/Intl/CollatorConstructor.h>
  13. #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
  14. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  15. #include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
  16. #include <LibJS/Runtime/Intl/Intl.h>
  17. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  18. #include <LibJS/Runtime/Intl/LocaleConstructor.h>
  19. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  20. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  21. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  22. #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
  23. #include <LibJS/Runtime/Intl/SingleUnitIdentifiers.h>
  24. #include <LibJS/Runtime/Temporal/TimeZone.h>
  25. #include <LibUnicode/DateTimeFormat.h>
  26. #include <LibUnicode/Locale.h>
  27. #include <LibUnicode/NumberFormat.h>
  28. #include <LibUnicode/UnicodeKeywords.h>
  29. namespace JS::Intl {
  30. GC_DEFINE_ALLOCATOR(Intl);
  31. // 8 The Intl Object, https://tc39.es/ecma402/#intl-object
  32. Intl::Intl(Realm& realm)
  33. : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
  34. {
  35. }
  36. void Intl::initialize(Realm& realm)
  37. {
  38. Base::initialize(realm);
  39. auto& vm = this->vm();
  40. // 8.1.1 Intl[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl-toStringTag
  41. define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Intl"_string), Attribute::Configurable);
  42. u8 attr = Attribute::Writable | Attribute::Configurable;
  43. define_intrinsic_accessor(vm.names.Collator, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_collator_constructor(); });
  44. define_intrinsic_accessor(vm.names.DateTimeFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_date_time_format_constructor(); });
  45. define_intrinsic_accessor(vm.names.DisplayNames, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_display_names_constructor(); });
  46. define_intrinsic_accessor(vm.names.DurationFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_duration_format_constructor(); });
  47. define_intrinsic_accessor(vm.names.ListFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_list_format_constructor(); });
  48. define_intrinsic_accessor(vm.names.Locale, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_locale_constructor(); });
  49. define_intrinsic_accessor(vm.names.NumberFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_number_format_constructor(); });
  50. define_intrinsic_accessor(vm.names.PluralRules, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_plural_rules_constructor(); });
  51. define_intrinsic_accessor(vm.names.RelativeTimeFormat, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_relative_time_format_constructor(); });
  52. define_intrinsic_accessor(vm.names.Segmenter, attr, [](auto& realm) -> Value { return realm.intrinsics().intl_segmenter_constructor(); });
  53. define_native_function(realm, vm.names.getCanonicalLocales, get_canonical_locales, 1, attr);
  54. define_native_function(realm, vm.names.supportedValuesOf, supported_values_of, 1, attr);
  55. }
  56. // 8.3.1 Intl.getCanonicalLocales ( locales ), https://tc39.es/ecma402/#sec-intl.getcanonicallocales
  57. JS_DEFINE_NATIVE_FUNCTION(Intl::get_canonical_locales)
  58. {
  59. auto& realm = *vm.current_realm();
  60. auto locales = vm.argument(0);
  61. // 1. Let ll be ? CanonicalizeLocaleList(locales).
  62. auto locale_list = TRY(canonicalize_locale_list(vm, locales));
  63. GC::MarkedVector<Value> marked_locale_list { vm.heap() };
  64. marked_locale_list.ensure_capacity(locale_list.size());
  65. for (auto& locale : locale_list)
  66. marked_locale_list.unchecked_append(PrimitiveString::create(vm, move(locale)));
  67. // 2. Return CreateArrayFromList(ll).
  68. return Array::create_from(realm, marked_locale_list);
  69. }
  70. // 6.5.4 AvailablePrimaryTimeZoneIdentifiers ( ), https://tc39.es/ecma402/#sec-availableprimarytimezoneidentifiers
  71. static Vector<String> available_primary_time_zone_identifiers()
  72. {
  73. // 1. Let records be AvailableNamedTimeZoneIdentifiers().
  74. auto const& records = available_named_time_zone_identifiers();
  75. // 2. Let result be a new empty List.
  76. Vector<String> result;
  77. // 3. For each element timeZoneIdentifierRecord of records, do
  78. for (auto const& time_zone_identifier_record : records) {
  79. // a. If timeZoneIdentifierRecord.[[Identifier]] is timeZoneIdentifierRecord.[[PrimaryIdentifier]], then
  80. if (time_zone_identifier_record.identifier == time_zone_identifier_record.primary_identifier) {
  81. // i. Append timeZoneIdentifierRecord.[[Identifier]] to result.
  82. result.append(time_zone_identifier_record.identifier);
  83. }
  84. }
  85. // 4. 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. Optional<Variant<ReadonlySpan<StringView>, ReadonlySpan<String>>> list;
  95. // 2. If key is "calendar", then
  96. if (key == "calendar"sv) {
  97. // a. Let list be ! AvailableCanonicalCalendars( ).
  98. list = Unicode::available_calendars().span();
  99. }
  100. // 3. Else if key is "collation", then
  101. else if (key == "collation"sv) {
  102. // a. Let list be ! AvailableCanonicalCollations( ).
  103. list = Unicode::available_collations().span();
  104. }
  105. // 4. Else if key is "currency", then
  106. else if (key == "currency"sv) {
  107. // a. Let list be ! AvailableCanonicalCurrencies( ).
  108. list = Unicode::available_currencies().span();
  109. }
  110. // 5. Else if key is "numberingSystem", then
  111. else if (key == "numberingSystem"sv) {
  112. // a. Let list be ! AvailableCanonicalNumberingSystems( ).
  113. list = Unicode::available_number_systems().span();
  114. }
  115. // 6. Else if key is "timeZone", then
  116. else if (key == "timeZone"sv) {
  117. // a. Let list be ! AvailablePrimaryTimeZoneIdentifiers( ).
  118. static auto const time_zones = available_primary_time_zone_identifiers();
  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 const 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 list->visit([&]<typename T>(ReadonlySpan<T> list) {
  134. return Array::create_from<T>(realm, list, [&](auto value) {
  135. return PrimitiveString::create(vm, value);
  136. });
  137. });
  138. }
  139. }