PlainMonthDay.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2021, 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 <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Temporal/Calendar.h>
  10. #include <LibJS/Runtime/Temporal/PlainDate.h>
  11. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  12. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  13. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  14. #include <LibJS/Runtime/Temporal/PlainTime.h>
  15. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  16. namespace JS::Temporal {
  17. // 10 Temporal.PlainMonthDay Objects, https://tc39.es/proposal-temporal/#sec-temporal-plainmonthday-objects
  18. PlainMonthDay::PlainMonthDay(u8 iso_month, u8 iso_day, i32 iso_year, Object& calendar, Object& prototype)
  19. : Object(prototype)
  20. , m_iso_year(iso_year)
  21. , m_iso_month(iso_month)
  22. , m_iso_day(iso_day)
  23. , m_calendar(calendar)
  24. {
  25. }
  26. void PlainMonthDay::visit_edges(Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(&m_calendar);
  30. }
  31. // 10.5.1 ToTemporalMonthDay ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalmonthday
  32. PlainMonthDay* to_temporal_month_day(GlobalObject& global_object, Value item, Object const* options)
  33. {
  34. auto& vm = global_object.vm();
  35. // 1. If options is not present, set options to ! OrdinaryObjectCreate(null).
  36. if (!options)
  37. options = Object::create(global_object, nullptr);
  38. // 2. Let referenceISOYear be 1972 (the first leap year after the Unix epoch).
  39. i32 reference_iso_year = 1972;
  40. // 3. If Type(item) is Object, then
  41. if (item.is_object()) {
  42. auto& item_object = item.as_object();
  43. // a. If item has an [[InitializedTemporalMonthDay]] internal slot, then
  44. if (is<PlainMonthDay>(item_object)) {
  45. // i. Return item.
  46. return static_cast<PlainMonthDay*>(&item_object);
  47. }
  48. Object* calendar = nullptr;
  49. bool calendar_absent;
  50. // b. If item has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  51. // i. Let calendar be item.[[Calendar]].
  52. // ii. Let calendarAbsent be false.
  53. if (is<PlainDate>(item_object)) {
  54. calendar = &static_cast<PlainDate&>(item_object).calendar();
  55. calendar_absent = false;
  56. } else if (is<PlainDateTime>(item_object)) {
  57. calendar = &static_cast<PlainDateTime&>(item_object).calendar();
  58. calendar_absent = false;
  59. } else if (is<PlainMonthDay>(item_object)) {
  60. calendar = &static_cast<PlainMonthDay&>(item_object).calendar();
  61. calendar_absent = false;
  62. } else if (is<PlainTime>(item_object)) {
  63. calendar = &static_cast<PlainTime&>(item_object).calendar();
  64. calendar_absent = false;
  65. } else if (is<PlainYearMonth>(item_object)) {
  66. calendar = &static_cast<PlainYearMonth&>(item_object).calendar();
  67. calendar_absent = false;
  68. } else if (is<ZonedDateTime>(item_object)) {
  69. calendar = &static_cast<ZonedDateTime&>(item_object).calendar();
  70. calendar_absent = false;
  71. } else {
  72. // i. Let calendar be ? Get(item, "calendar").
  73. auto calendar_value = item_object.get(vm.names.calendar);
  74. if (vm.exception())
  75. return {};
  76. // ii. If calendar is undefined, then
  77. // 1. Let calendarAbsent be true.
  78. // iii. Else,
  79. // 1. Let calendarAbsent be false.
  80. calendar_absent = calendar_value.is_undefined();
  81. // iv. Set calendar to ? ToTemporalCalendarWithISODefault(calendar).
  82. calendar = to_temporal_calendar_with_iso_default(global_object, calendar_value);
  83. if (vm.exception())
  84. return {};
  85. }
  86. // d. Let fieldNames be ? CalendarFields(calendar, « "day", "month", "monthCode", "year" »).
  87. auto field_names = calendar_fields(global_object, *calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv });
  88. if (vm.exception())
  89. return {};
  90. // e. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
  91. auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {});
  92. if (vm.exception())
  93. return {};
  94. // f. Let month be ? Get(fields, "month").
  95. auto month = fields->get(vm.names.month);
  96. if (vm.exception())
  97. return {};
  98. // g. Let monthCode be ? Get(fields, "monthCode").
  99. auto month_code = fields->get(vm.names.monthCode);
  100. if (vm.exception())
  101. return {};
  102. // h. Let year be ? Get(fields, "year").
  103. auto year = fields->get(vm.names.year);
  104. if (vm.exception())
  105. return {};
  106. // i. If calendarAbsent is true, and month is not undefined, and monthCode is undefined and year is undefined, then
  107. if (calendar_absent && !month.is_undefined() && month_code.is_undefined() && year.is_undefined()) {
  108. // i. Perform ! CreateDataPropertyOrThrow(fields, "year", 𝔽(referenceISOYear)).
  109. fields->create_data_property_or_throw(vm.names.year, Value(reference_iso_year));
  110. }
  111. // j. Return ? MonthDayFromFields(calendar, fields, options).
  112. return month_day_from_fields(global_object, *calendar, *fields, options);
  113. }
  114. // FIXME: The spec has an issue in this part which makes it unimplementable, namely:
  115. // - ParseTemporalMonthDayString doesn't return a [[Calendar]] field, which is required in step 7.
  116. // This is a known issue, see https://github.com/tc39/proposal-temporal/issues/1502
  117. TODO();
  118. }
  119. // 10.5.2 CreateTemporalMonthDay ( isoMonth, isoDay, calendar, referenceISOYear [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalmonthday
  120. PlainMonthDay* create_temporal_month_day(GlobalObject& global_object, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject const* new_target)
  121. {
  122. auto& vm = global_object.vm();
  123. // 1. Assert: isoMonth, isoDay, and referenceISOYear are integers.
  124. // 2. Assert: Type(calendar) is Object.
  125. // 3. If ! IsValidISODate(referenceISOYear, isoMonth, isoDay) is false, throw a RangeError exception.
  126. if (!is_valid_iso_date(reference_iso_year, iso_month, iso_day)) {
  127. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainMonthDay);
  128. return {};
  129. }
  130. // 4. If newTarget is not present, set it to %Temporal.PlainMonthDay%.
  131. if (!new_target)
  132. new_target = global_object.temporal_plain_month_day_constructor();
  133. // 5. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainMonthDay.prototype%", « [[InitializedTemporalMonthDay]], [[ISOMonth]], [[ISODay]], [[ISOYear]], [[Calendar]] »).
  134. // 6. Set object.[[ISOMonth]] to isoMonth.
  135. // 7. Set object.[[ISODay]] to isoDay.
  136. // 8. Set object.[[Calendar]] to calendar.
  137. // 9. Set object.[[ISOYear]] to referenceISOYear.
  138. auto* object = ordinary_create_from_constructor<PlainMonthDay>(global_object, *new_target, &GlobalObject::temporal_plain_month_day_prototype, iso_month, iso_day, reference_iso_year, calendar);
  139. if (vm.exception())
  140. return {};
  141. // 10. Return object.
  142. return object;
  143. }
  144. // 10.5.3 TemporalMonthDayToString ( monthDay, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalmonthdaytostring
  145. Optional<String> temporal_month_day_to_string(GlobalObject& global_object, PlainMonthDay& month_day, StringView show_calendar)
  146. {
  147. auto& vm = global_object.vm();
  148. // 1. Assert: Type(monthDay) is Object.
  149. // 2. Assert: monthDay has an [[InitializedTemporalMonthDay]] internal slot.
  150. // 3. Let month be monthDay.[[ISOMonth]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  151. // 4. Let day be monthDay.[[ISODay]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  152. // 5. Let result be the string-concatenation of month, the code unit 0x002D (HYPHEN-MINUS), and day.
  153. auto result = String::formatted("{:02}-{:02}", month_day.iso_month(), month_day.iso_day());
  154. // 6. Let calendarID be ? ToString(monthDay.[[Calendar]]).
  155. auto calendar_id = Value(&month_day.calendar()).to_string(global_object);
  156. if (vm.exception())
  157. return {};
  158. // 7. If calendarID is not "iso8601", then
  159. if (calendar_id != "iso8601"sv) {
  160. // a. Let year be ! PadISOYear(monthDay.[[ISOYear]]).
  161. // b. Set result to the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and result.
  162. result = String::formatted("{}-{}", pad_iso_year(month_day.iso_year()), result);
  163. }
  164. // 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar).
  165. auto calendar_string = format_calendar_annotation(calendar_id, show_calendar);
  166. // 9. Set result to the string-concatenation of result and calendarString.
  167. // 10. Return result.
  168. return String::formatted("{}{}", result, calendar_string);
  169. }
  170. }