ZonedDateTimeConstructor.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/Temporal/Calendar.h>
  8. #include <LibJS/Runtime/Temporal/Instant.h>
  9. #include <LibJS/Runtime/Temporal/TimeZone.h>
  10. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  11. #include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
  12. namespace JS::Temporal {
  13. // 6.1 The Temporal.ZonedDateTime Constructor, https://tc39.es/proposal-temporal/#sec-temporal-zoneddatetime-constructor
  14. ZonedDateTimeConstructor::ZonedDateTimeConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.ZonedDateTime.as_string(), *global_object.function_prototype())
  16. {
  17. }
  18. void ZonedDateTimeConstructor::initialize(GlobalObject& global_object)
  19. {
  20. NativeFunction::initialize(global_object);
  21. auto& vm = this->vm();
  22. // 6.2.1 Temporal.ZonedDateTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal-zoneddatetime-prototype
  23. define_direct_property(vm.names.prototype, global_object.temporal_zoned_date_time_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  25. }
  26. // 6.1.1 Temporal.ZonedDateTime ( epochNanoseconds, timeZoneLike [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime
  27. ThrowCompletionOr<Value> ZonedDateTimeConstructor::call()
  28. {
  29. auto& vm = this->vm();
  30. // 1. If NewTarget is undefined, then
  31. // a. Throw a TypeError exception.
  32. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.ZonedDateTime");
  33. }
  34. // 6.1.1 Temporal.ZonedDateTime ( epochNanoseconds, timeZoneLike [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime
  35. ThrowCompletionOr<Object*> ZonedDateTimeConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. // 2. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
  40. auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(global_object));
  41. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  42. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
  43. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  44. // 4. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
  45. auto* time_zone = TRY(to_temporal_time_zone(global_object, vm.argument(1)));
  46. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  47. auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, vm.argument(2)));
  48. // 6. Return ? CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar, NewTarget).
  49. return TRY(create_temporal_zoned_date_time(global_object, *epoch_nanoseconds, *time_zone, *calendar, &new_target));
  50. }
  51. }