ListFormatConstructor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/ListFormat.h>
  11. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  12. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  13. namespace JS::Intl {
  14. // 13.2 The Intl.ListFormat Constructor, https://tc39.es/ecma402/#sec-intl-listformat-constructor
  15. ListFormatConstructor::ListFormatConstructor(GlobalObject& global_object)
  16. : NativeFunction(vm().names.ListFormat.as_string(), *global_object.function_prototype())
  17. {
  18. }
  19. void ListFormatConstructor::initialize(GlobalObject& global_object)
  20. {
  21. NativeFunction::initialize(global_object);
  22. auto& vm = this->vm();
  23. // 13.3.1 Intl.ListFormat.prototype, https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype
  24. define_direct_property(vm.names.prototype, global_object.intl_list_format_prototype(), 0);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. }
  29. // 13.2.1 Intl.ListFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat
  30. Value ListFormatConstructor::call()
  31. {
  32. // 1. If NewTarget is undefined, throw a TypeError exception.
  33. vm().throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.ListFormat");
  34. return {};
  35. }
  36. // 13.2.1 Intl.ListFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat
  37. Value ListFormatConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto& global_object = this->global_object();
  41. auto locale_value = vm.argument(0);
  42. auto options_value = vm.argument(1);
  43. // 2. Let listFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%ListFormat.prototype%", « [[InitializedListFormat]], [[Locale]], [[Type]], [[Style]], [[Templates]] »).
  44. auto* list_format = TRY_OR_DISCARD(ordinary_create_from_constructor<ListFormat>(global_object, new_target, &GlobalObject::intl_list_format_prototype));
  45. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  46. auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locale_value));
  47. // 4. Set options to ? GetOptionsObject(options).
  48. auto* options = TRY_OR_DISCARD(Temporal::get_options_object(global_object, options_value));
  49. // 5. Let opt be a new Record.
  50. LocaleOptions opt {};
  51. // 6. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  52. auto matcher = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  53. // 7. Set opt.[[localeMatcher]] to matcher.
  54. opt.locale_matcher = matcher;
  55. // 8. Let localeData be %ListFormat%.[[LocaleData]].
  56. // 9. Let r be ResolveLocale(%ListFormat%.[[AvailableLocales]], requestedLocales, opt, %ListFormat%.[[RelevantExtensionKeys]], localeData).
  57. auto result = resolve_locale(requested_locales, opt, {});
  58. // 10. Set listFormat.[[Locale]] to r.[[locale]].
  59. list_format->set_locale(move(result.locale));
  60. // 11. Let type be ? GetOption(options, "type", "string", « "conjunction", "disjunction", "unit" », "conjunction").
  61. auto type = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.type, Value::Type::String, { "conjunction"sv, "disjunction"sv, "unit"sv }, "conjunction"sv));
  62. // 12. Set listFormat.[[Type]] to type.
  63. list_format->set_type(type.as_string().string());
  64. // 13. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long").
  65. auto style = TRY_OR_DISCARD(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
  66. // 14. Set listFormat.[[Style]] to style.
  67. list_format->set_style(style.as_string().string());
  68. // Note: The remaining steps are skipped in favor of deferring to LibUnicode.
  69. // 19. Return listFormat.
  70. return list_format;
  71. }
  72. // 13.3.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat.supportedLocalesOf
  73. JS_DEFINE_OLD_NATIVE_FUNCTION(ListFormatConstructor::supported_locales_of)
  74. {
  75. auto locales = vm.argument(0);
  76. auto options = vm.argument(1);
  77. // 1. Let availableLocales be %ListFormat%.[[AvailableLocales]].
  78. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  79. auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales));
  80. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  81. return TRY_OR_DISCARD(supported_locales(global_object, requested_locales, options));
  82. }
  83. }