Duration.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Object.h>
  9. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  10. #include <LibJS/Runtime/Temporal/Duration.h>
  11. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  12. namespace JS::Temporal {
  13. // 7 Temporal.Duration Objects, https://tc39.es/proposal-temporal/#sec-temporal-duration-objects
  14. Duration::Duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Object& prototype)
  15. : Object(prototype)
  16. , m_years(years)
  17. , m_months(months)
  18. , m_weeks(weeks)
  19. , m_days(days)
  20. , m_hours(hours)
  21. , m_minutes(minutes)
  22. , m_seconds(seconds)
  23. , m_milliseconds(milliseconds)
  24. , m_microseconds(microseconds)
  25. , m_nanoseconds(nanoseconds)
  26. {
  27. }
  28. // 7.5.1 ToTemporalDuration ( item ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalduration
  29. Duration* to_temporal_duration(GlobalObject& global_object, Value item)
  30. {
  31. auto& vm = global_object.vm();
  32. Optional<TemporalDuration> result;
  33. // 1. If Type(item) is Object, then
  34. if (item.is_object()) {
  35. // a. If item has an [[InitializedTemporalDuration]] internal slot, then
  36. if (is<Duration>(item.as_object())) {
  37. // i. Return item.
  38. return &static_cast<Duration&>(item.as_object());
  39. }
  40. // b. Let result be ? ToTemporalDurationRecord(item).
  41. result = to_temporal_duration_record(global_object, item.as_object());
  42. if (vm.exception())
  43. return {};
  44. }
  45. // 2. Else,
  46. else {
  47. // a. Let string be ? ToString(item).
  48. auto string = item.to_string(global_object);
  49. if (vm.exception())
  50. return {};
  51. // b. Let result be ? ParseTemporalDurationString(string).
  52. result = parse_temporal_duration_string(global_object, string);
  53. if (vm.exception())
  54. return {};
  55. }
  56. // 3. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
  57. return create_temporal_duration(global_object, result->years, result->months, result->weeks, result->days, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds);
  58. }
  59. // 7.5.2 ToTemporalDurationRecord ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldurationrecord
  60. TemporalDuration to_temporal_duration_record(GlobalObject& global_object, Object& temporal_duration_like)
  61. {
  62. auto& vm = global_object.vm();
  63. // 1. Assert: Type(temporalDurationLike) is Object.
  64. // 2. If temporalDurationLike has an [[InitializedTemporalDuration]] internal slot, then
  65. if (is<Duration>(temporal_duration_like)) {
  66. auto& duration = static_cast<Duration&>(temporal_duration_like);
  67. // a. Return the Record { [[Years]]: temporalDurationLike.[[Years]], [[Months]]: temporalDurationLike.[[Months]], [[Weeks]]: temporalDurationLike.[[Weeks]], [[Days]]: temporalDurationLike.[[Days]], [[Hours]]: temporalDurationLike.[[Hours]], [[Minutes]]: temporalDurationLike.[[Minutes]], [[Seconds]]: temporalDurationLike.[[Seconds]], [[Milliseconds]]: temporalDurationLike.[[Milliseconds]], [[Microseconds]]: temporalDurationLike.[[Microseconds]], [[Nanoseconds]]: temporalDurationLike.[[Nanoseconds]] }.
  68. return TemporalDuration { .years = duration.years(), .months = duration.months(), .weeks = duration.weeks(), .days = duration.days(), .hours = duration.hours(), .minutes = duration.minutes(), .seconds = duration.seconds(), .milliseconds = duration.milliseconds(), .microseconds = duration.microseconds(), .nanoseconds = duration.nanoseconds() };
  69. }
  70. // 3. Let result be a new Record with all the internal slots given in the Internal Slot column in Table 7.
  71. auto result = TemporalDuration {};
  72. // 4. Let any be false.
  73. auto any = false;
  74. // 5. For each row of Table 7, except the header row, in table order, do
  75. for (auto& [internal_slot, property] : temporal_duration_like_properties<TemporalDuration, double>(vm)) {
  76. // a. Let prop be the Property value of the current row.
  77. // b. Let val be ? Get(temporalDurationLike, prop).
  78. auto value = temporal_duration_like.get(property);
  79. if (vm.exception())
  80. return {};
  81. // c. If val is not undefined, then
  82. if (!value.is_undefined()) {
  83. // i. Set any to true.
  84. any = true;
  85. }
  86. // TODO: This is not in the spec but it seems to be implied, and is also what the polyfill does.
  87. // I think the steps d, e, and f should be conditional based on c - otherwise we call ToNumber(undefined),
  88. // get NaN and immediately fail the floor(val) ≠ val check, making the `any` flag pointless. See:
  89. // - https://github.com/tc39/proposal-temporal/blob/4b4dbd427d4b0468a3b064ca7082f25b209923bc/polyfill/lib/ecmascript.mjs#L556-L607
  90. // - https://github.com/tc39/proposal-temporal/blob/4b4dbd427d4b0468a3b064ca7082f25b209923bc/polyfill/lib/ecmascript.mjs#L876-L893
  91. else {
  92. continue;
  93. }
  94. // d. Let val be ? ToNumber(val).
  95. value = value.to_number(global_object);
  96. if (vm.exception())
  97. return {};
  98. // e. If floor(val) ≠ val, then
  99. if (floor(value.as_double()) != value.as_double()) {
  100. // i. Throw a RangeError exception.
  101. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValue, property.as_string(), value.to_string_without_side_effects());
  102. return {};
  103. }
  104. // f. Set result's internal slot whose name is the Internal Slot value of the current row to val.
  105. result.*internal_slot = value.as_double();
  106. }
  107. // 6. If any is false, then
  108. if (!any) {
  109. // a. Throw a TypeError exception.
  110. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
  111. return {};
  112. }
  113. // 7. Return result.
  114. return result;
  115. }
  116. // 7.5.3 DurationSign ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds )
  117. i8 duration_sign(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  118. {
  119. // 1. For each value v of « years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds », do
  120. for (auto& v : { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds }) {
  121. // a. If v < 0, return −1.
  122. if (v < 0)
  123. return -1;
  124. // b. If v > 0, return 1.
  125. if (v > 0)
  126. return 1;
  127. }
  128. // 2. Return 0.
  129. return 0;
  130. }
  131. // 7.5.4 IsValidDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds )
  132. bool is_valid_duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  133. {
  134. // 1. Let sign be ! DurationSign(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  135. auto sign = duration_sign(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  136. // 2. For each value v of « years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds », do
  137. for (auto& v : { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds }) {
  138. // a. If v is not finite, return false.
  139. if (!isfinite(v))
  140. return false;
  141. // b. If v < 0 and sign > 0, return false.
  142. if (v < 0 && sign > 0)
  143. return false;
  144. // c. If v > 0 and sign < 0, return false.
  145. if (v > 0 && sign < 0)
  146. return false;
  147. }
  148. // 3. Return true.
  149. return true;
  150. }
  151. // 7.5.6 ToPartialDuration ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-topartialduration
  152. PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_duration_like)
  153. {
  154. auto& vm = global_object.vm();
  155. // 1. If Type(temporalDurationLike) is not Object, then
  156. if (!temporal_duration_like.is_object()) {
  157. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, temporal_duration_like.to_string_without_side_effects());
  158. return {};
  159. }
  160. // 2. Let result be the new Record { [[Years]]: undefined, [[Months]]: undefined, [[Weeks]]: undefined, [[Days]]: undefined, [[Hours]]: undefined, [[Minutes]]: undefined, [[Seconds]]: undefined, [[Milliseconds]]: undefined, [[Microseconds]]: undefined, [[Nanoseconds]]: undefined }.
  161. auto result = PartialDuration {};
  162. // 3. Let any be false.
  163. auto any = false;
  164. // 4. For each row of Table 7, except the header row, in table order, do
  165. for (auto& [internal_slot, property] : temporal_duration_like_properties<PartialDuration, Optional<double>>(vm)) {
  166. // a. Let property be the Property value of the current row.
  167. // b. Let value be ? Get(temporalDurationLike, property).
  168. auto value = temporal_duration_like.as_object().get(property);
  169. if (vm.exception())
  170. return {};
  171. // c. If value is not undefined, then
  172. if (!value.is_undefined()) {
  173. // i. Set any to true.
  174. any = true;
  175. // ii. Set value to ? ToIntegerOrInfinity(value).
  176. auto value_number = value.to_integer_or_infinity(global_object);
  177. if (vm.exception())
  178. return {};
  179. // iii. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  180. result.*internal_slot = value_number;
  181. }
  182. }
  183. // 5. If any is false, then
  184. if (!any) {
  185. // a. Throw a TypeError exception.
  186. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
  187. return {};
  188. }
  189. // 6. Return result.
  190. return result;
  191. }
  192. // 7.5.7 CreateTemporalDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalduration
  193. Duration* create_temporal_duration(GlobalObject& global_object, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, FunctionObject* new_target)
  194. {
  195. auto& vm = global_object.vm();
  196. // 1. If ! IsValidDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) is false, throw a RangeError exception.
  197. if (!is_valid_duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)) {
  198. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDuration);
  199. return {};
  200. }
  201. // 2. If newTarget is not present, set it to %Temporal.Duration%.
  202. if (!new_target)
  203. new_target = global_object.temporal_duration_constructor();
  204. // 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Duration.prototype%", « [[InitializedTemporalDuration]], [[Years]], [[Months]], [[Weeks]], [[Days]], [[Hours]], [[Minutes]], [[Seconds]], [[Milliseconds]], [[Microseconds]], [[Nanoseconds]] »).
  205. // 4. Set object.[[Years]] to years.
  206. // 5. Set object.[[Months]] to months.
  207. // 6. Set object.[[Weeks]] to weeks.
  208. // 7. Set object.[[Days]] to days.
  209. // 8. Set object.[[Hours]] to hours.
  210. // 9. Set object.[[Minutes]] to minutes.
  211. // 10. Set object.[[Seconds]] to seconds.
  212. // 11. Set object.[[Milliseconds]] to milliseconds.
  213. // 12. Set object.[[Microseconds]] to microseconds.
  214. // 13. Set object.[[Nanoseconds]] to nanoseconds.
  215. auto* object = ordinary_create_from_constructor<Duration>(global_object, *new_target, &GlobalObject::temporal_duration_prototype, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  216. if (vm.exception())
  217. return {};
  218. // 14. Return object.
  219. return object;
  220. }
  221. }