ZonedDateTimeConstructor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(GlobalObject& global_object)
  17. : NativeFunction(vm().names.ZonedDateTime.as_string(), *global_object.function_prototype())
  18. {
  19. }
  20. void ZonedDateTimeConstructor::initialize(GlobalObject& global_object)
  21. {
  22. NativeFunction::initialize(global_object);
  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, global_object.temporal_zoned_date_time_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(vm.names.from, from, 1, attr);
  28. define_native_function(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>(global_object(), 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. auto& global_object = this->global_object();
  44. // 2. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
  45. auto* epoch_nanoseconds = TRY(vm.argument(0).to_bigint(global_object));
  46. // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  47. if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds))
  48. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  49. // 4. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
  50. auto* time_zone = TRY(to_temporal_time_zone(global_object, vm.argument(1)));
  51. // 5. Let calendar be ? ToTemporalCalendarWithISODefault(calendarLike).
  52. auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, vm.argument(2)));
  53. // 6. Return ? CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar, NewTarget).
  54. return TRY(create_temporal_zoned_date_time(global_object, *epoch_nanoseconds, *time_zone, *calendar, &new_target));
  55. }
  56. // 6.2.2 Temporal.ZonedDateTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.from
  57. JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimeConstructor::from)
  58. {
  59. // 1. Set options to ? GetOptionsObject(options).
  60. auto const* options = TRY(get_options_object(global_object, vm.argument(1)));
  61. auto item = vm.argument(0);
  62. // 2. If Type(item) is Object and item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  63. if (item.is_object() && is<ZonedDateTime>(item.as_object())) {
  64. auto& item_object = static_cast<ZonedDateTime&>(item.as_object());
  65. // a. Perform ? ToTemporalOverflow(options).
  66. (void)TRY(to_temporal_overflow(global_object, options));
  67. // b. Perform ? ToTemporalDisambiguation(options).
  68. (void)TRY(to_temporal_disambiguation(global_object, options));
  69. // c. Perform ? ToTemporalOffset(options, "reject").
  70. (void)TRY(to_temporal_offset(global_object, options, "reject"));
  71. // d. Return ! CreateTemporalZonedDateTime(item.[[Nanoseconds]], item.[[TimeZone]], item.[[Calendar]]).
  72. return MUST(create_temporal_zoned_date_time(global_object, item_object.nanoseconds(), item_object.time_zone(), item_object.calendar()));
  73. }
  74. // 3. Return ? ToTemporalZonedDateTime(item, options).
  75. return TRY(to_temporal_zoned_date_time(global_object, item, options));
  76. }
  77. // 6.2.3 Temporal.ZonedDateTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.compare
  78. JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimeConstructor::compare)
  79. {
  80. // 1. Set one to ? ToTemporalZonedDateTime(one).
  81. auto* one = TRY(to_temporal_zoned_date_time(global_object, vm.argument(0)));
  82. // 2. Set two to ? ToTemporalZonedDateTime(two).
  83. auto* two = TRY(to_temporal_zoned_date_time(global_object, vm.argument(1)));
  84. // 3. Return 𝔽(! CompareEpochNanoseconds(one.[[Nanoseconds]], two.[[Nanoseconds]])).
  85. return Value(compare_epoch_nanoseconds(one->nanoseconds(), two->nanoseconds()));
  86. }
  87. }