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 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var AsyncFunctionNext; | 14 var AsyncFunctionNext; |
15 var AsyncFunctionThrow; | 15 var AsyncFunctionThrow; |
16 var GlobalPromise; | 16 var GlobalPromise; |
| 17 var IsPromise; |
17 var NewPromiseCapability; | 18 var NewPromiseCapability; |
18 var PerformPromiseThen; | 19 var PerformPromiseThen; |
19 var PromiseCastResolved; | 20 var PromiseCreate; |
20 var RejectPromise; | 21 var RejectPromise; |
| 22 var ResolvePromise; |
21 | 23 |
22 utils.Import(function(from) { | 24 utils.Import(function(from) { |
23 AsyncFunctionNext = from.AsyncFunctionNext; | 25 AsyncFunctionNext = from.AsyncFunctionNext; |
24 AsyncFunctionThrow = from.AsyncFunctionThrow; | 26 AsyncFunctionThrow = from.AsyncFunctionThrow; |
| 27 IsPromise = from.IsPromise; |
25 GlobalPromise = from.GlobalPromise; | 28 GlobalPromise = from.GlobalPromise; |
26 NewPromiseCapability = from.NewPromiseCapability; | 29 NewPromiseCapability = from.NewPromiseCapability; |
27 PromiseCastResolved = from.PromiseCastResolved; | 30 PromiseCreate = from.PromiseCreate; |
28 PerformPromiseThen = from.PerformPromiseThen; | 31 PerformPromiseThen = from.PerformPromiseThen; |
29 RejectPromise = from.RejectPromise; | 32 RejectPromise = from.RejectPromise; |
| 33 ResolvePromise = from.ResolvePromise; |
30 }); | 34 }); |
31 | 35 |
| 36 var promiseAwaitHandlerSymbol = utils.ImportNow("promise_await_handler_symbol"); |
| 37 var promiseHandledHintSymbol = |
| 38 utils.ImportNow("promise_handled_hint_symbol"); |
| 39 |
32 // ------------------------------------------------------------------- | 40 // ------------------------------------------------------------------- |
33 | 41 |
34 function AsyncFunctionAwait(generator, value) { | 42 function PromiseCastResolved(value) { |
35 // Promise.resolve(value).then( | 43 if (IsPromise(value)) { |
| 44 return value; |
| 45 } else { |
| 46 var promise = PromiseCreate(); |
| 47 ResolvePromise(promise, value); |
| 48 return promise; |
| 49 } |
| 50 } |
| 51 |
| 52 // ES#abstract-ops-async-function-await |
| 53 // AsyncFunctionAwait ( value ) |
| 54 // Shared logic for the core of await. The parser desugars |
| 55 // await awaited |
| 56 // into |
| 57 // yield AsyncFunctionAwait{Caught,Uncaught}(.generator, awaited) |
| 58 // The 'awaited' parameter is the value; the generator stands in |
| 59 // for the asyncContext, and mark is metadata for debugging |
| 60 function AsyncFunctionAwait(generator, awaited, mark) { |
| 61 // Promise.resolve(awaited).then( |
36 // value => AsyncFunctionNext(value), | 62 // value => AsyncFunctionNext(value), |
37 // error => AsyncFunctionThrow(error) | 63 // error => AsyncFunctionThrow(error) |
38 // ); | 64 // ); |
39 var promise = PromiseCastResolved(value); | 65 var promise = PromiseCastResolved(awaited); |
40 | 66 |
41 var onFulfilled = | 67 var onFulfilled = |
42 (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue); | 68 (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue); |
43 var onRejected = | 69 var onRejected = |
44 (sentError) => %_Call(AsyncFunctionThrow, generator, sentError); | 70 (sentError) => %_Call(AsyncFunctionThrow, generator, sentError); |
45 | 71 |
46 // false debugEvent to avoid redundant ExceptionEvents | 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 |
47 var throwawayCapability = NewPromiseCapability(GlobalPromise, false); | 79 var throwawayCapability = NewPromiseCapability(GlobalPromise, false); |
48 return PerformPromiseThen(promise, onFulfilled, onRejected, | 80 return PerformPromiseThen(promise, onFulfilled, onRejected, |
49 throwawayCapability); | 81 throwawayCapability); |
50 } | 82 } |
51 | 83 |
| 84 // Called by the parser from the desugaring of 'await' when catch |
| 85 // prediction indicates no locally surrounding catch block |
| 86 function AsyncFunctionAwaitUncaught(generator, awaited) { |
| 87 // TODO(littledan): Install a dependency edge from awaited to outerPromise |
| 88 return AsyncFunctionAwait(generator, awaited, true); |
| 89 } |
| 90 |
| 91 // Called by the parser from the desugaring of 'await' when catch |
| 92 // prediction indicates that there is a locally surrounding catch block |
| 93 function AsyncFunctionAwaitCaught(generator, awaited) { |
| 94 if (DEBUG_IS_ACTIVE && IsPromise(awaited)) { |
| 95 SET_PRIVATE(awaited, promiseHandledHintSymbol, true); |
| 96 } |
| 97 return AsyncFunctionAwait(generator, awaited, false); |
| 98 } |
| 99 |
52 // How the parser rejects promises from async/await desugaring | 100 // How the parser rejects promises from async/await desugaring |
53 function RejectPromiseNoDebugEvent(promise, reason) { | 101 function RejectPromiseNoDebugEvent(promise, reason) { |
54 return RejectPromise(promise, reason, false); | 102 return RejectPromise(promise, reason, false); |
55 } | 103 } |
56 | 104 |
57 %InstallToContext([ | 105 %InstallToContext([ |
58 "async_function_await", AsyncFunctionAwait, | 106 "async_function_await_caught", AsyncFunctionAwaitCaught, |
| 107 "async_function_await_uncaught", AsyncFunctionAwaitUncaught, |
59 "reject_promise_no_debug_event", RejectPromiseNoDebugEvent, | 108 "reject_promise_no_debug_event", RejectPromiseNoDebugEvent, |
60 ]); | 109 ]); |
61 | 110 |
62 }) | 111 }) |
OLD | NEW |