RelativeTimeFormatConstructor.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2022-2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.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/NumberFormat.h>
  11. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  12. #include <LibJS/Runtime/Intl/PluralRules.h>
  13. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  14. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  15. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  16. #include <LibLocale/Locale.h>
  17. namespace JS::Intl {
  18. JS_DEFINE_ALLOCATOR(RelativeTimeFormatConstructor);
  19. // 17.1 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor
  20. RelativeTimeFormatConstructor::RelativeTimeFormatConstructor(Realm& realm)
  21. : NativeFunction(realm.vm().names.RelativeTimeFormat.as_string(), realm.intrinsics().function_prototype())
  22. {
  23. }
  24. void RelativeTimeFormatConstructor::initialize(Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. auto& vm = this->vm();
  28. // 17.2.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype
  29. define_direct_property(vm.names.prototype, realm.intrinsics().intl_relative_time_format_prototype(), 0);
  30. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  31. u8 attr = Attribute::Writable | Attribute::Configurable;
  32. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  33. }
  34. // 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  35. ThrowCompletionOr<Value> RelativeTimeFormatConstructor::call()
  36. {
  37. // 1. If NewTarget is undefined, throw a TypeError exception.
  38. return vm().throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Intl.RelativeTimeFormat");
  39. }
  40. // 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  41. ThrowCompletionOr<NonnullGCPtr<Object>> RelativeTimeFormatConstructor::construct(FunctionObject& new_target)
  42. {
  43. auto& vm = this->vm();
  44. auto locales = vm.argument(0);
  45. auto options = vm.argument(1);
  46. // 2. Let relativeTimeFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%RelativeTimeFormat.prototype%", « [[InitializedRelativeTimeFormat]], [[Locale]], [[DataLocale]], [[Style]], [[Numeric]], [[NumberFormat]], [[NumberingSystem]], [[PluralRules]] »).
  47. auto relative_time_format = TRY(ordinary_create_from_constructor<RelativeTimeFormat>(vm, new_target, &Intrinsics::intl_relative_time_format_prototype));
  48. // 3. Return ? InitializeRelativeTimeFormat(relativeTimeFormat, locales, options).
  49. return TRY(initialize_relative_time_format(vm, relative_time_format, locales, options));
  50. }
  51. // 17.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf
  52. JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of)
  53. {
  54. auto locales = vm.argument(0);
  55. auto options = vm.argument(1);
  56. // 1. Let availableLocales be %RelativeTimeFormat%.[[AvailableLocales]].
  57. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  58. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  59. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  60. return TRY(supported_locales(vm, requested_locales, options));
  61. }
  62. // 17.1.2 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat
  63. ThrowCompletionOr<NonnullGCPtr<RelativeTimeFormat>> initialize_relative_time_format(VM& vm, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value)
  64. {
  65. auto& realm = *vm.current_realm();
  66. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  67. auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
  68. // 2. Set options to ? CoerceOptionsToObject(options).
  69. auto* options = TRY(coerce_options_to_object(vm, options_value));
  70. // 3. Let opt be a new Record.
  71. LocaleOptions opt {};
  72. // 4. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  73. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
  74. // 5. Set opt.[[LocaleMatcher]] to matcher.
  75. opt.locale_matcher = matcher;
  76. // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", string, empty, undefined).
  77. auto numbering_system = TRY(get_option(vm, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {}));
  78. // 7. If numberingSystem is not undefined, then
  79. if (!numbering_system.is_undefined()) {
  80. // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  81. if (!::Locale::is_type_identifier(numbering_system.as_string().utf8_string_view()))
  82. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
  83. // 8. Set opt.[[nu]] to numberingSystem.
  84. opt.nu = numbering_system.as_string().utf8_string();
  85. }
  86. // 9. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
  87. // 10. Let r be ResolveLocale(%RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %RelativeTimeFormat%.[[RelevantExtensionKeys]], localeData).
  88. auto result = resolve_locale(requested_locales, opt, RelativeTimeFormat::relevant_extension_keys());
  89. // 11. Let locale be r.[[locale]].
  90. auto locale = move(result.locale);
  91. // 12. Set relativeTimeFormat.[[Locale]] to locale.
  92. relative_time_format.set_locale(locale);
  93. // 13. Set relativeTimeFormat.[[DataLocale]] to r.[[dataLocale]].
  94. relative_time_format.set_data_locale(move(result.data_locale));
  95. // 14. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]].
  96. if (result.nu.has_value())
  97. relative_time_format.set_numbering_system(result.nu.release_value());
  98. // 15. Let style be ? GetOption(options, "style", string, « "long", "short", "narrow" », "long").
  99. auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
  100. // 16. Set relativeTimeFormat.[[Style]] to style.
  101. relative_time_format.set_style(style.as_string().utf8_string_view());
  102. // 17. Let numeric be ? GetOption(options, "numeric", string, « "always", "auto" », "always").
  103. auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::String, { "always"sv, "auto"sv }, "always"sv));
  104. // 18. Set relativeTimeFormat.[[Numeric]] to numeric.
  105. relative_time_format.set_numeric(numeric.as_string().utf8_string_view());
  106. // 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
  107. auto number_format = MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, locale)));
  108. relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format.ptr()));
  109. // 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »).
  110. auto plural_rules = MUST(construct(vm, realm.intrinsics().intl_plural_rules_constructor(), PrimitiveString::create(vm, locale)));
  111. relative_time_format.set_plural_rules(static_cast<PluralRules*>(plural_rules.ptr()));
  112. // 21. Return relativeTimeFormat.
  113. return relative_time_format;
  114. }
  115. }