@@ -22,13 +22,24 @@ namespace JS {
22
22
ThrowCompletionOr<Object*> promise_resolve (GlobalObject& global_object, Object& constructor, Value value)
23
23
{
24
24
auto & vm = global_object.vm ();
25
+
26
+ // 1. If IsPromise(x) is true, then
25
27
if (value.is_object () && is<Promise>(value.as_object ())) {
28
+ // a. Let xConstructor be ? Get(x, "constructor").
26
29
auto value_constructor = TRY (value.as_object ().get (vm.names .constructor ));
30
+
31
+ // b. If SameValue(xConstructor, C) is true, return x.
27
32
if (same_value (value_constructor, &constructor))
28
33
return &static_cast <Promise&>(value.as_object ());
29
34
}
35
+
36
+ // 2. Let promiseCapability be ? NewPromiseCapability(C).
30
37
auto promise_capability = TRY (new_promise_capability (global_object, &constructor));
38
+
39
+ // 3. Perform ? Call(promiseCapability.[[Resolve]], undefined, « x »).
31
40
(void )TRY (vm.call (*promise_capability.resolve , js_undefined (), value));
41
+
42
+ // 4. Return promiseCapability.[[Promise]].
32
43
return promise_capability.promise ;
33
44
}
34
45
@@ -37,6 +48,7 @@ Promise* Promise::create(GlobalObject& global_object)
37
48
return global_object.heap ().allocate <Promise>(global_object, *global_object.promise_prototype ());
38
49
}
39
50
51
+ // 27.2 Promise Objects, https://tc39.es/ecma262/#sec-promise-objects
40
52
Promise::Promise (Object& prototype)
41
53
: Object(prototype)
42
54
{
@@ -48,70 +60,147 @@ Promise::ResolvingFunctions Promise::create_resolving_functions()
48
60
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / create_resolving_functions()]" , this );
49
61
auto & vm = this ->vm ();
50
62
63
+ // 1. Let alreadyResolved be the Record { [[Value]]: false }.
51
64
auto * already_resolved = vm.heap ().allocate_without_global_object <AlreadyResolved>();
52
65
66
+ // 2. Let stepsResolve be the algorithm steps defined in Promise Resolve Functions.
67
+ // 3. Let lengthResolve be the number of non-optional parameters of the function definition in Promise Resolve Functions.
68
+ // 4. Let resolve be ! CreateBuiltinFunction(stepsResolve, lengthResolve, "", « [[Promise]], [[AlreadyResolved]] »).
69
+ // 5. Set resolve.[[Promise]] to promise.
70
+ // 6. Set resolve.[[AlreadyResolved]] to alreadyResolved.
71
+
53
72
// 27.2.1.3.2 Promise Resolve Functions, https://tc39.es/ecma262/#sec-promise-resolve-functions
54
73
auto * resolve_function = PromiseResolvingFunction::create (global_object (), *this , *already_resolved, [](auto & vm, auto & global_object, auto & promise, auto & already_resolved) {
55
74
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Resolve function was called" , &promise);
75
+
76
+ auto resolution = vm.argument (0 );
77
+
78
+ // 1. Let F be the active function object.
79
+ // 2. Assert: F has a [[Promise]] internal slot whose value is an Object.
80
+ // 3. Let promise be F.[[Promise]].
81
+
82
+ // 4. Let alreadyResolved be F.[[AlreadyResolved]].
83
+ // 5. If alreadyResolved.[[Value]] is true, return undefined.
56
84
if (already_resolved.value ) {
57
85
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Promise is already resolved, returning undefined" , &promise);
58
86
return js_undefined ();
59
87
}
88
+
89
+ // 6. Set alreadyResolved.[[Value]] to true.
60
90
already_resolved.value = true ;
61
- auto resolution = vm.argument (0 );
91
+
92
+ // 7. If SameValue(resolution, promise) is true, then
62
93
if (resolution.is_object () && &resolution.as_object () == &promise) {
63
94
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Promise can't be resolved with itself, rejecting with error" , &promise);
95
+
96
+ // a. Let selfResolutionError be a newly created TypeError object.
64
97
auto * self_resolution_error = TypeError::create (global_object, " Cannot resolve promise with itself" );
98
+
99
+ // b. Return RejectPromise(promise, selfResolutionError).
65
100
return promise.reject (self_resolution_error);
66
101
}
102
+
103
+ // 8. If Type(resolution) is not Object, then
67
104
if (!resolution.is_object ()) {
105
+ // a. Return FulfillPromise(promise, resolution).
68
106
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Resolution is not an object, fulfilling with {}" , &promise, resolution);
69
107
return promise.fulfill (resolution);
70
108
}
109
+
110
+ // 9. Let then be Get(resolution, "then").
71
111
auto then = resolution.as_object ().get (vm.names .then );
112
+
113
+ // 10. If then is an abrupt completion, then
72
114
if (then.is_throw_completion ()) {
115
+ // a. Return RejectPromise(promise, then.[[Value]]).
73
116
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Exception while getting 'then' property, rejecting with error" , &promise);
74
117
vm.clear_exception ();
75
118
vm.stop_unwind ();
76
119
return promise.reject (then.throw_completion ().value ());
77
120
}
121
+
122
+ // 11. Let thenAction be then.[[Value]].
78
123
auto then_action = then.release_value ();
124
+
125
+ // 12. If IsCallable(thenAction) is false, then
79
126
if (!then_action.is_function ()) {
127
+ // a. Return FulfillPromise(promise, resolution).
80
128
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Then action is not a function, fulfilling with {}" , &promise, resolution);
81
129
return promise.fulfill (resolution);
82
130
}
131
+
132
+ // 13. Let thenJobCallback be HostMakeJobCallback(thenAction).
83
133
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Creating JobCallback for then action @ {}" , &promise, &then_action.as_function ());
84
134
auto then_job_callback = make_job_callback (then_action.as_function ());
135
+
136
+ // 14. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
85
137
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Creating PromiseResolveThenableJob for thenable {}" , &promise, resolution);
86
138
auto * job = PromiseResolveThenableJob::create (global_object, promise, resolution, then_job_callback);
139
+
140
+ // 15. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
87
141
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Enqueuing job @ {}" , &promise, job);
88
142
vm.enqueue_promise_job (*job);
143
+
144
+ // 16. Return undefined.
89
145
return js_undefined ();
90
146
});
91
147
resolve_function->define_direct_property (vm.names .name , js_string (vm, String::empty ()), Attribute::Configurable);
92
148
149
+ // 7. Let stepsReject be the algorithm steps defined in Promise Reject Functions.
150
+ // 8. Let lengthReject be the number of non-optional parameters of the function definition in Promise Reject Functions.
151
+ // 9. Let reject be ! CreateBuiltinFunction(stepsReject, lengthReject, "", « [[Promise]], [[AlreadyResolved]] »).
152
+ // 10. Set reject.[[Promise]] to promise.
153
+ // 11. Set reject.[[AlreadyResolved]] to alreadyResolved.
154
+
93
155
// 27.2.1.3.1 Promise Reject Functions, https://tc39.es/ecma262/#sec-promise-reject-functions
94
156
auto * reject_function = PromiseResolvingFunction::create (global_object (), *this , *already_resolved, [](auto & vm, auto &, auto & promise, auto & already_resolved) {
95
157
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / PromiseResolvingFunction]: Reject function was called" , &promise);
158
+
159
+ auto reason = vm.argument (0 );
160
+
161
+ // 1. Let F be the active function object.
162
+ // 2. Assert: F has a [[Promise]] internal slot whose value is an Object.
163
+ // 3. Let promise be F.[[Promise]].
164
+
165
+ // 4. Let alreadyResolved be F.[[AlreadyResolved]].
166
+ // 5. If alreadyResolved.[[Value]] is true, return undefined.
96
167
if (already_resolved.value )
97
168
return js_undefined ();
169
+
170
+ // 6. Set alreadyResolved.[[Value]] to true.
98
171
already_resolved.value = true ;
99
- auto reason = vm.argument (0 );
172
+
173
+ // 7. Return RejectPromise(promise, reason).
100
174
return promise.reject (reason);
101
175
});
102
176
reject_function->define_direct_property (vm.names .name , js_string (vm, String::empty ()), Attribute::Configurable);
103
177
178
+ // 12. Return the Record { [[Resolve]]: resolve, [[Reject]]: reject }.
104
179
return { *resolve_function, *reject_function };
105
180
}
106
181
107
182
// 27.2.1.4 FulfillPromise ( promise, value ), https://tc39.es/ecma262/#sec-fulfillpromise
108
183
Value Promise::fulfill (Value value)
109
184
{
110
185
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / fulfill()]: Fulfilling promise with value {}" , this , value);
186
+
187
+ // 1. Assert: The value of promise.[[PromiseState]] is pending.
111
188
VERIFY (m_state == State::Pending);
112
189
VERIFY (!value.is_empty ());
113
- m_state = State::Fulfilled;
190
+
191
+ // 2. Let reactions be promise.[[PromiseFulfillReactions]].
192
+ // NOTE: This is a noop, we do these steps in a slightly different order.
193
+
194
+ // 3. Set promise.[[PromiseResult]] to value.
114
195
m_result = value;
196
+
197
+ // 4. Set promise.[[PromiseFulfillReactions]] to undefined.
198
+ // 5. Set promise.[[PromiseRejectReactions]] to undefined.
199
+
200
+ // 6. Set promise.[[PromiseState]] to fulfilled.
201
+ m_state = State::Fulfilled;
202
+
203
+ // 7. Return TriggerPromiseReactions(reactions, value).
115
204
trigger_reactions ();
116
205
m_fulfill_reactions.clear ();
117
206
m_reject_reactions.clear ();
@@ -121,14 +210,31 @@ Value Promise::fulfill(Value value)
121
210
// 27.2.1.7 RejectPromise ( promise, reason ), https://tc39.es/ecma262/#sec-rejectpromise
122
211
Value Promise::reject (Value reason)
123
212
{
213
+
124
214
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / reject()]: Rejecting promise with reason {}" , this , reason);
215
+ auto & vm = this ->vm ();
216
+
217
+ // 1. Assert: The value of promise.[[PromiseState]] is pending.
125
218
VERIFY (m_state == State::Pending);
126
219
VERIFY (!reason.is_empty ());
127
- auto & vm = this ->vm ();
128
- m_state = State::Rejected;
220
+
221
+ // 2. Let reactions be promise.[[PromiseRejectReactions]].
222
+ // NOTE: This is a noop, we do these steps in a slightly different order.
223
+
224
+ // 3. Set promise.[[PromiseResult]] to reason.
129
225
m_result = reason;
226
+
227
+ // 4. Set promise.[[PromiseFulfillReactions]] to undefined.
228
+ // 5. Set promise.[[PromiseRejectReactions]] to undefined.
229
+
230
+ // 6. Set promise.[[PromiseState]] to rejected.
231
+ m_state = State::Rejected;
232
+
233
+ // 7. If promise.[[PromiseIsHandled]] is false, perform HostPromiseRejectionTracker(promise, "reject").
130
234
if (!m_is_handled)
131
235
vm.promise_rejection_tracker (*this , RejectionOperation::Reject);
236
+
237
+ // 8. Return TriggerPromiseReactions(reactions, reason).
132
238
trigger_reactions ();
133
239
m_fulfill_reactions.clear ();
134
240
m_reject_reactions.clear ();
@@ -143,58 +249,104 @@ void Promise::trigger_reactions() const
143
249
auto & reactions = m_state == State::Fulfilled
144
250
? m_fulfill_reactions
145
251
: m_reject_reactions;
252
+
253
+ // 1. For each element reaction of reactions, do
146
254
for (auto & reaction : reactions) {
255
+ // a. Let job be NewPromiseReactionJob(reaction, argument).
147
256
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / trigger_reactions()]: Creating PromiseReactionJob for PromiseReaction @ {} with argument {}" , this , &reaction, m_result);
148
257
auto * job = PromiseReactionJob::create (global_object (), *reaction, m_result);
258
+
259
+ // b. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
149
260
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / trigger_reactions()]: Enqueuing job @ {}" , this , job);
150
261
vm.enqueue_promise_job (*job);
151
262
}
263
+
152
264
if constexpr (PROMISE_DEBUG) {
153
265
if (reactions.is_empty ())
154
266
dbgln (" [Promise @ {} / trigger_reactions()]: No reactions!" , this );
155
267
}
268
+
269
+ // 2. Return undefined.
156
270
}
157
271
158
272
// 27.2.5.4.1 PerformPromiseThen ( promise, onFulfilled, onRejected [ , resultCapability ] ), https://tc39.es/ecma262/#sec-performpromisethen
159
273
Value Promise::perform_then (Value on_fulfilled, Value on_rejected, Optional<PromiseCapability> result_capability)
160
274
{
161
275
auto & vm = this ->vm ();
162
276
277
+ // 1. Assert: IsPromise(promise) is true.
278
+ // 2. If resultCapability is not present, then
279
+ // a. Set resultCapability to undefined.
280
+
281
+ // 3. If IsCallable(onFulfilled) is false, then
282
+ // a. Let onFulfilledJobCallback be empty.
163
283
Optional<JobCallback> on_fulfilled_job_callback;
284
+
285
+ // 4. Else,
164
286
if (on_fulfilled.is_function ()) {
287
+ // a. Let onFulfilledJobCallback be HostMakeJobCallback(onFulfilled).
165
288
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: Creating JobCallback for on_fulfilled function @ {}" , this , &on_fulfilled.as_function ());
166
289
on_fulfilled_job_callback = make_job_callback (on_fulfilled.as_function ());
167
290
}
168
291
292
+ // 5. If IsCallable(onRejected) is false, then
293
+ // a. Let onRejectedJobCallback be empty.
169
294
Optional<JobCallback> on_rejected_job_callback;
295
+
296
+ // 6. Else,
170
297
if (on_rejected.is_function ()) {
298
+ // a. Let onRejectedJobCallback be HostMakeJobCallback(onRejected).
171
299
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: Creating JobCallback for on_rejected function @ {}" , this , &on_rejected.as_function ());
172
300
on_rejected_job_callback = make_job_callback (on_rejected.as_function ());
173
301
}
174
302
303
+ // 7. Let fulfillReaction be the PromiseReaction { [[Capability]]: resultCapability, [[Type]]: Fulfill, [[Handler]]: onFulfilledJobCallback }.
175
304
auto * fulfill_reaction = PromiseReaction::create (vm, PromiseReaction::Type::Fulfill, result_capability, on_fulfilled_job_callback);
305
+
306
+ // 8. Let rejectReaction be the PromiseReaction { [[Capability]]: resultCapability, [[Type]]: Reject, [[Handler]]: onRejectedJobCallback }.
176
307
auto * reject_reaction = PromiseReaction::create (vm, PromiseReaction::Type::Reject, result_capability, on_rejected_job_callback);
177
308
178
309
switch (m_state) {
310
+ // 9. If promise.[[PromiseState]] is pending, then
179
311
case Promise::State::Pending:
180
312
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: state is State::Pending, adding fulfill/reject reactions" , this );
313
+
314
+ // a. Append fulfillReaction as the last element of the List that is promise.[[PromiseFulfillReactions]].
181
315
m_fulfill_reactions.append (fulfill_reaction);
316
+
317
+ // b. Append rejectReaction as the last element of the List that is promise.[[PromiseRejectReactions]].
182
318
m_reject_reactions.append (reject_reaction);
183
319
break ;
320
+ // 10. Else if promise.[[PromiseState]] is fulfilled, then
184
321
case Promise::State::Fulfilled: {
322
+ // a. Let value be promise.[[PromiseResult]].
185
323
auto value = m_result;
324
+
325
+ // b. Let fulfillJob be NewPromiseReactionJob(fulfillReaction, value).
186
326
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: State is State::Fulfilled, creating PromiseReactionJob for PromiseReaction @ {} with argument {}" , this , fulfill_reaction, value);
187
327
auto * fulfill_job = PromiseReactionJob::create (global_object (), *fulfill_reaction, value);
328
+
329
+ // c. Perform HostEnqueuePromiseJob(fulfillJob.[[Job]], fulfillJob.[[Realm]]).
188
330
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: Enqueuing job @ {}" , this , fulfill_job);
189
331
vm.enqueue_promise_job (*fulfill_job);
190
332
break ;
191
333
}
334
+ // 11. Else,
192
335
case Promise::State::Rejected: {
336
+ // a. Assert: The value of promise.[[PromiseState]] is rejected.
337
+
338
+ // b. Let reason be promise.[[PromiseResult]].
193
339
auto reason = m_result;
340
+
341
+ // c. If promise.[[PromiseIsHandled]] is false, perform HostPromiseRejectionTracker(promise, "handle").
194
342
if (!m_is_handled)
195
343
vm.promise_rejection_tracker (*this , RejectionOperation::Handle);
344
+
345
+ // d. Let rejectJob be NewPromiseReactionJob(rejectReaction, reason).
196
346
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: State is State::Rejected, creating PromiseReactionJob for PromiseReaction @ {} with argument {}" , this , reject_reaction, reason);
197
347
auto * reject_job = PromiseReactionJob::create (global_object (), *reject_reaction, reason);
348
+
349
+ // e. Perform HostEnqueuePromiseJob(rejectJob.[[Job]], rejectJob.[[Realm]]).
198
350
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: Enqueuing job @ {}" , this , reject_job);
199
351
vm.enqueue_promise_job (*reject_job);
200
352
break ;
@@ -203,12 +355,18 @@ Value Promise::perform_then(Value on_fulfilled, Value on_rejected, Optional<Prom
203
355
VERIFY_NOT_REACHED ();
204
356
}
205
357
358
+ // 12. Set promise.[[PromiseIsHandled]] to true.
206
359
m_is_handled = true ;
207
360
361
+ // 13. If resultCapability is undefined, then
208
362
if (!result_capability.has_value ()) {
363
+ // a. Return undefined.
209
364
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: No ResultCapability, returning undefined" , this );
210
365
return js_undefined ();
211
366
}
367
+
368
+ // 14. Else,
369
+ // a. Return resultCapability.[[Promise]].
212
370
auto * promise = result_capability.value ().promise ;
213
371
dbgln_if (PROMISE_DEBUG, " [Promise @ {} / perform_then()]: Returning Promise @ {} from ResultCapability @ {}" , this , promise, &result_capability.value ());
214
372
return promise;
0 commit comments