DisplayNamesConstructor.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2021-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/DisplayNames.h>
  11. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  12. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  13. #include <LibLocale/Locale.h>
  14. namespace JS::Intl {
  15. // 12.1 The Intl.DisplayNames Constructor, https://tc39.es/ecma402/#sec-intl-displaynames-constructor
  16. DisplayNamesConstructor::DisplayNamesConstructor(Realm& realm)
  17. : NativeFunction(realm.vm().names.DisplayNames.as_string(), realm.intrinsics().function_prototype())
  18. {
  19. }
  20. void DisplayNamesConstructor::initialize(Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. auto& vm = this->vm();
  24. // 12.2.1 Intl.DisplayNames.prototype, https://tc39.es/ecma402/#sec-Intl.DisplayNames.prototype
  25. define_direct_property(vm.names.prototype, realm.intrinsics().intl_display_names_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  28. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  29. }
  30. // 12.1.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
  31. ThrowCompletionOr<Value> DisplayNamesConstructor::call()
  32. {
  33. // 1. If NewTarget is undefined, throw a TypeError exception.
  34. return vm().throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Intl.DisplayNames");
  35. }
  36. // 12.1.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
  37. ThrowCompletionOr<NonnullGCPtr<Object>> DisplayNamesConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto locale_value = vm.argument(0);
  41. auto options_value = vm.argument(1);
  42. // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%", « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[LanguageDisplay]], [[Fields]] »).
  43. auto display_names = TRY(ordinary_create_from_constructor<DisplayNames>(vm, new_target, &Intrinsics::intl_display_names_prototype));
  44. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  45. auto requested_locales = TRY(canonicalize_locale_list(vm, locale_value));
  46. // 4. If options is undefined, throw a TypeError exception.
  47. if (options_value.is_undefined())
  48. return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "options"sv);
  49. // 5. Set options to ? GetOptionsObject(options).
  50. auto* options = TRY(Temporal::get_options_object(vm, options_value));
  51. // 6. Let opt be a new Record.
  52. LocaleOptions opt {};
  53. // 7. Let localeData be %DisplayNames%.[[LocaleData]].
  54. // 8. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  55. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  56. // 9. Set opt.[[localeMatcher]] to matcher.
  57. opt.locale_matcher = matcher;
  58. // 10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt, %DisplayNames%.[[RelevantExtensionKeys]]).
  59. auto result = TRY(resolve_locale(vm, requested_locales, opt, {}));
  60. // 11. Let style be ? GetOption(options, "style", string, « "narrow", "short", "long" », "long").
  61. auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv));
  62. // 12. Set displayNames.[[Style]] to style.
  63. display_names->set_style(style.as_string().utf8_string_view());
  64. // 13. Let type be ? GetOption(options, "type", string, « "language", "region", "script", "currency", "calendar", "dateTimeField" », undefined).
  65. auto type = TRY(get_option(vm, *options, vm.names.type, OptionType::String, { "language"sv, "region"sv, "script"sv, "currency"sv, "calendar"sv, "dateTimeField"sv }, Empty {}));
  66. // 14. If type is undefined, throw a TypeError exception.
  67. if (type.is_undefined())
  68. return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "options.type"sv);
  69. // 15. Set displayNames.[[Type]] to type.
  70. display_names->set_type(type.as_string().utf8_string_view());
  71. // 16. Let fallback be ? GetOption(options, "fallback", string, « "code", "none" », "code").
  72. auto fallback = TRY(get_option(vm, *options, vm.names.fallback, OptionType::String, { "code"sv, "none"sv }, "code"sv));
  73. // 17. Set displayNames.[[Fallback]] to fallback.
  74. display_names->set_fallback(fallback.as_string().utf8_string_view());
  75. // 18. Set displayNames.[[Locale]] to r.[[locale]].
  76. display_names->set_locale(move(result.locale));
  77. // Note: Several of the steps below are skipped in favor of deferring to LibUnicode.
  78. // 19. Let dataLocale be r.[[dataLocale]].
  79. // 20. Let dataLocaleData be localeData.[[<dataLocale>]].
  80. // 21. Let types be dataLocaleData.[[types]].
  81. // 22. Assert: types is a Record (see 12.4.3).
  82. // 23. Let languageDisplay be ? GetOption(options, "languageDisplay", string, « "dialect", "standard" », "dialect").
  83. auto language_display = TRY(get_option(vm, *options, vm.names.languageDisplay, OptionType::String, { "dialect"sv, "standard"sv }, "dialect"sv));
  84. // 24. Let typeFields be types.[[<type>]].
  85. // 25. Assert: typeFields is a Record (see 12.4.3).
  86. // 26. If type is "language", then
  87. if (display_names->type() == DisplayNames::Type::Language) {
  88. // a. Set displayNames.[[LanguageDisplay]] to languageDisplay.
  89. display_names->set_language_display(language_display.as_string().utf8_string_view());
  90. // b. Let typeFields be typeFields.[[<languageDisplay>]].
  91. // c. Assert: typeFields is a Record (see 12.4.3).
  92. }
  93. // 27. Let styleFields be typeFields.[[<style>]].
  94. // 28. Assert: styleFields is a Record (see 12.4.3).
  95. // 29. Set displayNames.[[Fields]] to styleFields.
  96. // 30. Return displayNames.
  97. return display_names;
  98. }
  99. // 12.2.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.DisplayNames.supportedLocalesOf
  100. JS_DEFINE_NATIVE_FUNCTION(DisplayNamesConstructor::supported_locales_of)
  101. {
  102. auto locales = vm.argument(0);
  103. auto options = vm.argument(1);
  104. // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]].
  105. // No-op, availability of each requested locale is checked via ::Locale::is_locale_available()
  106. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  107. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  108. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  109. return TRY(supported_locales(vm, requested_locales, options));
  110. }
  111. }