ListFormatConstructor.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2021-2024, 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, "%Intl.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 r be ResolveLocale(%Intl.ListFormat%.[[AvailableLocales]], requestedLocales, opt, %Intl.ListFormat%.[[RelevantExtensionKeys]], %Intl.ListFormat%.[[LocaleData]]).
  55. auto result = resolve_locale(requested_locales, opt, {});
  56. // 9. Set listFormat.[[Locale]] to r.[[Locale]].
  57. list_format->set_locale(move(result.locale));
  58. // 10. 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. // 11. Set listFormat.[[Type]] to type.
  61. list_format->set_type(type.as_string().utf8_string_view());
  62. // 12. 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. // 13. Set listFormat.[[Style]] to style.
  65. list_format->set_style(style.as_string().utf8_string_view());
  66. // 14. Let resolvedLocaleData be r.[[LocaleData]].
  67. // 15. Let dataLocaleTypes be resolvedLocaleData.[[<type>]].
  68. // 16. Set listFormat.[[Templates]] to dataLocaleTypes.[[<style>]].
  69. auto formatter = ::Locale::ListFormat::create(
  70. list_format->locale(),
  71. list_format->type(),
  72. list_format->style());
  73. list_format->set_formatter(move(formatter));
  74. // 17. Return listFormat.
  75. return list_format;
  76. }
  77. // 13.2.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat.supportedLocalesOf
  78. JS_DEFINE_NATIVE_FUNCTION(ListFormatConstructor::supported_locales_of)
  79. {
  80. auto locales = vm.argument(0);
  81. auto options = vm.argument(1);
  82. // 1. Let availableLocales be %ListFormat%.[[AvailableLocales]].
  83. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  84. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  85. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  86. return TRY(supported_locales(vm, requested_locales, options));
  87. }
  88. }