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