| 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 13 matching lines...) Expand all Loading... |
| 24 var GlobalPromise = global.Promise; | 24 var GlobalPromise = global.Promise; |
| 25 | 25 |
| 26 utils.Import(function(from) { | 26 utils.Import(function(from) { |
| 27 ObjectHasOwnProperty = from.ObjectHasOwnProperty; | 27 ObjectHasOwnProperty = from.ObjectHasOwnProperty; |
| 28 }); | 28 }); |
| 29 | 29 |
| 30 // ------------------------------------------------------------------- | 30 // ------------------------------------------------------------------- |
| 31 | 31 |
| 32 // Core functionality. | 32 // Core functionality. |
| 33 | 33 |
| 34 function PromiseDebugGetInfo(deferreds, status) { | 34 function PromiseDebugGetInfo(deferred_promise, status) { |
| 35 var id, name, instrumenting = DEBUG_IS_ACTIVE; | 35 var id, name, instrumenting = DEBUG_IS_ACTIVE; |
| 36 | 36 |
| 37 if (instrumenting) { | 37 if (instrumenting) { |
| 38 // In an async function, reuse the existing stack related to the outer | 38 // In an async function, reuse the existing stack related to the outer |
| 39 // Promise. Otherwise, e.g. in a direct call to then, save a new stack. | 39 // Promise. Otherwise, e.g. in a direct call to then, save a new stack. |
| 40 // Promises with multiple reactions with one or more of them being async | 40 // Promises with multiple reactions with one or more of them being async |
| 41 // functions will not get a good stack trace, as async functions require | 41 // functions will not get a good stack trace, as async functions require |
| 42 // different stacks from direct Promise use, but we save and restore a | 42 // different stacks from direct Promise use, but we save and restore a |
| 43 // stack once for all reactions. TODO(littledan): Improve this case. | 43 // stack once for all reactions. TODO(littledan): Improve this case. |
| 44 if (!IS_UNDEFINED(deferreds) && | 44 if (!IS_UNDEFINED(deferred_promise) && |
| 45 HAS_PRIVATE(deferreds.promise, promiseHandledBySymbol) && | 45 HAS_PRIVATE(deferred_promise, promiseHandledBySymbol) && |
| 46 HAS_PRIVATE(GET_PRIVATE(deferreds.promise, promiseHandledBySymbol), | 46 HAS_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), |
| 47 promiseAsyncStackIDSymbol)) { | 47 promiseAsyncStackIDSymbol)) { |
| 48 id = GET_PRIVATE(GET_PRIVATE(deferreds.promise, promiseHandledBySymbol), | 48 id = GET_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), |
| 49 promiseAsyncStackIDSymbol); | 49 promiseAsyncStackIDSymbol); |
| 50 name = "async function"; | 50 name = "async function"; |
| 51 } else { | 51 } else { |
| 52 id = %DebugNextMicrotaskId(); | 52 id = %DebugNextMicrotaskId(); |
| 53 name = status === kFulfilled ? "Promise.resolve" : "Promise.reject"; | 53 name = status === kFulfilled ? "Promise.resolve" : "Promise.reject"; |
| 54 %DebugAsyncTaskEvent("enqueue", id, name); | 54 %DebugAsyncTaskEvent("enqueue", id, name); |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 return [id, name]; | 57 return [id, name]; |
| 58 } | 58 } |
| 59 | 59 |
| 60 function PromiseIdResolveHandler(x) { return x; } | 60 function PromiseIdResolveHandler(x) { return x; } |
| 61 function PromiseIdRejectHandler(r) { %_ReThrow(r); } | 61 function PromiseIdRejectHandler(r) { %_ReThrow(r); } |
| 62 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); | 62 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); |
| 63 | 63 |
| 64 // ------------------------------------------------------------------- | 64 // ------------------------------------------------------------------- |
| 65 // Define exported functions. | 65 // Define exported functions. |
| 66 | 66 |
| 67 // For bootstrapper. | 67 // For bootstrapper. |
| 68 | 68 |
| 69 // This is used by utils and v8-extras. | 69 // This is used by utils and v8-extras. |
| 70 function PromiseCreate() { | 70 function PromiseCreate(parent) { |
| 71 return %promise_internal_constructor(UNDEFINED); | 71 return %promise_internal_constructor(parent); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Only used by async-await.js | 74 // Only used by async-await.js |
| 75 function RejectPromise(promise, reason, debugEvent) { | 75 function RejectPromise(promise, reason, debugEvent) { |
| 76 %PromiseReject(promise, reason, debugEvent); | 76 %PromiseReject(promise, reason, debugEvent); |
| 77 } | 77 } |
| 78 | 78 |
| 79 // Export to bindings | 79 // Export to bindings |
| 80 function DoRejectPromise(promise, reason) { | 80 function DoRejectPromise(promise, reason) { |
| 81 %PromiseReject(promise, reason, true); | 81 %PromiseReject(promise, reason, true); |
| 82 } | 82 } |
| 83 | 83 |
| 84 // The resultCapability.promise is only ever fulfilled internally, | |
| 85 // so we don't need the closures to protect against accidentally | |
| 86 // calling them multiple times. | |
| 87 function CreateInternalPromiseCapability(parent) { | |
| 88 return { | |
| 89 promise: %promise_internal_constructor(parent), | |
| 90 resolve: UNDEFINED, | |
| 91 reject: UNDEFINED | |
| 92 }; | |
| 93 } | |
| 94 | |
| 95 // ES#sec-newpromisecapability | 84 // ES#sec-newpromisecapability |
| 96 // NewPromiseCapability ( C ) | 85 // NewPromiseCapability ( C ) |
| 97 function NewPromiseCapability(C, debugEvent) { | 86 function NewPromiseCapability(C, debugEvent) { |
| 98 if (C === GlobalPromise) { | 87 if (C === GlobalPromise) { |
| 99 // Optimized case, avoid extra closure. | 88 // Optimized case, avoid extra closure. |
| 100 var promise = %promise_internal_constructor(UNDEFINED); | 89 var promise = %promise_internal_constructor(UNDEFINED); |
| 101 // TODO(gsathya): Remove container for callbacks when this is | 90 // TODO(gsathya): Remove container for callbacks when this is |
| 102 // moved to CPP/TF. | 91 // moved to CPP/TF. |
| 103 var callbacks = %create_resolving_functions(promise, debugEvent); | 92 var callbacks = %create_resolving_functions(promise, debugEvent); |
| 104 return { | 93 return { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 } | 248 } |
| 260 } catch (e) { | 249 } catch (e) { |
| 261 %_Call(deferred.reject, UNDEFINED, e); | 250 %_Call(deferred.reject, UNDEFINED, e); |
| 262 } | 251 } |
| 263 return deferred.promise; | 252 return deferred.promise; |
| 264 } | 253 } |
| 265 | 254 |
| 266 | 255 |
| 267 // Utility for debugger | 256 // Utility for debugger |
| 268 | 257 |
| 269 function PromiseHasUserDefinedRejectHandlerCheck(handler, deferred) { | 258 function PromiseHasUserDefinedRejectHandlerCheck(handler, deferred_promise) { |
| 270 // Recurse to the forwarding Promise, if any. This may be due to | 259 // Recurse to the forwarding Promise, if any. This may be due to |
| 271 // - await reaction forwarding to the throwaway Promise, which has | 260 // - await reaction forwarding to the throwaway Promise, which has |
| 272 // a dependency edge to the outer Promise. | 261 // a dependency edge to the outer Promise. |
| 273 // - PromiseIdResolveHandler forwarding to the output of .then | 262 // - PromiseIdResolveHandler forwarding to the output of .then |
| 274 // - Promise.all/Promise.race forwarding to a throwaway Promise, which | 263 // - Promise.all/Promise.race forwarding to a throwaway Promise, which |
| 275 // has a dependency edge to the generated outer Promise. | 264 // has a dependency edge to the generated outer Promise. |
| 276 if (GET_PRIVATE(handler, promiseForwardingHandlerSymbol)) { | 265 if (GET_PRIVATE(handler, promiseForwardingHandlerSymbol)) { |
| 277 return PromiseHasUserDefinedRejectHandlerRecursive(deferred.promise); | 266 return PromiseHasUserDefinedRejectHandlerRecursive(deferred_promise); |
| 278 } | 267 } |
| 279 | 268 |
| 280 // Otherwise, this is a real reject handler for the Promise | 269 // Otherwise, this is a real reject handler for the Promise |
| 281 return true; | 270 return true; |
| 282 } | 271 } |
| 283 | 272 |
| 284 function PromiseHasUserDefinedRejectHandlerRecursive(promise) { | 273 function PromiseHasUserDefinedRejectHandlerRecursive(promise) { |
| 285 // If this promise was marked as being handled by a catch block | 274 // If this promise was marked as being handled by a catch block |
| 286 // in an async function, then it has a user-defined reject handler. | 275 // in an async function, then it has a user-defined reject handler. |
| 287 if (GET_PRIVATE(promise, promiseHandledHintSymbol)) return true; | 276 if (GET_PRIVATE(promise, promiseHandledHintSymbol)) return true; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 ]); | 329 ]); |
| 341 | 330 |
| 342 %InstallToContext([ | 331 %InstallToContext([ |
| 343 "promise_create", PromiseCreate, | 332 "promise_create", PromiseCreate, |
| 344 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, | 333 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, |
| 345 "promise_reject", DoRejectPromise, | 334 "promise_reject", DoRejectPromise, |
| 346 // TODO(gsathya): Remove this once we update the promise builtin. | 335 // TODO(gsathya): Remove this once we update the promise builtin. |
| 347 "promise_internal_reject", RejectPromise, | 336 "promise_internal_reject", RejectPromise, |
| 348 "promise_debug_get_info", PromiseDebugGetInfo, | 337 "promise_debug_get_info", PromiseDebugGetInfo, |
| 349 "new_promise_capability", NewPromiseCapability, | 338 "new_promise_capability", NewPromiseCapability, |
| 350 "internal_promise_capability", CreateInternalPromiseCapability, | |
| 351 "promise_id_resolve_handler", PromiseIdResolveHandler, | 339 "promise_id_resolve_handler", PromiseIdResolveHandler, |
| 352 "promise_id_reject_handler", PromiseIdRejectHandler | 340 "promise_id_reject_handler", PromiseIdRejectHandler |
| 353 ]); | 341 ]); |
| 354 | 342 |
| 355 // This allows extras to create promises quickly without building extra | 343 // This allows extras to create promises quickly without building extra |
| 356 // resolve/reject closures, and allows them to later resolve and reject any | 344 // resolve/reject closures, and allows them to later resolve and reject any |
| 357 // promise without having to hold on to those closures forever. | 345 // promise without having to hold on to those closures forever. |
| 358 utils.InstallFunctions(extrasUtils, 0, [ | 346 utils.InstallFunctions(extrasUtils, 0, [ |
| 359 "createPromise", PromiseCreate, | 347 "createPromise", PromiseCreate, |
| 360 "rejectPromise", DoRejectPromise, | 348 "rejectPromise", DoRejectPromise, |
| 361 "markPromiseAsHandled", MarkPromiseAsHandled | 349 "markPromiseAsHandled", MarkPromiseAsHandled |
| 362 ]); | 350 ]); |
| 363 | 351 |
| 364 utils.Export(function(to) { | 352 utils.Export(function(to) { |
| 365 to.PromiseCreate = PromiseCreate; | 353 to.PromiseCreate = PromiseCreate; |
| 366 | 354 |
| 367 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; | |
| 368 to.RejectPromise = RejectPromise; | 355 to.RejectPromise = RejectPromise; |
| 369 }); | 356 }); |
| 370 | 357 |
| 371 }) | 358 }) |
| OLD | NEW |