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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 } | 162 } |
163 PromiseSet(promise, status, value); | 163 PromiseSet(promise, status, value); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 function PromiseHandle(value, handler, deferred) { | 167 function PromiseHandle(value, handler, deferred) { |
168 var debug_is_active = DEBUG_IS_ACTIVE; | 168 var debug_is_active = DEBUG_IS_ACTIVE; |
169 try { | 169 try { |
170 if (debug_is_active) %DebugPushPromise(deferred.promise); | 170 if (debug_is_active) %DebugPushPromise(deferred.promise); |
171 var result = handler(value); | 171 var result = handler(value); |
172 deferred.resolve(result); | 172 if (IS_UNDEFINED(deferred.resolve)) { |
173 ResolvePromise(deferred.promise, result); | |
174 } else { | |
175 deferred.resolve(result); | |
176 } | |
173 } %catch (exception) { // Natives syntax to mark this catch block. | 177 } %catch (exception) { // Natives syntax to mark this catch block. |
174 try { deferred.reject(exception); } catch (e) { } | 178 try { |
179 if (IS_UNDEFINED(deferred.reject)) { | |
180 // Pass false for debugEvent so .then chaining does not trigger | |
181 // redundant ExceptionEvents. | |
182 RejectPromise(deferred.promise, exception, false); | |
183 } else { | |
184 deferred.reject(exception); | |
185 } | |
186 } catch (e) { } | |
175 } finally { | 187 } finally { |
176 if (debug_is_active) %DebugPopPromise(); | 188 if (debug_is_active) %DebugPopPromise(); |
177 } | 189 } |
178 } | 190 } |
179 | 191 |
180 function PromiseEnqueue(value, tasks, deferreds, status) { | 192 function PromiseEnqueue(value, tasks, deferreds, status) { |
181 var id, name, instrumenting = DEBUG_IS_ACTIVE; | 193 var id, name, instrumenting = DEBUG_IS_ACTIVE; |
182 %EnqueueMicrotask(function() { | 194 %EnqueueMicrotask(function() { |
183 if (instrumenting) { | 195 if (instrumenting) { |
184 %DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name }); | 196 %DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name }); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 // ES#sec-promise.prototype.then | 457 // ES#sec-promise.prototype.then |
446 // Promise.prototype.then ( onFulfilled, onRejected ) | 458 // Promise.prototype.then ( onFulfilled, onRejected ) |
447 // Multi-unwrapped chaining with thenable coercion. | 459 // Multi-unwrapped chaining with thenable coercion. |
448 function PromiseThen(onResolve, onReject) { | 460 function PromiseThen(onResolve, onReject) { |
449 var status = GET_PRIVATE(this, promiseStateSymbol); | 461 var status = GET_PRIVATE(this, promiseStateSymbol); |
450 if (IS_UNDEFINED(status)) { | 462 if (IS_UNDEFINED(status)) { |
451 throw %make_type_error(kNotAPromise, this); | 463 throw %make_type_error(kNotAPromise, this); |
452 } | 464 } |
453 | 465 |
454 var constructor = SpeciesConstructor(this, GlobalPromise); | 466 var constructor = SpeciesConstructor(this, GlobalPromise); |
455 // Pass false for debugEvent so .then chaining does not trigger | 467 var resultCapability; |
456 // redundant ExceptionEvents. | 468 |
457 var resultCapability = NewPromiseCapability(constructor, false); | 469 // The resultCapability.promise is only ever fulfilled internally, |
470 // so we don't need the closures to protect against accidentally | |
471 // calling them multiple times. | |
472 if (constructor === GlobalPromise) { | |
473 resultCapability = { | |
adamk
2016/10/05 23:05:40
Can you add a TODO for a followup patch to combine
| |
474 promise: PromiseCreate(), | |
475 resolve: UNDEFINED, | |
476 reject: UNDEFINED | |
477 }; | |
478 } else { | |
479 // Pass false for debugEvent so .then chaining does not trigger | |
480 // redundant ExceptionEvents. | |
481 resultCapability = NewPromiseCapability(constructor, false); | |
482 } | |
458 return PerformPromiseThen(this, onResolve, onReject, resultCapability); | 483 return PerformPromiseThen(this, onResolve, onReject, resultCapability); |
459 } | 484 } |
460 | 485 |
461 // ES#sec-promise.prototype.catch | 486 // ES#sec-promise.prototype.catch |
462 // Promise.prototype.catch ( onRejected ) | 487 // Promise.prototype.catch ( onRejected ) |
463 function PromiseCatch(onReject) { | 488 function PromiseCatch(onReject) { |
464 return this.then(UNDEFINED, onReject); | 489 return this.then(UNDEFINED, onReject); |
465 } | 490 } |
466 | 491 |
467 // Combinators. | 492 // Combinators. |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
696 to.PromiseNextMicrotaskID = PromiseNextMicrotaskID; | 721 to.PromiseNextMicrotaskID = PromiseNextMicrotaskID; |
697 | 722 |
698 to.GlobalPromise = GlobalPromise; | 723 to.GlobalPromise = GlobalPromise; |
699 to.NewPromiseCapability = NewPromiseCapability; | 724 to.NewPromiseCapability = NewPromiseCapability; |
700 to.PerformPromiseThen = PerformPromiseThen; | 725 to.PerformPromiseThen = PerformPromiseThen; |
701 to.ResolvePromise = ResolvePromise; | 726 to.ResolvePromise = ResolvePromise; |
702 to.RejectPromise = RejectPromise; | 727 to.RejectPromise = RejectPromise; |
703 }); | 728 }); |
704 | 729 |
705 }) | 730 }) |
OLD | NEW |