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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 }); | 42 }); |
43 | 43 |
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 const kResolveCallback = 0; |
53 // CreateResolvingFunctions ( promise ) | 53 const kRejectCallback = 1; |
54 function CreateResolvingFunctions(promise, debugEvent) { | |
55 var alreadyResolved = false; | |
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 } | |
80 | |
81 | 54 |
82 // ES#sec-promise-executor | 55 // ES#sec-promise-executor |
83 // Promise ( executor ) | 56 // Promise ( executor ) |
84 var GlobalPromise = function Promise(executor) { | 57 var GlobalPromise = function Promise(executor) { |
85 if (executor === promiseRawSymbol) { | 58 if (executor === promiseRawSymbol) { |
86 return %_NewObject(GlobalPromise, new.target); | 59 return %_NewObject(GlobalPromise, new.target); |
87 } | 60 } |
88 if (IS_UNDEFINED(new.target)) throw %make_type_error(kNotAPromise, this); | 61 if (IS_UNDEFINED(new.target)) throw %make_type_error(kNotAPromise, this); |
89 if (!IS_CALLABLE(executor)) { | 62 if (!IS_CALLABLE(executor)) { |
90 throw %make_type_error(kResolverNotAFunction, executor); | 63 throw %make_type_error(kResolverNotAFunction, executor); |
91 } | 64 } |
92 | 65 |
93 var promise = PromiseInit(%_NewObject(GlobalPromise, new.target)); | 66 var promise = PromiseInit(%_NewObject(GlobalPromise, new.target)); |
94 // Calling the reject function would be a new exception, so debugEvent = true | 67 // Calling the reject function would be a new exception, so debugEvent = true |
95 var callbacks = CreateResolvingFunctions(promise, true); | 68 var callbacks = %create_resolving_functions(promise, true); |
Benedikt Meurer
2016/11/09 05:14:30
There seems to be no need to have the container ob
gsathya
2016/11/09 15:57:44
Yeah this container exists only because of the pie
| |
96 var debug_is_active = DEBUG_IS_ACTIVE; | 69 var debug_is_active = DEBUG_IS_ACTIVE; |
97 try { | 70 try { |
98 if (debug_is_active) %DebugPushPromise(promise); | 71 if (debug_is_active) %DebugPushPromise(promise); |
99 executor(callbacks.resolve, callbacks.reject); | 72 executor(callbacks[kResolveCallback], callbacks[kRejectCallback]); |
100 } %catch (e) { // Natives syntax to mark this catch block. | 73 } %catch (e) { // Natives syntax to mark this catch block. |
101 %_Call(callbacks.reject, UNDEFINED, e); | 74 %_Call(callbacks[kRejectCallback], UNDEFINED, e); |
102 } finally { | 75 } finally { |
103 if (debug_is_active) %DebugPopPromise(); | 76 if (debug_is_active) %DebugPopPromise(); |
104 } | 77 } |
105 | 78 |
106 return promise; | 79 return promise; |
107 } | 80 } |
108 | 81 |
109 // Core functionality. | 82 // Core functionality. |
110 | 83 |
111 function PromiseSet(promise, status, value) { | 84 function PromiseSet(promise, status, value) { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 } | 257 } |
285 // Don't cause a debug event as this case is forwarding a rejection | 258 // Don't cause a debug event as this case is forwarding a rejection |
286 %PromiseReject(promise, thenableValue, false); | 259 %PromiseReject(promise, thenableValue, false); |
287 PromiseSet(promise, kRejected, thenableValue); | 260 PromiseSet(promise, kRejected, thenableValue); |
288 SET_PRIVATE(resolution, promiseHasHandlerSymbol, true); | 261 SET_PRIVATE(resolution, promiseHasHandlerSymbol, true); |
289 return; | 262 return; |
290 } | 263 } |
291 } | 264 } |
292 | 265 |
293 if (IS_CALLABLE(then)) { | 266 if (IS_CALLABLE(then)) { |
294 var callbacks = CreateResolvingFunctions(promise, false); | 267 var callbacks = %create_resolving_functions(promise, false); |
Benedikt Meurer
2016/11/09 05:14:30
Same here.
gsathya
2016/11/09 15:57:45
Done.
| |
295 if (DEBUG_IS_ACTIVE && IsPromise(resolution)) { | 268 if (DEBUG_IS_ACTIVE && IsPromise(resolution)) { |
296 // Mark the dependency of the new promise on the resolution | 269 // Mark the dependency of the new promise on the resolution |
297 SET_PRIVATE(resolution, promiseHandledBySymbol, promise); | 270 SET_PRIVATE(resolution, promiseHandledBySymbol, promise); |
298 } | 271 } |
299 %EnqueuePromiseResolveThenableJob( | 272 %EnqueuePromiseResolveThenableJob( |
300 resolution, then, callbacks.resolve, callbacks.reject); | 273 resolution, then, callbacks[kResolveCallback], |
274 callbacks[kRejectCallback]); | |
301 return; | 275 return; |
302 } | 276 } |
303 } | 277 } |
304 %PromiseFulfill(promise, kFulfilled, resolution, | 278 %PromiseFulfill(promise, kFulfilled, resolution, |
305 promiseFulfillReactionsSymbol); | 279 promiseFulfillReactionsSymbol); |
306 PromiseSet(promise, kFulfilled, resolution); | 280 PromiseSet(promise, kFulfilled, resolution); |
307 } | 281 } |
308 | 282 |
309 // Only used by async-await.js | 283 // Only used by async-await.js |
310 function RejectPromise(promise, reason) { | 284 function RejectPromise(promise, reason, debugEvent) { |
311 %PromiseReject(promise, reason, false); | 285 %PromiseReject(promise, reason, debugEvent); |
312 PromiseSet(promise, kRejected, reason); | 286 PromiseSet(promise, kRejected, reason); |
313 } | 287 } |
314 | 288 |
315 // Export to bindings | 289 // Export to bindings |
316 function DoRejectPromise(promise, reason) { | 290 function DoRejectPromise(promise, reason) { |
317 %PromiseReject(promise, reason, true); | 291 %PromiseReject(promise, reason, true); |
318 PromiseSet(promise, kRejected, reason); | 292 PromiseSet(promise, kRejected, reason); |
319 } | 293 } |
320 | 294 |
321 // ES#sec-newpromisecapability | 295 // ES#sec-newpromisecapability |
322 // NewPromiseCapability ( C ) | 296 // NewPromiseCapability ( C ) |
323 function NewPromiseCapability(C, debugEvent) { | 297 function NewPromiseCapability(C, debugEvent) { |
324 if (C === GlobalPromise) { | 298 if (C === GlobalPromise) { |
325 // Optimized case, avoid extra closure. | 299 // Optimized case, avoid extra closure. |
326 var promise = PromiseCreate(); | 300 var promise = PromiseCreate(); |
327 var callbacks = CreateResolvingFunctions(promise, debugEvent); | 301 var callbacks = %create_resolving_functions(promise, debugEvent); |
Benedikt Meurer
2016/11/09 05:14:30
And here.
gsathya
2016/11/09 15:57:44
Done.
| |
328 return { | 302 return { |
329 promise: promise, | 303 promise: promise, |
330 resolve: callbacks.resolve, | 304 resolve: callbacks[kResolveCallback], |
331 reject: callbacks.reject | 305 reject: callbacks[kRejectCallback] |
332 }; | 306 }; |
333 } | 307 } |
334 | 308 |
335 var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; | 309 var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; |
336 result.promise = new C((resolve, reject) => { | 310 result.promise = new C((resolve, reject) => { |
337 if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject)) | 311 if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject)) |
338 throw %make_type_error(kPromiseExecutorAlreadyInvoked); | 312 throw %make_type_error(kPromiseExecutorAlreadyInvoked); |
339 result.resolve = resolve; | 313 result.resolve = resolve; |
340 result.reject = reject; | 314 result.reject = reject; |
341 }); | 315 }); |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
636 utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [ | 610 utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [ |
637 "then", PromiseThen, | 611 "then", PromiseThen, |
638 "catch", PromiseCatch | 612 "catch", PromiseCatch |
639 ]); | 613 ]); |
640 | 614 |
641 %InstallToContext([ | 615 %InstallToContext([ |
642 "promise_catch", PromiseCatch, | 616 "promise_catch", PromiseCatch, |
643 "promise_create", PromiseCreate, | 617 "promise_create", PromiseCreate, |
644 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, | 618 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, |
645 "promise_reject", DoRejectPromise, | 619 "promise_reject", DoRejectPromise, |
620 // TODO(gsathya): Remove this once we update the promise builtin. | |
621 "promise_internal_reject", RejectPromise, | |
646 "promise_resolve", ResolvePromise, | 622 "promise_resolve", ResolvePromise, |
647 "promise_then", PromiseThen, | 623 "promise_then", PromiseThen, |
648 "promise_handle", PromiseHandle, | 624 "promise_handle", PromiseHandle, |
649 "promise_debug_get_info", PromiseDebugGetInfo | 625 "promise_debug_get_info", PromiseDebugGetInfo |
650 ]); | 626 ]); |
651 | 627 |
652 // This allows extras to create promises quickly without building extra | 628 // This allows extras to create promises quickly without building extra |
653 // resolve/reject closures, and allows them to later resolve and reject any | 629 // resolve/reject closures, and allows them to later resolve and reject any |
654 // promise without having to hold on to those closures forever. | 630 // promise without having to hold on to those closures forever. |
655 utils.InstallFunctions(extrasUtils, 0, [ | 631 utils.InstallFunctions(extrasUtils, 0, [ |
656 "createPromise", PromiseCreate, | 632 "createPromise", PromiseCreate, |
657 "resolvePromise", ResolvePromise, | 633 "resolvePromise", ResolvePromise, |
658 "rejectPromise", DoRejectPromise | 634 "rejectPromise", DoRejectPromise |
659 ]); | 635 ]); |
660 | 636 |
661 utils.Export(function(to) { | 637 utils.Export(function(to) { |
662 to.IsPromise = IsPromise; | 638 to.IsPromise = IsPromise; |
663 to.PromiseCreate = PromiseCreate; | 639 to.PromiseCreate = PromiseCreate; |
664 to.PromiseThen = PromiseThen; | 640 to.PromiseThen = PromiseThen; |
665 | 641 |
666 to.GlobalPromise = GlobalPromise; | 642 to.GlobalPromise = GlobalPromise; |
667 to.NewPromiseCapability = NewPromiseCapability; | 643 to.NewPromiseCapability = NewPromiseCapability; |
668 to.PerformPromiseThen = PerformPromiseThen; | 644 to.PerformPromiseThen = PerformPromiseThen; |
669 to.ResolvePromise = ResolvePromise; | 645 to.ResolvePromise = ResolvePromise; |
670 to.RejectPromise = RejectPromise; | 646 to.RejectPromise = RejectPromise; |
671 }); | 647 }); |
672 | 648 |
673 }) | 649 }) |
OLD | NEW |