Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 18 matching lines...) Expand all Loading... | |
| 29 NewPromiseCapability = from.NewPromiseCapability; | 29 NewPromiseCapability = from.NewPromiseCapability; |
| 30 PromiseCreate = from.PromiseCreate; | 30 PromiseCreate = from.PromiseCreate; |
| 31 PerformPromiseThen = from.PerformPromiseThen; | 31 PerformPromiseThen = from.PerformPromiseThen; |
| 32 RejectPromise = from.RejectPromise; | 32 RejectPromise = from.RejectPromise; |
| 33 ResolvePromise = from.ResolvePromise; | 33 ResolvePromise = from.ResolvePromise; |
| 34 }); | 34 }); |
| 35 | 35 |
| 36 var promiseAwaitHandlerSymbol = utils.ImportNow("promise_await_handler_symbol"); | 36 var promiseAwaitHandlerSymbol = utils.ImportNow("promise_await_handler_symbol"); |
| 37 var promiseHandledHintSymbol = | 37 var promiseHandledHintSymbol = |
| 38 utils.ImportNow("promise_handled_hint_symbol"); | 38 utils.ImportNow("promise_handled_hint_symbol"); |
| 39 var promiseHasHandlerSymbol = | |
| 40 utils.ImportNow("promise_has_handler_symbol"); | |
| 39 | 41 |
| 40 // ------------------------------------------------------------------- | 42 // ------------------------------------------------------------------- |
| 41 | 43 |
| 42 function PromiseCastResolved(value) { | 44 function PromiseCastResolved(value) { |
| 43 if (IsPromise(value)) { | 45 if (IsPromise(value)) { |
| 44 return value; | 46 return value; |
| 45 } else { | 47 } else { |
| 46 var promise = PromiseCreate(); | 48 var promise = PromiseCreate(); |
| 47 ResolvePromise(promise, value); | 49 ResolvePromise(promise, value); |
| 48 return promise; | 50 return promise; |
| 49 } | 51 } |
| 50 } | 52 } |
| 51 | 53 |
| 52 // ES#abstract-ops-async-function-await | 54 // ES#abstract-ops-async-function-await |
| 53 // AsyncFunctionAwait ( value ) | 55 // AsyncFunctionAwait ( value ) |
| 54 // Shared logic for the core of await. The parser desugars | 56 // Shared logic for the core of await. The parser desugars |
| 55 // await awaited | 57 // await awaited |
| 56 // into | 58 // into |
| 57 // yield AsyncFunctionAwait{Caught,Uncaught}(.generator, awaited) | 59 // yield AsyncFunctionAwait{Caught,Uncaught}(.generator, awaited, .promise) |
| 58 // The 'awaited' parameter is the value; the generator stands in | 60 // The 'awaited' parameter is the value; the generator stands in |
| 59 // for the asyncContext, and mark is metadata for debugging | 61 // for the asyncContext, and .promise is the larger promise under |
| 60 function AsyncFunctionAwait(generator, awaited, mark) { | 62 // construction by the enclosing async function. |
| 63 function AsyncFunctionAwait(generator, awaited, outerPromise) { | |
| 61 // Promise.resolve(awaited).then( | 64 // Promise.resolve(awaited).then( |
| 62 // value => AsyncFunctionNext(value), | 65 // value => AsyncFunctionNext(value), |
| 63 // error => AsyncFunctionThrow(error) | 66 // error => AsyncFunctionThrow(error) |
| 64 // ); | 67 // ); |
| 65 var promise = PromiseCastResolved(awaited); | 68 var promise = PromiseCastResolved(awaited); |
| 66 | 69 |
| 67 var onFulfilled = | 70 var onFulfilled = |
| 68 (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue); | 71 (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue); |
| 69 var onRejected = | 72 var onRejected = |
| 70 (sentError) => %_Call(AsyncFunctionThrow, generator, sentError); | 73 (sentError) => %_Call(AsyncFunctionThrow, generator, sentError); |
| 71 | 74 |
| 72 if (mark && DEBUG_IS_ACTIVE && IsPromise(awaited)) { | |
| 73 // Mark the reject handler callback such that it does not influence | |
| 74 // catch prediction. | |
| 75 SET_PRIVATE(onRejected, promiseAwaitHandlerSymbol, true); | |
| 76 } | |
| 77 | |
| 78 // Just forwarding the exception, so no debugEvent for throwawayCapability | 75 // Just forwarding the exception, so no debugEvent for throwawayCapability |
| 79 var throwawayCapability = NewPromiseCapability(GlobalPromise, false); | 76 var throwawayCapability = NewPromiseCapability(GlobalPromise, false); |
| 80 return PerformPromiseThen(promise, onFulfilled, onRejected, | 77 // The Promise will be thrown away and not handled, but it shouldn't trigger |
| 81 throwawayCapability); | 78 // unhandled reject events as its work is done |
| 79 SET_PRIVATE(throwawayCapability.promise, promiseHasHandlerSymbol, true); | |
| 80 PerformPromiseThen(promise, onFulfilled, onRejected, throwawayCapability); | |
| 81 | |
| 82 if (DEBUG_IS_ACTIVE && !IS_UNDEFINED(outerPromise)) { | |
| 83 if (IsPromise(awaited)) { | |
| 84 // Mark the reject handler callback to continue recursing to outerPromise | |
| 85 SET_PRIVATE(onRejected, promiseAwaitHandlerSymbol, outerPromise); | |
|
adamk
2016/09/15 22:39:40
Most other places that use promiseAwaitHandlerSymb
Dan Ehrenberg
2016/09/17 00:04:31
It's the first bullet.
| |
| 86 } | |
| 87 | |
| 88 // Mark the dependency to outerPromise in case the throwaway Promise is | |
| 89 // found on the Promise stack | |
| 90 SET_PRIVATE(throwawayCapability.promise, promiseAwaitHandlerSymbol, | |
| 91 outerPromise); | |
| 92 } | |
| 82 } | 93 } |
| 83 | 94 |
| 84 // Called by the parser from the desugaring of 'await' when catch | 95 // Called by the parser from the desugaring of 'await' when catch |
| 85 // prediction indicates no locally surrounding catch block | 96 // prediction indicates no locally surrounding catch block |
| 86 function AsyncFunctionAwaitUncaught(generator, awaited) { | 97 function AsyncFunctionAwaitUncaught(generator, awaited, outerPromise) { |
| 87 // TODO(littledan): Install a dependency edge from awaited to outerPromise | 98 AsyncFunctionAwait(generator, awaited, outerPromise); |
| 88 return AsyncFunctionAwait(generator, awaited, true); | |
| 89 } | 99 } |
| 90 | 100 |
| 91 // Called by the parser from the desugaring of 'await' when catch | 101 // Called by the parser from the desugaring of 'await' when catch |
| 92 // prediction indicates that there is a locally surrounding catch block | 102 // prediction indicates that there is a locally surrounding catch block |
| 93 function AsyncFunctionAwaitCaught(generator, awaited) { | 103 function AsyncFunctionAwaitCaught(generator, awaited, outerPromise) { |
| 94 if (DEBUG_IS_ACTIVE && IsPromise(awaited)) { | 104 if (DEBUG_IS_ACTIVE && IsPromise(awaited)) { |
| 95 SET_PRIVATE(awaited, promiseHandledHintSymbol, true); | 105 SET_PRIVATE(awaited, promiseHandledHintSymbol, true); |
| 96 } | 106 } |
| 97 return AsyncFunctionAwait(generator, awaited, false); | 107 // Pass undefined for the outer Promise to not waste time setting up |
| 108 // or following the dependency chain when this Promise is already marked | |
| 109 // as handled | |
| 110 AsyncFunctionAwait(generator, awaited, UNDEFINED); | |
| 98 } | 111 } |
| 99 | 112 |
| 100 // How the parser rejects promises from async/await desugaring | 113 // How the parser rejects promises from async/await desugaring |
| 101 function RejectPromiseNoDebugEvent(promise, reason) { | 114 function RejectPromiseNoDebugEvent(promise, reason) { |
| 102 return RejectPromise(promise, reason, false); | 115 return RejectPromise(promise, reason, false); |
| 103 } | 116 } |
| 104 | 117 |
| 105 %InstallToContext([ | 118 %InstallToContext([ |
| 106 "async_function_await_caught", AsyncFunctionAwaitCaught, | 119 "async_function_await_caught", AsyncFunctionAwaitCaught, |
| 107 "async_function_await_uncaught", AsyncFunctionAwaitUncaught, | 120 "async_function_await_uncaught", AsyncFunctionAwaitUncaught, |
| 108 "reject_promise_no_debug_event", RejectPromiseNoDebugEvent, | 121 "reject_promise_no_debug_event", RejectPromiseNoDebugEvent, |
| 109 ]); | 122 ]); |
| 110 | 123 |
| 111 }) | 124 }) |
| OLD | NEW |