PlainMonthDay.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Intrinsics.h>
  10. #include <LibJS/Runtime/Realm.h>
  11. #include <LibJS/Runtime/Temporal/Calendar.h>
  12. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  13. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  14. #include <LibJS/Runtime/VM.h>
  15. namespace JS::Temporal {
  16. GC_DEFINE_ALLOCATOR(PlainMonthDay);
  17. // 10 Temporal.PlainMonthDay Objects, https://tc39.es/proposal-temporal/#sec-temporal-plainmonthday-objects
  18. PlainMonthDay::PlainMonthDay(ISODate iso_date, String calendar, Object& prototype)
  19. : Object(ConstructWithPrototypeTag::Tag, prototype)
  20. , m_iso_date(iso_date)
  21. , m_calendar(move(calendar))
  22. {
  23. }
  24. // 10.5.1 ToTemporalMonthDay ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalmonthday
  25. ThrowCompletionOr<GC::Ref<PlainMonthDay>> to_temporal_month_day(VM& vm, Value item, Value options)
  26. {
  27. // 1. If options is not present, set options to undefined.
  28. // 2. If item is a Object, then
  29. if (item.is_object()) {
  30. auto const& object = item.as_object();
  31. // a. If item has an [[InitializedTemporalMonthDay]] internal slot, then
  32. if (is<PlainMonthDay>(object)) {
  33. auto const& plain_month_day = static_cast<PlainMonthDay const&>(object);
  34. // i. Let resolvedOptions be ? GetOptionsObject(options).
  35. auto resolved_options = TRY(get_options_object(vm, options));
  36. // ii. Perform ? GetTemporalOverflowOption(resolvedOptions).
  37. TRY(get_temporal_overflow_option(vm, resolved_options));
  38. // iii. Return ! CreateTemporalMonthDay(item.[[ISODate]], item.[[Calendar]]).
  39. return MUST(create_temporal_month_day(vm, plain_month_day.iso_date(), plain_month_day.calendar()));
  40. }
  41. // b. Let calendar be ? GetTemporalCalendarIdentifierWithISODefault(item).
  42. auto calendar = TRY(get_temporal_calendar_identifier_with_iso_default(vm, object));
  43. // c. Let fields be ? PrepareCalendarFields(calendar, item, « YEAR, MONTH, MONTH-CODE, DAY », «», «»).
  44. auto fields = TRY(prepare_calendar_fields(vm, calendar, object, { { CalendarField::Year, CalendarField::Month, CalendarField::MonthCode, CalendarField::Day } }, {}, CalendarFieldList {}));
  45. // d. Let resolvedOptions be ? GetOptionsObject(options).
  46. auto resolved_options = TRY(get_options_object(vm, options));
  47. // e. Let overflow be ? GetTemporalOverflowOption(resolvedOptions).
  48. auto overflow = TRY(get_temporal_overflow_option(vm, resolved_options));
  49. // f. Let isoDate be ? CalendarMonthDayFromFields(calendar, fields, overflow).
  50. auto iso_date = TRY(calendar_month_day_from_fields(vm, calendar, move(fields), overflow));
  51. // g. Return ! CreateTemporalMonthDay(isoDate, calendar).
  52. return MUST(create_temporal_month_day(vm, iso_date, move(calendar)));
  53. }
  54. // 3. If item is not a String, throw a TypeError exception.
  55. if (!item.is_string())
  56. return vm.throw_completion<TypeError>(ErrorType::TemporalInvalidPlainMonthDay);
  57. // 4. Let result be ? ParseISODateTime(item, « TemporalMonthDayString »).
  58. auto parse_result = TRY(parse_iso_date_time(vm, item.as_string().utf8_string_view(), { { Production::TemporalMonthDayString } }));
  59. // 5. Let calendar be result.[[Calendar]].
  60. // 6. If calendar is empty, set calendar to "iso8601".
  61. auto calendar = parse_result.calendar.value_or("iso8601"_string);
  62. // 7. Set calendar to ? CanonicalizeCalendar(calendar).
  63. calendar = TRY(canonicalize_calendar(vm, calendar));
  64. // 8. Let resolvedOptions be ? GetOptionsObject(options).
  65. auto resolved_options = TRY(get_options_object(vm, options));
  66. // 9. Perform ? GetTemporalOverflowOption(resolvedOptions).
  67. TRY(get_temporal_overflow_option(vm, resolved_options));
  68. // 10. If result.[[Year]] is empty, then
  69. if (!parse_result.year.has_value()) {
  70. // a. Assert: calendar is "iso8601".
  71. VERIFY(calendar == "iso8601"sv);
  72. // b. Let referenceISOYear be 1972 (the first ISO 8601 leap year after the epoch).
  73. static constexpr i32 reference_iso_year = 1972;
  74. // c. Let isoDate be CreateISODateRecord(referenceISOYear, result.[[Month]], result.[[Day]]).
  75. auto iso_date = create_iso_date_record(reference_iso_year, parse_result.month, parse_result.day);
  76. // d. Return ! CreateTemporalMonthDay(isoDate, calendar).
  77. return MUST(create_temporal_month_day(vm, iso_date, move(calendar)));
  78. }
  79. // 11. Let isoDate be CreateISODateRecord(result.[[Year]], result.[[Month]], result.[[Day]]).
  80. auto iso_date = create_iso_date_record(*parse_result.year, parse_result.month, parse_result.day);
  81. // 12. Set result to ISODateToFields(calendar, isoDate, MONTH-DAY).
  82. auto result = iso_date_to_fields(calendar, iso_date, DateType::MonthDay);
  83. // 13. NOTE: The following operation is called with CONSTRAIN regardless of the value of overflow, in order for the
  84. // calendar to store a canonical value in the [[Year]] field of the [[ISODate]] internal slot of the result.
  85. // 14. Set isoDate to ? CalendarMonthDayFromFields(calendar, result, CONSTRAIN).
  86. iso_date = TRY(calendar_month_day_from_fields(vm, calendar, result, Overflow::Constrain));
  87. // 15. Return ! CreateTemporalMonthDay(isoDate, calendar).
  88. return MUST(create_temporal_month_day(vm, iso_date, move(calendar)));
  89. }
  90. // 10.5.2 CreateTemporalMonthDay ( isoDate, calendar [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalmonthday
  91. ThrowCompletionOr<GC::Ref<PlainMonthDay>> create_temporal_month_day(VM& vm, ISODate iso_date, String calendar, GC::Ptr<FunctionObject> new_target)
  92. {
  93. auto& realm = *vm.current_realm();
  94. // 1. If ISODateWithinLimits(isoDate) is false, throw a RangeError exception.
  95. if (!iso_date_within_limits(iso_date))
  96. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainMonthDay);
  97. // 2. If newTarget is not present, set newTarget to %Temporal.PlainMonthDay%.
  98. if (!new_target)
  99. new_target = realm.intrinsics().temporal_plain_month_day_constructor();
  100. // 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainMonthDay.prototype%", « [[InitializedTemporalMonthDay]], [[ISODate]], [[Calendar]] »).
  101. // 4. Set object.[[ISODate]] to isoDate.
  102. // 5. Set object.[[Calendar]] to calendar.
  103. auto object = TRY(ordinary_create_from_constructor<PlainMonthDay>(vm, *new_target, &Intrinsics::temporal_plain_month_day_prototype, iso_date, move(calendar)));
  104. // 6. Return object.
  105. return object;
  106. }
  107. // 10.5.3 TemporalMonthDayToString ( monthDay, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalmonthdaytostring
  108. String temporal_month_day_to_string(PlainMonthDay const& month_day, ShowCalendar show_calendar)
  109. {
  110. // 1. Let month be ToZeroPaddedDecimalString(monthDay.[[ISODate]].[[Month]], 2).
  111. // 2. Let day be ToZeroPaddedDecimalString(monthDay.[[ISODate]].[[Day]], 2).
  112. // 3. Let result be the string-concatenation of month, the code unit 0x002D (HYPHEN-MINUS), and day.
  113. auto result = MUST(String::formatted("{:02}-{:02}", month_day.iso_date().month, month_day.iso_date().day));
  114. // 4. If showCalendar is one of ALWAYS or CRITICAL, or if monthDay.[[Calendar]] is not "iso8601", then
  115. if (show_calendar == ShowCalendar::Always || show_calendar == ShowCalendar::Critical || month_day.calendar() != "iso8601"sv) {
  116. // a. Let year be PadISOYear(monthDay.[[ISODate]].[[Year]]).
  117. auto year = pad_iso_year(month_day.iso_date().year);
  118. // b. Set result to the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and result.
  119. result = MUST(String::formatted("{}-{}", year, result));
  120. }
  121. // 5. Let calendarString be FormatCalendarAnnotation(monthDay.[[Calendar]], showCalendar).
  122. auto calendar_string = format_calendar_annotation(month_day.calendar(), show_calendar);
  123. // 6. Set result to the string-concatenation of result and calendarString.
  124. result = MUST(String::formatted("{}{}", result, calendar_string));
  125. // 7. Return result.
  126. return result;
  127. }
  128. }