| 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 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 utils.Import(function(from) { | 33 utils.Import(function(from) { |
| 34 ObjectHasOwnProperty = from.ObjectHasOwnProperty; | 34 ObjectHasOwnProperty = from.ObjectHasOwnProperty; |
| 35 SpeciesConstructor = from.SpeciesConstructor; | 35 SpeciesConstructor = from.SpeciesConstructor; |
| 36 }); | 36 }); |
| 37 | 37 |
| 38 // ------------------------------------------------------------------- | 38 // ------------------------------------------------------------------- |
| 39 | 39 |
| 40 // Core functionality. | 40 // Core functionality. |
| 41 | 41 |
| 42 function PromiseDebugGetInfo(deferreds, status) { | 42 function PromiseDebugGetInfo(deferred_promise, status) { |
| 43 var id, name, instrumenting = DEBUG_IS_ACTIVE; | 43 var id, name, instrumenting = DEBUG_IS_ACTIVE; |
| 44 | 44 |
| 45 if (instrumenting) { | 45 if (instrumenting) { |
| 46 // In an async function, reuse the existing stack related to the outer | 46 // In an async function, reuse the existing stack related to the outer |
| 47 // Promise. Otherwise, e.g. in a direct call to then, save a new stack. | 47 // Promise. Otherwise, e.g. in a direct call to then, save a new stack. |
| 48 // Promises with multiple reactions with one or more of them being async | 48 // Promises with multiple reactions with one or more of them being async |
| 49 // functions will not get a good stack trace, as async functions require | 49 // functions will not get a good stack trace, as async functions require |
| 50 // different stacks from direct Promise use, but we save and restore a | 50 // different stacks from direct Promise use, but we save and restore a |
| 51 // stack once for all reactions. TODO(littledan): Improve this case. | 51 // stack once for all reactions. TODO(littledan): Improve this case. |
| 52 if (!IS_UNDEFINED(deferreds) && | 52 if (!IS_UNDEFINED(deferred_promise) && |
| 53 HAS_PRIVATE(deferreds.promise, promiseHandledBySymbol) && | 53 HAS_PRIVATE(deferred_promise, promiseHandledBySymbol) && |
| 54 HAS_PRIVATE(GET_PRIVATE(deferreds.promise, promiseHandledBySymbol), | 54 HAS_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), |
| 55 promiseAsyncStackIDSymbol)) { | 55 promiseAsyncStackIDSymbol)) { |
| 56 id = GET_PRIVATE(GET_PRIVATE(deferreds.promise, promiseHandledBySymbol), | 56 id = GET_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), |
| 57 promiseAsyncStackIDSymbol); | 57 promiseAsyncStackIDSymbol); |
| 58 name = "async function"; | 58 name = "async function"; |
| 59 } else { | 59 } else { |
| 60 id = %DebugNextMicrotaskId(); | 60 id = %DebugNextMicrotaskId(); |
| 61 name = status === kFulfilled ? "Promise.resolve" : "Promise.reject"; | 61 name = status === kFulfilled ? "Promise.resolve" : "Promise.reject"; |
| 62 %DebugAsyncTaskEvent("enqueue", id, name); | 62 %DebugAsyncTaskEvent("enqueue", id, name); |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 return [id, name]; | 65 return [id, name]; |
| 66 } | 66 } |
| 67 | 67 |
| 68 function PromiseIdResolveHandler(x) { return x; } | 68 function PromiseIdResolveHandler(x) { return x; } |
| 69 function PromiseIdRejectHandler(r) { %_ReThrow(r); } | 69 function PromiseIdRejectHandler(r) { %_ReThrow(r); } |
| 70 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); | 70 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); |
| 71 | 71 |
| 72 // ------------------------------------------------------------------- | 72 // ------------------------------------------------------------------- |
| 73 // Define exported functions. | 73 // Define exported functions. |
| 74 | 74 |
| 75 // For bootstrapper. | 75 // For bootstrapper. |
| 76 | 76 |
| 77 // This is used by utils and v8-extras. | 77 // This is used by utils and v8-extras. |
| 78 function PromiseCreate() { | 78 function PromiseCreate(parent) { |
| 79 return %promise_internal_constructor(UNDEFINED); | 79 return %promise_internal_constructor(parent); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Only used by async-await.js | 82 // Only used by async-await.js |
| 83 function RejectPromise(promise, reason, debugEvent) { | 83 function RejectPromise(promise, reason, debugEvent) { |
| 84 %PromiseReject(promise, reason, debugEvent); | 84 %PromiseReject(promise, reason, debugEvent); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Export to bindings | 87 // Export to bindings |
| 88 function DoRejectPromise(promise, reason) { | 88 function DoRejectPromise(promise, reason) { |
| 89 %PromiseReject(promise, reason, true); | 89 %PromiseReject(promise, reason, true); |
| 90 } | 90 } |
| 91 | 91 |
| 92 // The resultCapability.promise is only ever fulfilled internally, | |
| 93 // so we don't need the closures to protect against accidentally | |
| 94 // calling them multiple times. | |
| 95 function CreateInternalPromiseCapability(parent) { | |
| 96 return { | |
| 97 promise: %promise_internal_constructor(parent), | |
| 98 resolve: UNDEFINED, | |
| 99 reject: UNDEFINED | |
| 100 }; | |
| 101 } | |
| 102 | |
| 103 // ES#sec-newpromisecapability | 92 // ES#sec-newpromisecapability |
| 104 // NewPromiseCapability ( C ) | 93 // NewPromiseCapability ( C ) |
| 105 function NewPromiseCapability(C, debugEvent) { | 94 function NewPromiseCapability(C, debugEvent) { |
| 106 if (C === GlobalPromise) { | 95 if (C === GlobalPromise) { |
| 107 // Optimized case, avoid extra closure. | 96 // Optimized case, avoid extra closure. |
| 108 var promise = %promise_internal_constructor(UNDEFINED); | 97 var promise = %promise_internal_constructor(UNDEFINED); |
| 109 // TODO(gsathya): Remove container for callbacks when this is | 98 // TODO(gsathya): Remove container for callbacks when this is |
| 110 // moved to CPP/TF. | 99 // moved to CPP/TF. |
| 111 var callbacks = %create_resolving_functions(promise, debugEvent); | 100 var callbacks = %create_resolving_functions(promise, debugEvent); |
| 112 return { | 101 return { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 } | 262 } |
| 274 } catch (e) { | 263 } catch (e) { |
| 275 %_Call(deferred.reject, UNDEFINED, e); | 264 %_Call(deferred.reject, UNDEFINED, e); |
| 276 } | 265 } |
| 277 return deferred.promise; | 266 return deferred.promise; |
| 278 } | 267 } |
| 279 | 268 |
| 280 | 269 |
| 281 // Utility for debugger | 270 // Utility for debugger |
| 282 | 271 |
| 283 function PromiseHasUserDefinedRejectHandlerCheck(handler, deferred) { | 272 function PromiseHasUserDefinedRejectHandlerCheck(handler, deferred_promise) { |
| 284 // Recurse to the forwarding Promise, if any. This may be due to | 273 // Recurse to the forwarding Promise, if any. This may be due to |
| 285 // - await reaction forwarding to the throwaway Promise, which has | 274 // - await reaction forwarding to the throwaway Promise, which has |
| 286 // a dependency edge to the outer Promise. | 275 // a dependency edge to the outer Promise. |
| 287 // - PromiseIdResolveHandler forwarding to the output of .then | 276 // - PromiseIdResolveHandler forwarding to the output of .then |
| 288 // - Promise.all/Promise.race forwarding to a throwaway Promise, which | 277 // - Promise.all/Promise.race forwarding to a throwaway Promise, which |
| 289 // has a dependency edge to the generated outer Promise. | 278 // has a dependency edge to the generated outer Promise. |
| 290 if (GET_PRIVATE(handler, promiseForwardingHandlerSymbol)) { | 279 if (GET_PRIVATE(handler, promiseForwardingHandlerSymbol)) { |
| 291 return PromiseHasUserDefinedRejectHandlerRecursive(deferred.promise); | 280 return PromiseHasUserDefinedRejectHandlerRecursive(deferred_promise); |
| 292 } | 281 } |
| 293 | 282 |
| 294 // Otherwise, this is a real reject handler for the Promise | 283 // Otherwise, this is a real reject handler for the Promise |
| 295 return true; | 284 return true; |
| 296 } | 285 } |
| 297 | 286 |
| 298 function PromiseHasUserDefinedRejectHandlerRecursive(promise) { | 287 function PromiseHasUserDefinedRejectHandlerRecursive(promise) { |
| 299 // If this promise was marked as being handled by a catch block | 288 // If this promise was marked as being handled by a catch block |
| 300 // in an async function, then it has a user-defined reject handler. | 289 // in an async function, then it has a user-defined reject handler. |
| 301 if (GET_PRIVATE(promise, promiseHandledHintSymbol)) return true; | 290 if (GET_PRIVATE(promise, promiseHandledHintSymbol)) return true; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 | 353 |
| 365 %InstallToContext([ | 354 %InstallToContext([ |
| 366 "promise_catch", GlobalPromise.prototype.catch, | 355 "promise_catch", GlobalPromise.prototype.catch, |
| 367 "promise_create", PromiseCreate, | 356 "promise_create", PromiseCreate, |
| 368 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, | 357 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, |
| 369 "promise_reject", DoRejectPromise, | 358 "promise_reject", DoRejectPromise, |
| 370 // TODO(gsathya): Remove this once we update the promise builtin. | 359 // TODO(gsathya): Remove this once we update the promise builtin. |
| 371 "promise_internal_reject", RejectPromise, | 360 "promise_internal_reject", RejectPromise, |
| 372 "promise_debug_get_info", PromiseDebugGetInfo, | 361 "promise_debug_get_info", PromiseDebugGetInfo, |
| 373 "new_promise_capability", NewPromiseCapability, | 362 "new_promise_capability", NewPromiseCapability, |
| 374 "internal_promise_capability", CreateInternalPromiseCapability, | |
| 375 "promise_id_resolve_handler", PromiseIdResolveHandler, | 363 "promise_id_resolve_handler", PromiseIdResolveHandler, |
| 376 "promise_id_reject_handler", PromiseIdRejectHandler | 364 "promise_id_reject_handler", PromiseIdRejectHandler |
| 377 ]); | 365 ]); |
| 378 | 366 |
| 379 // This allows extras to create promises quickly without building extra | 367 // This allows extras to create promises quickly without building extra |
| 380 // resolve/reject closures, and allows them to later resolve and reject any | 368 // resolve/reject closures, and allows them to later resolve and reject any |
| 381 // promise without having to hold on to those closures forever. | 369 // promise without having to hold on to those closures forever. |
| 382 utils.InstallFunctions(extrasUtils, 0, [ | 370 utils.InstallFunctions(extrasUtils, 0, [ |
| 383 "createPromise", PromiseCreate, | 371 "createPromise", PromiseCreate, |
| 384 "rejectPromise", DoRejectPromise, | 372 "rejectPromise", DoRejectPromise, |
| 385 "markPromiseAsHandled", MarkPromiseAsHandled | 373 "markPromiseAsHandled", MarkPromiseAsHandled |
| 386 ]); | 374 ]); |
| 387 | 375 |
| 388 utils.Export(function(to) { | 376 utils.Export(function(to) { |
| 389 to.PromiseCreate = PromiseCreate; | 377 to.PromiseCreate = PromiseCreate; |
| 390 to.PromiseThen = PromiseThen; | 378 to.PromiseThen = PromiseThen; |
| 391 | 379 |
| 392 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; | |
| 393 to.RejectPromise = RejectPromise; | 380 to.RejectPromise = RejectPromise; |
| 394 }); | 381 }); |
| 395 | 382 |
| 396 }) | 383 }) |
| OLD | NEW |