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 // TODO(gsathya): Combine this into NewPromiseCapability. |
| 474 resultCapability = { |
| 475 promise: PromiseCreate(), |
| 476 resolve: UNDEFINED, |
| 477 reject: UNDEFINED |
| 478 }; |
| 479 } else { |
| 480 // Pass false for debugEvent so .then chaining does not trigger |
| 481 // redundant ExceptionEvents. |
| 482 resultCapability = NewPromiseCapability(constructor, false); |
| 483 } |
458 return PerformPromiseThen(this, onResolve, onReject, resultCapability); | 484 return PerformPromiseThen(this, onResolve, onReject, resultCapability); |
459 } | 485 } |
460 | 486 |
461 // ES#sec-promise.prototype.catch | 487 // ES#sec-promise.prototype.catch |
462 // Promise.prototype.catch ( onRejected ) | 488 // Promise.prototype.catch ( onRejected ) |
463 function PromiseCatch(onReject) { | 489 function PromiseCatch(onReject) { |
464 return this.then(UNDEFINED, onReject); | 490 return this.then(UNDEFINED, onReject); |
465 } | 491 } |
466 | 492 |
467 // Combinators. | 493 // Combinators. |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 to.PromiseNextMicrotaskID = PromiseNextMicrotaskID; | 722 to.PromiseNextMicrotaskID = PromiseNextMicrotaskID; |
697 | 723 |
698 to.GlobalPromise = GlobalPromise; | 724 to.GlobalPromise = GlobalPromise; |
699 to.NewPromiseCapability = NewPromiseCapability; | 725 to.NewPromiseCapability = NewPromiseCapability; |
700 to.PerformPromiseThen = PerformPromiseThen; | 726 to.PerformPromiseThen = PerformPromiseThen; |
701 to.ResolvePromise = ResolvePromise; | 727 to.ResolvePromise = ResolvePromise; |
702 to.RejectPromise = RejectPromise; | 728 to.RejectPromise = RejectPromise; |
703 }); | 729 }); |
704 | 730 |
705 }) | 731 }) |
OLD | NEW |