From 7452ad2baf6d3b53c8d8a824dda4a0190a7e022d Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Wed, 20 Sep 2023 11:17:16 -0700 Subject: [PATCH 1/2] Editorial: Make early returns from UnbalanceDateDurationRelative infallible If we return before we do any calculations, then the _months_, _weeks_, and _days_ quantities come directly from a previous Duration Record, so they cannot cause CreateDateDurationRecord to fail. Reverse the sense of the check in the _largestUnit_ = *"day"* clause so it also becomes an early return and infallible. --- spec/duration.html | 48 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/spec/duration.html b/spec/duration.html index d654773334..0faa3aa951 100644 --- a/spec/duration.html +++ b/spec/duration.html @@ -1377,7 +1377,7 @@

1. Else, 1. Let _calendar_ be *undefined*. 1. If _largestUnit_ is *"month"*, then - 1. If _years_ = 0, return ? CreateDateDurationRecord(0, _months_, _weeks_, _days_). + 1. If _years_ = 0, return ! CreateDateDurationRecord(0, _months_, _weeks_, _days_). 1. If _calendar_ is *undefined*, then 1. Throw a *RangeError* exception. 1. If _calendar_ is an Object, then @@ -1396,7 +1396,7 @@

1. Set _years_ to _years_ - _sign_. 1. Set _months_ to _months_ + _oneYearMonths_. 1. Else if _largestUnit_ is *"week"*, then - 1. If _years_ = 0 and _months_ = 0, return ? CreateDateDurationRecord(0, 0, _weeks_, _days_). + 1. If _years_ = 0 and _months_ = 0, return ! CreateDateDurationRecord(0, 0, _weeks_, _days_). 1. If _calendar_ is *undefined*, then 1. Throw a *RangeError* exception. 1. If _calendar_ is an Object, then @@ -1414,28 +1414,28 @@

1. Set _days_ to _days_ + _moveResult_.[[Days]]. 1. Set _months_ to _months_ - _sign_. 1. Else, - 1. If _years_ ≠ 0, or _months_ ≠ 0, or _weeks_ ≠ zero, then - 1. If _calendar_ is *undefined*, then - 1. Throw a *RangeError* exception. - 1. If _calendar_ is an Object, then - 1. Let _dateAdd_ be ? GetMethod(_calendar_, *"dateAdd"*). - 1. Else, - 1. Let _dateAdd_ be ~unused~. - 1. Repeat, while _years_ ≠ 0, - 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneYear_, _dateAdd_). - 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. - 1. Set _days_ to _days_ + _moveResult_.[[Days]]. - 1. Set _years_ to _years_ - _sign_. - 1. Repeat, while _months_ ≠ 0, - 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneMonth_, _dateAdd_). - 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. - 1. Set _days_ to _days_ +_moveResult_.[[Days]]. - 1. Set _months_ to _months_ - _sign_. - 1. Repeat, while _weeks_ ≠ 0, - 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneWeek_, _dateAdd_). - 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. - 1. Set _days_ to _days_ + _moveResult_.[[Days]]. - 1. Set _weeks_ to _weeks_ - _sign_. + 1. If _years_ = 0, and _months_ = 0, and _weeks_ = 0, return ! CreateDateDurationRecord(0, 0, 0, _days_). + 1. If _calendar_ is *undefined*, then + 1. Throw a *RangeError* exception. + 1. If _calendar_ is an Object, then + 1. Let _dateAdd_ be ? GetMethod(_calendar_, *"dateAdd"*). + 1. Else, + 1. Let _dateAdd_ be ~unused~. + 1. Repeat, while _years_ ≠ 0, + 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneYear_, _dateAdd_). + 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. + 1. Set _days_ to _days_ + _moveResult_.[[Days]]. + 1. Set _years_ to _years_ - _sign_. + 1. Repeat, while _months_ ≠ 0, + 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneMonth_, _dateAdd_). + 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. + 1. Set _days_ to _days_ +_moveResult_.[[Days]]. + 1. Set _months_ to _months_ - _sign_. + 1. Repeat, while _weeks_ ≠ 0, + 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneWeek_, _dateAdd_). + 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. + 1. Set _days_ to _days_ + _moveResult_.[[Days]]. + 1. Set _weeks_ to _weeks_ - _sign_. 1. Return ? CreateDateDurationRecord(_years_, _months_, _weeks_, _days_). From e2e0e18ac577dafdaf93b766223b11bb02d60579 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Wed, 20 Sep 2023 11:21:36 -0700 Subject: [PATCH 2/2] Editorial: Make returns in UnbalanceDateDurationRelative clearer Adding returns at the end of each largestUnit case makes it clearer that some duration units are definitely zero when we return. --- spec/duration.html | 50 ++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/spec/duration.html b/spec/duration.html index 0faa3aa951..4115b1c819 100644 --- a/spec/duration.html +++ b/spec/duration.html @@ -1395,7 +1395,8 @@

