Skip to content

Commit

Permalink
Editorial: Refactor add/subtract to common AOs (#2166)
Browse files Browse the repository at this point in the history
Most of the operations between add and subtract are the same except negative value. Refactor to shared AOs between them with a sign which either 1 or -1.
  • Loading branch information
FrankYFTang authored May 5, 2022
1 parent ce5c223 commit 2f96efc
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 93 deletions.
35 changes: 25 additions & 10 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,7 @@ <h1>Temporal.Duration.prototype.add ( _other_ [ , _options_ ] )</h1>
<emu-alg>
1. Let _duration_ be the *this* value.
1. Perform ? RequireInternalSlot(_duration_, [[InitializedTemporalDuration]]).
1. Set _other_ to ? ToTemporalDurationRecord(_other_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _relativeTo_ be ? ToRelativeTemporalObject(_options_).
1. Let _result_ be ? AddDuration(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]], _other_.[[Years]], _other_.[[Months]], _other_.[[Weeks]], _other_.[[Days]], _other_.[[Hours]], _other_.[[Minutes]], _other_.[[Seconds]], _other_.[[Milliseconds]], _other_.[[Microseconds]], _other_.[[Nanoseconds]], _relativeTo_).
1. Return ! CreateTemporalDuration(_result_.[[Years]], _result_.[[Months]], _result_.[[Weeks]], _result_.[[Days]], _result_.[[Hours]], _result_.[[Minutes]], _result_.[[Seconds]], _result_.[[Milliseconds]], _result_.[[Microseconds]], _result_.[[Nanoseconds]]).
1. Return ? AddDurationToOrSubtractDurationFromDuration(~add~, _duration_, _other_, _options_).
</emu-alg>
</emu-clause>

