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 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var InternalArray = utils.InternalArray; | 14 var InternalArray = utils.InternalArray; |
15 var promiseAsyncStackIDSymbol = | |
16 utils.ImportNow("promise_async_stack_id_symbol"); | |
17 var promiseHandledBySymbol = | 15 var promiseHandledBySymbol = |
18 utils.ImportNow("promise_handled_by_symbol"); | 16 utils.ImportNow("promise_handled_by_symbol"); |
19 var promiseForwardingHandlerSymbol = | 17 var promiseForwardingHandlerSymbol = |
20 utils.ImportNow("promise_forwarding_handler_symbol"); | 18 utils.ImportNow("promise_forwarding_handler_symbol"); |
21 var ObjectHasOwnProperty; // Used by HAS_PRIVATE. | |
22 var GlobalPromise = global.Promise; | 19 var GlobalPromise = global.Promise; |
23 | 20 |
24 utils.Import(function(from) { | |
25 ObjectHasOwnProperty = from.ObjectHasOwnProperty; | |
26 }); | |
27 | |
28 // ------------------------------------------------------------------- | 21 // ------------------------------------------------------------------- |
29 | 22 |
30 // Core functionality. | 23 // Core functionality. |
31 | 24 |
32 function PromiseDebugGetInfo(deferred_promise, status) { | |
33 var id, name, instrumenting = DEBUG_IS_ACTIVE; | |
34 | |
35 if (instrumenting) { | |
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. | |
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 | |
40 // different stacks from direct Promise use, but we save and restore a | |
41 // stack once for all reactions. TODO(littledan): Improve this case. | |
42 if (!IS_UNDEFINED(deferred_promise) && | |
43 HAS_PRIVATE(deferred_promise, promiseHandledBySymbol) && | |
44 HAS_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), | |
45 promiseAsyncStackIDSymbol)) { | |
46 id = GET_PRIVATE(GET_PRIVATE(deferred_promise, promiseHandledBySymbol), | |
47 promiseAsyncStackIDSymbol); | |
48 name = "async function"; | |
49 } else { | |
50 id = %DebugNextMicrotaskId(); | |
51 name = status === kFulfilled ? "Promise.resolve" : "Promise.reject"; | |
52 %DebugAsyncTaskEvent("enqueue", id, name); | |
53 } | |
54 } | |
55 return [id, name]; | |
56 } | |
57 | |
58 function PromiseIdResolveHandler(x) { return x; } | 25 function PromiseIdResolveHandler(x) { return x; } |
59 function PromiseIdRejectHandler(r) { %_ReThrow(r); } | 26 function PromiseIdRejectHandler(r) { %_ReThrow(r); } |
60 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); | 27 SET_PRIVATE(PromiseIdRejectHandler, promiseForwardingHandlerSymbol, true); |
61 | 28 |
62 // ------------------------------------------------------------------- | 29 // ------------------------------------------------------------------- |
63 // Define exported functions. | 30 // Define exported functions. |
64 | 31 |
65 // For bootstrapper. | 32 // For bootstrapper. |
66 | 33 |
67 // This is used by utils and v8-extras. | 34 // This is used by utils and v8-extras. |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 "all", PromiseAll, | 229 "all", PromiseAll, |
263 "race", PromiseRace, | 230 "race", PromiseRace, |
264 "resolve", PromiseResolve | 231 "resolve", PromiseResolve |
265 ]); | 232 ]); |
266 | 233 |
267 %InstallToContext([ | 234 %InstallToContext([ |
268 "promise_create", PromiseCreate, | 235 "promise_create", PromiseCreate, |
269 "promise_reject", DoRejectPromise, | 236 "promise_reject", DoRejectPromise, |
270 // TODO(gsathya): Remove this once we update the promise builtin. | 237 // TODO(gsathya): Remove this once we update the promise builtin. |
271 "promise_internal_reject", RejectPromise, | 238 "promise_internal_reject", RejectPromise, |
272 "promise_debug_get_info", PromiseDebugGetInfo, | |
273 "new_promise_capability", NewPromiseCapability, | 239 "new_promise_capability", NewPromiseCapability, |
274 "promise_id_resolve_handler", PromiseIdResolveHandler, | 240 "promise_id_resolve_handler", PromiseIdResolveHandler, |
275 "promise_id_reject_handler", PromiseIdRejectHandler | 241 "promise_id_reject_handler", PromiseIdRejectHandler |
276 ]); | 242 ]); |
277 | 243 |
278 // This allows extras to create promises quickly without building extra | 244 // This allows extras to create promises quickly without building extra |
279 // resolve/reject closures, and allows them to later resolve and reject any | 245 // resolve/reject closures, and allows them to later resolve and reject any |
280 // promise without having to hold on to those closures forever. | 246 // promise without having to hold on to those closures forever. |
281 utils.InstallFunctions(extrasUtils, 0, [ | 247 utils.InstallFunctions(extrasUtils, 0, [ |
282 "createPromise", PromiseCreate, | 248 "createPromise", PromiseCreate, |
283 "rejectPromise", DoRejectPromise, | 249 "rejectPromise", DoRejectPromise, |
284 "markPromiseAsHandled", MarkPromiseAsHandled | 250 "markPromiseAsHandled", MarkPromiseAsHandled |
285 ]); | 251 ]); |
286 | 252 |
287 utils.Export(function(to) { | 253 utils.Export(function(to) { |
288 to.PromiseCreate = PromiseCreate; | 254 to.PromiseCreate = PromiseCreate; |
289 | 255 |
290 to.RejectPromise = RejectPromise; | 256 to.RejectPromise = RejectPromise; |
291 }); | 257 }); |
292 | 258 |
293 }) | 259 }) |
OLD | NEW |