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

Editorial: use abstract closures for anonymous functions in more places #2439

Merged
merged 8 commits into from
Jun 24, 2021

Conversation

bakkot
Copy link
Contributor

@bakkot bakkot commented Jun 20, 2021

Followup to #2109. Fixes #1894, fixes #933, fixes #1077 (in passing, in the "less handwaving" commit).

There's still six places where I haven't adopted the new style: two (x, x) in CreateResolvingFunctions (used mainly in the Promise constructor), one in PerformPromiseAll, two (x, x) in PerformPromiseAllSettled, and one in PerformPromiseAny.

These remaining cases generally seemed nontrivial enough that I didn't think it worth inlining them. I could, I suppose, replace each with an abstract operation which returns the appropriate abstract closure (or built-in function), but that doesn't seem like it would be much of an improvement.

This is mostly a mechanical transformation. Where there was non-mutable state provided by setting internal slots, I have instead had the abstract closure capture the relevant aliases. I've left mutable state as internal slots, since abstract closures can't write to captured aliases.

One thing of note is that the Default Constructor Functions took a rest parameter, which isn't something we have established syntax for with abstract closures; I decided to mostly mimic what we do in the function constructor and refer to "the List of arguments that was passed to this function by [[Call]] or [[Construct]]" explicitly, rather than introducing syntax for rest parameters in Abstract Closures.

@bakkot bakkot force-pushed the more-abstract-closures branch from e266aad to 96c0b63 Compare June 21, 2021 04:01
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +39227 to +39242
1. Let _thenFinallyClosure_ be a new Abstract Closure with parameters (_value_) that captures _onFinally_ and _C_ and performs the following steps when called:
1. Let _result_ be ? Call(_onFinally_, *undefined*).
1. Let _promise_ be ? PromiseResolve(_C_, _result_).
1. Let _returnValue_ be a new Abstract Closure with no parameters that captures _value_ and performs the following steps when called:
1. Return _value_.
1. Let _valueThunk_ be ! CreateBuiltinFunction(_returnValue_, 0, *""*, « »).
1. Return ? Invoke(_promise_, *"then"*, « _valueThunk_ »).
1. Let _thenFinally_ be ! CreateBuiltinFunction(_thenFinallyClosure_, 1, *""*, « »).
1. Let _catchFinallyClosure_ be a new Abstract Closure with parameters (_reason_) that captures _onFinally_ and _C_ and performs the following steps when called:
1. Let _result_ be ? Call(_onFinally_, *undefined*).
1. Let _promise_ be ? PromiseResolve(_C_, _result_).
1. Let _throwReason_ be a new Abstract Closure with no parameters that captures _reason_ and performs the following steps when called:
1. Return ThrowCompletion(_reason_).
1. Let _thrower_ be ! CreateBuiltinFunction(_throwReason_, 0, *""*, « »).
1. Return ? Invoke(_promise_, *"then"*, « _thrower_ »).
1. Let _catchFinally_ be ! CreateBuiltinFunction(_catchFinallyClosure_, 1, *""*, « »).
Copy link
Member

Choose a reason for hiding this comment

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

fwiw, confirmed these exact changes pass in https://npmjs.com/promise.prototype.finally

Copy link
Member

@michaelficarra michaelficarra left a comment

Choose a reason for hiding this comment

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

LGTM otherwise.

@ljharb ljharb requested review from syg and a team June 23, 2021 21:42
Copy link
Contributor

@syg syg left a comment

Choose a reason for hiding this comment

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

lgtm modulo the Proxy revoke comment.

spec.html Show resolved Hide resolved
@bakkot bakkot force-pushed the more-abstract-closures branch from 073baa2 to 9eb54b2 Compare June 24, 2021 01:59
@bakkot bakkot added the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Jun 24, 2021
@ljharb ljharb force-pushed the more-abstract-closures branch from 9eb54b2 to 6d3c629 Compare June 24, 2021 05:22
@ljharb ljharb merged commit 6d3c629 into master Jun 24, 2021
@ljharb ljharb deleted the more-abstract-closures branch June 24, 2021 05:26
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
mathiasbynens pushed a commit to mathiasbynens/ecma262 that referenced this pull request Oct 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editorial change ready to merge Editors believe this PR needs no further reviews, and is ready to land.
Projects
None yet
4 participants