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

Side by Side 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: rmacnak review Created 3 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/parser.h" 5 #include "vm/parser.h"
6 #include "vm/flags.h" 6 #include "vm/flags.h"
7 7
8 #ifndef DART_PRECOMPILED_RUNTIME 8 #ifndef DART_PRECOMPILED_RUNTIME
9 9
10 #include "lib/invocation_mirror.h" 10 #include "lib/invocation_mirror.h"
(...skipping 6799 matching lines...) Expand 10 before | Expand all | Expand 10 after
6810 LocalVariable* saved_stack_trace_var = NULL; 6810 LocalVariable* saved_stack_trace_var = NULL;
6811 SetupExceptionVariables(current_block_->scope, true, &context_var, 6811 SetupExceptionVariables(current_block_->scope, true, &context_var,
6812 &exception_var, &stack_trace_var, 6812 &exception_var, &stack_trace_var,
6813 &saved_exception_var, &saved_stack_trace_var); 6813 &saved_exception_var, &saved_stack_trace_var);
6814 6814
6815 // Open the try block. 6815 // Open the try block.
6816 OpenBlock(); 6816 OpenBlock();
6817 // This is the outermost try-catch in the function. 6817 // This is the outermost try-catch in the function.
6818 ASSERT(try_stack_ == NULL); 6818 ASSERT(try_stack_ == NULL);
6819 PushTry(current_block_); 6819 PushTry(current_block_);
6820 // Validate that we always get try index of 0.
6821 ASSERT(try_stack_->try_index() == CatchClauseNode::kImplicitAsyncTryIndex);
6820 6822
6821 SetupSavedTryContext(context_var); 6823 SetupSavedTryContext(context_var);
6822 } 6824 }
6823 6825
6824 6826
6825 void Parser::AddSyncGenClosureParameters(ParamList* params) { 6827 void Parser::AddSyncGenClosureParameters(ParamList* params) {
6826 // Create the parameter list for the body closure of a sync generator: 6828 // Create the parameter list for the body closure of a sync generator:
6827 // 1) Implicit closure parameter; 6829 // 1) Implicit closure parameter;
6828 // 2) Iterator 6830 // 2) Iterator
6829 // Add implicit closure parameter if not already present. 6831 // Add implicit closure parameter if not already present.
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
7079 void Parser::AddAsyncGeneratorVariables() { 7081 void Parser::AddAsyncGeneratorVariables() {
7080 // Add to current block's scope: 7082 // Add to current block's scope:
7081 // var :controller; 7083 // var :controller;
7082 // The :controller variable is used by the async generator closure to 7084 // The :controller variable is used by the async generator closure to
7083 // store the StreamController object to which the yielded expressions 7085 // store the StreamController object to which the yielded expressions
7084 // are added. 7086 // are added.
7085 // var :async_op; 7087 // var :async_op;
7086 // var :async_then_callback; 7088 // var :async_then_callback;
7087 // var :async_catch_error_callback; 7089 // var :async_catch_error_callback;
7088 // var :async_stack_trace; 7090 // var :async_stack_trace;
7091 // var :controller_stream;
7089 // These variables are used to store the async generator closure containing 7092 // These variables are used to store the async generator closure containing
7090 // the body of the async* function. They are used by the await operator. 7093 // the body of the async* function. They are used by the await operator.
7091 LocalVariable* controller_var = 7094 LocalVariable* controller_var =
7092 new (Z) LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, 7095 new (Z) LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
7093 Symbols::Controller(), Object::dynamic_type()); 7096 Symbols::Controller(), Object::dynamic_type());
7094 current_block_->scope->AddVariable(controller_var); 7097 current_block_->scope->AddVariable(controller_var);
7095 LocalVariable* async_op_var = 7098 LocalVariable* async_op_var =
7096 new (Z) LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, 7099 new (Z) LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
7097 Symbols::AsyncOperation(), Object::dynamic_type()); 7100 Symbols::AsyncOperation(), Object::dynamic_type());
7098 current_block_->scope->AddVariable(async_op_var); 7101 current_block_->scope->AddVariable(async_op_var);
7099 LocalVariable* async_then_callback_var = new (Z) 7102 LocalVariable* async_then_callback_var = new (Z)
7100 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, 7103 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
7101 Symbols::AsyncThenCallback(), Object::dynamic_type()); 7104 Symbols::AsyncThenCallback(), Object::dynamic_type());
7102 current_block_->scope->AddVariable(async_then_callback_var); 7105 current_block_->scope->AddVariable(async_then_callback_var);
7103 LocalVariable* async_catch_error_callback_var = new (Z) 7106 LocalVariable* async_catch_error_callback_var = new (Z)
7104 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, 7107 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
7105 Symbols::AsyncCatchErrorCallback(), Object::dynamic_type()); 7108 Symbols::AsyncCatchErrorCallback(), Object::dynamic_type());
7106 current_block_->scope->AddVariable(async_catch_error_callback_var); 7109 current_block_->scope->AddVariable(async_catch_error_callback_var);
7107 LocalVariable* async_stack_trace = new (Z) 7110 LocalVariable* async_stack_trace = new (Z)
7108 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, 7111 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
7109 Symbols::AsyncStackTraceVar(), Object::dynamic_type()); 7112 Symbols::AsyncStackTraceVar(), Object::dynamic_type());
7110 current_block_->scope->AddVariable(async_stack_trace); 7113 current_block_->scope->AddVariable(async_stack_trace);
7114 LocalVariable* controller_stream = new (Z)
7115 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource,
7116 Symbols::ControllerStream(), Object::dynamic_type());
7117 current_block_->scope->AddVariable(controller_stream);
7111 } 7118 }
7112 7119
7113 7120
7114 RawFunction* Parser::OpenAsyncGeneratorFunction(TokenPosition async_func_pos) { 7121 RawFunction* Parser::OpenAsyncGeneratorFunction(TokenPosition async_func_pos) {
7115 TRACE_PARSER("OpenAsyncGeneratorFunction"); 7122 TRACE_PARSER("OpenAsyncGeneratorFunction");
7116 AddContinuationVariables(); 7123 AddContinuationVariables();
7117 AddAsyncGeneratorVariables(); 7124 AddAsyncGeneratorVariables();
7118 7125
7119 Function& closure = Function::Handle(Z); 7126 Function& closure = Function::Handle(Z);
7120 bool is_new_closure = false; 7127 bool is_new_closure = false;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
7170 // var :controller; 7177 // var :controller;
7171 // var :await_jump_var = -1; 7178 // var :await_jump_var = -1;
7172 // var :await_context_var; 7179 // var :await_context_var;
7173 // f_async_body() { 7180 // f_async_body() {
7174 // ... source code of f ... 7181 // ... source code of f ...
7175 // } 7182 // }
7176 // var :async_op = f_async_body; 7183 // var :async_op = f_async_body;
7177 // var :async_then_callback = _asyncThenWrapperHelper(:async_op); 7184 // var :async_then_callback = _asyncThenWrapperHelper(:async_op);
7178 // var :async_catch_error_callback = _asyncCatchErrorWrapperHelper(:async_op); 7185 // var :async_catch_error_callback = _asyncCatchErrorWrapperHelper(:async_op);
7179 // :controller = new _AsyncStarStreamController(:async_op); 7186 // :controller = new _AsyncStarStreamController(:async_op);
7180 // return :controller.stream; 7187 // var :controller_stream = :controller.stream;
7188 // return :controller_stream;
7181 // } 7189 // }
7182 SequenceNode* Parser::CloseAsyncGeneratorFunction(const Function& closure_func, 7190 SequenceNode* Parser::CloseAsyncGeneratorFunction(const Function& closure_func,
7183 SequenceNode* closure_body) { 7191 SequenceNode* closure_body) {
7184 TRACE_PARSER("CloseAsyncGeneratorFunction"); 7192 TRACE_PARSER("CloseAsyncGeneratorFunction");
7185 ASSERT(!closure_func.IsNull()); 7193 ASSERT(!closure_func.IsNull());
7186 ASSERT(closure_body != NULL); 7194 ASSERT(closure_body != NULL);
7187 7195
7188 // Explicitly reference variables of the async genenerator function from the 7196 // Explicitly reference variables of the async genenerator function from the
7189 // closure body in order to mark them as captured. 7197 // closure body in order to mark them as captured.
7190 LocalVariable* existing_var = 7198 LocalVariable* existing_var =
(...skipping 10 matching lines...) Expand all
7201 ASSERT((existing_var != NULL) && existing_var->is_captured()); 7209 ASSERT((existing_var != NULL) && existing_var->is_captured());
7202 existing_var = closure_body->scope()->LookupVariable( 7210 existing_var = closure_body->scope()->LookupVariable(
7203 Symbols::AsyncThenCallback(), false); 7211 Symbols::AsyncThenCallback(), false);
7204 ASSERT((existing_var != NULL) && existing_var->is_captured()); 7212 ASSERT((existing_var != NULL) && existing_var->is_captured());
7205 existing_var = closure_body->scope()->LookupVariable( 7213 existing_var = closure_body->scope()->LookupVariable(
7206 Symbols::AsyncCatchErrorCallback(), false); 7214 Symbols::AsyncCatchErrorCallback(), false);
7207 ASSERT((existing_var != NULL) && existing_var->is_captured()); 7215 ASSERT((existing_var != NULL) && existing_var->is_captured());
7208 existing_var = closure_body->scope()->LookupVariable( 7216 existing_var = closure_body->scope()->LookupVariable(
7209 Symbols::AsyncStackTraceVar(), false); 7217 Symbols::AsyncStackTraceVar(), false);
7210 ASSERT((existing_var != NULL) && existing_var->is_captured()); 7218 ASSERT((existing_var != NULL) && existing_var->is_captured());
7219 existing_var =
7220 closure_body->scope()->LookupVariable(Symbols::ControllerStream(), false);
7221 ASSERT((existing_var != NULL) && existing_var->is_captured());
7211 7222
7212 const Library& async_lib = Library::Handle(Library::AsyncLibrary()); 7223 const Library& async_lib = Library::Handle(Library::AsyncLibrary());
7213 7224
7214 const Class& controller_class = Class::Handle( 7225 const Class& controller_class = Class::Handle(
7215 Z, 7226 Z,
7216 async_lib.LookupClassAllowPrivate(Symbols::_AsyncStarStreamController())); 7227 async_lib.LookupClassAllowPrivate(Symbols::_AsyncStarStreamController()));
7217 ASSERT(!controller_class.IsNull()); 7228 ASSERT(!controller_class.IsNull());
7218 const Function& controller_constructor = Function::ZoneHandle( 7229 const Function& controller_constructor = Function::ZoneHandle(
7219 Z, controller_class.LookupConstructorAllowPrivate( 7230 Z, controller_class.LookupConstructorAllowPrivate(
7220 Symbols::_AsyncStarStreamControllerConstructor())); 7231 Symbols::_AsyncStarStreamControllerConstructor()));
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
7309 ConstructorCallNode* controller_constructor_call = 7320 ConstructorCallNode* controller_constructor_call =
7310 new (Z) ConstructorCallNode(TokenPosition::kNoSource, 7321 new (Z) ConstructorCallNode(TokenPosition::kNoSource,
7311 TypeArguments::ZoneHandle(Z), 7322 TypeArguments::ZoneHandle(Z),
7312 controller_constructor, arguments); 7323 controller_constructor, arguments);
7313 LocalVariable* controller_var = 7324 LocalVariable* controller_var =
7314 current_block_->scope->LookupVariable(Symbols::Controller(), false); 7325 current_block_->scope->LookupVariable(Symbols::Controller(), false);
7315 StoreLocalNode* store_controller = new (Z) StoreLocalNode( 7326 StoreLocalNode* store_controller = new (Z) StoreLocalNode(
7316 TokenPosition::kNoSource, controller_var, controller_constructor_call); 7327 TokenPosition::kNoSource, controller_var, controller_constructor_call);
7317 current_block_->statements->Add(store_controller); 7328 current_block_->statements->Add(store_controller);
7318 7329
7330 // Grab :controller.stream
7331 InstanceGetterNode* controller_stream = new (Z) InstanceGetterNode(
7332 TokenPosition::kNoSource,
7333 new (Z) LoadLocalNode(TokenPosition::kNoSource, controller_var),
7334 Symbols::Stream());
7335
7336 // Store :controller.stream into :controller_stream inside the closure.
7337 // We have to remember the stream because a new instance is generated for
7338 // each getter invocation and in order to recreate the linkage, we need the
7339 // awaited on instance.
7340 LocalVariable* controller_stream_var =
7341 current_block_->scope->LookupVariable(Symbols::ControllerStream(), false);
7342 ASSERT(controller_stream_var != NULL);
7343
7344 StoreLocalNode* store_controller_stream = new (Z) StoreLocalNode(
7345 TokenPosition::kNoSource, controller_stream_var, controller_stream);
7346 current_block_->statements->Add(store_controller_stream);
7347
7319 // return :controller.stream; 7348 // return :controller.stream;
7320 ReturnNode* return_node = new (Z) ReturnNode( 7349 ReturnNode* return_node = new (Z) ReturnNode(
7321 TokenPosition::kNoSource, 7350 TokenPosition::kNoSource,
7322 new (Z) InstanceGetterNode( 7351 new (Z) LoadLocalNode(TokenPosition::kNoSource, controller_stream_var));
7323 TokenPosition::kNoSource,
7324 new (Z) LoadLocalNode(TokenPosition::kNoSource, controller_var),
7325 Symbols::Stream()));
7326 current_block_->statements->Add(return_node); 7352 current_block_->statements->Add(return_node);
7327 return CloseBlock(); 7353 return CloseBlock();
7328 } 7354 }
7329 7355
7330 7356
7331 void Parser::OpenAsyncGeneratorClosure() { 7357 void Parser::OpenAsyncGeneratorClosure() {
7332 async_temp_scope_ = current_block_->scope; 7358 async_temp_scope_ = current_block_->scope;
7333 OpenAsyncTryBlock(); 7359 OpenAsyncTryBlock();
7334 } 7360 }
7335 7361
(...skipping 1723 matching lines...) Expand 10 before | Expand all | Expand 10 after
9059 // contexts and if they have a matching token position range, 9085 // contexts and if they have a matching token position range,
9060 // it can be an issue (cf. bug 26941). 9086 // it can be an issue (cf. bug 26941).
9061 OpenBlock(); 9087 OpenBlock();
9062 const Block* await_for_block = current_block_; 9088 const Block* await_for_block = current_block_;
9063 9089
9064 const TokenPosition stream_expr_pos = TokenPos(); 9090 const TokenPosition stream_expr_pos = TokenPos();
9065 AstNode* stream_expr = 9091 AstNode* stream_expr =
9066 ParseAwaitableExpr(kAllowConst, kConsumeCascades, NULL); 9092 ParseAwaitableExpr(kAllowConst, kConsumeCascades, NULL);
9067 ExpectToken(Token::kRPAREN); 9093 ExpectToken(Token::kRPAREN);
9068 9094
9095 // Create :stream to store the stream into temporarily.
9096 LocalVariable* stream_var =
9097 new (Z) LocalVariable(stream_expr_pos, stream_expr_pos,
9098 Symbols::ColonStream(), Object::dynamic_type());
9099 current_block_->scope->AddVariable(stream_var);
9100
9101 // Store the stream expression into a variable.
9102 StoreLocalNode* store_stream_var =
9103 new (Z) StoreLocalNode(stream_expr_pos, stream_var, stream_expr);
9104 current_block_->statements->Add(store_stream_var);
9105
9106 // Register the awaiter on the stream by invoking `_asyncStarListenHelper`.
9107 const Library& async_lib = Library::Handle(Library::AsyncLibrary());
9108 const Function& async_star_listen_helper = Function::ZoneHandle(
9109 Z,
9110 async_lib.LookupFunctionAllowPrivate(Symbols::_AsyncStarListenHelper()));
9111 ASSERT(!async_star_listen_helper.IsNull());
9112 LocalVariable* async_op_var =
9113 current_block_->scope->LookupVariable(Symbols::AsyncOperation(), false);
9114 ASSERT(async_op_var != NULL);
9115 ArgumentListNode* async_star_listen_helper_args =
9116 new (Z) ArgumentListNode(stream_expr_pos);
9117 async_star_listen_helper_args->Add(
9118 new (Z) LoadLocalNode(stream_expr_pos, stream_var));
9119 async_star_listen_helper_args->Add(
9120 new (Z) LoadLocalNode(stream_expr_pos, async_op_var));
9121 StaticCallNode* async_star_listen_helper_call = new (Z) StaticCallNode(
9122 stream_expr_pos, async_star_listen_helper, async_star_listen_helper_args);
9123
9124 current_block_->statements->Add(async_star_listen_helper_call);
9125
9069 // Build creation of implicit StreamIterator. 9126 // Build creation of implicit StreamIterator.
9070 // var :for-in-iter = new StreamIterator(stream_expr). 9127 // var :for-in-iter = new StreamIterator(stream_expr).
9071 const Class& stream_iterator_cls = 9128 const Class& stream_iterator_cls =
9072 Class::ZoneHandle(Z, I->object_store()->stream_iterator_class()); 9129 Class::ZoneHandle(Z, I->object_store()->stream_iterator_class());
9073 ASSERT(!stream_iterator_cls.IsNull()); 9130 ASSERT(!stream_iterator_cls.IsNull());
9074 const Function& iterator_ctor = Function::ZoneHandle( 9131 const Function& iterator_ctor = Function::ZoneHandle(
9075 Z, 9132 Z,
9076 stream_iterator_cls.LookupFunction(Symbols::StreamIteratorConstructor())); 9133 stream_iterator_cls.LookupFunction(Symbols::StreamIteratorConstructor()));
9077 ASSERT(!iterator_ctor.IsNull()); 9134 ASSERT(!iterator_ctor.IsNull());
9078 ArgumentListNode* ctor_args = new (Z) ArgumentListNode(stream_expr_pos); 9135 ArgumentListNode* ctor_args = new (Z) ArgumentListNode(stream_expr_pos);
9079 ctor_args->Add(stream_expr); 9136 ctor_args->Add(new (Z) LoadLocalNode(stream_expr_pos, stream_var));
9080 ConstructorCallNode* ctor_call = new (Z) ConstructorCallNode( 9137 ConstructorCallNode* ctor_call = new (Z) ConstructorCallNode(
9081 stream_expr_pos, TypeArguments::ZoneHandle(Z), iterator_ctor, ctor_args); 9138 stream_expr_pos, TypeArguments::ZoneHandle(Z), iterator_ctor, ctor_args);
9082 const AbstractType& iterator_type = Object::dynamic_type(); 9139 const AbstractType& iterator_type = Object::dynamic_type();
9083 LocalVariable* iterator_var = new (Z) LocalVariable( 9140 LocalVariable* iterator_var = new (Z) LocalVariable(
9084 stream_expr_pos, stream_expr_pos, Symbols::ForInIter(), iterator_type); 9141 stream_expr_pos, stream_expr_pos, Symbols::ForInIter(), iterator_type);
9085 current_block_->scope->AddVariable(iterator_var); 9142 current_block_->scope->AddVariable(iterator_var);
9086 AstNode* iterator_init = 9143 AstNode* iterator_init =
9087 new (Z) StoreLocalNode(stream_expr_pos, iterator_var, ctor_call); 9144 new (Z) StoreLocalNode(stream_expr_pos, iterator_var, ctor_call);
9088 current_block_->statements->Add(iterator_init); 9145 current_block_->statements->Add(iterator_init);
9089 9146
(...skipping 5985 matching lines...) Expand 10 before | Expand all | Expand 10 after
15075 const ArgumentListNode& function_args, 15132 const ArgumentListNode& function_args,
15076 const LocalVariable* temp_for_last_arg, 15133 const LocalVariable* temp_for_last_arg,
15077 bool is_super_invocation) { 15134 bool is_super_invocation) {
15078 UNREACHABLE(); 15135 UNREACHABLE();
15079 return NULL; 15136 return NULL;
15080 } 15137 }
15081 15138
15082 } // namespace dart 15139 } // namespace dart
15083 15140
15084 #endif // DART_PRECOMPILED_RUNTIME 15141 #endif // DART_PRECOMPILED_RUNTIME
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698