Sfoglia il codice sorgente

LibJS: Inline ConstrainISOYearMonth into RegulateISOYearMonth

This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/c97932b
Linus Groh 3 anni fa
parent
commit
07041498f8

+ 9 - 20
Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp

@@ -103,8 +103,11 @@ ThrowCompletionOr<ISOYearMonth> regulate_iso_year_month(GlobalObject& global_obj
         if (!AK::is_within_range<i32>(year))
             return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
 
-        // a. Return ! ConstrainISOYearMonth(year, month).
-        return constrain_iso_year_month(year, month);
+        // a. Set month to the result of clamping month between 1 and 12.
+        month = clamp(month, 1, 12);
+
+        // b. Return the Record { [[Year]]: year, [[Month]]: month }.
+        return ISOYearMonth { .year = static_cast<i32>(year), .month = static_cast<u8>(month), .reference_iso_day = 0 };
     }
     // 4. Else,
     else {
@@ -183,21 +186,7 @@ ISOYearMonth balance_iso_year_month(double year, double month)
     return ISOYearMonth { .year = static_cast<i32>(year), .month = static_cast<u8>(month), .reference_iso_day = 0 };
 }
 
-// 9.5.6 ConstrainISOYearMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-constrainisoyearmonth
-ISOYearMonth constrain_iso_year_month(double year, double month)
-{
-    // 1. Assert: year and month are integers.
-    VERIFY(year == trunc(year) && month == trunc(month));
-
-    // 2. Set month to the result of clamping month between 1 and 12.
-    month = clamp(month, 1, 12);
-
-    // 3. Return the Record { [[Year]]: year, [[Month]]: month }.
-    // NOTE: `year` is known to be in the i32 range.
-    return ISOYearMonth { .year = static_cast<i32>(year), .month = static_cast<u8>(month), .reference_iso_day = 0 };
-}
-
-// 9.5.7 CreateTemporalYearMonth ( isoYear, isoMonth, calendar, referenceISODay [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalyearmonth
+// 9.5.6 CreateTemporalYearMonth ( isoYear, isoMonth, calendar, referenceISODay [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalyearmonth
 ThrowCompletionOr<PlainYearMonth*> create_temporal_year_month(GlobalObject& global_object, i32 iso_year, u8 iso_month, Object& calendar, u8 reference_iso_day, FunctionObject const* new_target)
 {
     auto& vm = global_object.vm();
@@ -228,7 +217,7 @@ ThrowCompletionOr<PlainYearMonth*> create_temporal_year_month(GlobalObject& glob
     return object;
 }
 
-// 9.5.8 TemporalYearMonthToString ( yearMonth, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalyearmonthtostring
+// 9.5.7 TemporalYearMonthToString ( yearMonth, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalyearmonthtostring
 ThrowCompletionOr<String> temporal_year_month_to_string(GlobalObject& global_object, PlainYearMonth& year_month, StringView show_calendar)
 {
     // 1. Assert: Type(yearMonth) is Object.
@@ -257,7 +246,7 @@ ThrowCompletionOr<String> temporal_year_month_to_string(GlobalObject& global_obj
     return String::formatted("{}{}", result, calendar_string);
 }
 
-// 9.5.9 DifferenceTemporalPlainYearMonth ( operation, yearMonth, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplainyearmonth
+// 9.5.8 DifferenceTemporalPlainYearMonth ( operation, yearMonth, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplainyearmonth
 ThrowCompletionOr<Duration*> difference_temporal_plain_year_month(GlobalObject& global_object, DifferenceOperation operation, PlainYearMonth& year_month, Value other_value, Value options_value)
 {
     auto& vm = global_object.vm();
@@ -351,7 +340,7 @@ ThrowCompletionOr<Duration*> difference_temporal_plain_year_month(GlobalObject&
     return MUST(create_temporal_duration(global_object, sign * result.years, sign * result.months, 0, 0, 0, 0, 0, 0, 0, 0));
 }
 
-// 9.5.10 AddDurationToOrSubtractDurationFromPlainYearMonth ( operation, yearMonth, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-addtemporalplainyearmonth
+// 9.5.9 AddDurationToOrSubtractDurationFromPlainYearMonth ( operation, yearMonth, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-addtemporalplainyearmonth
 ThrowCompletionOr<PlainYearMonth*> add_duration_to_or_subtract_duration_from_plain_year_month(GlobalObject& global_object, ArithmeticOperation operation, PlainYearMonth& year_month, Value temporal_duration_like, Value options_value)
 {
     auto& vm = global_object.vm();

+ 0 - 1
Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.h

@@ -45,7 +45,6 @@ ThrowCompletionOr<ISOYearMonth> regulate_iso_year_month(GlobalObject&, double ye
 bool is_valid_iso_month(u8 month);
 bool iso_year_month_within_limits(i32 year, u8 month);
 ISOYearMonth balance_iso_year_month(double year, double month);
-ISOYearMonth constrain_iso_year_month(double year, double month);
 ThrowCompletionOr<PlainYearMonth*> create_temporal_year_month(GlobalObject&, i32 iso_year, u8 iso_month, Object& calendar, u8 reference_iso_day, FunctionObject const* new_target = nullptr);
 ThrowCompletionOr<String> temporal_year_month_to_string(GlobalObject&, PlainYearMonth&, StringView show_calendar);
 ThrowCompletionOr<Duration*> difference_temporal_plain_year_month(GlobalObject&, DifferenceOperation, PlainYearMonth&, Value other, Value options);