Kaynağa Gözat

LibJS: Implement the rest of to_temporal_month_day()

Always throws at the moment, because parse_temporal_month_day_string()
is basically a stub, and parse_iso_date_time() isn't functional either.

The spec issue has been resolved though, so I figured we might as well
get one small step further :^)
Linus Groh 3 yıl önce
ebeveyn
işleme
fdffdc43fa

+ 23 - 0
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -1091,6 +1091,29 @@ ThrowCompletionOr<TemporalDuration> parse_temporal_duration_string(GlobalObject&
     return vm.throw_completion<InternalError>(global_object, ErrorType::NotImplemented, "ParseTemporalDurationString");
 }
 
+// 13.41 ParseTemporalMonthDayString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalmonthdaystring
+ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject& global_object, String const& iso_string)
+{
+    // 1. Assert: Type(isoString) is String.
+
+    // 2. If isoString does not satisfy the syntax of a TemporalMonthDayString (see 13.33), then
+    // a. Throw a RangeError exception.
+    // TODO
+
+    // 3. Let result be ? ParseISODateTime(isoString).
+    auto result = TRY(parse_iso_date_time(global_object, iso_string));
+
+    // 4. Let year be result.[[Year]].
+    Optional<i32> year = result.year;
+
+    // 5. If no part of isoString is produced by the DateYear production, then
+    // a. Set year to undefined.
+    // TODO (this is the case when TemporalMonthDayString is a DateSpecMonthDay)
+
+    // 6. Return the Record { [[Year]]: year, [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[Calendar]]: result.[[Calendar]] }.
+    return TemporalMonthDay { .year = year, .month = result.month, .day = result.day, .calendar = move(result.calendar) };
+}
+
 // 13.43 ParseTemporalTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimestring
 ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject& global_object, [[maybe_unused]] String const& iso_string)
 {

+ 8 - 0
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h

@@ -78,6 +78,13 @@ struct TemporalYearMonth {
     Optional<String> calendar = {};
 };
 
+struct TemporalMonthDay {
+    Optional<i32> year;
+    u8 month;
+    u8 day;
+    Optional<String> calendar = {};
+};
+
 struct TemporalZonedDateTime {
     ISODateTime date_time;
     TemporalTimeZone time_zone;
@@ -123,6 +130,7 @@ ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject&, String c
 ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject&, String const& iso_string);
 ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject&, String const& iso_string);
 ThrowCompletionOr<TemporalDuration> parse_temporal_duration_string(GlobalObject&, String const& iso_string);
+ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject&, String const& iso_string);
 ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject&, String const& iso_string);
 ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String const& iso_string);
 ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject&, String const& iso_string);

+ 26 - 4
Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp

@@ -118,10 +118,32 @@ ThrowCompletionOr<PlainMonthDay*> to_temporal_month_day(GlobalObject& global_obj
         return month_day_from_fields(global_object, *calendar, *fields, options);
     }
 
-    // FIXME: The spec has an issue in this part which makes it unimplementable, namely:
-    //        - ParseTemporalMonthDayString doesn't return a [[Calendar]] field, which is required in step 7.
-    //        This is a known issue, see https://github.com/tc39/proposal-temporal/issues/1502
-    return vm.throw_completion<InternalError>(global_object, ErrorType::NotImplemented, "ToTemporalMonthDay");
+    // 4. Perform ? ToTemporalOverflow(options).
+    TRY(to_temporal_overflow(global_object, *options));
+
+    // 5. Let string be ? ToString(item).
+    auto string = TRY(item.to_string(global_object));
+
+    // 6. Let result be ? ParseTemporalMonthDayString(string).
+    auto result = TRY(parse_temporal_month_day_string(global_object, string));
+
+    // 7. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]).
+    auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, result.calendar.has_value() ? js_string(vm, move(*result.calendar)) : js_undefined()));
+
+    // 8. If result.[[Year]] is undefined, then
+    if (!result.year.has_value()) {
+        // a. Return ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, referenceISOYear).
+        return TRY(create_temporal_month_day(global_object, result.month, result.day, *calendar, reference_iso_year));
+    }
+
+    // 9. Set result to ? CreateTemporalMonthDay(result.[[Month]], result.[[Day]], calendar, referenceISOYear).
+    auto* plain_month_day = TRY(create_temporal_month_day(global_object, result.month, result.day, *calendar, reference_iso_year));
+
+    // 10. Let canonicalMonthDayOptions be ! OrdinaryObjectCreate(null).
+    auto* canonical_month_day_options = Object::create(global_object, nullptr);
+
+    // 11. Return ? MonthDayFromFields(calendar, result, canonicalMonthDayOptions).
+    return TRY(month_day_from_fields(global_object, *calendar, *plain_month_day, canonical_month_day_options));
 }
 
 // 10.5.2 CreateTemporalMonthDay ( isoMonth, isoDay, calendar, referenceISOYear [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalmonthday