DisplayNamesConstructor.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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 <LibUnicode/Locale.h>
  14. namespace JS::Intl {
  15. // 12.2 The Intl.DisplayNames Constructor, https://tc39.es/ecma402/#sec-intl-displaynames-constructor
  16. DisplayNamesConstructor::DisplayNamesConstructor(GlobalObject& global_object)
  17. : NativeFunction(vm().names.DisplayNames.as_string(), *global_object.function_prototype())
  18. {
  19. }
  20. void DisplayNamesConstructor::initialize(GlobalObject& global_object)
  21. {
  22. NativeFunction::initialize(global_object);
  23. auto& vm = this->vm();
  24. // 12.3.1 Intl.DisplayNames.prototype, https://tc39.es/ecma402/#sec-Intl.DisplayNames.prototype
  25. define_direct_property(vm.names.prototype, global_object.intl_display_names_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  28. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  29. }
  30. // 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
  31. Value DisplayNamesConstructor::call()
  32. {
  33. // 1. If NewTarget is undefined, throw a TypeError exception.
  34. vm().throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.DisplayNames");
  35. return {};
  36. }
  37. // 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
  38. Value DisplayNamesConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. auto& global_object = this->global_object();
  42. auto locales = vm.argument(0);
  43. auto options = vm.argument(1);
  44. // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%", « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[Fields]] »).
  45. auto* display_names = ordinary_create_from_constructor<DisplayNames>(global_object, new_target, &GlobalObject::intl_display_names_prototype);
  46. if (vm.exception())
  47. return {};
  48. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  49. auto requested_locales = canonicalize_locale_list(global_object, locales);
  50. if (vm.exception())
  51. return {};
  52. // 4. If options is undefined, throw a TypeError exception.
  53. if (options.is_undefined()) {
  54. vm.throw_exception<TypeError>(global_object, ErrorType::IsUndefined, "options"sv);
  55. return {};
  56. }
  57. // 5. Set options to ? GetOptionsObject(options).
  58. options = Temporal::get_options_object(global_object, options);
  59. if (vm.exception())
  60. return {};
  61. // 6. Let opt be a new Record.
  62. LocaleOptions opt {};
  63. // 7. Let localeData be %DisplayNames%.[[LocaleData]].
  64. // 8. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  65. auto matcher = get_option(global_object, options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv);
  66. if (vm.exception())
  67. return {};
  68. // 9. Set opt.[[localeMatcher]] to matcher.
  69. opt.locale_matcher = matcher;
  70. // 10. Let r be ResolveLocale(%DisplayNames%.[[AvailableLocales]], requestedLocales, opt, %DisplayNames%.[[RelevantExtensionKeys]]).
  71. auto result = resolve_locale(requested_locales, opt, {});
  72. // 11. Let style be ? GetOption(options, "style", "string", « "narrow", "short", "long" », "long").
  73. auto style = get_option(global_object, options, vm.names.style, Value::Type::String, { "narrow"sv, "short"sv, "long"sv }, "long"sv);
  74. if (vm.exception())
  75. return {};
  76. // 12. Set displayNames.[[Style]] to style.
  77. display_names->set_style(style.as_string().string());
  78. // 13. Let type be ? GetOption(options, "type", "string", « "language", "region", "script", "currency" », undefined).
  79. auto type = get_option(global_object, options, vm.names.type, Value::Type::String, { "language"sv, "region"sv, "script"sv, "currency"sv }, Empty {});
  80. if (vm.exception())
  81. return {};
  82. // 14. If type is undefined, throw a TypeError exception.
  83. if (type.is_undefined()) {
  84. vm.throw_exception<TypeError>(global_object, ErrorType::IsUndefined, "options.type"sv);
  85. return {};
  86. }
  87. // 15. Set displayNames.[[Type]] to type.
  88. display_names->set_type(type.as_string().string());
  89. // 16. Let fallback be ? GetOption(options, "fallback", "string", « "code", "none" », "code").
  90. auto fallback = get_option(global_object, options, vm.names.fallback, Value::Type::String, { "code"sv, "none"sv }, "code"sv);
  91. if (vm.exception())
  92. return {};
  93. // 17. Set displayNames.[[Fallback]] to fallback.
  94. display_names->set_fallback(fallback.as_string().string());
  95. // 18. Set displayNames.[[Locale]] to r.[[locale]].
  96. display_names->set_locale(move(result.locale));
  97. // Note: The remaining steps are skipped in favor of deferring to LibUnicode. We could copy
  98. // the data from LibUnicode to the DisplayNames object, but for now we do not do that.
  99. // 28. Return displayNames.
  100. return display_names;
  101. }
  102. // 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.DisplayNames.supportedLocalesOf
  103. JS_DEFINE_NATIVE_FUNCTION(DisplayNamesConstructor::supported_locales_of)
  104. {
  105. auto locales = vm.argument(0);
  106. auto options = vm.argument(1);
  107. // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]].
  108. // No-op, availability of each requested locale is checked via Unicode::is_locale_available()
  109. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  110. auto requested_locales = canonicalize_locale_list(global_object, locales);
  111. if (vm.exception())
  112. return {};
  113. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  114. return supported_locales(global_object, requested_locales, options);
  115. }
  116. }