Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Unified Diff: runtime/vm/parser.cc

Issue 2692803006: Track the 'awaiter return' call stack use it to detect uncaught exceptions in async functions (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/parser.cc
diff --git a/runtime/vm/parser.cc b/runtime/vm/parser.cc
index 1afacf5b0017cb5c7c053202c0a0c4ff1e2e99f6..b91849be02a5d13c690170a9be26a86c43a05d15 100644
--- a/runtime/vm/parser.cc
+++ b/runtime/vm/parser.cc
@@ -6816,6 +6816,8 @@ void Parser::OpenAsyncTryBlock() {
// This is the outermost try-catch in the function.
ASSERT(try_stack_ == NULL);
PushTry(current_block_);
+ // Validate that we always get try index of 0.
+ ASSERT(try_stack_->try_index() == CatchClauseNode::kImplicitAsyncTryIndex);
SetupSavedTryContext(context_var);
}
@@ -7085,6 +7087,7 @@ void Parser::AddAsyncGeneratorVariables() {
// var :async_then_callback;
// var :async_catch_error_callback;
// var :async_stack_trace;
+ // var :controller_stream;
// These variables are used to store the async generator closure containing
// the body of the async* function. They are used by the await operator.
LocalVariable* controller_var =
@@ -7107,6 +7110,10 @@ void Parser::AddAsyncGeneratorVariables() {
LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
Symbols::AsyncStackTraceVar(), Object::dynamic_type());
current_block_->scope->AddVariable(async_stack_trace);
+ LocalVariable* controller_stream = new (Z)
+ LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
+ Symbols::ControllerStream(), Object::dynamic_type());
+ current_block_->scope->AddVariable(controller_stream);
}
@@ -7176,7 +7183,8 @@ RawFunction* Parser::OpenAsyncGeneratorFunction(TokenPosition async_func_pos) {
// var :async_then_callback = _asyncThenWrapperHelper(:async_op);
// var :async_catch_error_callback = _asyncCatchErrorWrapperHelper(:async_op);
// :controller = new _AsyncStarStreamController(:async_op);
-// return :controller.stream;
+// var :controller_stream = :controller.stream;
+// return :controller_stream;
// }
SequenceNode* Parser::CloseAsyncGeneratorFunction(const Function& closure_func,
SequenceNode* closure_body) {
@@ -7207,6 +7215,9 @@ SequenceNode* Parser::CloseAsyncGeneratorFunction(const Function& closure_func,
existing_var = closure_body->scope()->LookupVariable(
Symbols::AsyncStackTraceVar(), false);
ASSERT((existing_var != NULL) && existing_var->is_captured());
+ existing_var =
+ closure_body->scope()->LookupVariable(Symbols::ControllerStream(), false);
+ ASSERT((existing_var != NULL) && existing_var->is_captured());
const Library& async_lib = Library::Handle(Library::AsyncLibrary());
@@ -7315,13 +7326,28 @@ SequenceNode* Parser::CloseAsyncGeneratorFunction(const Function& closure_func,
TokenPosition::kNoSource, controller_var, controller_constructor_call);
current_block_->statements->Add(store_controller);
+ // Grab :controller.stream
+ InstanceGetterNode* controller_stream = new (Z) InstanceGetterNode(
+ TokenPosition::kNoSource,
+ new (Z) LoadLocalNode(TokenPosition::kNoSource, controller_var),
+ Symbols::Stream());
+
+ // Store :controller.stream into :controller_stream inside the closure.
+ // We have to remember the stream because a new instance is generated for
+ // each getter invocation and in order to recreate the linkage, we need the
+ // awaited on instance.
+ LocalVariable* controller_stream_var =
+ current_block_->scope->LookupVariable(Symbols::ControllerStream(), false);
+ ASSERT(controller_stream_var != NULL);
+
+ StoreLocalNode* store_controller_stream = new (Z) StoreLocalNode(
+ TokenPosition::kNoSource, controller_stream_var, controller_stream);
+ current_block_->statements->Add(store_controller_stream);
+
// return :controller.stream;
ReturnNode* return_node = new (Z) ReturnNode(
TokenPosition::kNoSource,
- new (Z) InstanceGetterNode(
- TokenPosition::kNoSource,
- new (Z) LoadLocalNode(TokenPosition::kNoSource, controller_var),
- Symbols::Stream()));
+ new (Z) LoadLocalNode(TokenPosition::kNoSource, controller_stream_var));
current_block_->statements->Add(return_node);
return CloseBlock();
}
@@ -9065,6 +9091,37 @@ AstNode* Parser::ParseAwaitForStatement(String* label_name) {
ParseAwaitableExpr(kAllowConst, kConsumeCascades, NULL);
ExpectToken(Token::kRPAREN);
+ // Create :stream to store the stream into temporarily.
+ LocalVariable* stream_var =
+ new (Z) LocalVariable(stream_expr_pos, stream_expr_pos,
+ Symbols::ColonStream(), Object::dynamic_type());
+ current_block_->scope->AddVariable(stream_var);
+
+ // Store the stream expression into a variable.
+ StoreLocalNode* store_stream_var =
+ new (Z) StoreLocalNode(stream_expr_pos, stream_var, stream_expr);
+ current_block_->statements->Add(store_stream_var);
+
+ // Register the awaiter on the stream by invoking `_asyncStarListenHelper`.
+ const Library& async_lib = Library::Handle(Library::AsyncLibrary());
+ const Function& async_star_listen_helper = Function::ZoneHandle(
+ Z,
+ async_lib.LookupFunctionAllowPrivate(Symbols::_AsyncStarListenHelper()));
+ ASSERT(!async_star_listen_helper.IsNull());
+ LocalVariable* async_op_var =
+ current_block_->scope->LookupVariable(Symbols::AsyncOperation(), false);
+ ASSERT(async_op_var != NULL);
+ ArgumentListNode* async_star_listen_helper_args =
+ new (Z) ArgumentListNode(stream_expr_pos);
+ async_star_listen_helper_args->Add(
+ new (Z) LoadLocalNode(stream_expr_pos, stream_var));
+ async_star_listen_helper_args->Add(
+ new (Z) LoadLocalNode(stream_expr_pos, async_op_var));
+ StaticCallNode* async_star_listen_helper_call = new (Z) StaticCallNode(
+ stream_expr_pos, async_star_listen_helper, async_star_listen_helper_args);
+
+ current_block_->statements->Add(async_star_listen_helper_call);
+
// Build creation of implicit StreamIterator.
// var :for-in-iter = new StreamIterator(stream_expr).
const Class& stream_iterator_cls =
@@ -9075,7 +9132,7 @@ AstNode* Parser::ParseAwaitForStatement(String* label_name) {
stream_iterator_cls.LookupFunction(Symbols::StreamIteratorConstructor()));
ASSERT(!iterator_ctor.IsNull());
ArgumentListNode* ctor_args = new (Z) ArgumentListNode(stream_expr_pos);
- ctor_args->Add(stream_expr);
+ ctor_args->Add(new (Z) LoadLocalNode(stream_expr_pos, stream_var));
ConstructorCallNode* ctor_call = new (Z) ConstructorCallNode(
stream_expr_pos, TypeArguments::ZoneHandle(Z), iterator_ctor, ctor_args);
const AbstractType& iterator_type = Object::dynamic_type();
« runtime/vm/object.cc ('K') | « runtime/vm/object_test.cc ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698