commit 2c60938c4a62298a29b06e6da3d802439c4dff92
parent ba872e97641321bf4609d5f41086e0800d0b6db6
Author: Matthew Gaudet <mgaudet@mozilla.com>
Date: Wed, 10 Dec 2025 22:00:15 +0000
Bug 2004851 - Reorder steps in Temporal Duration method to ensure error handling happens after fastpath for equal DateTimes r=anba
More testing will come in https://github.com/tc39/test262/pull/4772
Differential Revision: https://phabricator.services.mozilla.com/D275679
Diffstat:
6 files changed, 36 insertions(+), 24 deletions(-)
diff --git a/js/src/builtin/temporal/Duration.cpp b/js/src/builtin/temporal/Duration.cpp
@@ -3732,14 +3732,6 @@ static bool Duration_round(JSContext* cx, const CallArgs& args) {
// Step 28.g.
auto targetDateTime = ISODateTime{targetDate, targetTime.time};
- // DifferencePlainDateTimeWithRounding, step 2.
- if (!ISODateTimeWithinLimits(isoDateTime) ||
- !ISODateTimeWithinLimits(targetDateTime)) {
- JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
- JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID);
- return false;
- }
-
// Step 28.h.
if (!DifferencePlainDateTimeWithRounding(cx, isoDateTime, targetDateTime,
calendar,
@@ -3954,14 +3946,6 @@ static bool Duration_total(JSContext* cx, const CallArgs& args) {
// Step 13.g.
auto targetDateTime = ISODateTime{targetDate, targetTime.time};
- // DifferencePlainDateTimeWithTotal, step 2.
- if (!ISODateTimeWithinLimits(isoDateTime) ||
- !ISODateTimeWithinLimits(targetDateTime)) {
- JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
- JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID);
- return false;
- }
-
// Step 13.h.
if (!DifferencePlainDateTimeWithTotal(cx, isoDateTime, targetDateTime,
calendar, unit, &total)) {
diff --git a/js/src/builtin/temporal/PlainDateTime.cpp b/js/src/builtin/temporal/PlainDateTime.cpp
@@ -574,9 +574,6 @@ bool js::temporal::DifferencePlainDateTimeWithRounding(
JSContext* cx, const ISODateTime& isoDateTime1,
const ISODateTime& isoDateTime2, Handle<CalendarValue> calendar,
const DifferenceSettings& settings, InternalDuration* result) {
- MOZ_ASSERT(ISODateTimeWithinLimits(isoDateTime1));
- MOZ_ASSERT(ISODateTimeWithinLimits(isoDateTime2));
-
// Step 1.
if (isoDateTime1 == isoDateTime2) {
// Step 1.a.
@@ -584,7 +581,13 @@ bool js::temporal::DifferencePlainDateTimeWithRounding(
return true;
}
- // Step 2. (Not applicable in our implementation.)
+ // Step 2.
+ if (!ISODateTimeWithinLimits(isoDateTime1) ||
+ !ISODateTimeWithinLimits(isoDateTime2)) {
+ JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
+ JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID);
+ return false;
+ }
// Step 3.
InternalDuration diff;
@@ -622,9 +625,6 @@ bool js::temporal::DifferencePlainDateTimeWithTotal(
JSContext* cx, const ISODateTime& isoDateTime1,
const ISODateTime& isoDateTime2, Handle<CalendarValue> calendar,
TemporalUnit unit, double* result) {
- MOZ_ASSERT(ISODateTimeWithinLimits(isoDateTime1));
- MOZ_ASSERT(ISODateTimeWithinLimits(isoDateTime2));
-
// Step 1.
if (isoDateTime1 == isoDateTime2) {
// Step 1.a.
@@ -632,7 +632,13 @@ bool js::temporal::DifferencePlainDateTimeWithTotal(
return true;
}
- // Step 2. (Not applicable in our implementation.)
+ // Step 2.
+ if (!ISODateTimeWithinLimits(isoDateTime1) ||
+ !ISODateTimeWithinLimits(isoDateTime2)) {
+ JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
+ JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID);
+ return false;
+ }
// Step 3.
InternalDuration diff;
diff --git a/js/src/tests/non262/Temporal/Duration/browser.js b/js/src/tests/non262/Temporal/Duration/browser.js
diff --git a/js/src/tests/non262/Temporal/Duration/round-fastpath.js b/js/src/tests/non262/Temporal/Duration/round-fastpath.js
@@ -0,0 +1,11 @@
+// |reftest| skip-if(!this.hasOwnProperty("Temporal")||!this.hasOwnProperty("Intl"))
+
+assertEq(
+ new Temporal.Duration(0).round({ smallestUnit: 'years', relativeTo: '-271821-04-19' }).blank,
+ true,
+ "Support fast path for invalid isodate");
+
+
+if (typeof reportCompare === "function")
+ reportCompare(true, true);
+
diff --git a/js/src/tests/non262/Temporal/Duration/shell.js b/js/src/tests/non262/Temporal/Duration/shell.js
diff --git a/js/src/tests/non262/Temporal/Duration/total-fastpath.js b/js/src/tests/non262/Temporal/Duration/total-fastpath.js
@@ -0,0 +1,11 @@
+// |reftest| skip-if(!this.hasOwnProperty("Temporal")||!this.hasOwnProperty("Intl"))
+
+assertEq(
+ new Temporal.Duration(0).total({ unit: 'years', relativeTo: '-271821-04-19' }),
+ 0,
+ "Support fast path for invalid isodate");
+
+
+if (typeof reportCompare === "function")
+ reportCompare(true, true);
+