PlainMonthDay.cpp 10 KB

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