ListFormatConstructor.cpp 4.1 KB

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