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 PromiseHandle(value, handler, deferred) { | |
43 var debug_is_active = DEBUG_IS_ACTIVE; | |
44 try { | |
45 if (debug_is_active) %DebugPushPromise(deferred.promise); | |
46 var result = handler(value); | |
47 if (IS_UNDEFINED(deferred.resolve)) { | |
48 %promise_resolve(deferred.promise, result); | |
49 } else { | |
50 %_Call(deferred.resolve, UNDEFINED, result); | |
51 } | |
52 } %catch (exception) { // Natives syntax to mark this catch block. | |
53 try { | |
54 if (IS_UNDEFINED(deferred.reject)) { | |
55 // Pass false for debugEvent so .then chaining or throwaway promises | |
56 // in async functions do not trigger redundant ExceptionEvents. | |
57 %PromiseReject(deferred.promise, exception, false); | |
58 } else { | |
59 %_Call(deferred.reject, UNDEFINED, exception); | |
60 } | |
61 } catch (e) { } | |
62 } finally { | |
63 if (debug_is_active) %DebugPopPromise(); | |
64 } | |
65 } | |
66 | |
67 function PromiseDebugGetInfo(deferreds, status) { | 42 function PromiseDebugGetInfo(deferreds, status) { |
68 var id, name, instrumenting = DEBUG_IS_ACTIVE; | 43 var id, name, instrumenting = DEBUG_IS_ACTIVE; |
69 | 44 |
70 if (instrumenting) { | 45 if (instrumenting) { |
71 // 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 |
72 // 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. |
73 // 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 |
74 // 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 |
75 // 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 |
76 // stack once for all reactions. TODO(littledan): Improve this case. | 51 // stack once for all reactions. TODO(littledan): Improve this case. |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 362 |
388 %SetCode(GlobalPromise.prototype.catch, PromiseCatch); | 363 %SetCode(GlobalPromise.prototype.catch, PromiseCatch); |
389 | 364 |
390 %InstallToContext([ | 365 %InstallToContext([ |
391 "promise_catch", GlobalPromise.prototype.catch, | 366 "promise_catch", GlobalPromise.prototype.catch, |
392 "promise_create", PromiseCreate, | 367 "promise_create", PromiseCreate, |
393 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, | 368 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, |
394 "promise_reject", DoRejectPromise, | 369 "promise_reject", DoRejectPromise, |
395 // TODO(gsathya): Remove this once we update the promise builtin. | 370 // TODO(gsathya): Remove this once we update the promise builtin. |
396 "promise_internal_reject", RejectPromise, | 371 "promise_internal_reject", RejectPromise, |
397 "promise_handle", PromiseHandle, | |
398 "promise_debug_get_info", PromiseDebugGetInfo, | 372 "promise_debug_get_info", PromiseDebugGetInfo, |
399 "new_promise_capability", NewPromiseCapability, | 373 "new_promise_capability", NewPromiseCapability, |
400 "internal_promise_capability", CreateInternalPromiseCapability, | 374 "internal_promise_capability", CreateInternalPromiseCapability, |
401 "promise_id_resolve_handler", PromiseIdResolveHandler, | 375 "promise_id_resolve_handler", PromiseIdResolveHandler, |
402 "promise_id_reject_handler", PromiseIdRejectHandler | 376 "promise_id_reject_handler", PromiseIdRejectHandler |
403 ]); | 377 ]); |
404 | 378 |
405 // This allows extras to create promises quickly without building extra | 379 // This allows extras to create promises quickly without building extra |
406 // resolve/reject closures, and allows them to later resolve and reject any | 380 // resolve/reject closures, and allows them to later resolve and reject any |
407 // promise without having to hold on to those closures forever. | 381 // promise without having to hold on to those closures forever. |
408 utils.InstallFunctions(extrasUtils, 0, [ | 382 utils.InstallFunctions(extrasUtils, 0, [ |
409 "createPromise", PromiseCreate, | 383 "createPromise", PromiseCreate, |
410 "rejectPromise", DoRejectPromise, | 384 "rejectPromise", DoRejectPromise, |
411 "markPromiseAsHandled", MarkPromiseAsHandled | 385 "markPromiseAsHandled", MarkPromiseAsHandled |
412 ]); | 386 ]); |
413 | 387 |
414 utils.Export(function(to) { | 388 utils.Export(function(to) { |
415 to.PromiseCreate = PromiseCreate; | 389 to.PromiseCreate = PromiseCreate; |
416 to.PromiseThen = PromiseThen; | 390 to.PromiseThen = PromiseThen; |
417 | 391 |
418 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; | 392 to.CreateInternalPromiseCapability = CreateInternalPromiseCapability; |
419 to.RejectPromise = RejectPromise; | 393 to.RejectPromise = RejectPromise; |
420 }); | 394 }); |
421 | 395 |
422 }) | 396 }) |
OLD | NEW |