Duration.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 undefined, then
  82. if (value.is_undefined()) {
  83. // i. Set result's internal slot whose name is the Internal Slot value of the current row to 0.
  84. result.*internal_slot = 0;
  85. }
  86. // d. Else,
  87. else {
  88. // i. Set any to true.
  89. any = true;
  90. // ii. Let val be ? ToNumber(val).
  91. value = value.to_number(global_object);
  92. if (vm.exception())
  93. return {};
  94. // iii. If val is NaN, +∞ or -∞, then
  95. if (value.is_nan() || value.is_infinity()) {
  96. // 1. Throw a RangeError exception.
  97. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects());
  98. return {};
  99. }
  100. // iv. If floor(val) ≠ val, then
  101. if (floor(value.as_double()) != value.as_double()) {
  102. // 1. Throw a RangeError exception.
  103. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects());
  104. return {};
  105. }
  106. // v. Set result's internal slot whose name is the Internal Slot value of the current row to val.
  107. result.*internal_slot = value.as_double();
  108. }
  109. }
  110. // 6. If any is false, then
  111. if (!any) {
  112. // a. Throw a TypeError exception.
  113. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
  114. return {};
  115. }
  116. // 7. Return result.
  117. return result;
  118. }
  119. // 7.5.3 DurationSign ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-durationsign
  120. i8 duration_sign(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  121. {
  122. // 1. For each value v of « years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds », do
  123. for (auto& v : { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds }) {
  124. // a. If v < 0, return −1.
  125. if (v < 0)
  126. return -1;
  127. // b. If v > 0, return 1.
  128. if (v > 0)
  129. return 1;
  130. }
  131. // 2. Return 0.
  132. return 0;
  133. }
  134. // 7.5.4 IsValidDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidduration
  135. bool is_valid_duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  136. {
  137. // 1. Let sign be ! DurationSign(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  138. auto sign = duration_sign(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  139. // 2. For each value v of « years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds », do
  140. for (auto& v : { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds }) {
  141. // a. If v is not finite, return false.
  142. if (!isfinite(v))
  143. return false;
  144. // b. If v < 0 and sign > 0, return false.
  145. if (v < 0 && sign > 0)
  146. return false;
  147. // c. If v > 0 and sign < 0, return false.
  148. if (v > 0 && sign < 0)
  149. return false;
  150. }
  151. // 3. Return true.
  152. return true;
  153. }
  154. // 7.5.6 ToPartialDuration ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-topartialduration
  155. PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_duration_like)
  156. {
  157. auto& vm = global_object.vm();
  158. // 1. If Type(temporalDurationLike) is not Object, then
  159. if (!temporal_duration_like.is_object()) {
  160. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, temporal_duration_like.to_string_without_side_effects());
  161. return {};
  162. }
  163. // 2. Let result be the Record { [[Years]]: undefined, [[Months]]: undefined, [[Weeks]]: undefined, [[Days]]: undefined, [[Hours]]: undefined, [[Minutes]]: undefined, [[Seconds]]: undefined, [[Milliseconds]]: undefined, [[Microseconds]]: undefined, [[Nanoseconds]]: undefined }.
  164. auto result = PartialDuration {};
  165. // 3. Let any be false.
  166. auto any = false;
  167. // 4. For each row of Table 7, except the header row, in table order, do
  168. for (auto& [internal_slot, property] : temporal_duration_like_properties<PartialDuration, Optional<double>>(vm)) {
  169. // a. Let property be the Property value of the current row.
  170. // b. Let value be ? Get(temporalDurationLike, property).
  171. auto value = temporal_duration_like.as_object().get(property);
  172. if (vm.exception())
  173. return {};
  174. // c. If value is not undefined, then
  175. if (!value.is_undefined()) {
  176. // i. Set any to true.
  177. any = true;
  178. // ii. Set value to ? ToIntegerOrInfinity(value).
  179. auto value_number = value.to_integer_or_infinity(global_object);
  180. if (vm.exception())
  181. return {};
  182. // iii. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  183. result.*internal_slot = value_number;
  184. }
  185. }
  186. // 5. If any is false, then
  187. if (!any) {
  188. // a. Throw a TypeError exception.
  189. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
  190. return {};
  191. }
  192. // 6. Return result.
  193. return result;
  194. }
  195. // 7.5.7 CreateTemporalDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalduration
  196. 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)
  197. {
  198. auto& vm = global_object.vm();
  199. // 1. If ! IsValidDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) is false, throw a RangeError exception.
  200. if (!is_valid_duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)) {
  201. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDuration);
  202. return {};
  203. }
  204. // 2. If newTarget is not present, set it to %Temporal.Duration%.
  205. if (!new_target)
  206. new_target = global_object.temporal_duration_constructor();
  207. // 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Duration.prototype%", « [[InitializedTemporalDuration]], [[Years]], [[Months]], [[Weeks]], [[Days]], [[Hours]], [[Minutes]], [[Seconds]], [[Milliseconds]], [[Microseconds]], [[Nanoseconds]] »).
  208. // 4. Set object.[[Years]] to years.
  209. // 5. Set object.[[Months]] to months.
  210. // 6. Set object.[[Weeks]] to weeks.
  211. // 7. Set object.[[Days]] to days.
  212. // 8. Set object.[[Hours]] to hours.
  213. // 9. Set object.[[Minutes]] to minutes.
  214. // 10. Set object.[[Seconds]] to seconds.
  215. // 11. Set object.[[Milliseconds]] to milliseconds.
  216. // 12. Set object.[[Microseconds]] to microseconds.
  217. // 13. Set object.[[Nanoseconds]] to nanoseconds.
  218. 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);
  219. if (vm.exception())
  220. return {};
  221. // 14. Return object.
  222. return object;
  223. }
  224. // 7.5.19 ToLimitedTemporalDuration ( temporalDurationLike, disallowedFields ),https://tc39.es/proposal-temporal/#sec-temporal-tolimitedtemporalduration
  225. Optional<TemporalDuration> to_limited_temporal_duration(GlobalObject& global_object, Value temporal_duration_like, Vector<StringView> const& disallowed_fields)
  226. {
  227. auto& vm = global_object.vm();
  228. Optional<TemporalDuration> duration;
  229. // 1. If Type(temporalDurationLike) is not Object, then
  230. if (!temporal_duration_like.is_object()) {
  231. // a. Let str be ? ToString(temporalDurationLike).
  232. auto str = temporal_duration_like.to_string(global_object);
  233. if (vm.exception())
  234. return {};
  235. // b. Let duration be ? ParseTemporalDurationString(str).
  236. duration = parse_temporal_duration_string(global_object, str);
  237. if (vm.exception())
  238. return {};
  239. }
  240. // 2. Else,
  241. else {
  242. // a. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
  243. duration = to_temporal_duration_record(global_object, temporal_duration_like.as_object());
  244. if (vm.exception())
  245. return {};
  246. }
  247. // 3. If ! IsValidDuration(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]) is false, throw a RangeError exception.
  248. if (!is_valid_duration(duration->years, duration->months, duration->weeks, duration->days, duration->hours, duration->minutes, duration->seconds, duration->milliseconds, duration->microseconds, duration->nanoseconds)) {
  249. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDuration);
  250. return {};
  251. }
  252. // 4. For each row of Table 7, except the header row, in table order, do
  253. for (auto& [internal_slot, property] : temporal_duration_like_properties<TemporalDuration, double>(vm)) {
  254. // a. Let prop be the Property value of the current row.
  255. // b. Let value be duration's internal slot whose name is the Internal Slot value of the current row.
  256. auto value = (*duration).*internal_slot;
  257. // If value is not 0 and disallowedFields contains prop, then
  258. if (value != 0 && disallowed_fields.contains_slow(property.as_string())) {
  259. // i. Throw a RangeError exception.
  260. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonZero, property.as_string(), value);
  261. return {};
  262. }
  263. }
  264. // 5. Return duration.
  265. return duration;
  266. }
  267. }