mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 17:06:31 +00:00
LibJS: Remove the ConstrainToRange AO
This is an editorial change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/537b3e6
This commit is contained in:
parent
1521c5f55c
commit
2ad69d789b
|
@ -964,15 +964,6 @@ String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u
|
|||
return String::formatted("{}.{}", seconds_string, fraction_string);
|
||||
}
|
||||
|
||||
// 13.27 ConstrainToRange ( x, minimum, maximum ), https://tc39.es/proposal-temporal/#sec-temporal-constraintorange
|
||||
double constrain_to_range(double x, double minimum, double maximum)
|
||||
{
|
||||
// 1. Assert: x, minimum and maximum are mathematical values.
|
||||
|
||||
// 2. Return min(max(x, minimum), maximum).
|
||||
return min(max(x, minimum), maximum);
|
||||
}
|
||||
|
||||
// NOTE: We have two variants of this function, one using doubles and one using BigInts - most of the time
|
||||
// doubles will be fine, but take care to choose the right one. The spec is not very clear about this, as
|
||||
// it uses mathematical values which can be arbitrarily (but not infinitely) large.
|
||||
|
@ -1015,7 +1006,7 @@ i64 round_number_to_increment(double x, u64 increment, StringView rounding_mode)
|
|||
return (i64)rounded * (i64)increment;
|
||||
}
|
||||
|
||||
// 13.30 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement
|
||||
// 13.29 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement
|
||||
BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x, u64 increment, StringView rounding_mode)
|
||||
{
|
||||
auto& heap = global_object.heap();
|
||||
|
@ -1069,7 +1060,7 @@ BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x,
|
|||
return js_bigint(heap, rounded.multiplied_by(increment_big_int));
|
||||
}
|
||||
|
||||
// 13.32 ParseISODateTime ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parseisodatetime
|
||||
// 13.31 ParseISODateTime ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parseisodatetime
|
||||
ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject& global_object, ParseResult const& parse_result)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1194,7 +1185,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject& global_object,
|
|||
return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = Optional<String>(move(calendar_part)) };
|
||||
}
|
||||
|
||||
// 13.33 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring
|
||||
// 13.32 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring
|
||||
ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1230,7 +1221,7 @@ ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(GlobalObject& g
|
|||
return TemporalInstant { .year = result.year, .month = result.month, .day = result.day, .hour = result.hour, .minute = result.minute, .second = result.second, .millisecond = result.millisecond, .microsecond = result.microsecond, .nanosecond = result.nanosecond, .time_zone_offset = move(offset_string) };
|
||||
}
|
||||
|
||||
// 13.34 ParseTemporalZonedDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalzoneddatetimestring
|
||||
// 13.33 ParseTemporalZonedDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalzoneddatetimestring
|
||||
ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_zoned_date_time_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1256,7 +1247,7 @@ ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_zoned_date_time_string(G
|
|||
return TemporalZonedDateTime { .date_time = move(result), .time_zone = move(time_zone_result) };
|
||||
}
|
||||
|
||||
// 13.35 ParseTemporalCalendarString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalcalendarstring
|
||||
// 13.34 ParseTemporalCalendarString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalcalendarstring
|
||||
ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1283,7 +1274,7 @@ ThrowCompletionOr<String> parse_temporal_calendar_string(GlobalObject& global_ob
|
|||
return id_part.value();
|
||||
}
|
||||
|
||||
// 13.36 ParseTemporalDateString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatestring
|
||||
// 13.35 ParseTemporalDateString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatestring
|
||||
ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1310,7 +1301,7 @@ ThrowCompletionOr<TemporalDate> parse_temporal_date_string(GlobalObject& global_
|
|||
return TemporalDate { .year = result.year, .month = result.month, .day = result.day, .calendar = move(result.calendar) };
|
||||
}
|
||||
|
||||
// 13.37 ParseTemporalDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatetimestring
|
||||
// 13.36 ParseTemporalDateTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldatetimestring
|
||||
ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1334,7 +1325,7 @@ ThrowCompletionOr<ISODateTime> parse_temporal_date_time_string(GlobalObject& glo
|
|||
return parse_iso_date_time(global_object, *parse_result);
|
||||
}
|
||||
|
||||
// 13.38 ParseTemporalDurationString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldurationstring
|
||||
// 13.37 ParseTemporalDurationString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaldurationstring
|
||||
ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1474,7 +1465,7 @@ ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(GlobalObject& g
|
|||
return create_duration_record(global_object, years * factor, months * factor, weeks * factor, days * factor, hours * factor, floor(minutes) * factor, floor(seconds) * factor, floor(milliseconds) * factor, floor(microseconds) * factor, floor(nanoseconds) * factor);
|
||||
}
|
||||
|
||||
// 13.39 ParseTemporalMonthDayString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalmonthdaystring
|
||||
// 13.38 ParseTemporalMonthDayString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalmonthdaystring
|
||||
ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1510,7 +1501,7 @@ ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(GlobalObject
|
|||
return TemporalMonthDay { .year = year, .month = result.month, .day = result.day, .calendar = move(result.calendar) };
|
||||
}
|
||||
|
||||
// 13.40 ParseTemporalRelativeToString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalrelativetostring
|
||||
// 13.39 ParseTemporalRelativeToString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalrelativetostring
|
||||
ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_relative_to_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1559,7 +1550,7 @@ ThrowCompletionOr<TemporalZonedDateTime> parse_temporal_relative_to_string(Globa
|
|||
return TemporalZonedDateTime { .date_time = move(result), .time_zone = { .z = z, .offset_string = move(offset_string), .name = move(time_zone) } };
|
||||
}
|
||||
|
||||
// 13.41 ParseTemporalTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimestring
|
||||
// 13.40 ParseTemporalTimeString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimestring
|
||||
ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1590,7 +1581,7 @@ ThrowCompletionOr<TemporalTime> parse_temporal_time_string(GlobalObject& global_
|
|||
return TemporalTime { .hour = result.hour, .minute = result.minute, .second = result.second, .millisecond = result.millisecond, .microsecond = result.microsecond, .nanosecond = result.nanosecond, .calendar = move(result.calendar) };
|
||||
}
|
||||
|
||||
// 13.42 ParseTemporalTimeZoneString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezonestring
|
||||
// 13.41 ParseTemporalTimeZoneString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezonestring
|
||||
ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1633,7 +1624,7 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
|
|||
return TemporalTimeZone { .z = false, .offset_string = Optional<String>(move(offset_string)), .name = Optional<String>(move(name)) };
|
||||
}
|
||||
|
||||
// 13.43 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring
|
||||
// 13.42 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring
|
||||
ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject& global_object, String const& iso_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1660,7 +1651,7 @@ ThrowCompletionOr<TemporalYearMonth> parse_temporal_year_month_string(GlobalObje
|
|||
return TemporalYearMonth { .year = result.year, .month = result.month, .day = result.day, .calendar = move(result.calendar) };
|
||||
}
|
||||
|
||||
// 13.44 ToPositiveInteger ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-topositiveinteger
|
||||
// 13.43 ToPositiveInteger ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-topositiveinteger
|
||||
ThrowCompletionOr<double> to_positive_integer(GlobalObject& global_object, Value argument)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1678,7 +1669,7 @@ ThrowCompletionOr<double> to_positive_integer(GlobalObject& global_object, Value
|
|||
return integer;
|
||||
}
|
||||
|
||||
// 13.47 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields
|
||||
// 13.46 PrepareTemporalFields ( fields, fieldNames, requiredFields ), https://tc39.es/proposal-temporal/#sec-temporal-preparetemporalfields
|
||||
ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject& global_object, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
@ -1729,7 +1720,7 @@ ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject& global_object,
|
|||
return result;
|
||||
}
|
||||
|
||||
// 13.48 PreparePartialTemporalFields ( fields, fieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-preparepartialtemporalfields
|
||||
// 13.47 PreparePartialTemporalFields ( fields, fieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-preparepartialtemporalfields
|
||||
ThrowCompletionOr<Object*> prepare_partial_temporal_fields(GlobalObject& global_object, Object const& fields, Vector<String> const& field_names)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
|
|
@ -144,7 +144,7 @@ ThrowCompletionOr<double> to_positive_integer(GlobalObject&, Value argument);
|
|||
ThrowCompletionOr<Object*> prepare_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields);
|
||||
ThrowCompletionOr<Object*> prepare_partial_temporal_fields(GlobalObject&, Object const& fields, Vector<String> const& field_names);
|
||||
|
||||
// 13.45 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
|
||||
// 13.44 ToIntegerThrowOnInfinity ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerthrowoninfinity
|
||||
template<typename... Args>
|
||||
ThrowCompletionOr<double> to_integer_throw_on_infinity(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args)
|
||||
{
|
||||
|
@ -163,7 +163,7 @@ ThrowCompletionOr<double> to_integer_throw_on_infinity(GlobalObject& global_obje
|
|||
return integer;
|
||||
}
|
||||
|
||||
// 13.46 ToIntegerWithoutRounding ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerwithoutrounding
|
||||
// 13.45 ToIntegerWithoutRounding ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-tointegerwithoutrounding
|
||||
template<typename... Args>
|
||||
ThrowCompletionOr<double> to_integer_without_rounding(GlobalObject& global_object, Value argument, ErrorType error_type, Args... args)
|
||||
{
|
||||
|
|
|
@ -363,12 +363,16 @@ ThrowCompletionOr<ISODate> regulate_iso_date(GlobalObject& global_object, double
|
|||
|
||||
auto y = static_cast<i32>(year);
|
||||
|
||||
// a. Set month to ! ConstrainToRange(month, 1, 12).
|
||||
month = constrain_to_range(month, 1, 12);
|
||||
// b. Set day to ! ConstrainToRange(day, 1, ! ISODaysInMonth(year, month)).
|
||||
day = constrain_to_range(day, 1, iso_days_in_month(y, month));
|
||||
// a. Set month to the result of clamping month between 1 and 12.
|
||||
month = clamp(month, 1, 12);
|
||||
|
||||
// c. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
|
||||
// b. Let daysInMonth be ! ISODaysInMonth(year, month).
|
||||
auto days_in_month = iso_days_in_month(y, (u8)month);
|
||||
|
||||
// c. Set day to the result of clamping day between 1 and daysInMonth.
|
||||
day = clamp(day, 1, days_in_month);
|
||||
|
||||
// d. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
|
||||
return ISODate { .year = y, .month = static_cast<u8>(month), .day = static_cast<u8>(day) };
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
|
|
|
@ -328,23 +328,23 @@ TemporalTime constrain_time(double hour, double minute, double second, double mi
|
|||
{
|
||||
// 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
|
||||
|
||||
// 2. Set hour to ! ConstrainToRange(hour, 0, 23).
|
||||
hour = constrain_to_range(hour, 0, 23);
|
||||
// 2. Set hour to the result of clamping hour between 0 and 23.
|
||||
hour = clamp(hour, 0, 23);
|
||||
|
||||
// 3. Set minute to ! ConstrainToRange(minute, 0, 59).
|
||||
minute = constrain_to_range(minute, 0, 59);
|
||||
// 3. Set minute to the result of clamping minute between 0 and 59.
|
||||
minute = clamp(minute, 0, 59);
|
||||
|
||||
// 4. Set second to ! ConstrainToRange(second, 0, 59).
|
||||
second = constrain_to_range(second, 0, 59);
|
||||
// 4. Set second to the result of clamping second between 0 and 59.
|
||||
second = clamp(second, 0, 59);
|
||||
|
||||
// 5. Set millisecond to ! ConstrainToRange(millisecond, 0, 999).
|
||||
millisecond = constrain_to_range(millisecond, 0, 999);
|
||||
// 5. Set millisecond to the result of clamping millisecond between 0 and 999.
|
||||
millisecond = clamp(millisecond, 0, 999);
|
||||
|
||||
// 6. Set microsecond to ! ConstrainToRange(microsecond, 0, 999).
|
||||
microsecond = constrain_to_range(microsecond, 0, 999);
|
||||
// 6. Set microsecond to the result of clamping microsecond between 0 and 999.
|
||||
microsecond = clamp(microsecond, 0, 999);
|
||||
|
||||
// 7. Set nanosecond to ! ConstrainToRange(nanosecond, 0, 999).
|
||||
nanosecond = constrain_to_range(nanosecond, 0, 999);
|
||||
// 7. Set nanosecond to the result of clamping nanosecond between 0 and 999.
|
||||
nanosecond = clamp(nanosecond, 0, 999);
|
||||
|
||||
// 8. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
|
||||
return TemporalTime { .hour = static_cast<u8>(hour), .minute = static_cast<u8>(minute), .second = static_cast<u8>(second), .millisecond = static_cast<u16>(millisecond), .microsecond = static_cast<u16>(microsecond), .nanosecond = static_cast<u16>(nanosecond) };
|
||||
|
|
|
@ -192,8 +192,8 @@ 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 ! ConstrainToRange(month, 1, 12).
|
||||
month = constrain_to_range(month, 1, 12);
|
||||
// 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.
|
||||
|
|
Loading…
Reference in a new issue