DateTimeFormatConstructor.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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& global_object = this->global_object();
  34. // 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]] »).
  35. auto* date_time_format = TRY(ordinary_create_from_constructor<DateTimeFormat>(global_object, new_target, &GlobalObject::intl_date_time_format_prototype));
  36. // 5. Return dateTimeFormat.
  37. return date_time_format;
  38. }
  39. }