DurationConstructor.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  9. #include <LibJS/Runtime/Temporal/Duration.h>
  10. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  11. namespace JS::Temporal {
  12. GC_DEFINE_ALLOCATOR(DurationConstructor);
  13. // 7.1 The Temporal.Duration Constructor, https://tc39.es/proposal-temporal/#sec-temporal-duration-constructor
  14. DurationConstructor::DurationConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.Duration.as_string(), realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void DurationConstructor::initialize(Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. auto& vm = this->vm();
  22. // 7.2.1 Temporal.Duration.prototype, https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().temporal_duration_prototype(), 0);
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(realm, vm.names.from, from, 1, attr);
  26. define_native_function(realm, vm.names.compare, compare, 2, attr);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. }
  29. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  30. ThrowCompletionOr<Value> DurationConstructor::call()
  31. {
  32. auto& vm = this->vm();
  33. // 1. If NewTarget is undefined, then
  34. // a. Throw a TypeError exception.
  35. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.Duration");
  36. }
  37. // 7.1.1 Temporal.Duration ( [ years [ , months [ , weeks [ , days [ , hours [ , minutes [ , seconds [ , milliseconds [ , microseconds [ , nanoseconds ] ] ] ] ] ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration
  38. ThrowCompletionOr<GC::Ref<Object>> DurationConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. auto next_integer_argument = [&, index = 0]() mutable -> ThrowCompletionOr<double> {
  42. if (auto value = vm.argument(index++); !value.is_undefined())
  43. return to_integer_if_integral(vm, value, ErrorType::TemporalInvalidDuration);
  44. return 0;
  45. };
  46. // 2. If years is undefined, let y be 0; else let y be ? ToIntegerIfIntegral(years).
  47. auto years = TRY(next_integer_argument());
  48. // 3. If months is undefined, let mo be 0; else let mo be ? ToIntegerIfIntegral(months).
  49. auto months = TRY(next_integer_argument());
  50. // 4. If weeks is undefined, let w be 0; else let w be ? ToIntegerIfIntegral(weeks).
  51. auto weeks = TRY(next_integer_argument());
  52. // 5. If days is undefined, let d be 0; else let d be ? ToIntegerIfIntegral(days).
  53. auto days = TRY(next_integer_argument());
  54. // 6. If hours is undefined, let h be 0; else let h be ? ToIntegerIfIntegral(hours).
  55. auto hours = TRY(next_integer_argument());
  56. // 7. If minutes is undefined, let m be 0; else let m be ? ToIntegerIfIntegral(minutes).
  57. auto minutes = TRY(next_integer_argument());
  58. // 8. If seconds is undefined, let s be 0; else let s be ? ToIntegerIfIntegral(seconds).
  59. auto seconds = TRY(next_integer_argument());
  60. // 9. If milliseconds is undefined, let ms be 0; else let ms be ? ToIntegerIfIntegral(milliseconds).
  61. auto milliseconds = TRY(next_integer_argument());
  62. // 10. If microseconds is undefined, let mis be 0; else let mis be ? ToIntegerIfIntegral(microseconds).
  63. auto microseconds = TRY(next_integer_argument());
  64. // 11. If nanoseconds is undefined, let ns be 0; else let ns be ? ToIntegerIfIntegral(nanoseconds).
  65. auto nanoseconds = TRY(next_integer_argument());
  66. // 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
  67. return TRY(create_temporal_duration(vm, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, new_target));
  68. }
  69. // 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
  70. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
  71. {
  72. // 1. Return ? ToTemporalDuration(item).
  73. return TRY(to_temporal_duration(vm, vm.argument(0)));
  74. }
  75. // 7.2.3 Temporal.Duration.compare ( one, two [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.compare
  76. JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::compare)
  77. {
  78. // 1. Set one to ? ToTemporalDuration(one).
  79. auto one = TRY(to_temporal_duration(vm, vm.argument(0)));
  80. // 2. Set two to ? ToTemporalDuration(two).
  81. auto two = TRY(to_temporal_duration(vm, vm.argument(1)));
  82. // 3. Let resolvedOptions be ? GetOptionsObject(options).
  83. auto resolved_options = TRY(get_options_object(vm, vm.argument(2)));
  84. // 4. Let relativeToRecord be ? GetTemporalRelativeToOption(resolvedOptions).
  85. auto relative_to_record = TRY(get_temporal_relative_to_option(vm, resolved_options));
  86. // 5. If one.[[Years]] = two.[[Years]], and one.[[Months]] = two.[[Months]], and one.[[Weeks]] = two.[[Weeks]], and
  87. // one.[[Days]] = two.[[Days]], and one.[[Hours]] = two.[[Hours]], and one.[[Minutes]] = two.[[Minutes]], and
  88. // one.[[Seconds]] = two.[[Seconds]], and one.[[Milliseconds]] = two.[[Milliseconds]], and
  89. // one.[[Microseconds]] = two.[[Microseconds]], and one.[[Nanoseconds]] = two.[[Nanoseconds]], then
  90. if (one->years() == two->years()
  91. && one->months() == two->months()
  92. && one->weeks() == two->weeks()
  93. && one->days() == two->days()
  94. && one->hours() == two->hours()
  95. && one->minutes() == two->minutes()
  96. && one->seconds() == two->seconds()
  97. && one->milliseconds() == two->milliseconds()
  98. && one->microseconds() == two->microseconds()
  99. && one->nanoseconds() == two->nanoseconds()) {
  100. // a. Return +0𝔽.
  101. return 0;
  102. }
  103. // 6. Let zonedRelativeTo be relativeToRecord.[[ZonedRelativeTo]].
  104. // 7. Let plainRelativeTo be relativeToRecord.[[PlainRelativeTo]].
  105. auto [zoned_relative_to, plain_relative_to] = relative_to_record;
  106. // 8. Let largestUnit1 be DefaultTemporalLargestUnit(one).
  107. auto largest_unit1 = default_temporal_largest_unit(one);
  108. // 9. Let largestUnit2 be DefaultTemporalLargestUnit(two).
  109. auto largest_unit2 = default_temporal_largest_unit(two);
  110. // 10. Let duration1 be ToInternalDurationRecord(one).
  111. auto duration1 = to_internal_duration_record(vm, one);
  112. // 11. Let duration2 be ToInternalDurationRecord(two).
  113. auto duration2 = to_internal_duration_record(vm, two);
  114. // 12. If zonedRelativeTo is not undefined, and either TemporalUnitCategory(largestUnit1) or TemporalUnitCategory(largestUnit2) is date, then
  115. if (zoned_relative_to && (temporal_unit_category(largest_unit1) == UnitCategory::Date || temporal_unit_category(largest_unit2) == UnitCategory::Date)) {
  116. // FIXME: a. Let timeZone be zonedRelativeTo.[[TimeZone]].
  117. // FIXME: b. Let calendar be zonedRelativeTo.[[Calendar]].
  118. // FIXME: c. Let after1 be ? AddZonedDateTime(zonedRelativeTo.[[EpochNanoseconds]], timeZone, calendar, duration1, constrain).
  119. // FIXME: d. Let after2 be ? AddZonedDateTime(zonedRelativeTo.[[EpochNanoseconds]], timeZone, calendar, duration2, constrain).
  120. // FIXME: e. If after1 > after2, return 1𝔽.
  121. // FIXME: f. If after1 < after2, return -1𝔽.
  122. // g. Return +0𝔽.
  123. return 0;
  124. }
  125. double days1 = 0;
  126. double days2 = 0;
  127. // 13. If IsCalendarUnit(largestUnit1) is true or IsCalendarUnit(largestUnit2) is true, then
  128. if (is_calendar_unit(largest_unit1) || is_calendar_unit(largest_unit2)) {
  129. // a. If plainRelativeTo is undefined, throw a RangeError exception.
  130. if (!plain_relative_to)
  131. return vm.throw_completion<RangeError>(ErrorType::TemporalMissingStartingPoint, "calendar units");
  132. // FIXME: b. Let days1 be ? DateDurationDays(duration1.[[Date]], plainRelativeTo).
  133. // FIXME: c. Let days2 be ? DateDurationDays(duration2.[[Date]], plainRelativeTo).
  134. }
  135. // 14. Else,
  136. else {
  137. // a. Let days1 be one.[[Days]].
  138. days1 = one->days();
  139. // b. Let days2 be two.[[Days]].
  140. days2 = two->days();
  141. }
  142. // 15. Let timeDuration1 be ? Add24HourDaysToTimeDuration(duration1.[[Time]], days1).
  143. auto time_duration1 = TRY(add_24_hour_days_to_time_duration(vm, duration1.time, days1));
  144. // 16. Let timeDuration2 be ? Add24HourDaysToTimeDuration(duration2.[[Time]], days2).
  145. auto time_duration2 = TRY(add_24_hour_days_to_time_duration(vm, duration2.time, days2));
  146. // 17. Return 𝔽(CompareTimeDuration(timeDuration1, timeDuration2)).
  147. return compare_time_duration(time_duration1, time_duration2);
  148. }
  149. }