LibJS: Add workaround for invalid ISODateTimes in DifferenceISODateTime

The assertions can be hit when Temporal.Duration.prototype.round / total
are provided a PlainDate at the very limits of valid date-times. Tests
were recently added to test262 which trip these assertions, thus we are
now crashing in those tests. Let's throw a RangeError instead, as this
is the behavior expected by the tests.
This commit is contained in:
Timothy Flynn 2024-12-05 11:38:33 -05:00 committed by Tim Flynn
parent 784e872851
commit 6dfa6993e5
Notes: github-actions[bot] 2024-12-05 21:06:25 +00:00
4 changed files with 24 additions and 7 deletions

View file

@ -286,13 +286,14 @@ ISODateTime round_iso_date_time(ISODateTime const& iso_date_time, u64 increment,
}
// 5.5.12 DifferenceISODateTime ( isoDateTime1, isoDateTime2, calendar, largestUnit ), https://tc39.es/proposal-temporal/#sec-temporal-differenceisodatetime
InternalDuration difference_iso_date_time(VM& vm, ISODateTime const& iso_date_time1, ISODateTime const& iso_date_time2, StringView calendar, Unit largest_unit)
ThrowCompletionOr<InternalDuration> difference_iso_date_time(VM& vm, ISODateTime const& iso_date_time1, ISODateTime const& iso_date_time2, StringView calendar, Unit largest_unit)
{
// 1. Assert: ISODateTimeWithinLimits(isoDateTime1) is true.
VERIFY(iso_date_time_within_limits(iso_date_time1));
// 2. Assert: ISODateTimeWithinLimits(isoDateTime2) is true.
VERIFY(iso_date_time_within_limits(iso_date_time2));
// FIXME: Spec issue: Rather than asserting, throw a RangeError. See:
// https://github.com/tc39/proposal-temporal/issues/3015
if (!iso_date_time_within_limits(iso_date_time1) || !iso_date_time_within_limits(iso_date_time2))
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidISODateTime);
// 3. Let timeDuration be DifferenceTime(isoDateTime1.[[Time]], isoDateTime2.[[Time]]).
auto time_duration = difference_time(iso_date_time1.time, iso_date_time2.time);
@ -344,7 +345,7 @@ ThrowCompletionOr<InternalDuration> difference_plain_date_time_with_rounding(VM&
}
// 2. Let diff be DifferenceISODateTime(isoDateTime1, isoDateTime2, calendar, largestUnit).
auto diff = difference_iso_date_time(vm, iso_date_time1, iso_date_time2, calendar, largest_unit);
auto diff = TRY(difference_iso_date_time(vm, iso_date_time1, iso_date_time2, calendar, largest_unit));
// 3. If smallestUnit is NANOSECOND and roundingIncrement = 1, return diff.
if (smallest_unit == Unit::Nanosecond && rounding_increment == 1)
@ -367,7 +368,7 @@ ThrowCompletionOr<Crypto::BigFraction> difference_plain_date_time_with_total(VM&
}
// 2. Let diff be DifferenceISODateTime(isoDateTime1, isoDateTime2, calendar, unit).
auto diff = difference_iso_date_time(vm, iso_date_time1, iso_date_time2, calendar, unit);
auto diff = TRY(difference_iso_date_time(vm, iso_date_time1, iso_date_time2, calendar, unit));
// 3. If unit is NANOSECOND, return diff.[[Time]].
if (unit == Unit::Nanosecond)

View file

@ -41,7 +41,7 @@ ThrowCompletionOr<GC::Ref<PlainDateTime>> create_temporal_date_time(VM&, ISODate
String iso_date_time_to_string(ISODateTime const&, StringView calendar, SecondsStringPrecision::Precision, ShowCalendar);
i8 compare_iso_date_time(ISODateTime const&, ISODateTime const&);
ISODateTime round_iso_date_time(ISODateTime const&, u64 increment, Unit, RoundingMode);
InternalDuration difference_iso_date_time(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit);
ThrowCompletionOr<InternalDuration> difference_iso_date_time(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit);
ThrowCompletionOr<InternalDuration> difference_plain_date_time_with_rounding(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit largest_unit, u64 rounding_increment, Unit smallest_unit, RoundingMode);
ThrowCompletionOr<Crypto::BigFraction> difference_plain_date_time_with_total(VM&, ISODateTime const&, ISODateTime const&, StringView calendar, Unit);
ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_date_time(VM&, DurationOperation, PlainDateTime const&, Value other, Value options);

View file

@ -185,6 +185,14 @@ describe("errors", () => {
}).toThrowWithMessage(RangeError, "Largest unit must not be year");
});
test("relativeTo with invalid date", () => {
const duration = new Temporal.Duration(0, 0, 0, 31);
expect(() => {
duration.round({ smallestUnit: "minutes", relativeTo: "-271821-04-19" });
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
// Spec Issue: https://github.com/tc39/proposal-temporal/issues/2124
// Spec Fix: https://github.com/tc39/proposal-temporal/commit/66f7464aaec64d3cd21fb2ec37f6502743b9a730
test("balancing calendar units with largestUnit set to 'year' and relativeTo unset throws instead of crashing", () => {

View file

@ -85,4 +85,12 @@ describe("errors", () => {
duration.total({ unit: "second" });
}).toThrowWithMessage(RangeError, "Largest unit must not be year");
});
test("relativeTo with invalid date", () => {
const duration = new Temporal.Duration(0, 0, 0, 31);
expect(() => {
duration.total({ unit: "minute", relativeTo: "-271821-04-19" });
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
});