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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 // For bootstrapper. | 111 // For bootstrapper. |
| 112 | 112 |
| 113 // This is used by utils and v8-extras. | 113 // This is used by utils and v8-extras. |
| 114 function PromiseCreate() { | 114 function PromiseCreate() { |
| 115 return %promise_internal_constructor(); | 115 return %promise_internal_constructor(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 // ES#sec-promise-resolve-functions | 118 // ES#sec-promise-resolve-functions |
| 119 // Promise Resolve Functions, steps 6-13 | 119 // Promise Resolve Functions, steps 6-13 |
| 120 function ResolvePromise(promise, resolution) { | 120 function ResolvePromise(promise, resolution) { |
| 121 if (resolution === promise) { | 121 %resolve_promise(promise, resolution); |
|
jgruber
2016/12/05 09:25:17
Can we replace calls to ResolvePromise by %resolve
gsathya
2016/12/05 16:03:05
Nope. We use this in async-await and v8-extras
caitp
2016/12/05 16:22:51
Couldn't we just export %resolve_promise from the
| |
| 122 var exception = %make_type_error(kPromiseCyclic, resolution); | |
| 123 %PromiseReject(promise, exception, true); | |
| 124 return; | |
| 125 } | |
| 126 if (IS_RECEIVER(resolution)) { | |
| 127 // 25.4.1.3.2 steps 8-12 | |
| 128 try { | |
| 129 var then = resolution.then; | |
| 130 } catch (e) { | |
| 131 %PromiseReject(promise, e, true); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 // Resolution is a native promise and if it's already resolved or | |
| 136 // rejected, shortcircuit the resolution procedure by directly | |
| 137 // reusing the value from the promise. | |
| 138 if (%is_promise(resolution) && then === PromiseThen) { | |
| 139 var thenableState = %PromiseStatus(resolution); | |
| 140 if (thenableState === kFulfilled) { | |
| 141 // This goes inside the if-else to save one symbol lookup in | |
| 142 // the slow path. | |
| 143 var thenableValue = %PromiseResult(resolution); | |
| 144 %PromiseFulfill(promise, kFulfilled, thenableValue); | |
| 145 SET_PRIVATE(promise, promiseHasHandlerSymbol, true); | |
| 146 return; | |
| 147 } else if (thenableState === kRejected) { | |
| 148 var thenableValue = %PromiseResult(resolution); | |
| 149 if (!HAS_DEFINED_PRIVATE(resolution, promiseHasHandlerSymbol)) { | |
| 150 // Promise has already been rejected, but had no handler. | |
| 151 // Revoke previously triggered reject event. | |
| 152 %PromiseRevokeReject(resolution); | |
| 153 } | |
| 154 // Don't cause a debug event as this case is forwarding a rejection | |
| 155 %PromiseReject(promise, thenableValue, false); | |
| 156 SET_PRIVATE(resolution, promiseHasHandlerSymbol, true); | |
| 157 return; | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 if (IS_CALLABLE(then)) { | |
| 162 if (DEBUG_IS_ACTIVE && %is_promise(resolution)) { | |
| 163 // Mark the dependency of the new promise on the resolution | |
| 164 SET_PRIVATE(resolution, promiseHandledBySymbol, promise); | |
| 165 } | |
| 166 %EnqueuePromiseResolveThenableJob(promise, resolution, then); | |
| 167 return; | |
| 168 } | |
| 169 } | |
| 170 %PromiseFulfill(promise, kFulfilled, resolution); | |
| 171 } | 122 } |
| 172 | 123 |
| 173 // Only used by async-await.js | 124 // Only used by async-await.js |
| 174 function RejectPromise(promise, reason, debugEvent) { | 125 function RejectPromise(promise, reason, debugEvent) { |
| 175 %PromiseReject(promise, reason, debugEvent); | 126 %PromiseReject(promise, reason, debugEvent); |
| 176 } | 127 } |
| 177 | 128 |
| 178 // Export to bindings | 129 // Export to bindings |
| 179 function DoRejectPromise(promise, reason) { | 130 function DoRejectPromise(promise, reason) { |
| 180 %PromiseReject(promise, reason, true); | 131 %PromiseReject(promise, reason, true); |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 | 395 |
| 445 utils.InstallFunctions(GlobalPromise, DONT_ENUM, [ | 396 utils.InstallFunctions(GlobalPromise, DONT_ENUM, [ |
| 446 "reject", PromiseReject, | 397 "reject", PromiseReject, |
| 447 "all", PromiseAll, | 398 "all", PromiseAll, |
| 448 "race", PromiseRace, | 399 "race", PromiseRace, |
| 449 "resolve", PromiseResolve | 400 "resolve", PromiseResolve |
| 450 ]); | 401 ]); |
| 451 | 402 |
| 452 utils.InstallGetter(GlobalPromise, speciesSymbol, PromiseSpecies); | 403 utils.InstallGetter(GlobalPromise, speciesSymbol, PromiseSpecies); |
| 453 | 404 |
| 454 utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [ | 405 %SetCode(GlobalPromise.prototype.catch, PromiseCatch); |
|
jgruber
2016/12/05 09:25:17
This and below + corresponding changes in bootstra
gsathya
2016/12/05 16:03:05
I need to install an empty .catch in bootstrapper
jgruber
2016/12/07 12:59:38
Acknowledged.
| |
| 455 "catch", PromiseCatch | |
| 456 ]); | |
| 457 | 406 |
| 458 %InstallToContext([ | 407 %InstallToContext([ |
| 459 "promise_catch", PromiseCatch, | 408 "promise_catch", PromiseCatch, |
| 460 "promise_create", PromiseCreate, | 409 "promise_create", PromiseCreate, |
| 461 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, | 410 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, |
| 462 "promise_reject", DoRejectPromise, | 411 "promise_reject", DoRejectPromise, |
| 463 // TODO(gsathya): Remove this once we update the promise builtin. | 412 // TODO(gsathya): Remove this once we update the promise builtin. |
| 464 "promise_internal_reject", RejectPromise, | 413 "promise_internal_reject", RejectPromise, |
| 465 "promise_resolve", ResolvePromise, | 414 "promise_resolve", ResolvePromise, |
| 466 "promise_then", PromiseThen, | |
| 467 "promise_handle", PromiseHandle, | 415 "promise_handle", PromiseHandle, |
| 468 "promise_debug_get_info", PromiseDebugGetInfo, | 416 "promise_debug_get_info", PromiseDebugGetInfo, |
| 469 "new_promise_capability", NewPromiseCapability, | 417 "new_promise_capability", NewPromiseCapability, |
| 470 "internal_promise_capability", CreateInternalPromiseCapability, | 418 "internal_promise_capability", CreateInternalPromiseCapability, |
| 471 "promise_id_resolve_handler", PromiseIdResolveHandler, | 419 "promise_id_resolve_handler", PromiseIdResolveHandler, |
| 472 "promise_id_reject_handler", PromiseIdRejectHandler | 420 "promise_id_reject_handler", PromiseIdRejectHandler |
| 473 ]); | 421 ]); |
| 474 | 422 |
| 475 // This allows extras to create promises quickly without building extra | 423 // This allows extras to create promises quickly without building extra |
| 476 // resolve/reject closures, and allows them to later resolve and reject any | 424 // resolve/reject closures, and allows them to later resolve and reject any |
| 477 // promise without having to hold on to those closures forever. | 425 // promise without having to hold on to those closures forever. |
| 478 utils.InstallFunctions(extrasUtils, 0, [ | 426 utils.InstallFunctions(extrasUtils, 0, [ |
| 479 "createPromise", PromiseCreate, | 427 "createPromise", PromiseCreate, |
| 480 "resolvePromise", ResolvePromise, | 428 "resolvePromise", ResolvePromise, |
| 481 "rejectPromise", DoRejectPromise, | 429 "rejectPromise", DoRejectPromise, |
| 482 "markPromiseAsHandled", MarkPromiseAsHandled | 430 "markPromiseAsHandled", MarkPromiseAsHandled |
| 483 ]); | 431 ]); |
| 484 | 432 |
| 485 utils.Export(function(to) { | 433 utils.Export(function(to) { |
| 486 to.PromiseCreate = PromiseCreate; | 434 to.PromiseCreate = PromiseCreate; |
| 487 to.PromiseThen = PromiseThen; | 435 to.PromiseThen = PromiseThen; |
| 488 | 436 |
| 489 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; | 437 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; |
| 490 to.ResolvePromise = ResolvePromise; | 438 to.ResolvePromise = ResolvePromise; |
| 491 to.RejectPromise = RejectPromise; | 439 to.RejectPromise = RejectPromise; |
| 492 }); | 440 }); |
| 493 | 441 |
| 494 }) | 442 }) |
| OLD | NEW |