ListFormatConstructor.cpp 4.7 KB

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