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 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 // Shortcut Promise.reject and Promise.resolve() implementations, used by | 384 // Shortcut Promise.reject and Promise.resolve() implementations, used by |
385 // Async Functions implementation. | 385 // Async Functions implementation. |
386 function PromiseCreateRejected(r) { | 386 function PromiseCreateRejected(r) { |
387 return %_Call(PromiseReject, GlobalPromise, r); | 387 return %_Call(PromiseReject, GlobalPromise, r); |
388 } | 388 } |
389 | 389 |
390 function PromiseCreateResolved(x) { | 390 function PromiseCreateResolved(x) { |
391 return %_Call(PromiseResolve, GlobalPromise, x); | 391 return %_Call(PromiseResolve, GlobalPromise, x); |
392 } | 392 } |
393 | 393 |
| 394 function PerformPromiseThen(promise, onResolve, onReject, resultCapability) { |
| 395 if (!IS_CALLABLE(onResolve)) onResolve = PromiseIdResolveHandler; |
| 396 if (!IS_CALLABLE(onReject)) onReject = PromiseIdRejectHandler; |
| 397 |
| 398 var status = GET_PRIVATE(promise, promiseStateSymbol); |
| 399 switch (status) { |
| 400 case kPending: |
| 401 PromiseAttachCallbacks(promise, resultCapability, onResolve, onReject); |
| 402 break; |
| 403 case kFulfilled: |
| 404 PromiseEnqueue(GET_PRIVATE(promise, promiseResultSymbol), |
| 405 onResolve, resultCapability, kFulfilled); |
| 406 break; |
| 407 case kRejected: |
| 408 if (!HAS_DEFINED_PRIVATE(promise, promiseHasHandlerSymbol)) { |
| 409 // Promise has already been rejected, but had no handler. |
| 410 // Revoke previously triggered reject event. |
| 411 %PromiseRevokeReject(promise); |
| 412 } |
| 413 PromiseEnqueue(GET_PRIVATE(promise, promiseResultSymbol), |
| 414 onReject, resultCapability, kRejected); |
| 415 break; |
| 416 } |
| 417 |
| 418 // Mark this promise as having handler. |
| 419 SET_PRIVATE(promise, promiseHasHandlerSymbol, true); |
| 420 return resultCapability.promise; |
| 421 } |
| 422 |
394 // ES#sec-promise.prototype.then | 423 // ES#sec-promise.prototype.then |
395 // Promise.prototype.then ( onFulfilled, onRejected ) | 424 // Promise.prototype.then ( onFulfilled, onRejected ) |
396 // Multi-unwrapped chaining with thenable coercion. | 425 // Multi-unwrapped chaining with thenable coercion. |
397 function PromiseThen(onResolve, onReject) { | 426 function PromiseThen(onResolve, onReject) { |
398 var status = GET_PRIVATE(this, promiseStateSymbol); | 427 var status = GET_PRIVATE(this, promiseStateSymbol); |
399 if (IS_UNDEFINED(status)) { | 428 if (IS_UNDEFINED(status)) { |
400 throw MakeTypeError(kNotAPromise, this); | 429 throw MakeTypeError(kNotAPromise, this); |
401 } | 430 } |
402 | 431 |
403 var constructor = SpeciesConstructor(this, GlobalPromise); | 432 var constructor = SpeciesConstructor(this, GlobalPromise); |
404 onResolve = IS_CALLABLE(onResolve) ? onResolve : PromiseIdResolveHandler; | 433 var resultCapability = NewPromiseCapability(constructor); |
405 onReject = IS_CALLABLE(onReject) ? onReject : PromiseIdRejectHandler; | 434 return PerformPromiseThen(this, onResolve, onReject, resultCapability); |
406 var deferred = NewPromiseCapability(constructor); | |
407 switch (status) { | |
408 case kPending: | |
409 PromiseAttachCallbacks(this, deferred, onResolve, onReject); | |
410 break; | |
411 case kFulfilled: | |
412 PromiseEnqueue(GET_PRIVATE(this, promiseResultSymbol), | |
413 onResolve, deferred, kFulfilled); | |
414 break; | |
415 case kRejected: | |
416 if (!HAS_DEFINED_PRIVATE(this, promiseHasHandlerSymbol)) { | |
417 // Promise has already been rejected, but had no handler. | |
418 // Revoke previously triggered reject event. | |
419 %PromiseRevokeReject(this); | |
420 } | |
421 PromiseEnqueue(GET_PRIVATE(this, promiseResultSymbol), | |
422 onReject, deferred, kRejected); | |
423 break; | |
424 } | |
425 // Mark this promise as having handler. | |
426 SET_PRIVATE(this, promiseHasHandlerSymbol, true); | |
427 return deferred.promise; | |
428 } | 435 } |
429 | 436 |
430 // Unspecified V8-specific legacy function | 437 // Unspecified V8-specific legacy function |
431 // Chain is left around for now as an alias for then | 438 // Chain is left around for now as an alias for then |
432 function PromiseChain(onResolve, onReject) { | 439 function PromiseChain(onResolve, onReject) { |
433 %IncrementUseCounter(kPromiseChain); | 440 %IncrementUseCounter(kPromiseChain); |
434 return %_Call(PromiseThen, this, onResolve, onReject); | 441 return %_Call(PromiseThen, this, onResolve, onReject); |
435 } | 442 } |
436 | 443 |
437 // ES#sec-promise.prototype.catch | 444 // ES#sec-promise.prototype.catch |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 fn => %FunctionRemovePrototype(fn)); | 623 fn => %FunctionRemovePrototype(fn)); |
617 | 624 |
618 utils.Export(function(to) { | 625 utils.Export(function(to) { |
619 to.PromiseChain = PromiseChain; | 626 to.PromiseChain = PromiseChain; |
620 to.PromiseDefer = PromiseDefer; | 627 to.PromiseDefer = PromiseDefer; |
621 to.PromiseAccept = PromiseAccept; | 628 to.PromiseAccept = PromiseAccept; |
622 | 629 |
623 to.PromiseCreateRejected = PromiseCreateRejected; | 630 to.PromiseCreateRejected = PromiseCreateRejected; |
624 to.PromiseCreateResolved = PromiseCreateResolved; | 631 to.PromiseCreateResolved = PromiseCreateResolved; |
625 to.PromiseThen = PromiseThen; | 632 to.PromiseThen = PromiseThen; |
| 633 |
| 634 to.GlobalPromise = GlobalPromise; |
| 635 to.NewPromiseCapability = NewPromiseCapability; |
| 636 to.PerformPromiseThen = PerformPromiseThen; |
626 }); | 637 }); |
627 | 638 |
628 }) | 639 }) |
OLD | NEW |