1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

LibJS: Inline ConstrainISOYearMonth into RegulateISOYearMonth

This is an editorial change in the Temporal spec.

See: c97932b
This commit is contained in:
Linus Groh 2022-06-14 23:48:08 +01:00
parent aaa9524a52
commit 07041498f8
2 changed files with 9 additions and 21 deletions

View file

@ -103,8 +103,11 @@ ThrowCompletionOr<ISOYearMonth> regulate_iso_year_month(GlobalObject& global_obj
if (!AK::is_within_range<i32>(year)) if (!AK::is_within_range<i32>(year))
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth); return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainYearMonth);
// a. Return ! ConstrainISOYearMonth(year, month). // a. Set month to the result of clamping month between 1 and 12.
return constrain_iso_year_month(year, month); 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, // 4. Else,
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 }; 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 // 9.5.6 CreateTemporalYearMonth ( isoYear, isoMonth, calendar, referenceISODay [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalyearmonth
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
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) 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(); auto& vm = global_object.vm();
@ -228,7 +217,7 @@ ThrowCompletionOr<PlainYearMonth*> create_temporal_year_month(GlobalObject& glob
return object; 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) ThrowCompletionOr<String> temporal_year_month_to_string(GlobalObject& global_object, PlainYearMonth& year_month, StringView show_calendar)
{ {
// 1. Assert: Type(yearMonth) is Object. // 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); 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) 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(); 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)); 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) 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(); auto& vm = global_object.vm();

View file

@ -45,7 +45,6 @@ ThrowCompletionOr<ISOYearMonth> regulate_iso_year_month(GlobalObject&, double ye
bool is_valid_iso_month(u8 month); bool is_valid_iso_month(u8 month);
bool iso_year_month_within_limits(i32 year, u8 month); bool iso_year_month_within_limits(i32 year, u8 month);
ISOYearMonth balance_iso_year_month(double year, double 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<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<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); ThrowCompletionOr<Duration*> difference_temporal_plain_year_month(GlobalObject&, DifferenceOperation, PlainYearMonth&, Value other, Value options);