Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate duplication between Duration Records and Temporal.Duration instances #2943

Merged
merged 26 commits into from
Sep 20, 2024

Conversation

ptomato
Copy link
Collaborator

@ptomato ptomato commented Sep 20, 2024

A series of refactors that does three things:

  • Change operations that took a long series of Duration components as arguments (i.e., years, months, weeks, etc.) to take a Record or a Temporal.Duration instance instead. (The exception is IsValidDuration.)
  • Make duration operations all explicitly use the "normalize - calculate - unnormalize" pattern, where we convert a Temporal.Duration instance into a Normalized Duration Record, do the calculations with the normalized form since that's how implementations calculate internally, and then convert back to a Temporal.Duration instance to expose to user code. This is a pattern that we already mostly used, but now that everything is refactored to take Records instead of individual Duration components, it can be expressed much more explicitly and concisely.
  • Get rid of Duration Records and Time Duration Records, instead using either Temporal.Duration instances or the normalized form.

Closes: #2308

@ptomato ptomato requested a review from gibson042 September 20, 2024 00:53
Also introduce operations to take the sign of a Date Duration Record and
of a Normalized Duration Record, which replace some uses of DurationSign
where there wasn't a Duration instance handy to pass to it.

See: #2308
Instead of picking a _sign_ of -1 or 1 and multiplying the components by
it, create the Duration instance and then pass it through
CreateNegatedTemporalDuration if the operation is ~since~.

See: #2308
In the reference code, move AddISODate into calendar.mjs because it's only
used once there (and replace the other calls of it with BalanceISODate.)
This is a small step towards untangling the circular import in
calendar.mjs.

See: #2308
Similar to DifferenceTemporal___, instead of picking a _sign_ of -1 or 1
and multiplying the duration components by it, create the Duration
instance and then pass it through CreateNegatedTemporalDuration if the
operation is ~subtract~.

This was already started in #2290, and brought into consistency everywhere
in this commit.

See: #2308
In order to avoid passing long lists of components, refactor Normalized
Duration Records to have only two fields: a Date Duration Record and a
Normalized Time Duration Record. This requires several changes throughout.
…normalize

This is a pattern that we already mostly use, but now that everything is
refactored to take Records instead of individual Duration components, it
can be expressed much more concisely.

See: #2308
…c-unnormalize

This allows inlining AddDateTime into AddDurationTo...PlainDateTime. It's
the only place AddDateTime was used.

See: #2308
In a few places, we did the operation of balancing the normalized time
duration up to days, adding the days to the [[Days]] field of the date
duration record, and discarding the time duration. Encapsulate this in its
own AO.

See: #2308
BalanceTimeDuration is now only used in UnnormalizeDuration. Inline it
there.

There is no longer any usage of Time Duration Records, so remove them.

See: #2308
In a few places, we want to create a new Date Duration Record but with one
or more fields adjusted, as if Date Duration Records had a with() method.
This is easy to do with spreading and destructuring in JS, but is unwieldy
in spec language. Add an AdjustDateDurationRecord abstract operation to do
this more concisely.

See: #2308
@Ms2ger Ms2ger force-pushed the 2308-duration-records branch from f5eab0a to df118ba Compare September 20, 2024 09:55
These are no longer necessary. We should either be using Normalized
Duration Records (or their subcomponents, Date Duration Records and
Normalized Time Duration Records) or Temporal.Duration instances.

The records were still used in a few places: ToTemporalDurationRecord can
be inlined into ToTemporalDuration since that is its only caller, and
ParseTemporalDurationString can just return a Temporal.Duration instance
directly.