Expand All @@ -400,11 +396,7 @@ <h1>Temporal.Duration.prototype.subtract ( _other_ [ , _options_ ] )</h1>
<emu-alg>
1. Let _duration_ be the *this* value.
1. Perform ? RequireInternalSlot(_duration_, [[InitializedTemporalDuration]]).
1. Set _other_ to ? ToTemporalDurationRecord(_other_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _relativeTo_ be ? ToRelativeTemporalObject(_options_).
1. Let _result_ be ? AddDuration(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]], -_other_.[[Years]], -_other_.[[Months]], -_other_.[[Weeks]], -_other_.[[Days]], -_other_.[[Hours]], -_other_.[[Minutes]], -_other_.[[Seconds]], -_other_.[[Milliseconds]], -_other_.[[Microseconds]], -_other_.[[Nanoseconds]], _relativeTo_).
1. Return ! CreateTemporalDuration(_result_.[[Years]], _result_.[[Months]], _result_.[[Weeks]], _result_.[[Days]], _result_.[[Hours]], _result_.[[Minutes]], _result_.[[Seconds]], _result_.[[Milliseconds]], _result_.[[Microseconds]], _result_.[[Nanoseconds]]).
1. Return ? AddDurationToOrSubtractDurationFromDuration(~subtract~, _duration_, _other_, _options_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -1847,5 +1839,28 @@ <h1>
1. Return _result_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-adddurationtoorsubtractdurationfromduration" type="abstract operation">
<h1>
AddDurationToOrSubtractDurationFromDuration (
_operation_: ~add~ or ~subtract~,
_duration_: a Temporal.Duration,
_other_: an ECMAScript language value,
_options_: an ECMAScript language value,
): either a normal completion containing a Temporal.Duration or an abrupt completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It adds/subtracts _other_ to/from _duration_, resulting in a longer/shorter duration.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Set _other_ to ? ToTemporalDurationRecord(_other_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _relativeTo_ be ? ToRelativeTemporalObject(_options_).
1. Let _result_ be ? AddDuration(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]], _sign_ &times; _other_.[[Years]], _sign_ &times; _other_.[[Months]], _sign_ &times; _other_.[[Weeks]], _sign_ &times; _other_.[[Days]], _sign_ &times; _other_.[[Hours]], _sign_ &times; _other_.[[Minutes]], _sign_ &times; _other_.[[Seconds]], _sign_ &times; _other_.[[Milliseconds]], _sign_ &times; _other_.[[Microseconds]], _sign_ &times; _other_.[[Nanoseconds]], _relativeTo_).
1. Return ! CreateTemporalDuration(_result_.[[Years]], _result_.[[Months]], _result_.[[Weeks]], _result_.[[Days]], _result_.[[Hours]], _result_.[[Minutes]], _result_.[[Seconds]], _result_.[[Milliseconds]], _result_.[[Microseconds]], _result_.[[Nanoseconds]]).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
27 changes: 21 additions & 6 deletions spec/instant.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ <h1>Temporal.Instant.prototype.add ( _temporalDurationLike_ )</h1>
<emu-alg>
1. Let _instant_ be the *this* value.
1. Perform ? RequireInternalSlot(_instant_, [[InitializedTemporalInstant]]).
1. Let _duration_ be ? ToLimitedTemporalDuration(_temporalDurationLike_, « *"years"*, *"months"*, *"weeks"*, *"days"* »).
1. Let _ns_ be ? AddInstant(_instant_.[[Nanoseconds]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Return ! CreateTemporalInstant(_ns_).
1. Return ? AddDurationToOrSubtractDurationFromInstant(~add~, _instant_, _temporalDurationLike_).
</emu-alg>
</emu-clause>

Expand All @@ -241,9 +239,7 @@ <h1>Temporal.Instant.prototype.subtract ( _temporalDurationLike_ )</h1>
<emu-alg>
1. Let _instant_ be the *this* value.
1. Perform ? RequireInternalSlot(_instant_, [[InitializedTemporalInstant]]).
1. Let _duration_ be ? ToLimitedTemporalDuration(_temporalDurationLike_, « *"years"*, *"months"*, *"weeks"*, *"days"* »).
1. Let _ns_ be ? AddInstant(_instant_.[[Nanoseconds]], -_duration_.[[Hours]], -_duration_.[[Minutes]], -_duration_.[[Seconds]], -_duration_.[[Milliseconds]], -_duration_.[[Microseconds]], -_duration_.[[Nanoseconds]]).
1. Return ! CreateTemporalInstant(_ns_).
1. Return ? AddDurationToOrSubtractDurationFromInstant(~subtract~, _instant_, _temporalDurationLike_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -647,5 +643,24 @@ <h1>TemporalInstantToString ( _instant_, _timeZone_, _precision_ )</h1>
1. Return the string-concatenation of _dateTimeString_ and _timeZoneString_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-temporal-adddurationtoorsubtractdurationfrominstant" type="abstract operation">
<h1>
AddDurationToOrSubtractDurationFromInstant (
_operation_: ~add~ or ~subtract~,
_instant_: a Temporal.Instant,
_temporalDurationLike_: an ECMAScript language value,
): either a normal completion containing a Temporal.Instant or an abrupt completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It adds/subtracts _temporalDurationLike_ to/from _instant_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _duration_ be ? ToLimitedTemporalDuration(_temporalDurationLike_, « *"years"*, *"months"*, *"weeks"*, *"days"* »).
1. Let _ns_ be ? AddInstant(_instant_.[[Nanoseconds]], _sign_ &times; _duration_.[[Hours]], _sign_ &times; _duration_.[[Minutes]], _sign_ &times; _duration_.[[Seconds]], _sign_ &times; _duration_.[[Milliseconds]], _sign_ &times; _duration_.[[Microseconds]], _sign_ &times; _duration_.[[Nanoseconds]]).
1. Return ! CreateTemporalInstant(_ns_).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
37 changes: 25 additions & 12 deletions spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,7 @@ <h1>Temporal.PlainDateTime.prototype.add ( _temporalDurationLike_ [ , _options_
<emu-alg>
1. Let _dateTime_ be the *this* value.
1. Perform ? RequireInternalSlot(_dateTime_, [[InitializedTemporalDateTime]]).
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _result_ be ? AddDateTime(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _dateTime_.[[Calendar]], _duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]], _options_).
1. Assert: IsValidISODate(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]]) is *true*.
1. Assert: IsValidTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]) is *true*.
1. Return ? CreateTemporalDateTime(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]], _result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]], _dateTime_.[[Calendar]]).
1. Return ? AddDurationToOrSubtractDurationFromPlainDateTime(~add~, _dateTime_, _temporalDurationLike_, _options_).
</emu-alg>
</emu-clause>

