ZonedDateTimeConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Temporal/Calendar.h>
  10. #include <LibJS/Runtime/Temporal/Instant.h>
  11. #include <LibJS/Runtime/Temporal/TimeZone.h>
  12. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  13. #include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
  14. namespace JS::Temporal {
  15. // 6.1 The Temporal.ZonedDateTime Constructor, https://tc39.es/proposal-temporal/#sec-temporal-zoneddatetime-constructor
  16. ZonedDateTimeConstructor::ZonedDateTimeConstructor(Realm& realm)
  17. : NativeFunction(realm.vm().names.ZonedDateTime.as_string(), *realm.intrinsics().function_prototype())
  18. {
  19. }
  20. void ZonedDateTimeConstructor::initialize(Realm& realm)
  21. {
  22. NativeFunction::initialize(realm);
  23. auto& vm = this->vm();
  24. // 6.2.1 Temporal.ZonedDateTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype
  25. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_zoned_date_time_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(realm, vm.names.from, from, 1, attr);
  28. define_native_function(realm, vm.names.compare, compare, 2, attr);
  29. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  30. }
  31. // 6.1.1 Temporal.ZonedDateTime ( epochNanoseconds, timeZoneLike [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime
  32. ThrowCompletionOr<Value> ZonedDateTimeConstructor::call()
  33. {
  34. auto& vm = this->vm();
  35. // 1. If NewTarget is undefined, then
  36. // a. Throw a TypeError exception.
  37. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.ZonedDateTime");
  38. }
  39. // 6.1.1 Temporal.ZonedDateTime ( epochNanoseconds, timeZoneLike [ , calendarLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime
  40. ThrowCompletionOr<Object*> ZonedDateTimeConstructor::construct(FunctionObject& new_target)
  41. {
  42. auto& vm = this->vm();
  43. // 2. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
  44. auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(vm));
  45. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  46. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
  47. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
  48. // 4. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
  49. auto* time_zone = TRY(to_temporal_time_zone(vm, vm.argument(1)));
  50. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  51. auto* calendar = TRY(to_temporal_calendar_with_iso_default(vm, vm.argument(2)));
  52. // 6. Return ? CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar, NewTarget).
  53. return TRY(create_temporal_zoned_date_time(vm, *epoch_nanoseconds, *time_zone, *calendar, &new_target));
  54. }
  55. // 6.2.2 Temporal.ZonedDateTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.from
  56. JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimeConstructor::from)
  57. {
  58. // 1. Set options to ? GetOptionsObject(options).
  59. auto const* options = TRY(get_options_object(vm, vm.argument(1)));
  60. auto item = vm.argument(0);
  61. // 2. If Type(item) is Object and item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  62. if (item.is_object() && is<ZonedDateTime>(item.as_object())) {
  63. auto& item_object = static_cast<ZonedDateTime&>(item.as_object());
  64. // a. Perform ? ToTemporalOverflow(options).
  65. (void)TRY(to_temporal_overflow(vm, options));
  66. // b. Perform ? ToTemporalDisambiguation(options).
  67. (void)TRY(to_temporal_disambiguation(vm, options));
  68. // c. Perform ? ToTemporalOffset(options, "reject").
  69. (void)TRY(to_temporal_offset(vm, options, "reject"));
  70. // d. Return ! CreateTemporalZonedDateTime(item.[[Nanoseconds]], item.[[TimeZone]], item.[[Calendar]]).
  71. return MUST(create_temporal_zoned_date_time(vm, item_object.nanoseconds(), item_object.time_zone(), item_object.calendar()));
  72. }
  73. // 3. Return ? ToTemporalZonedDateTime(item, options).
  74. return TRY(to_temporal_zoned_date_time(vm, item, options));
  75. }
  76. // 6.2.3 Temporal.ZonedDateTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.compare
  77. JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimeConstructor::compare)
  78. {
  79. // 1. Set one to ? ToTemporalZonedDateTime(one).
  80. auto* one = TRY(to_temporal_zoned_date_time(vm, vm.argument(0)));
  81. // 2. Set two to ? ToTemporalZonedDateTime(two).
  82. auto* two = TRY(to_temporal_zoned_date_time(vm, vm.argument(1)));
  83. // 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])).
  84. return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds()));
  85. }
  86. }