Chromium Code Reviews

Side by Side Diff: src/js/harmony-async-await.js

Issue 2317383002: Async/await Promise dependency graph (Closed)
Patch Set: Fix parser Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
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 36 matching lines...)
47 ResolvePromise(promise, value); 47 ResolvePromise(promise, value);
48 return promise; 48 return promise;
49 } 49 }
50 } 50 }
51 51
52 // ES#abstract-ops-async-function-await 52 // ES#abstract-ops-async-function-await
53 // AsyncFunctionAwait ( value ) 53 // AsyncFunctionAwait ( value )
54 // Shared logic for the core of await. The parser desugars 54 // Shared logic for the core of await. The parser desugars
55 // await awaited 55 // await awaited
56 // into 56 // into
57 // yield AsyncFunctionAwait{Caught,Uncaught}(.generator, awaited) 57 // yield AsyncFunctionAwait{Caught,Uncaught}(.generator, awaited, .promise)
58 // The 'awaited' parameter is the value; the generator stands in 58 // The 'awaited' parameter is the value; the generator stands in
59 // for the asyncContext, and mark is metadata for debugging 59 // for the asyncContext, and .promise is the larger promise under
60 function AsyncFunctionAwait(generator, awaited, mark) { 60 // construction by the enclosing async function.
61 function AsyncFunctionAwait(generator, awaited, outerPromise) {
61 // Promise.resolve(awaited).then( 62 // Promise.resolve(awaited).then(
62 // value => AsyncFunctionNext(value), 63 // value => AsyncFunctionNext(value),
63 // error => AsyncFunctionThrow(error) 64 // error => AsyncFunctionThrow(error)
64 // ); 65 // );
65 var promise = PromiseCastResolved(awaited); 66 var promise = PromiseCastResolved(awaited);
66 67
67 var onFulfilled = 68 var onFulfilled =
68 (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue); 69 (sentValue) => %_Call(AsyncFunctionNext, generator, sentValue);
69 var onRejected = 70 var onRejected =
70 (sentError) => %_Call(AsyncFunctionThrow, generator, sentError); 71 (sentError) => %_Call(AsyncFunctionThrow, generator, sentError);
71 72
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 73 // Just forwarding the exception, so no debugEvent for throwawayCapability
79 var throwawayCapability = NewPromiseCapability(GlobalPromise, false); 74 var throwawayCapability = NewPromiseCapability(GlobalPromise, false);
80 return PerformPromiseThen(promise, onFulfilled, onRejected, 75 PerformPromiseThen(promise, onFulfilled, onRejected, throwawayCapability);
81 throwawayCapability); 76
77 if (DEBUG_IS_ACTIVE && !IS_UNDEFINED(outerPromise)) {
78 if (IsPromise(awaited)) {
79 // Mark the reject handler callback to continue recursing to outerPromise
80 SET_PRIVATE(onRejected, promiseAwaitHandlerSymbol, outerPromise);
81 }
82
83 // Mark the dependency to outerPromise in case the throwaway Promise is
84 // found on the Promise stack
85 SET_PRIVATE(throwawayCapability.promise, promiseAwaitHandlerSymbol,
86 outerPromise);
87 }
82 } 88 }
83 89
84 // Called by the parser from the desugaring of 'await' when catch 90 // Called by the parser from the desugaring of 'await' when catch
85 // prediction indicates no locally surrounding catch block 91 // prediction indicates no locally surrounding catch block
86 function AsyncFunctionAwaitUncaught(generator, awaited) { 92 function AsyncFunctionAwaitUncaught(generator, awaited, outerPromise) {
87 // TODO(littledan): Install a dependency edge from awaited to outerPromise 93 AsyncFunctionAwait(generator, awaited, outerPromise);
88 return AsyncFunctionAwait(generator, awaited, true); 94 return outerPromise;
89 } 95 }
90 96
91 // Called by the parser from the desugaring of 'await' when catch 97 // Called by the parser from the desugaring of 'await' when catch
92 // prediction indicates that there is a locally surrounding catch block 98 // prediction indicates that there is a locally surrounding catch block
93 function AsyncFunctionAwaitCaught(generator, awaited) { 99 function AsyncFunctionAwaitCaught(generator, awaited, outerPromise) {
94 if (DEBUG_IS_ACTIVE && IsPromise(awaited)) { 100 if (DEBUG_IS_ACTIVE && IsPromise(awaited)) {
95 SET_PRIVATE(awaited, promiseHandledHintSymbol, true); 101 SET_PRIVATE(awaited, promiseHandledHintSymbol, true);
96 } 102 }
97 return AsyncFunctionAwait(generator, awaited, false); 103 // Pass undefined for the outer Promise to not waste time setting up
104 // or following the dependency chain when this Promise is already marked
105 // as handled
106 AsyncFunctionAwait(generator, awaited, UNDEFINED);
107 return outerPromise;
98 } 108 }
99 109
100 // How the parser rejects promises from async/await desugaring 110 // How the parser rejects promises from async/await desugaring
101 function RejectPromiseNoDebugEvent(promise, reason) { 111 function RejectPromiseNoDebugEvent(promise, reason) {
102 return RejectPromise(promise, reason, false); 112 return RejectPromise(promise, reason, false);
103 } 113 }
104 114
105 %InstallToContext([ 115 %InstallToContext([
106 "async_function_await_caught", AsyncFunctionAwaitCaught, 116 "async_function_await_caught", AsyncFunctionAwaitCaught,
107 "async_function_await_uncaught", AsyncFunctionAwaitUncaught, 117 "async_function_await_uncaught", AsyncFunctionAwaitUncaught,
108 "reject_promise_no_debug_event", RejectPromiseNoDebugEvent, 118 "reject_promise_no_debug_event", RejectPromiseNoDebugEvent,
109 ]); 119 ]);
110 120
111 }) 121 })
OLDNEW
« no previous file with comments | « src/js/collection.js ('k') | src/js/promise.js » ('j') | src/parsing/parser.cc » ('J')

Powered by Google App Engine