Expand All @@ -470,12 +465,7 @@ <h1>Temporal.PlainDateTime.prototype.subtract ( _temporalDurationLike_ [ , _opti
<emu-alg>
1. Let _dateTime_ be the *this* value.
1. Perform ? RequireInternalSlot(_dateTime_, [[InitializedTemporalDateTime]]).
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _result_ be ? AddDateTime(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _dateTime_.[[Calendar]], -_duration_.[[Years]], -_duration_.[[Months]], -_duration_.[[Weeks]], -_duration_.[[Days]], -_duration_.[[Hours]], -_duration_.[[Minutes]], -_duration_.[[Seconds]], -_duration_.[[Milliseconds]], -_duration_.[[Microseconds]], -_duration_.[[Nanoseconds]], _options_).
1. Assert: IsValidISODate(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]]) is *true*.
1. Assert: IsValidTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]) is *true*.
1. Return ? CreateTemporalDateTime(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]], _result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]], _dateTime_.[[Calendar]]).
1. Return ? AddDurationToOrSubtractDurationFromPlainDateTime(~subtract~, _dateTime_, _temporalDurationLike_, _options_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -1157,5 +1147,28 @@ <h1>
1. Return ! CreateDurationRecord(_dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], _balanceResult_.[[Days]], _balanceResult_.[[Hours]], _balanceResult_.[[Minutes]], _balanceResult_.[[Seconds]], _balanceResult_.[[Milliseconds]], _balanceResult_.[[Microseconds]], _balanceResult_.[[Nanoseconds]]).
</emu-alg>
</emu-clause>
<emu-clause id="sec-temporal-adddurationtoorsubtractdurationfromplaindatetime" type="abstract operation">
<h1>
AddDurationToOrSubtractDurationFromPlainDateTime (
_operation_: ~add~ or ~subtract~,
_dateTime_: a Temporal.PlainDateTime,
_temporalDurationLike_: an ECMAScript language value,
_options_: an ECMAScript language value,
): either a normal completion containing a Temporal.PlainDateTime or an abrupt completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It adds/subtracts _temporalDurationLike_ to/from _dateTime_, returning a point in time that is in the future/past relative to _datetime_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _result_ be ? AddDateTime(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _dateTime_.[[Calendar]], _sign_ &times; _duration_.[[Years]], _sign_ &times; _duration_.[[Months]], _sign_ &times; _duration_.[[Weeks]], _sign_ &times; _duration_.[[Days]], _sign_ &times; _duration_.[[Hours]], _sign_ &times; _duration_.[[Minutes]], _sign_ &times; _duration_.[[Seconds]], _sign_ &times; _duration_.[[Milliseconds]], _sign_ &times; _duration_.[[Microseconds]], _sign_ &times; _duration_.[[Nanoseconds]], _options_).
1. Assert: IsValidISODate(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]]) is *true*.
1. Assert: IsValidTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]) is *true*.
1. Return ? CreateTemporalDateTime(_result_.[[Year]], _result_.[[Month]], _result_.[[Day]], _result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]], _dateTime_.[[Calendar]]).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
30 changes: 22 additions & 8 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ <h1>Temporal.PlainTime.prototype.add ( _temporalDurationLike_ )</h1>
<emu-alg>
1. Let _temporalTime_ be the *this* value.
1. Perform ? RequireInternalSlot(_temporalTime_, [[InitializedTemporalTime]]).
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Let _result_ be ! AddTime(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Assert: IsValidTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]) is *true*.
1. Return ? CreateTemporalTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]).
1. Return ? AddDurationToOrSubtractDurationFromPlainTime(~add~, _temporalTime_, _temporalTime_).
</emu-alg>
</emu-clause>

Expand All @@ -226,10 +223,7 @@ <h1>Temporal.PlainTime.prototype.subtract ( _temporalDurationLike_ )</h1>
<emu-alg>
1. Let _temporalTime_ be the *this* value.
1. Perform ? RequireInternalSlot(_temporalTime_, [[InitializedTemporalTime]]).
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Let _result_ be ! AddTime(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], -_duration_.[[Hours]], -_duration_.[[Minutes]], -_duration_.[[Seconds]], -_duration_.[[Milliseconds]], -_duration_.[[Microseconds]], -_duration_.[[Nanoseconds]]).
1. Assert: IsValidTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]) is *true*.
1. Return ? CreateTemporalTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]).
1. Return ? AddDurationToOrSubtractDurationFromPlainTime(~subtract~, _temporalTime_, _temporalTime_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -963,5 +957,25 @@ <h1>RoundTime ( _hour_, _minute_, _second_, _millisecond_, _microsecond_, _nanos
1. Return ! BalanceTime(_hour_, _minute_, _second_, _millisecond_, _microsecond_, _result_).
</emu-alg>
</emu-clause>
<emu-clause id="sec-temporal-adddurationtoorsubtractdurationfromplaintime" type="abstract operation">
<h1>
AddDurationToOrSubtractDurationFromPlainTime (
_operation_: ~add~ or ~subtract~,
_temporalTime_: a Temporal.PlainTime,
_temporalDurationLike_: an ECMAScript language value,
): either a normal completion containing a Temporal.PlainTime or an abrupt completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It adds/subtracts _temporalDurationLike_ to/from _temporalTime_, returning a point in time that is in the future/past relative to _temporalTime_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Let _result_ be ! AddTime(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], _sign_ &times; _duration_.[[Hours]], _sign_ &times; _duration_.[[Minutes]], _sign_ &times; _duration_.[[Seconds]], _sign_ &times; _duration_.[[Milliseconds]], _sign_ &times; _duration_.[[Microseconds]], _sign_ &times; _duration_.[[Nanoseconds]]).
1. Assert: IsValidTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]) is *true*.
1. Return ? CreateTemporalTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]).
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>
Loading

0 comments on commit 2f96efc

Please sign in to comment.