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 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var InternalArray = utils.InternalArray; | 14 var InternalArray = utils.InternalArray; |
| 15 var promiseAwaitHandlerSymbol = |
| 16 utils.ImportNow("promise_await_handler_symbol"); |
15 var promiseCombinedDeferredSymbol = | 17 var promiseCombinedDeferredSymbol = |
16 utils.ImportNow("promise_combined_deferred_symbol"); | 18 utils.ImportNow("promise_combined_deferred_symbol"); |
17 var promiseHasHandlerSymbol = | 19 var promiseHasHandlerSymbol = |
18 utils.ImportNow("promise_has_handler_symbol"); | 20 utils.ImportNow("promise_has_handler_symbol"); |
19 var promiseRejectReactionsSymbol = | 21 var promiseRejectReactionsSymbol = |
20 utils.ImportNow("promise_reject_reactions_symbol"); | 22 utils.ImportNow("promise_reject_reactions_symbol"); |
21 var promiseFulfillReactionsSymbol = | 23 var promiseFulfillReactionsSymbol = |
22 utils.ImportNow("promise_fulfill_reactions_symbol"); | 24 utils.ImportNow("promise_fulfill_reactions_symbol"); |
23 var promiseDeferredReactionsSymbol = | 25 var promiseDeferredReactionsSymbol = |
24 utils.ImportNow("promise_deferred_reactions_symbol"); | 26 utils.ImportNow("promise_deferred_reactions_symbol"); |
| 27 var promiseHandledHintSymbol = |
| 28 utils.ImportNow("promise_handled_hint_symbol"); |
25 var promiseRawSymbol = utils.ImportNow("promise_raw_symbol"); | 29 var promiseRawSymbol = utils.ImportNow("promise_raw_symbol"); |
26 var promiseStateSymbol = utils.ImportNow("promise_state_symbol"); | 30 var promiseStateSymbol = utils.ImportNow("promise_state_symbol"); |
27 var promiseResultSymbol = utils.ImportNow("promise_result_symbol"); | 31 var promiseResultSymbol = utils.ImportNow("promise_result_symbol"); |
28 var SpeciesConstructor; | 32 var SpeciesConstructor; |
29 var speciesSymbol = utils.ImportNow("species_symbol"); | 33 var speciesSymbol = utils.ImportNow("species_symbol"); |
30 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 34 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
31 | 35 |
32 utils.Import(function(from) { | 36 utils.Import(function(from) { |
33 SpeciesConstructor = from.SpeciesConstructor; | 37 SpeciesConstructor = from.SpeciesConstructor; |
34 }); | 38 }); |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 // equivalent to throwing an exception directly. | 378 // equivalent to throwing an exception directly. |
375 %PromiseRejectEventFromStack(promise, r); | 379 %PromiseRejectEventFromStack(promise, r); |
376 return promise; | 380 return promise; |
377 } else { | 381 } else { |
378 var promiseCapability = NewPromiseCapability(this, true); | 382 var promiseCapability = NewPromiseCapability(this, true); |
379 %_Call(promiseCapability.reject, UNDEFINED, r); | 383 %_Call(promiseCapability.reject, UNDEFINED, r); |
380 return promiseCapability.promise; | 384 return promiseCapability.promise; |
381 } | 385 } |
382 } | 386 } |
383 | 387 |
384 function PromiseCastResolved(value) { | |
385 if (IsPromise(value)) { | |
386 return value; | |
387 } else { | |
388 var promise = PromiseInit(new GlobalPromise(promiseRawSymbol)); | |
389 var resolveResult = ResolvePromise(promise, value); | |
390 return promise; | |
391 } | |
392 } | |
393 | |
394 function PerformPromiseThen(promise, onResolve, onReject, resultCapability) { | 388 function PerformPromiseThen(promise, onResolve, onReject, resultCapability) { |
395 if (!IS_CALLABLE(onResolve)) onResolve = PromiseIdResolveHandler; | 389 if (!IS_CALLABLE(onResolve)) onResolve = PromiseIdResolveHandler; |
396 if (!IS_CALLABLE(onReject)) onReject = PromiseIdRejectHandler; | 390 if (!IS_CALLABLE(onReject)) onReject = PromiseIdRejectHandler; |
397 | 391 |
398 var status = GET_PRIVATE(promise, promiseStateSymbol); | 392 var status = GET_PRIVATE(promise, promiseStateSymbol); |
399 switch (status) { | 393 switch (status) { |
400 case kPending: | 394 case kPending: |
401 PromiseAttachCallbacks(promise, resultCapability, onResolve, onReject); | 395 PromiseAttachCallbacks(promise, resultCapability, onResolve, onReject); |
402 break; | 396 break; |
403 case kFulfilled: | 397 case kFulfilled: |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 } catch (e) { | 530 } catch (e) { |
537 deferred.reject(e) | 531 deferred.reject(e) |
538 } | 532 } |
539 return deferred.promise; | 533 return deferred.promise; |
540 } | 534 } |
541 | 535 |
542 | 536 |
543 // Utility for debugger | 537 // Utility for debugger |
544 | 538 |
545 function PromiseHasUserDefinedRejectHandlerCheck(handler, deferred) { | 539 function PromiseHasUserDefinedRejectHandlerCheck(handler, deferred) { |
| 540 if (GET_PRIVATE(handler, promiseAwaitHandlerSymbol)) return false; |
546 if (handler !== PromiseIdRejectHandler) { | 541 if (handler !== PromiseIdRejectHandler) { |
547 var combinedDeferred = GET_PRIVATE(handler, promiseCombinedDeferredSymbol); | 542 var combinedDeferred = GET_PRIVATE(handler, promiseCombinedDeferredSymbol); |
548 if (IS_UNDEFINED(combinedDeferred)) return true; | 543 if (IS_UNDEFINED(combinedDeferred)) return true; |
549 if (PromiseHasUserDefinedRejectHandlerRecursive(combinedDeferred.promise)) { | 544 if (PromiseHasUserDefinedRejectHandlerRecursive(combinedDeferred.promise)) { |
550 return true; | 545 return true; |
551 } | 546 } |
552 } else if (PromiseHasUserDefinedRejectHandlerRecursive(deferred.promise)) { | 547 } else if (PromiseHasUserDefinedRejectHandlerRecursive(deferred.promise)) { |
553 return true; | 548 return true; |
554 } | 549 } |
555 return false; | 550 return false; |
556 } | 551 } |
557 | 552 |
558 function PromiseHasUserDefinedRejectHandlerRecursive(promise) { | 553 function PromiseHasUserDefinedRejectHandlerRecursive(promise) { |
| 554 var handledHintSymbol = GET_PRIVATE(promise, promiseHandledHintSymbol); |
| 555 if (handledHintSymbol) return true; |
559 var queue = GET_PRIVATE(promise, promiseRejectReactionsSymbol); | 556 var queue = GET_PRIVATE(promise, promiseRejectReactionsSymbol); |
560 var deferreds = GET_PRIVATE(promise, promiseDeferredReactionsSymbol); | 557 var deferreds = GET_PRIVATE(promise, promiseDeferredReactionsSymbol); |
561 if (IS_UNDEFINED(queue)) return false; | 558 if (IS_UNDEFINED(queue)) return false; |
562 if (!IS_ARRAY(queue)) { | 559 if (!IS_ARRAY(queue)) { |
563 return PromiseHasUserDefinedRejectHandlerCheck(queue, deferreds); | 560 return PromiseHasUserDefinedRejectHandlerCheck(queue, deferreds); |
564 } else { | 561 } else { |
565 for (var i = 0; i < queue.length; i += 2) { | 562 for (var i = 0; i < queue.length; i += 2) { |
566 if (PromiseHasUserDefinedRejectHandlerCheck(queue[i], queue[i + 1])) { | 563 if (PromiseHasUserDefinedRejectHandlerCheck(queue[i], queue[i + 1])) { |
567 return true; | 564 return true; |
568 } | 565 } |
569 } | 566 } |
| 567 return false; |
570 } | 568 } |
571 return false; | |
572 } | 569 } |
573 | 570 |
574 // Return whether the promise will be handled by a user-defined reject | 571 // Return whether the promise will be handled by a user-defined reject |
575 // handler somewhere down the promise chain. For this, we do a depth-first | 572 // handler somewhere down the promise chain. For this, we do a depth-first |
576 // search for a reject handler that's not the default PromiseIdRejectHandler. | 573 // search for a reject handler that's not the default PromiseIdRejectHandler. |
| 574 // If the Promise was awaited as part of async/await, then an associated |
| 575 // dependency graph is traversed. |
577 function PromiseHasUserDefinedRejectHandler() { | 576 function PromiseHasUserDefinedRejectHandler() { |
578 return PromiseHasUserDefinedRejectHandlerRecursive(this); | 577 return PromiseHasUserDefinedRejectHandlerRecursive(this); |
579 }; | 578 }; |
580 | 579 |
581 | 580 |
582 function PromiseSpecies() { | 581 function PromiseSpecies() { |
583 return this; | 582 return this; |
584 } | 583 } |
585 | 584 |
586 // ------------------------------------------------------------------- | 585 // ------------------------------------------------------------------- |
(...skipping 29 matching lines...) Expand all Loading... |
616 // This allows extras to create promises quickly without building extra | 615 // This allows extras to create promises quickly without building extra |
617 // resolve/reject closures, and allows them to later resolve and reject any | 616 // resolve/reject closures, and allows them to later resolve and reject any |
618 // promise without having to hold on to those closures forever. | 617 // promise without having to hold on to those closures forever. |
619 utils.InstallFunctions(extrasUtils, 0, [ | 618 utils.InstallFunctions(extrasUtils, 0, [ |
620 "createPromise", PromiseCreate, | 619 "createPromise", PromiseCreate, |
621 "resolvePromise", ResolvePromise, | 620 "resolvePromise", ResolvePromise, |
622 "rejectPromise", DoRejectPromise | 621 "rejectPromise", DoRejectPromise |
623 ]); | 622 ]); |
624 | 623 |
625 utils.Export(function(to) { | 624 utils.Export(function(to) { |
626 to.PromiseCastResolved = PromiseCastResolved; | 625 to.IsPromise = IsPromise; |
| 626 to.PromiseCreate = PromiseCreate; |
627 to.PromiseThen = PromiseThen; | 627 to.PromiseThen = PromiseThen; |
628 | 628 |
629 to.GlobalPromise = GlobalPromise; | 629 to.GlobalPromise = GlobalPromise; |
630 to.NewPromiseCapability = NewPromiseCapability; | 630 to.NewPromiseCapability = NewPromiseCapability; |
631 to.PerformPromiseThen = PerformPromiseThen; | 631 to.PerformPromiseThen = PerformPromiseThen; |
| 632 to.ResolvePromise = ResolvePromise; |
632 to.RejectPromise = RejectPromise; | 633 to.RejectPromise = RejectPromise; |
633 }); | 634 }); |
634 | 635 |
635 }) | 636 }) |
OLD | NEW |