ZonedDateTimeConstructor.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. Value ZonedDateTimeConstructor::call()
  28. {
  29. auto& vm = this->vm();
  30. // 1. If NewTarget is undefined, then
  31. // a. Throw a TypeError exception.
  32. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.ZonedDateTime");
  33. return {};
  34. }
  35. // 6.1.1 Temporal.ZonedDateTime ( epochNanoseconds, timeZoneLike [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime
  36. Value ZonedDateTimeConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto& global_object = this->global_object();
  40. // 2. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
  41. auto* epoch_nanoseconds = vm.argument(0).to_bigint(global_object);
  42. if (vm.exception())
  43. return {};
  44. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  45. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) {
  46. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  47. return {};
  48. }
  49. // 4. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
  50. auto* time_zone = TRY_OR_DISCARD(to_temporal_time_zone(global_object, vm.argument(1)));
  51. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  52. auto* calendar = TRY_OR_DISCARD(to_temporal_calendar_with_iso_default(global_object, vm.argument(2)));
  53. // 6. Return ? CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar, NewTarget).
  54. return TRY_OR_DISCARD(create_temporal_zoned_date_time(global_object, *epoch_nanoseconds, *time_zone, *calendar, &new_target));
  55. }
  56. }