1. Set _plainRelativeTo_ to _newRelativeTo_. 1. Set _years_ to _years_ - _sign_. 1. Set _months_ to _months_ + _oneYearMonths_. - 1. Else if _largestUnit_ is *"week"*, then + 1. Return ? CreateDateDurationRecord(0, _months_, _weeks_, _days_). + 1. If _largestUnit_ is *"week"*, then 1. If _years_ = 0 and _months_ = 0, return ! CreateDateDurationRecord(0, 0, _weeks_, _days_). 1. If _calendar_ is *undefined*, then 1. Throw a *RangeError* exception. @@ -1413,30 +1414,31 @@

1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. 1. Set _days_ to _days_ + _moveResult_.[[Days]]. 1. Set _months_ to _months_ - _sign_. + 1. Return ? CreateDateDurationRecord(0, 0, _weeks_, _days_). + 1. Assert: _largestUnit_ is *"day"*. + 1. If _years_ = 0, and _months_ = 0, and _weeks_ = 0, return ! CreateDateDurationRecord(0, 0, 0, _days_). + 1. If _calendar_ is *undefined*, then + 1. Throw a *RangeError* exception. + 1. If _calendar_ is an Object, then + 1. Let _dateAdd_ be ? GetMethod(_calendar_, *"dateAdd"*). 1. Else, - 1. If _years_ = 0, and _months_ = 0, and _weeks_ = 0, return ! CreateDateDurationRecord(0, 0, 0, _days_). - 1. If _calendar_ is *undefined*, then - 1. Throw a *RangeError* exception. - 1. If _calendar_ is an Object, then - 1. Let _dateAdd_ be ? GetMethod(_calendar_, *"dateAdd"*). - 1. Else, - 1. Let _dateAdd_ be ~unused~. - 1. Repeat, while _years_ ≠ 0, - 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneYear_, _dateAdd_). - 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. - 1. Set _days_ to _days_ + _moveResult_.[[Days]]. - 1. Set _years_ to _years_ - _sign_. - 1. Repeat, while _months_ ≠ 0, - 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneMonth_, _dateAdd_). - 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. - 1. Set _days_ to _days_ +_moveResult_.[[Days]]. - 1. Set _months_ to _months_ - _sign_. - 1. Repeat, while _weeks_ ≠ 0, - 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneWeek_, _dateAdd_). - 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. - 1. Set _days_ to _days_ + _moveResult_.[[Days]]. - 1. Set _weeks_ to _weeks_ - _sign_. - 1. Return ? CreateDateDurationRecord(_years_, _months_, _weeks_, _days_). + 1. Let _dateAdd_ be ~unused~. + 1. Repeat, while _years_ ≠ 0, + 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneYear_, _dateAdd_). + 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. + 1. Set _days_ to _days_ + _moveResult_.[[Days]]. + 1. Set _years_ to _years_ - _sign_. + 1. Repeat, while _months_ ≠ 0, + 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneMonth_, _dateAdd_). + 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. + 1. Set _days_ to _days_ +_moveResult_.[[Days]]. + 1. Set _months_ to _months_ - _sign_. + 1. Repeat, while _weeks_ ≠ 0, + 1. Let _moveResult_ be ? MoveRelativeDate(_calendar_, _plainRelativeTo_, _oneWeek_, _dateAdd_). + 1. Set _plainRelativeTo_ to _moveResult_.[[RelativeTo]]. + 1. Set _days_ to _days_ + _moveResult_.[[Days]]. + 1. Set _weeks_ to _weeks_ - _sign_. + 1. Return ? CreateDateDurationRecord(0, 0, 0, _days_).