This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathabstract-operations.html
198 lines (182 loc) · 11.6 KB
/
abstract-operations.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<emu-clause id="sec-asyncgeneratorrequest-records">
<h1>AsyncGeneratorRequest Records</h1>
<p>The AsyncGeneratorRequest is a Record value used to store information about how an async generator should be resumed and contains capabilities for fulfilling or rejecting the corresponding promise.</p>
<p>They have the following fields:</p>
<emu-table caption="AsyncGeneratorRequest Record Fields">
<table>
<tbody>
<tr>
<th>Field Name</th>
<th>Value</th>
<th>Meaning</th>
</tr>
<tr>
<td>[[Completion]]</td>
<td>A Completion record</td>
<td>The completion which should be used to resume the async generator.</td>
</tr>
<tr>
<td>[[Capability]]</td>
<td>A PromiseCapability record</td>
<td>The promise capabilities associated with this request.</td>
</tr>
</tbody>
</table>
</emu-table>
</emu-clause>
<emu-clause id="sec-asyncgeneratorstart" aoid="AsyncGeneratorStart">
<h1>AsyncGeneratorStart ( _generator_, _generatorBody_ )</h1>
<emu-alg>
1. Assert: _generator_ is an AsyncGenerator instance.
1. Assert: _generator_.[[AsyncGeneratorState]] is *undefined*.
1. Let _genContext_ be the running execution context.
1. Set the Generator component of _genContext_ to _generator_.
1. Set the code evaluation state of _genContext_ such that when evaluation is resumed for that execution context the following steps will be performed:
1. Let _result_ be the result of evaluating _generatorBody_.
1. Assert: If we return here, the async generator either threw an exception or performed either an implicit or explicit return.
1. Remove _genContext_ from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
1. Set _generator_.[[AsyncGeneratorState]] to `"completed"`.
1. If _result_ is a normal completion, let _resultValue_ be *undefined*.
1. Else,
1. Let _resultValue_ be _result_.[[Value]].
1. If _result_.[[Type]] is not ~return~, then
1. Return ! AsyncGeneratorReject(_generator_, _resultValue_).
1. Return ! AsyncGeneratorResolve(_generator_, _resultValue_, *true*).
1. Set _generator_.[[AsyncGeneratorContext]] to _genContext_.
1. Set _generator_.[[AsyncGeneratorState]] to `"suspendedStart"`.
1. Set _generator_.[[AsyncGeneratorQueue]] to a new empty List.
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asyncgeneratorresolve" aoid="AsyncGeneratorResolve">
<h1>AsyncGeneratorResolve ( _generator_, _value_, _done_ )</h1>
<emu-alg>
1. Assert: _generator_ is an AsyncGenerator instance.
1. Let _queue_ be _generator_.[[AsyncGeneratorQueue]].
1. Assert: _queue_ is not an empty List.
1. Remove the first element from _queue_ and let _next_ be the value of that element.
1. Let _promiseCapability_ be _next_.[[Capability]].
1. Let _iteratorResult_ be ! CreateIterResultObject(_value_, _done_).
1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _iteratorResult_ »).
1. Perform ! AsyncGeneratorResumeNext(_generator_).
1. Return *undefined*.
</emu-clause>
</emu-alg>
<emu-clause id="sec-asyncgeneratorreject" aoid="AsyncGeneratorReject">
<h1>AsyncGeneratorReject ( _generator_, _exception_ )</h1>
<emu-alg>
1. Assert: _generator_ is an AsyncGenerator instance.
1. Let _queue_ be _generator_.[[AsyncGeneratorQueue]].
1. Assert: _queue_ is not an empty List.
1. Remove the first element from _queue_ and let _next_ be the value of that element.
1. Let _promiseCapability_ be _next_.[[Capability]].
1. Perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _exception_ »).
1. Perform ! AsyncGeneratorResumeNext(_generator_).
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-asyncgeneratorresumenext" aoid="AsyncGeneratorResumeNext">
<h1>AsyncGeneratorResumeNext ( _generator_ )</h1>
<emu-alg>
1. Assert: _generator_ is an AsyncGenerator instance.
1. Let _state_ be _generator_.[[AsyncGeneratorState]].
1. Assert: _state_ is not `"executing"`.
1. If _state_ is `"awaiting-return"`, return *undefined*.
1. Let _queue_ be _generator_.[[AsyncGeneratorQueue]].
1. If _queue_ is an empty List, return *undefined*.
1. Let _next_ be the value of the first element of _queue_.
1. Assert: _next_ is an AsyncGeneratorRequest record.
1. Let _completion_ be _next_.[[Completion]].
1. If _completion_ is an abrupt completion, then
1. If _state_ is `"suspendedStart"`, then
1. Set _generator_.[[AsyncGeneratorState]] to `"completed"`.
1. Set _state_ to `"completed"`.
1. If _state_ is `"completed"`, then
1. If _completion_.[[Type]] is ~return~:
1. Set _generator_.[[AsyncGeneratorState]] to `"awaiting-return"`.
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. Perform ! Call(_promiseCapability_.[[Resolve]], *undefined*, « _completion_.[[Value]] »).
1. Let _onFulfilled_ be a new built-in function object as defined in <emu-xref href="#async-generator-resume-next-return-processor-fulfilled" title></emu-xref>.
1. Let _onRejected_ be a new built-in function object as defined in <emu-xref href="#async-generator-resume-next-return-processor-rejected" title></emu-xref>.
1. Set _onFulfilled_ and _onRejected_'s [[Generator]] internal slots to _generator_.
1. Let _throwawayCapability_ be NewPromiseCapability(%Promise%).
1. Set _throwawayCapability_.[[Promise]].[[PromiseIsHandled]] to *true*.
1. Perform ! PerformPromiseThen(_promiseCapability_.[[Promise]], _onFulfilled_, _onRejected_, _throwawayCapability_).
1. Return *undefined*.
1. Else,
1. Assert: _completion_.[[Type]] is ~throw~.
1. Perform ! AsyncGeneratorReject(_generator_, _completion_.[[Value]]).
1. Return *undefined*.
1. Else if _state_ is `"completed"`, then return ! AsyncGeneratorResolve(_generator_, *undefined*, *true*).
1. Assert: _state_ is either `"suspendedStart"` or `"suspendedYield"`.
1. Let _genContext_ be _generator_.[[AsyncGeneratorContext]].
1. Let _callerContext_ be the running execution context.
1. Suspend _callerContext_.
1. Set _generator_.[[AsyncGeneratorState]] to `"executing"`.
1. Push _genContext_ onto the execution context stack; _genContext_ is now the running execution context.
1. Resume the suspended evaluation of _genContext_ using _completion_ as the result of the operation that suspended it. Let _result_ be the completion record returned by the resumed computation.
1. Assert: _result_ is never an abrupt completion.
1. Assert: When we return here, _genContext_ has already been removed from the execution context stack and _callerContext_ is the currently running execution context.
1. Return *undefined*.
</emu-alg>
<emu-clause id="async-generator-resume-next-return-processor-fulfilled">
<h1>AsyncGeneratorResumeNext Return Processor Fulfilled Functions</h1>
<p>An AsyncGeneratorResumeNext return processor fulfilled function is an anonymous built-in function that is used as part of the AsyncGeneratorResumeNext specification device to unwrap promises passed in to the <emu-xref href="#sec-asyncgenerator-prototype-return" title></emu-xref> method. Each AsyncGeneratorResumeNext return processor fulfilled function has a [[Generator]] internal slot.</p>
<p>When an AsyncGeneratorResumeNext return processor fulfilled function _F_ is called with argument _value_, the following steps are taken:</p>
<emu-alg>
1. Set _F_.[[Generator]].[[AsyncGeneratorState]] to `"completed"`.
1. Return ! AsyncGeneratorResolve(_F_.[[Generator]], _value_, *true*).
</emu-alg>
<p>The `length` property of an AsyncGeneratorResumeNext return processor fulfilled function is 1.</p>
</emu-clause>
<emu-clause id="async-generator-resume-next-return-processor-rejected">
<h1>AsyncGeneratorResumeNext Return Processor Rejected Functions</h1>
<p>An AsyncGeneratorResumeNext return processor rejected function is an anonymous built-in function that is used as part of the AsyncGeneratorResumeNext specification device to unwrap promises passed in to the <emu-xref href="#sec-asyncgenerator-prototype-return" title></emu-xref> method. Each AsyncGeneratorResumeNext return processor rejected function has a [[Generator]] internal slot.</p>
<p>When an AsyncGeneratorResumeNext return processor rejected function _F_ is called with argument _reason_, the following steps are taken:</p>
<emu-alg>
1. Set _F_.[[Generator]].[[AsyncGeneratorState]] to `"completed"`.
1. Return ! AsyncGeneratorReject(_F_.[[Generator]], _reason_).
</emu-alg>
<p>The `length` property of an AsyncGeneratorResumeNext return processor rejected function is 1.</p>
</emu-clause>
</emu-clause>
<emu-clause id="sec-asyncgeneratorenqueue" aoid="AsyncGeneratorEnqueue">
<h1>AsyncGeneratorEnqueue ( _generator_, _completion_ )</h1>
<emu-alg>
1. Assert: _completion_ is a Completion Record.
1. Let _promiseCapability_ be ! NewPromiseCapability(%Promise%).
1. If Type(_generator_) is not Object, or if _generator_ does not have an [[AsyncGeneratorState]] internal slot, then
1. Let _badGeneratorError_ be a new *TypeError* exception.
1. Perform ! Call(_promiseCapability_.[[Reject]], *undefined*, « _badGeneratorError_ »).
1. Return _promiseCapability_.[[Promise]].
1. Let _queue_ be _generator_.[[AsyncGeneratorQueue]].
1. Let _request_ be AsyncGeneratorRequest{[[Completion]]: _completion_, [[Capability]]: _promiseCapability_}.
1. Append _request_ to the end of _queue_.
1. Let _state_ be _generator_.[[AsyncGeneratorState]].
1. If _state_ is not `"executing"`, then
1. Perform ! AsyncGeneratorResumeNext(_generator_).
1. Return _promiseCapability_.[[Promise]].
</emu-alg>
</emu-clause>
<emu-clause id="sec-asyncgeneratoryield" aoid="AsyncGeneratorYield">
<h1><ins>AsyncGeneratorYield ( _value_ )</ins></h1>
<p>The abstract operation AsyncGeneratorYield with argument _value_ performs the following steps:</p>
<emu-alg>
1. Let _genContext_ be the running execution context.
1. Assert: _genContext_ is the execution context of a generator.
1. Let _generator_ be the value of the Generator component of _genContext_.
1. Assert: GetGeneratorKind() is ~async~.
1. Set _value_ to ? Await(_value_).
1. Set _generator_.[[AsyncGeneratorState]] to `"suspendedYield"`.
1. Remove _genContext_ from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
1. Set the code evaluation state of _genContext_ such that when evaluation is resumed with a Completion _resumptionValue_ the following steps will be performed:
1. If _resumptionValue_.[[Type]] is not ~return~, return Completion(_resumptionValue_).
1. Let _awaited_ be Await(_resumptionValue_.[[Value]]).
1. If _awaited_.[[Type]] is ~throw~, return Completion(_awaited_).
1. Assert: _awaited_.[[Type]] is ~normal~.
1. Return Completion{[[Type]]: ~return~, [[Value]]: _awaited_.[[Value]], [[Target]]: ~empty~}.
1. NOTE: When one of the above steps returns, it returns to the evaluation of the |YieldExpression| production that originally called this abstract operation.
1. Return ! AsyncGeneratorResolve(_generator_, _value_, *false*).
1. NOTE: This returns to the evaluation of the operation that had most previously resumed evaluation of _genContext_.
</emu-alg>
</emu-clause>