OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function(global, utils, extrasUtils) { | 5 (function(global, utils, extrasUtils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 if (debug_is_active) %DebugPushPromise(deferred.promise); | 125 if (debug_is_active) %DebugPushPromise(deferred.promise); |
126 var result = handler(value); | 126 var result = handler(value); |
127 if (IS_UNDEFINED(deferred.resolve)) { | 127 if (IS_UNDEFINED(deferred.resolve)) { |
128 ResolvePromise(deferred.promise, result); | 128 ResolvePromise(deferred.promise, result); |
129 } else { | 129 } else { |
130 %_Call(deferred.resolve, UNDEFINED, result); | 130 %_Call(deferred.resolve, UNDEFINED, result); |
131 } | 131 } |
132 } %catch (exception) { // Natives syntax to mark this catch block. | 132 } %catch (exception) { // Natives syntax to mark this catch block. |
133 try { | 133 try { |
134 if (IS_UNDEFINED(deferred.reject)) { | 134 if (IS_UNDEFINED(deferred.reject)) { |
135 // Pass false for debugEvent so .then chaining does not trigger | 135 // Pass false for debugEvent so .then chaining or throwaway promises |
136 // redundant ExceptionEvents. | 136 // in async functions do not trigger redundant ExceptionEvents. |
137 %PromiseReject(deferred.promise, exception, false); | 137 %PromiseReject(deferred.promise, exception, false); |
138 PromiseSet(deferred.promise, kRejected, exception); | 138 PromiseSet(deferred.promise, kRejected, exception); |
139 } else { | 139 } else { |
140 %_Call(deferred.reject, UNDEFINED, exception); | 140 %_Call(deferred.reject, UNDEFINED, exception); |
141 } | 141 } |
142 } catch (e) { } | 142 } catch (e) { } |
143 } finally { | 143 } finally { |
144 if (debug_is_active) %DebugPopPromise(); | 144 if (debug_is_active) %DebugPopPromise(); |
145 } | 145 } |
146 } | 146 } |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 %PromiseReject(promise, reason, debugEvent); | 284 %PromiseReject(promise, reason, debugEvent); |
285 PromiseSet(promise, kRejected, reason); | 285 PromiseSet(promise, kRejected, reason); |
286 } | 286 } |
287 | 287 |
288 // Export to bindings | 288 // Export to bindings |
289 function DoRejectPromise(promise, reason) { | 289 function DoRejectPromise(promise, reason) { |
290 %PromiseReject(promise, reason, true); | 290 %PromiseReject(promise, reason, true); |
291 PromiseSet(promise, kRejected, reason); | 291 PromiseSet(promise, kRejected, reason); |
292 } | 292 } |
293 | 293 |
| 294 // The resultCapability.promise is only ever fulfilled internally, |
| 295 // so we don't need the closures to protect against accidentally |
| 296 // calling them multiple times. |
| 297 function CreateInternalPromiseCapability() { |
| 298 return { |
| 299 promise: PromiseCreate(), |
| 300 resolve: UNDEFINED, |
| 301 reject: UNDEFINED |
| 302 }; |
| 303 } |
| 304 |
294 // ES#sec-newpromisecapability | 305 // ES#sec-newpromisecapability |
295 // NewPromiseCapability ( C ) | 306 // NewPromiseCapability ( C ) |
296 function NewPromiseCapability(C, debugEvent) { | 307 function NewPromiseCapability(C, debugEvent) { |
297 if (C === GlobalPromise) { | 308 if (C === GlobalPromise) { |
298 // Optimized case, avoid extra closure. | 309 // Optimized case, avoid extra closure. |
299 var promise = PromiseCreate(); | 310 var promise = PromiseCreate(); |
300 // TODO(gsathya): Remove container for callbacks when this is | 311 // TODO(gsathya): Remove container for callbacks when this is |
301 // moved to CPP/TF. | 312 // moved to CPP/TF. |
302 var callbacks = %create_resolving_functions(promise, debugEvent); | 313 var callbacks = %create_resolving_functions(promise, debugEvent); |
303 return { | 314 return { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 // Promise.prototype.then ( onFulfilled, onRejected ) | 385 // Promise.prototype.then ( onFulfilled, onRejected ) |
375 // Multi-unwrapped chaining with thenable coercion. | 386 // Multi-unwrapped chaining with thenable coercion. |
376 function PromiseThen(onResolve, onReject) { | 387 function PromiseThen(onResolve, onReject) { |
377 var status = GET_PRIVATE(this, promiseStateSymbol); | 388 var status = GET_PRIVATE(this, promiseStateSymbol); |
378 if (IS_UNDEFINED(status)) { | 389 if (IS_UNDEFINED(status)) { |
379 throw %make_type_error(kNotAPromise, this); | 390 throw %make_type_error(kNotAPromise, this); |
380 } | 391 } |
381 | 392 |
382 var constructor = SpeciesConstructor(this, GlobalPromise); | 393 var constructor = SpeciesConstructor(this, GlobalPromise); |
383 var resultCapability; | 394 var resultCapability; |
384 | |
385 // The resultCapability.promise is only ever fulfilled internally, | |
386 // so we don't need the closures to protect against accidentally | |
387 // calling them multiple times. | |
388 if (constructor === GlobalPromise) { | 395 if (constructor === GlobalPromise) { |
389 // TODO(gsathya): Combine this into NewPromiseCapability. | 396 resultCapability = CreateInternalPromiseCapability(); |
390 resultCapability = { | |
391 promise: PromiseCreate(), | |
392 resolve: UNDEFINED, | |
393 reject: UNDEFINED | |
394 }; | |
395 } else { | 397 } else { |
396 // Pass false for debugEvent so .then chaining does not trigger | 398 // Pass false for debugEvent so .then chaining does not trigger |
397 // redundant ExceptionEvents. | 399 // redundant ExceptionEvents. |
398 resultCapability = NewPromiseCapability(constructor, false); | 400 resultCapability = NewPromiseCapability(constructor, false); |
399 } | 401 } |
400 return PerformPromiseThen(this, onResolve, onReject, resultCapability); | 402 return PerformPromiseThen(this, onResolve, onReject, resultCapability); |
401 } | 403 } |
402 | 404 |
403 // ES#sec-promise.prototype.catch | 405 // ES#sec-promise.prototype.catch |
404 // Promise.prototype.catch ( onRejected ) | 406 // Promise.prototype.catch ( onRejected ) |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 "resolvePromise", ResolvePromise, | 636 "resolvePromise", ResolvePromise, |
635 "rejectPromise", DoRejectPromise | 637 "rejectPromise", DoRejectPromise |
636 ]); | 638 ]); |
637 | 639 |
638 utils.Export(function(to) { | 640 utils.Export(function(to) { |
639 to.IsPromise = IsPromise; | 641 to.IsPromise = IsPromise; |
640 to.PromiseCreate = PromiseCreate; | 642 to.PromiseCreate = PromiseCreate; |
641 to.PromiseThen = PromiseThen; | 643 to.PromiseThen = PromiseThen; |
642 | 644 |
643 to.GlobalPromise = GlobalPromise; | 645 to.GlobalPromise = GlobalPromise; |
644 to.NewPromiseCapability = NewPromiseCapability; | 646 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; |
645 to.PerformPromiseThen = PerformPromiseThen; | 647 to.PerformPromiseThen = PerformPromiseThen; |
646 to.ResolvePromise = ResolvePromise; | 648 to.ResolvePromise = ResolvePromise; |
647 to.RejectPromise = RejectPromise; | 649 to.RejectPromise = RejectPromise; |
648 }); | 650 }); |
649 | 651 |
650 }) | 652 }) |
OLD | NEW |