Chromium Code Reviews| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 // ------------------------------------------------------------------- | 44 // ------------------------------------------------------------------- |
| 45 | 45 |
| 46 // [[PromiseState]] values: | 46 // [[PromiseState]] values: |
| 47 // These values should be kept in sync with PromiseStatus in globals.h | 47 // These values should be kept in sync with PromiseStatus in globals.h |
| 48 const kPending = 0; | 48 const kPending = 0; |
| 49 const kFulfilled = +1; | 49 const kFulfilled = +1; |
| 50 const kRejected = +2; | 50 const kRejected = +2; |
| 51 | 51 |
| 52 // ES#sec-createresolvingfunctions | 52 // ES#sec-createresolvingfunctions |
| 53 // CreateResolvingFunctions ( promise ) | 53 // CreateResolvingFunctions ( promise ) |
| 54 function CreateResolvingFunctions(promise, debugEvent) { | 54 function CreateResolvingFunctions(promise, debugEvent) { |
|
adamk
2016/11/08 18:09:55
Why do we still need this wrapper function?
gsathya
2016/11/08 20:03:19
Done.
| |
| 55 var alreadyResolved = false; | 55 return %create_resolving_functions(promise, debugEvent); |
| 56 | |
| 57 // ES#sec-promise-resolve-functions | |
| 58 // Promise Resolve Functions | |
| 59 var resolve = value => { | |
| 60 if (alreadyResolved === true) return; | |
| 61 alreadyResolved = true; | |
| 62 ResolvePromise(promise, value); | |
| 63 }; | |
| 64 | |
| 65 // ES#sec-promise-reject-functions | |
| 66 // Promise Reject Functions | |
| 67 var reject = reason => { | |
| 68 if (alreadyResolved === true) return; | |
| 69 alreadyResolved = true; | |
| 70 %PromiseReject(promise, reason, debugEvent); | |
| 71 PromiseSet(promise, kRejected, reason); | |
| 72 }; | |
| 73 | |
| 74 return { | |
| 75 __proto__: null, | |
| 76 resolve: resolve, | |
| 77 reject: reject | |
| 78 }; | |
| 79 } | 56 } |
| 80 | 57 |
| 81 | 58 |
| 82 // ES#sec-promise-executor | 59 // ES#sec-promise-executor |
| 83 // Promise ( executor ) | 60 // Promise ( executor ) |
| 84 var GlobalPromise = function Promise(executor) { | 61 var GlobalPromise = function Promise(executor) { |
| 85 if (executor === promiseRawSymbol) { | 62 if (executor === promiseRawSymbol) { |
| 86 return %_NewObject(GlobalPromise, new.target); | 63 return %_NewObject(GlobalPromise, new.target); |
| 87 } | 64 } |
| 88 if (IS_UNDEFINED(new.target)) throw %make_type_error(kNotAPromise, this); | 65 if (IS_UNDEFINED(new.target)) throw %make_type_error(kNotAPromise, this); |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 resolution, then, callbacks.resolve, callbacks.reject); | 277 resolution, then, callbacks.resolve, callbacks.reject); |
| 301 return; | 278 return; |
| 302 } | 279 } |
| 303 } | 280 } |
| 304 %PromiseFulfill(promise, kFulfilled, resolution, | 281 %PromiseFulfill(promise, kFulfilled, resolution, |
| 305 promiseFulfillReactionsSymbol); | 282 promiseFulfillReactionsSymbol); |
| 306 PromiseSet(promise, kFulfilled, resolution); | 283 PromiseSet(promise, kFulfilled, resolution); |
| 307 } | 284 } |
| 308 | 285 |
| 309 // Only used by async-await.js | 286 // Only used by async-await.js |
| 310 function RejectPromise(promise, reason) { | 287 function RejectPromise(promise, reason, debugEvent) { |
| 311 %PromiseReject(promise, reason, false); | 288 %PromiseReject(promise, reason, debugEvent); |
| 312 PromiseSet(promise, kRejected, reason); | 289 PromiseSet(promise, kRejected, reason); |
| 313 } | 290 } |
| 314 | 291 |
| 315 // Export to bindings | 292 // Export to bindings |
| 316 function DoRejectPromise(promise, reason) { | 293 function DoRejectPromise(promise, reason) { |
| 317 %PromiseReject(promise, reason, true); | 294 %PromiseReject(promise, reason, true); |
| 318 PromiseSet(promise, kRejected, reason); | 295 PromiseSet(promise, kRejected, reason); |
| 319 } | 296 } |
| 320 | 297 |
| 321 // ES#sec-newpromisecapability | 298 // ES#sec-newpromisecapability |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 636 utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [ | 613 utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [ |
| 637 "then", PromiseThen, | 614 "then", PromiseThen, |
| 638 "catch", PromiseCatch | 615 "catch", PromiseCatch |
| 639 ]); | 616 ]); |
| 640 | 617 |
| 641 %InstallToContext([ | 618 %InstallToContext([ |
| 642 "promise_catch", PromiseCatch, | 619 "promise_catch", PromiseCatch, |
| 643 "promise_create", PromiseCreate, | 620 "promise_create", PromiseCreate, |
| 644 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, | 621 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, |
| 645 "promise_reject", DoRejectPromise, | 622 "promise_reject", DoRejectPromise, |
| 623 //TODO(gsathya): Remove this once we update the promise builtin. | |
| 624 "promise_internal_reject", RejectPromise, | |
| 646 "promise_resolve", ResolvePromise, | 625 "promise_resolve", ResolvePromise, |
| 647 "promise_then", PromiseThen, | 626 "promise_then", PromiseThen, |
| 648 "promise_handle", PromiseHandle, | 627 "promise_handle", PromiseHandle, |
| 649 "promise_debug_get_info", PromiseDebugGetInfo | 628 "promise_debug_get_info", PromiseDebugGetInfo |
| 650 ]); | 629 ]); |
| 651 | 630 |
| 652 // This allows extras to create promises quickly without building extra | 631 // This allows extras to create promises quickly without building extra |
| 653 // resolve/reject closures, and allows them to later resolve and reject any | 632 // resolve/reject closures, and allows them to later resolve and reject any |
| 654 // promise without having to hold on to those closures forever. | 633 // promise without having to hold on to those closures forever. |
| 655 utils.InstallFunctions(extrasUtils, 0, [ | 634 utils.InstallFunctions(extrasUtils, 0, [ |
| 656 "createPromise", PromiseCreate, | 635 "createPromise", PromiseCreate, |
| 657 "resolvePromise", ResolvePromise, | 636 "resolvePromise", ResolvePromise, |
| 658 "rejectPromise", DoRejectPromise | 637 "rejectPromise", DoRejectPromise |
| 659 ]); | 638 ]); |
| 660 | 639 |
| 661 utils.Export(function(to) { | 640 utils.Export(function(to) { |
| 662 to.IsPromise = IsPromise; | 641 to.IsPromise = IsPromise; |
| 663 to.PromiseCreate = PromiseCreate; | 642 to.PromiseCreate = PromiseCreate; |
| 664 to.PromiseThen = PromiseThen; | 643 to.PromiseThen = PromiseThen; |
| 665 | 644 |
| 666 to.GlobalPromise = GlobalPromise; | 645 to.GlobalPromise = GlobalPromise; |
| 667 to.NewPromiseCapability = NewPromiseCapability; | 646 to.NewPromiseCapability = NewPromiseCapability; |
| 668 to.PerformPromiseThen = PerformPromiseThen; | 647 to.PerformPromiseThen = PerformPromiseThen; |
| 669 to.ResolvePromise = ResolvePromise; | 648 to.ResolvePromise = ResolvePromise; |
| 670 to.RejectPromise = RejectPromise; | 649 to.RejectPromise = RejectPromise; |
| 671 }); | 650 }); |
| 672 | 651 |
| 673 }) | 652 }) |
| OLD | NEW |