ListFormatConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. ThrowCompletionOr<Value> ListFormatConstructor::call()
  31. {
  32. // 1. If NewTarget is undefined, throw a TypeError exception.
  33. return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.ListFormat");
  34. }
  35. // 13.2.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& global_object = this->global_object();
  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>(global_object, new_target, &GlobalObject::intl_list_format_prototype));
  44. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  45. auto requested_locales = TRY(canonicalize_locale_list(global_object, locale_value));
  46. // 4. Set options to ? GetOptionsObject(options).
  47. auto* options = TRY(Temporal::get_options_object(global_object, 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(global_object, *options, vm.names.localeMatcher, Value::Type::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(global_object, *options, vm.names.type, Value::Type::String, { "conjunction"sv, "disjunction"sv, "unit"sv }, "conjunction"sv));
  61. // 12. Set listFormat.[[Type]] to type.
  62. list_format->set_type(type.as_string().string());
  63. // 13. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long").
  64. auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
  65. // 14. Set listFormat.[[Style]] to style.
  66. list_format->set_style(style.as_string().string());
  67. // Note: The remaining steps are skipped in favor of deferring to LibUnicode.
  68. // 19. Return listFormat.
  69. return list_format;
  70. }
  71. // 13.3.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(global_object, locales));
  79. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  80. return TRY(supported_locales(global_object, requested_locales, options));
  81. }
  82. }