DateTimeFormatConstructor.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/DateTimeFormat.h>
  11. #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
  12. namespace JS::Intl {
  13. // 11.2 The Intl.DateTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-datetimeformat-constructor
  14. DateTimeFormatConstructor::DateTimeFormatConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.DateTimeFormat.as_string(), *global_object.function_prototype())
  16. {
  17. }
  18. void DateTimeFormatConstructor::initialize(GlobalObject& global_object)
  19. {
  20. NativeFunction::initialize(global_object);
  21. auto& vm = this->vm();
  22. // 11.3.1 Intl.DateTimeFormat.prototype, https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype
  23. define_direct_property(vm.names.prototype, global_object.intl_date_time_format_prototype(), 0);
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. }
  28. // 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
  29. ThrowCompletionOr<Value> DateTimeFormatConstructor::call()
  30. {
  31. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  32. return TRY(construct(*this));
  33. }
  34. // 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
  35. ThrowCompletionOr<Object*> DateTimeFormatConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. auto locales = vm.argument(0);
  40. auto options = vm.argument(1);
  41. // 2. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%DateTimeFormat.prototype%", « [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[Weekday]], [[Era]], [[Year]], [[Month]], [[Day]], [[DayPeriod]], [[Hour]], [[Minute]], [[Second]], [[FractionalSecondDigits]], [[TimeZoneName]], [[HourCycle]], [[Pattern]], [[BoundFormat]] »).
  42. auto* date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(global_object, new_target, &GlobalObject::intl_date_time_format_prototype));
  43. // 3. Perform ? InitializeDateTimeFormat(dateTimeFormat, locales, options).
  44. TRY(initialize_date_time_format(global_object, *date_time_format, locales, options));
  45. // 4. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
  46. // a. Let this be the this value.
  47. // b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this).
  48. // 5. Return dateTimeFormat.
  49. return date_time_format;
  50. }
  51. // 11.3.2 Intl.DateTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat.supportedlocalesof
  52. JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatConstructor::supported_locales_of)
  53. {
  54. auto locales = vm.argument(0);
  55. auto options = vm.argument(1);
  56. // 1. Let availableLocales be %DateTimeFormat%.[[AvailableLocales]].
  57. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  58. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
  59. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  60. return TRY(supported_locales(global_object, requested_locales, options));
  61. }
  62. }