| 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 11 matching lines...) Expand all Loading... |
| 22 var GlobalPromise = global.Promise; | 22 var GlobalPromise = global.Promise; |
| 23 | 23 |
| 24 utils.Import(function(from) { | 24 utils.Import(function(from) { |
| 25 ObjectHasOwnProperty = from.ObjectHasOwnProperty; | 25 ObjectHasOwnProperty = from.ObjectHasOwnProperty; |
| 26 }); | 26 }); |
| 27 | 27 |
| 28 // ------------------------------------------------------------------- | 28 // ------------------------------------------------------------------- |
| 29 | 29 |
| 30 // Core functionality. | 30 // Core functionality. |
| 31 | 31 |
| 32 function PromiseDebugGetInfo(deferreds, status) { | 32 function PromiseDebugGetInfo(deferred_promise, status) { |
| 33 var id, name, instrumenting = DEBUG_IS_ACTIVE; | 33 var id, name, instrumenting = DEBUG_IS_ACTIVE; |
| 34 | 34 |
| 35 if (instrumenting) { | 35 if (instrumenting) { |
| 36 // In an async function, reuse the existing stack related to the outer | 36 // In an async function, reuse the existing stack related to the outer |
| 37 // Promise. Otherwise, e.g. in a direct call to then, save a new stack. | 37 // Promise. Otherwise, e.g. in a direct call to then, save a new stack. |
| 38 // Promises with multiple reactions with one or more of them being async | 38 // Promises with multiple reactions with one or more of them being async |
| 39 // functions will not get a good stack trace, as async functions require | 39 // functions will not get a good stack trace, as async functions require |
| 40 // different stacks from direct Promise use, but we save and restore a | 40 // different stacks from direct Promise use, but we save and restore a |
| 41 // stack once for all reactions. TODO(littledan): Improve this case. | 41 // stack once for all reactions. TODO(littledan): Improve this case. |
| 42 if (!IS_UNDEFINED(deferreds) && | 42 if (!IS_UNDEFINED(deferred_promise) && |
| 43 HAS_PRIVATE(deferreds.promise, promiseHandledBySymbol) && | 43 HAS_PRIVATE(deferred_promise, promiseHandledBySymbol) && |
| 44 HAS_PRIVATE(GET_PRIVATE(deferreds.promise, promiseHandledBySymbol), | 44 HAS_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), |
| 45 promiseAsyncStackIDSymbol)) { | 45 promiseAsyncStackIDSymbol)) { |
| 46 id = GET_PRIVATE(GET_PRIVATE(deferreds.promise, promiseHandledBySymbol), | 46 id = GET_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), |
| 47 promiseAsyncStackIDSymbol); | 47 promiseAsyncStackIDSymbol); |
| 48 name = "async function"; | 48 name = "async function"; |
| 49 } else { | 49 } else { |
| 50 id = %DebugNextMicrotaskId(); | 50 id = %DebugNextMicrotaskId(); |
| 51 name = status === kFulfilled ? "Promise.resolve" : "Promise.reject"; | 51 name = status === kFulfilled ? "Promise.resolve" : "Promise.reject"; |
| 52 %DebugAsyncTaskEvent("enqueue", id, name); | 52 %DebugAsyncTaskEvent("enqueue", id, name); |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 return [id, name]; | 55 return [id, name]; |
| 56 } | 56 } |
| 57 | 57 |
| 58 function PromiseIdResolveHandler(x) { return x; } | 58 function PromiseIdResolveHandler(x) { return x; } |
| 59 function PromiseIdRejectHandler(r) { %_ReThrow(r); } | 59 function PromiseIdRejectHandler(r) { %_ReThrow(r); } |
| 60 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); | 60 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); |
| 61 | 61 |
| 62 // ------------------------------------------------------------------- | 62 // ------------------------------------------------------------------- |
| 63 // Define exported functions. | 63 // Define exported functions. |
| 64 | 64 |
| 65 // For bootstrapper. | 65 // For bootstrapper. |
| 66 | 66 |
| 67 // This is used by utils and v8-extras. | 67 // This is used by utils and v8-extras. |
| 68 function PromiseCreate() { | 68 function PromiseCreate(parent) { |
| 69 return %promise_internal_constructor(UNDEFINED); | 69 return %promise_internal_constructor(parent); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Only used by async-await.js | 72 // Only used by async-await.js |
| 73 function RejectPromise(promise, reason, debugEvent) { | 73 function RejectPromise(promise, reason, debugEvent) { |
| 74 %PromiseReject(promise, reason, debugEvent); | 74 %PromiseReject(promise, reason, debugEvent); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Export to bindings | 77 // Export to bindings |
| 78 function DoRejectPromise(promise, reason) { | 78 function DoRejectPromise(promise, reason) { |
| 79 %PromiseReject(promise, reason, true); | 79 %PromiseReject(promise, reason, true); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // The resultCapability.promise is only ever fulfilled internally, | |
| 83 // so we don't need the closures to protect against accidentally | |
| 84 // calling them multiple times. | |
| 85 function CreateInternalPromiseCapability(parent) { | |
| 86 return { | |
| 87 promise: %promise_internal_constructor(parent), | |
| 88 resolve: UNDEFINED, | |
| 89 reject: UNDEFINED | |
| 90 }; | |
| 91 } | |
| 92 | |
| 93 // ES#sec-newpromisecapability | 82 // ES#sec-newpromisecapability |
| 94 // NewPromiseCapability ( C ) | 83 // NewPromiseCapability ( C ) |
| 95 function NewPromiseCapability(C, debugEvent) { | 84 function NewPromiseCapability(C, debugEvent) { |
| 96 if (C === GlobalPromise) { | 85 if (C === GlobalPromise) { |
| 97 // Optimized case, avoid extra closure. | 86 // Optimized case, avoid extra closure. |
| 98 var promise = %promise_internal_constructor(UNDEFINED); | 87 var promise = %promise_internal_constructor(UNDEFINED); |
| 99 // TODO(gsathya): Remove container for callbacks when this is | 88 // TODO(gsathya): Remove container for callbacks when this is |
| 100 // moved to CPP/TF. | 89 // moved to CPP/TF. |
| 101 var callbacks = %create_resolving_functions(promise, debugEvent); | 90 var callbacks = %create_resolving_functions(promise, debugEvent); |
| 102 return { | 91 return { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 "resolve", PromiseResolve | 264 "resolve", PromiseResolve |
| 276 ]); | 265 ]); |
| 277 | 266 |
| 278 %InstallToContext([ | 267 %InstallToContext([ |
| 279 "promise_create", PromiseCreate, | 268 "promise_create", PromiseCreate, |
| 280 "promise_reject", DoRejectPromise, | 269 "promise_reject", DoRejectPromise, |
| 281 // TODO(gsathya): Remove this once we update the promise builtin. | 270 // TODO(gsathya): Remove this once we update the promise builtin. |
| 282 "promise_internal_reject", RejectPromise, | 271 "promise_internal_reject", RejectPromise, |
| 283 "promise_debug_get_info", PromiseDebugGetInfo, | 272 "promise_debug_get_info", PromiseDebugGetInfo, |
| 284 "new_promise_capability", NewPromiseCapability, | 273 "new_promise_capability", NewPromiseCapability, |
| 285 "internal_promise_capability", CreateInternalPromiseCapability, | |
| 286 "promise_id_resolve_handler", PromiseIdResolveHandler, | 274 "promise_id_resolve_handler", PromiseIdResolveHandler, |
| 287 "promise_id_reject_handler", PromiseIdRejectHandler | 275 "promise_id_reject_handler", PromiseIdRejectHandler |
| 288 ]); | 276 ]); |
| 289 | 277 |
| 290 // This allows extras to create promises quickly without building extra | 278 // This allows extras to create promises quickly without building extra |
| 291 // resolve/reject closures, and allows them to later resolve and reject any | 279 // resolve/reject closures, and allows them to later resolve and reject any |
| 292 // promise without having to hold on to those closures forever. | 280 // promise without having to hold on to those closures forever. |
| 293 utils.InstallFunctions(extrasUtils, 0, [ | 281 utils.InstallFunctions(extrasUtils, 0, [ |
| 294 "createPromise", PromiseCreate, | 282 "createPromise", PromiseCreate, |
| 295 "rejectPromise", DoRejectPromise, | 283 "rejectPromise", DoRejectPromise, |
| 296 "markPromiseAsHandled", MarkPromiseAsHandled | 284 "markPromiseAsHandled", MarkPromiseAsHandled |
| 297 ]); | 285 ]); |
| 298 | 286 |
| 299 utils.Export(function(to) { | 287 utils.Export(function(to) { |
| 300 to.PromiseCreate = PromiseCreate; | 288 to.PromiseCreate = PromiseCreate; |
| 301 | 289 |
| 302 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; | |
| 303 to.RejectPromise = RejectPromise; | 290 to.RejectPromise = RejectPromise; |
| 304 }); | 291 }); |
| 305 | 292 |
| 306 }) | 293 }) |
| OLD | NEW |