We keep ToTemporalDurationRecordRaw for the validStrings.js test.
@Ms2ger Ms2ger force-pushed the 2308-duration-records branch from df118ba to 4cbf8c0 Compare September 20, 2024 10:02
@Ms2ger Ms2ger merged commit 748f27f into main Sep 20, 2024
6 checks passed
@Ms2ger Ms2ger deleted the 2308-duration-records branch September 20, 2024 10:54
Comment on lines -450 to 448
1. Let _dateDuration_ be CreateDurationRecord(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]] + _targetTime_.[[Days]], 0, 0, 0, 0, 0, 0).
1. Let _dateDuration_ be ? AdjustDateDurationRecord(_normalizedDuration_.[[Date]], _targetTime_.[[Days]]).
1. Let _targetDate_ be ? CalendarDateAdd(_calendar_, _isoRelativeToDate_, _dateDuration_, *"constrain"*).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this new ? constitute a normative change? A surface reading suggests that an exception is now possible before CalendarDateAdd that was not possible before.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this should be ! (and the same for your other comment below.) I'll fix this in a follow up PR. (Proof: targetTime.[[Days]] cannot exceed the original value of duration.[[Days]] because its absolute value can only be equal or smaller, coming out of AddTime.)

Comment on lines -495 to 490
1. Let _dateDuration_ be CreateDurationRecord(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]] + _targetTime_.[[Days]], 0, 0, 0, 0, 0, 0).
1. Let _dateDuration_ be ? AdjustDateDurationRecord(_normalizedDuration_.[[Date]], _targetTime_.[[Days]]).
1. Let _targetDate_ be ? CalendarDateAdd(_calendar_, _isoRelativeToDate_, _dateDuration_, *"constrain"*).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here.

ptomato added a commit that referenced this pull request Sep 21, 2024
This was a mistake in #2943. _targetTime_.[[Days]] cannot exceed the
original value of _duration_.[[Days]] here because its absolute value can
only be equal or smaller coming out of AddTime.
ptomato added a commit that referenced this pull request Sep 24, 2024
This was a mistake in #2943. _targetTime_.[[Days]] cannot exceed the
original value of _duration_.[[Days]] here because its absolute value can
only be equal or smaller coming out of AddTime.
ptomato added a commit that referenced this pull request Sep 24, 2024
This was a mistake in #2943. _targetTime_.[[Days]] cannot exceed the
original value of _duration_.[[Days]] here because its absolute value can
only be equal or smaller coming out of AddTime.
ptomato added a commit that referenced this pull request Oct 2, 2024
This mistakenly became an assertion in PR #2943. By inspection it is easy
to come up with a case where CreateTemporalDuration is fallible, and this
is well covered in test262.

Closes: #2992
Ms2ger pushed a commit that referenced this pull request Oct 3, 2024
This mistakenly became an assertion in PR #2943. By inspection it is easy
to come up with a case where CreateTemporalDuration is fallible, and this
is well covered in test262.

Closes: #2992
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Oct 3, 2024
ptomato added a commit that referenced this pull request Oct 8, 2024
ptomato added a commit that referenced this pull request Oct 8, 2024
Another couple of leftovers from PR #2943.
ptomato added a commit that referenced this pull request Oct 9, 2024
ptomato added a commit that referenced this pull request Oct 9, 2024
Another couple of leftovers from PR #2943.
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this pull request Oct 9, 2024
<tc39/proposal-temporal#2943> should address the fixme note.

Differential Revision: https://phabricator.services.mozilla.com/D223554

UltraBlame original commit: de7fc134aa463dddcc4f75ff9ef2ba2b9b670a6f
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this pull request Oct 9, 2024
<tc39/proposal-temporal#2943> should address the fixme note.

Differential Revision: https://phabricator.services.mozilla.com/D223554

UltraBlame original commit: de7fc134aa463dddcc4f75ff9ef2ba2b9b670a6f
i3roly pushed a commit to i3roly/firefox-dynasty that referenced this pull request Oct 9, 2024
ErichDonGubler pushed a commit to erichdongubler-mozilla/firefox that referenced this pull request Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can the overlap between Duration instances and Duration Records be eliminated?
3 participants