ListFormatConstructor.cpp 4.7 KB

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