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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! §27.2.6 — what a Promise is, underneath the methods.
//!
//! A promise is an ordinary object with six internal slots and no exotic behaviour at all: its
//! properties, its prototype and its extensibility work exactly as any object's do. Everything a
//! program can observe about it comes from `then`, and everything `then` reads is here.
//!
//! # Why the reactions are kept and not the callbacks
//!
//! §27.2.1.2's PromiseReaction Record is three things — the capability whose promise is settled
//! afterwards, whether this reaction is for a fulfilment or a rejection, and the handler, which may
//! be **absent**. Absent is not `undefined`: an absent handler makes the reaction *pass through*,
//! which is how `p.then(undefined, f)` propagates a fulfilment to the promise `then` answered with
//! rather than dropping it. A list of plain callbacks cannot say that.
//!
//! # `[[PromiseIsHandled]]` is here now, and what brought it back
//!
//! This section used to say the slot was left out: §27.2.6 lists it, nothing in the language reads
//! it, its one use is §27.2.1.7 step 7's `HostPromiseRejectionTracker`, and ViperJS had no such
//! hook — so it would have been written and never read. That was right, and it ended with "it comes
//! back with the tracker".
//!
//! The tracker is built (§9.13, `Vm::unhandled_rejections`), and what asked for it was a **silent
//! stop**: a promise chain that re-armed itself ran out of heap, the RangeError was thrown inside a
//! job where §9.5 step 3 discards it, and the queue emptied with an exit status of zero. Nothing in
//! the engine was in a position to notice, because the only record that a rejection had gone
//! unclaimed was this slot and it did not exist. See DR-0029.
//!
//! # `[[AlreadyResolved]]` belongs to the pair, and not to the promise
//!
//! §27.2.1.3 puts it in a record shared by the two resolving functions. Putting it on the promise
//! instead looks equivalent and is not: §27.2.2.2 step 1.b makes a **second** pair for a promise
//! that is already resolved, so that a thenable can settle it, and that pair has to start
//! unresolved. With one flag on the promise the second pair is dead on arrival and every promise
//! resolved with another one waits for ever.
//!
//! So the flag is a cell the pair shares, which is what the specification says it is.
use crate;
use crateValue;
use ;
use Rc;
/// `[[PromiseState]]` — §27.2.6.
///
/// Settled once and never again: every transition out of `Pending` is guarded by
/// `[[AlreadyResolved]]`, which is what makes a promise's answer stable no matter how many times
/// its resolving functions are called.
/// Which of a reaction's two halves this is — §27.2.1.2's `[[Type]]`.
/// §27.2.1.1 — a PromiseCapability Record: a promise and the two functions that settle it.
///
/// Carried around rather than looked up, because §27.2.1.5 may have built it from a *subclass*
/// constructor, and then the only way back to its resolve and reject is the pair that constructor
/// handed the executor.
// No `PartialEq`: [`Value`] deliberately has none, JavaScript having three equalities.
/// §27.6.3.1 — an AsyncGeneratorRequest Record: one `next`, `return` or `throw` awaiting service.
///
/// The completion the body will be resumed with, and the promise to settle with whatever that
/// resumption produces. Both halves are needed at two different moments — the completion when the
/// request reaches the front of the queue, the capability when the body next stops — and a request
/// may wait arbitrarily long in between.
/// §27.2.1.2 — a PromiseReaction Record: one `then` waiting for one answer.
/// §27.2.6's slots, all six of them.
/// The two things a resolving function carries — §27.2.1.3's `[[Promise]]` and
/// `[[AlreadyResolved]]`.
///
/// The flag is shared with the other half of the pair, which is what makes `resolve` and `reject`
/// contradict each other exactly never: whichever is called first settles the promise, and the
/// other then finds the flag set and does nothing. Shared by `Rc` because that is what "the same
/// record" means when neither function owns the other.
/// Which of §27.2.4's combinators a group of elements belongs to.
///
/// They differ in three small ways and each is a whole clause: what a rejection does, whether the
/// value is recorded or the outcome is, and whether anything is recorded at all.
/// What a group of elements shares — §27.2.4.1.1's `values` and `remainingElementsCount`.
///
/// One record behind an `Rc`, because the specification's is shared by every element function and
/// by the walk that made them: each holds it, each changes it, and the last one to put it down is
/// the one that settles the promise.
/// What one of §27.2's function objects carries where the specification writes a closure.
///
/// Three of them capture state: the resolve and reject functions capture the promise they settle
/// (§27.2.1.3), and the executor `NewPromiseCapability` passes to a constructor captures the
/// capability it is filling in (§27.2.1.5.1). A built-in body is a bare function pointer holding
/// nothing, so the capture is on the object rather than in the body — which is what the
/// specification says it is anyway, these being ordinary function objects with internal slots.