PlainDate.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@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/Temporal/Calendar.h>
  9. #include <LibJS/Runtime/Temporal/PlainDate.h>
  10. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  11. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  12. namespace JS::Temporal {
  13. // 3 Temporal.PlainDate Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-objects
  14. PlainDate::PlainDate(i32 year, u8 month, u8 day, Object& calendar, Object& prototype)
  15. : Object(prototype)
  16. , m_iso_year(year)
  17. , m_iso_month(month)
  18. , m_iso_day(day)
  19. , m_calendar(calendar)
  20. {
  21. }
  22. void PlainDate::visit_edges(Visitor& visitor)
  23. {
  24. visitor.visit(&m_calendar);
  25. }
  26. // 3.5.1 CreateTemporalDate ( isoYear, isoMonth, isoDay, calendar [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaldate
  27. PlainDate* create_temporal_date(GlobalObject& global_object, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject* new_target)
  28. {
  29. auto& vm = global_object.vm();
  30. // 1. Assert: isoYear is an integer.
  31. // 2. Assert: isoMonth is an integer.
  32. // 3. Assert: isoDay is an integer.
  33. // 4. Assert: Type(calendar) is Object.
  34. // 5. If ! IsValidISODate(isoYear, isoMonth, isoDay) is false, throw a RangeError exception.
  35. if (!is_valid_iso_date(iso_year, iso_month, iso_day)) {
  36. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  37. return {};
  38. }
  39. // 6. If ! ISODateTimeWithinLimits(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0) is false, throw a RangeError exception.
  40. if (!iso_date_time_within_limits(global_object, iso_year, iso_month, iso_day, 12, 0, 0, 0, 0, 0)) {
  41. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  42. return {};
  43. }
  44. // 7. If newTarget is not present, set it to %Temporal.PlainDate%.
  45. if (!new_target)
  46. new_target = global_object.temporal_plain_date_constructor();
  47. // 8. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainDate.prototype%", « [[InitializedTemporalDate]], [[ISOYear]], [[ISOMonth]], [[ISODay]], [[Calendar]] »).
  48. // 9. Set object.[[ISOYear]] to isoYear.
  49. // 10. Set object.[[ISOMonth]] to isoMonth.
  50. // 11. Set object.[[ISODay]] to isoDay.
  51. // 12. Set object.[[Calendar]] to calendar.
  52. auto* object = ordinary_create_from_constructor<PlainDate>(global_object, *new_target, &GlobalObject::temporal_plain_date_prototype, iso_year, iso_month, iso_day, calendar);
  53. if (vm.exception())
  54. return {};
  55. return object;
  56. }
  57. // 3.5.2 ToTemporalDate ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldate
  58. PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* options)
  59. {
  60. auto& vm = global_object.vm();
  61. // 1. If options is not present, set options to ! OrdinaryObjectCreate(null).
  62. if (!options)
  63. options = Object::create(global_object, nullptr);
  64. // 2. Assert: Type(options) is Object.
  65. // 3. If Type(item) is Object, then
  66. if (item.is_object()) {
  67. auto& item_object = item.as_object();
  68. // a. If item has an [[InitializedTemporalDate]] internal slot, then
  69. if (is<PlainDate>(item_object)) {
  70. // i. Return item.
  71. return static_cast<PlainDate*>(&item_object);
  72. }
  73. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  74. // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
  75. // ii. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
  76. // iii. Return ! CreateTemporalDate(plainDateTime.[[ISOYear]], plainDateTime.[[ISOMonth]], plainDateTime.[[ISODay]], plainDateTime.[[Calendar]]).
  77. // TODO
  78. // c. If item has an [[InitializedTemporalDateTime]] internal slot, then
  79. if (is<PlainDateTime>(item_object)) {
  80. auto& date_time_item = static_cast<PlainDateTime&>(item_object);
  81. // i. Return ! CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
  82. return create_temporal_date(global_object, date_time_item.iso_year(), date_time_item.iso_month(), date_time_item.iso_day(), date_time_item.calendar());
  83. }
  84. // d. Let calendar be ? GetTemporalCalendarWithISODefault(item).
  85. auto* calendar = get_temporal_calendar_with_iso_default(global_object, item_object);
  86. if (vm.exception())
  87. return {};
  88. // e. Let fieldNames be ? CalendarFields(calendar, « "day", "month", "monthCode", "year" »).
  89. auto field_names = calendar_fields(global_object, *calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv });
  90. if (vm.exception())
  91. return {};
  92. // f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
  93. auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {});
  94. if (vm.exception())
  95. return {};
  96. // g. Return ? DateFromFields(calendar, fields, options).
  97. return date_from_fields(global_object, *calendar, *fields, *options);
  98. }
  99. // 4. Perform ? ToTemporalOverflow(options).
  100. (void)to_temporal_overflow(global_object, *options);
  101. if (vm.exception())
  102. return {};
  103. // 5. Let string be ? ToString(item).
  104. auto string = item.to_string(global_object);
  105. if (vm.exception())
  106. return {};
  107. // 6. Let result be ? ParseTemporalDateString(string).
  108. auto result = parse_temporal_date_string(global_object, string);
  109. if (vm.exception())
  110. return {};
  111. // 7. Assert: ! IsValidISODate(result.[[Year]], result.[[Month]], result.[[Day]]) is true.
  112. VERIFY(is_valid_iso_date(result->year, result->month, result->day));
  113. // 8. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]).
  114. auto calendar = to_temporal_calendar_with_iso_default(global_object, result->calendar.has_value() ? js_string(vm, *result->calendar) : js_undefined());
  115. if (vm.exception())
  116. return {};
  117. // 9. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar).
  118. return create_temporal_date(global_object, result->year, result->month, result->day, *calendar);
  119. }
  120. // 3.5.4 RegulateISODate ( year, month, day, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulateisodate
  121. Optional<TemporalDate> regulate_iso_date(GlobalObject& global_object, double year, double month, double day, String const& overflow)
  122. {
  123. auto& vm = global_object.vm();
  124. // 1. Assert: year, month, and day are integers.
  125. VERIFY(year == trunc(year) && month == trunc(month) && day == trunc(day));
  126. // 2. Assert: overflow is either "constrain" or "reject".
  127. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  128. // 3. If overflow is "reject", then
  129. if (overflow == "reject"sv) {
  130. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  131. // This does not change the exposed behaviour as the call to IsValidISODate will immediately check that these values are valid ISO
  132. // values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  133. if (!AK::is_within_range<i32>(year) || !AK::is_within_range<u8>(month) || !AK::is_within_range<u8>(day)) {
  134. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  135. return {};
  136. }
  137. auto y = static_cast<i32>(year);
  138. auto m = static_cast<u8>(month);
  139. auto d = static_cast<u8>(day);
  140. // a. If ! IsValidISODate(year, month, day) is false, throw a RangeError exception.
  141. if (is_valid_iso_date(y, m, d)) {
  142. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  143. return {};
  144. }
  145. // b. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
  146. return TemporalDate { .year = y, .month = m, .day = d, .calendar = {} };
  147. }
  148. // 4. If overflow is "constrain", then
  149. else if (overflow == "constrain"sv) {
  150. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat this double as normal integer from this point onwards. This
  151. // does not change the exposed behaviour as the parent's call to CreateTemporalDate will immediately check that this value is a valid
  152. // ISO value for years: -273975 - 273975, which is a subset of this check.
  153. if (!AK::is_within_range<i32>(year)) {
  154. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  155. return {};
  156. }
  157. auto y = static_cast<i32>(year);
  158. // a. Set month to ! ConstrainToRange(month, 1, 12).
  159. month = constrain_to_range(month, 1, 12);
  160. // b. Set day to ! ConstrainToRange(day, 1, ! ISODaysInMonth(year, month)).
  161. day = constrain_to_range(day, 1, iso_days_in_month(y, month));
  162. // c. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
  163. return TemporalDate { .year = y, .month = static_cast<u8>(month), .day = static_cast<u8>(day), .calendar = {} };
  164. }
  165. VERIFY_NOT_REACHED();
  166. }
  167. // 3.5.5 IsValidISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidisodate
  168. bool is_valid_iso_date(i32 year, u8 month, u8 day)
  169. {
  170. // 1. Assert: year, month, and day are integers.
  171. // 2. If month < 1 or month > 12, then
  172. if (month < 1 || month > 12) {
  173. // a. Return false.
  174. return false;
  175. }
  176. // 3. Let daysInMonth be ! ISODaysInMonth(year, month).
  177. auto days_in_month = iso_days_in_month(year, month);
  178. // 4. If day < 1 or day > daysInMonth, then
  179. if (day < 1 || day > days_in_month) {
  180. // a. Return false.
  181. return false;
  182. }
  183. // 5. Return true.
  184. return true;
  185. }
  186. }