Skip to content

Commit e37aa9a

Browse files
authored
Reduce unnecessary Awaits for nullish values in blocks containing await using (#219)
* Add PromiseCapability wrapper around sync dispose used in an `await using` * Reduce unnecessary Awaits for nullish values in blocks containing `await using` * Update DisposeResources to match tentative consensus * Match spec text for each * Fix formatting * Remove extraneous assignment * fix indent
1 parent 6277c43 commit e37aa9a

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

spec.emu

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,18 +1108,36 @@ contributors: Ron Buckton, Ecma International
11081108
</h1>
11091109
<dl class="header"></dl>
11101110
<emu-alg>
1111-
1. For each _resource_ of _disposeCapability_.[[DisposableResourceStack]], in reverse list order, do
1112-
1. Let _result_ be Dispose(_resource_.[[ResourceValue]], _resource_.[[Hint]], _resource_.[[DisposeMethod]]).
1113-
1. If _result_.[[Type]] is ~throw~, then
1114-
1. If _completion_.[[Type]] is ~throw~, then
1115-
1. Set _result_ to _result_.[[Value]].
1116-
1. Let _suppressed_ be _completion_.[[Value]].
1117-
1. Let _error_ be a newly created *SuppressedError* object.
1118-
1. Perform CreateNonEnumerableDataPropertyOrThrow(_error_, *"error"*, _result_).
1119-
1. Perform CreateNonEnumerableDataPropertyOrThrow(_error_, *"suppressed"*, _suppressed_).
1120-
1. Set _completion_ to ThrowCompletion(_error_).
1121-
1. Else,
1122-
1. Set _completion_ to _result_.
1111+
1. Let _needsAwait_ be *false*.
1112+
1. Let _hasAwaited_ be *false*.
1113+
1. For each element _resource_ of _disposeCapability_.[[DisposableResourceStack]], in reverse list order, do
1114+
1. Let _value_ be _resource_.[[ResourceValue]].
1115+
1. Let _hint_ be _resource_.[[Hint]].
1116+
1. Let _method_ be _resource_.[[DisposeMethod]].
1117+
1. If _hint_ is ~sync-dispose~ and _needsAwait_ is *true* and _hasAwaited_ is *false*, then
1118+
1. Perform ! Await(*undefined*).
1119+
1. Set _needsAwait_ to *false*.
1120+
1. If _method_ is not *undefined*, then
1121+
1. Let _result_ be Completion(Call(_method_, _value_)).
1122+
1. If _result_ is a normal completion and _hint_ is ~async-dispose~, then
1123+
1. Set _result_ to Completion(Await(_result_.[[Value]])).
1124+
1. Set _hasAwaited_ to *true*.
1125+
1. If _result_ is a throw completion, then
1126+
1. If _completion_ is a throw completion, then
1127+
1. Set _result_ to _result_.[[Value]].
1128+
1. Let _suppressed_ be _completion_.[[Value]].
1129+
1. Let _error_ be a newly created *SuppressedError* object.
1130+
1. Perform CreateNonEnumerableDataPropertyOrThrow(_error_, *"error"*, _result_).
1131+
1. Perform CreateNonEnumerableDataPropertyOrThrow(_error_, *"suppressed"*, _suppressed_).
1132+
1. Set _completion_ to ThrowCompletion(_error_).
1133+
1. Else,
1134+
1. Set _completion_ to _result_.
1135+
1. Else,
1136+
1. Assert: _hint_ is ~async-dispose~.
1137+
1. Set _needsAwait_ to *true*.
1138+
1. NOTE: This can only indicate a case where either *null* or *undefined* was the initialized value of an `await using` declaration.
1139+
1. If _needsAwait_ is *true* and _hasAwaited_ is *false*, then
1140+
1. Perform ! Await(*undefined*).
11231141
1. NOTE: After _disposeCapability_ has been disposed, it will never be used again. The contents of _disposeCapability_.[[DisposableResourceStack]] can be discarded in implementations, such as by garbage collection, at this point.
11241142
1. Set _disposeCapability_.[[DisposableResourceStack]] to a new empty List.
11251143
1. Return _completion_.

0 commit comments

Comments
 (0)