DateTimeFormatConstructor.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/DateTimeFormat.h>
  9. #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
  10. namespace JS::Intl {
  11. // 11.2 The Intl.DateTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-datetimeformat-constructor
  12. DateTimeFormatConstructor::DateTimeFormatConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.DateTimeFormat.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void DateTimeFormatConstructor::initialize(GlobalObject& global_object)
  17. {
  18. NativeFunction::initialize(global_object);
  19. auto& vm = this->vm();
  20. // 11.3.1 Intl.DateTimeFormat.prototype, https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype
  21. define_direct_property(vm.names.prototype, global_object.intl_date_time_format_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  23. }
  24. // 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
  25. ThrowCompletionOr<Value> DateTimeFormatConstructor::call()
  26. {
  27. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  28. return TRY(construct(*this));
  29. }
  30. // 11.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat
  31. ThrowCompletionOr<Object*> DateTimeFormatConstructor::construct(FunctionObject& new_target)
  32. {
  33. auto& vm = this->vm();
  34. auto& global_object = this->global_object();
  35. auto locales = vm.argument(0);
  36. auto options = vm.argument(1);
  37. // 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]] »).
  38. auto* date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(global_object, new_target, &GlobalObject::intl_date_time_format_prototype));
  39. // 3. Perform ? InitializeDateTimeFormat(dateTimeFormat, locales, options).
  40. TRY(initialize_date_time_format(global_object, *date_time_format, locales, options));
  41. // 4. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
  42. // a. Let this be the this value.
  43. // b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this).
  44. // 5. Return dateTimeFormat.
  45. return date_time_format;
  46. }
  47. }