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

Side by Side Diff: src/parsing/parser.cc

Issue 1895603002: [esnext] prototype runtime implementation for async functions (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@AsyncFunction
Patch Set: Partially fix `throw` completions (resumption works, but no resumption doesn't) Created 4 years, 8 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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 #include "src/parsing/parser.h" 5 #include "src/parsing/parser.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast/ast.h" 8 #include "src/ast/ast.h"
9 #include "src/ast/ast-expression-rewriter.h" 9 #include "src/ast/ast-expression-rewriter.h"
10 #include "src/ast/ast-expression-visitor.h" 10 #include "src/ast/ast-expression-visitor.h"
(...skipping 2663 matching lines...) Expand 10 before | Expand all | Expand 10 after
2674 is_object_conditional, pos); 2674 is_object_conditional, pos);
2675 } 2675 }
2676 2676
2677 // ES6 14.6.1 Static Semantics: IsInTailPosition 2677 // ES6 14.6.1 Static Semantics: IsInTailPosition
2678 if (allow_tailcalls() && !is_sloppy(language_mode())) { 2678 if (allow_tailcalls() && !is_sloppy(language_mode())) {
2679 function_state_->AddExpressionInTailPosition(return_value); 2679 function_state_->AddExpressionInTailPosition(return_value);
2680 } 2680 }
2681 } 2681 }
2682 ExpectSemicolon(CHECK_OK); 2682 ExpectSemicolon(CHECK_OK);
2683 2683
2684 if (is_generator()) { 2684 if (is_generator() || is_async_function()) {
2685 return_value = BuildIteratorResult(return_value, true); 2685 return_value = BuildIteratorResult(return_value, true);
2686 } 2686 }
2687 2687
2688 result = factory()->NewReturnStatement(return_value, loc.beg_pos); 2688 result = factory()->NewReturnStatement(return_value, loc.beg_pos);
2689 2689
2690 Scope* decl_scope = scope_->DeclarationScope(); 2690 Scope* decl_scope = scope_->DeclarationScope();
2691 if (decl_scope->is_script_scope() || decl_scope->is_eval_scope()) { 2691 if (decl_scope->is_script_scope() || decl_scope->is_eval_scope()) {
2692 ReportMessageAt(loc, MessageTemplate::kIllegalReturn); 2692 ReportMessageAt(loc, MessageTemplate::kIllegalReturn);
2693 *ok = false; 2693 *ok = false;
2694 return NULL; 2694 return NULL;
(...skipping 1324 matching lines...) Expand 10 before | Expand all | Expand 10 after
4019 // Getter :: 4019 // Getter ::
4020 // '(' ')' '{' FunctionBody '}' 4020 // '(' ')' '{' FunctionBody '}'
4021 // 4021 //
4022 // Setter :: 4022 // Setter ::
4023 // '(' PropertySetParameterList ')' '{' FunctionBody '}' 4023 // '(' PropertySetParameterList ')' '{' FunctionBody '}'
4024 4024
4025 int pos = function_token_pos == RelocInfo::kNoPosition 4025 int pos = function_token_pos == RelocInfo::kNoPosition
4026 ? peek_position() : function_token_pos; 4026 ? peek_position() : function_token_pos;
4027 4027
4028 bool is_generator = IsGeneratorFunction(kind); 4028 bool is_generator = IsGeneratorFunction(kind);
4029 bool is_async = IsAsyncFunction(kind);
4029 4030
4030 // Anonymous functions were passed either the empty symbol or a null 4031 // Anonymous functions were passed either the empty symbol or a null
4031 // handle as the function name. Remember if we were passed a non-empty 4032 // handle as the function name. Remember if we were passed a non-empty
4032 // handle to decide whether to invoke function name inference. 4033 // handle to decide whether to invoke function name inference.
4033 bool should_infer_name = function_name == NULL; 4034 bool should_infer_name = function_name == NULL;
4034 4035
4035 // We want a non-null handle as the function name. 4036 // We want a non-null handle as the function name.
4036 if (should_infer_name) { 4037 if (should_infer_name) {
4037 function_name = ast_value_factory()->empty_string(); 4038 function_name = ast_value_factory()->empty_string();
4038 } 4039 }
(...skipping 11 matching lines...) Expand all
4050 bool should_be_used_once_hint = false; 4051 bool should_be_used_once_hint = false;
4051 bool has_duplicate_parameters; 4052 bool has_duplicate_parameters;
4052 // Parse function. 4053 // Parse function.
4053 { 4054 {
4054 AstNodeFactory function_factory(ast_value_factory()); 4055 AstNodeFactory function_factory(ast_value_factory());
4055 FunctionState function_state(&function_state_, &scope_, scope, kind, 4056 FunctionState function_state(&function_state_, &scope_, scope, kind,
4056 &function_factory); 4057 &function_factory);
4057 scope_->SetScopeName(function_name); 4058 scope_->SetScopeName(function_name);
4058 ExpressionClassifier formals_classifier(this, &duplicate_finder); 4059 ExpressionClassifier formals_classifier(this, &duplicate_finder);
4059 4060
4060 if (is_generator) { 4061 if (is_generator || is_async) {
4061 // For generators, allocating variables in contexts is currently a win 4062 // For generators, allocating variables in contexts is currently a win
4062 // because it minimizes the work needed to suspend and resume an 4063 // because it minimizes the work needed to suspend and resume an
4063 // activation. The machine code produced for generators (by full-codegen) 4064 // activation. The machine code produced for generators (by full-codegen)
4064 // relies on this forced context allocation, but not in an essential way. 4065 // relies on this forced context allocation, but not in an essential way.
4065 scope_->ForceContextAllocation(); 4066 scope_->ForceContextAllocation();
4066 4067
4067 // Calling a generator returns a generator object. That object is stored 4068 // Calling a generator returns a generator object. That object is stored
4068 // in a temporary variable, a definition that is used by "yield" 4069 // in a temporary variable, a definition that is used by "yield"
4069 // expressions. This also marks the FunctionState as a generator. 4070 // expressions. This also marks the FunctionState as a generator.
4070 Variable* temp = scope_->NewTemporary( 4071 Variable* temp = scope_->NewTemporary(
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
4534 // 4535 //
4535 // - InitialYield yields the actual generator object. 4536 // - InitialYield yields the actual generator object.
4536 // - Any return statement inside the body will have its argument wrapped 4537 // - Any return statement inside the body will have its argument wrapped
4537 // in a "done" iterator result object. 4538 // in a "done" iterator result object.
4538 // - If the generator terminates for whatever reason, we must close it. 4539 // - If the generator terminates for whatever reason, we must close it.
4539 // Hence the finally clause. 4540 // Hence the finally clause.
4540 4541
4541 Block* try_block = 4542 Block* try_block =
4542 factory()->NewBlock(nullptr, 3, false, RelocInfo::kNoPosition); 4543 factory()->NewBlock(nullptr, 3, false, RelocInfo::kNoPosition);
4543 4544
4544 { 4545 ZoneList<Expression*>* arguments =
caitp (gmail) 2016/04/16 18:33:03 Bunch of phantom edits to clean up, since the asyn
4545 ZoneList<Expression*>* arguments = 4546 new (zone()) ZoneList<Expression*>(2, zone());
4546 new (zone()) ZoneList<Expression*>(2, zone()); 4547 arguments->Add(factory()->NewThisFunction(pos), zone());
4547 arguments->Add(factory()->NewThisFunction(pos), zone()); 4548 arguments->Add(ThisExpression(scope_, factory(), RelocInfo::kNoPosition),
4548 arguments->Add( 4549 zone());
4549 ThisExpression(scope_, factory(), RelocInfo::kNoPosition), zone()); 4550 CallRuntime* allocation = factory()->NewCallRuntime(
4550 CallRuntime* allocation = factory()->NewCallRuntime( 4551 Runtime::kCreateJSGeneratorObject, arguments, pos);
4551 Runtime::kCreateJSGeneratorObject, arguments, pos);
4552 4552
4553 VariableProxy* init_proxy = factory()->NewVariableProxy( 4553 VariableProxy* init_proxy = factory()->NewVariableProxy(
4554 function_state_->generator_object_variable()); 4554 function_state_->generator_object_variable());
4555 Assignment* assignment = factory()->NewAssignment( 4555 Assignment* assignment = factory()->NewAssignment(
4556 Token::INIT, init_proxy, allocation, RelocInfo::kNoPosition); 4556 Token::INIT, init_proxy, allocation, RelocInfo::kNoPosition);
4557 VariableProxy* get_proxy = factory()->NewVariableProxy( 4557
4558 function_state_->generator_object_variable()); 4558 VariableProxy* get_proxy = factory()->NewVariableProxy(
4559 Yield* yield = 4559 function_state_->generator_object_variable());
4560 factory()->NewYield(get_proxy, assignment, RelocInfo::kNoPosition); 4560
4561 try_block->statements()->Add( 4561 Yield* yield =
4562 factory()->NewExpressionStatement(yield, RelocInfo::kNoPosition), 4562 factory()->NewYield(get_proxy, assignment, RelocInfo::kNoPosition);
4563 zone()); 4563
4564 } 4564 try_block->statements()->Add(
4565 factory()->NewExpressionStatement(yield, RelocInfo::kNoPosition),
4566 zone());
4565 4567
4566 ParseStatementList(try_block->statements(), Token::RBRACE, CHECK_OK); 4568 ParseStatementList(try_block->statements(), Token::RBRACE, CHECK_OK);
4567 4569
4568 Statement* final_return = factory()->NewReturnStatement( 4570 Statement* final_return = factory()->NewReturnStatement(
4569 BuildIteratorResult(nullptr, true), RelocInfo::kNoPosition); 4571 BuildIteratorResult(nullptr, true), RelocInfo::kNoPosition);
4570 try_block->statements()->Add(final_return, zone()); 4572 try_block->statements()->Add(final_return, zone());
4571 4573
4572 Block* finally_block = 4574 Block* finally_block =
4573 factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition); 4575 factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition);
4574 ZoneList<Expression*>* args = 4576 ZoneList<Expression*>* args =
4575 new (zone()) ZoneList<Expression*>(1, zone()); 4577 new (zone()) ZoneList<Expression*>(1, zone());
4576 VariableProxy* call_proxy = factory()->NewVariableProxy( 4578 VariableProxy* call_proxy = factory()->NewVariableProxy(
4577 function_state_->generator_object_variable()); 4579 function_state_->generator_object_variable());
4578 args->Add(call_proxy, zone()); 4580 args->Add(call_proxy, zone());
4579 Expression* call = factory()->NewCallRuntime( 4581 Expression* call = factory()->NewCallRuntime(
4580 Runtime::kGeneratorClose, args, RelocInfo::kNoPosition); 4582 Runtime::kGeneratorClose, args, RelocInfo::kNoPosition);
4583
4581 finally_block->statements()->Add( 4584 finally_block->statements()->Add(
4582 factory()->NewExpressionStatement(call, RelocInfo::kNoPosition), 4585 factory()->NewExpressionStatement(call, RelocInfo::kNoPosition),
4583 zone()); 4586 zone());
4584 4587
4585 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, 4588 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block,
4586 RelocInfo::kNoPosition), 4589 RelocInfo::kNoPosition),
4587 zone()); 4590 zone());
4591 } else if (IsAsyncFunction(kind)) {
4592 ZoneList<Expression*>* arguments =
4593 new (zone()) ZoneList<Expression*>(2, zone());
4594 arguments->Add(factory()->NewThisFunction(pos), zone());
4595 arguments->Add(ThisExpression(scope_, factory(), RelocInfo::kNoPosition),
4596 zone());
4597 CallRuntime* allocation = factory()->NewCallRuntime(
4598 Runtime::kCreateJSGeneratorObject, arguments, pos);
4599
4600 VariableProxy* init_proxy = factory()->NewVariableProxy(
4601 function_state_->generator_object_variable());
4602 Assignment* assignment = factory()->NewAssignment(
4603 Token::INIT, init_proxy, allocation, RelocInfo::kNoPosition);
4604
4605 VariableProxy* get_proxy = factory()->NewVariableProxy(
4606 function_state_->generator_object_variable());
4607
4608 ZoneList<Expression*>* async_function_start_args =
4609 new (zone()) ZoneList<Expression*>(1, zone());
4610 async_function_start_args->Add(get_proxy, zone());
4611 CallRuntime* async_function_start = factory()->NewCallRuntime(
4612 Context::ASYNC_FUNCTION_START_INDEX, async_function_start_args,
4613 RelocInfo::kNoPosition);
4614 Yield* yield = factory()->NewAsyncFunctionStart(
4615 assignment, async_function_start, RelocInfo::kNoPosition);
4616
4617 body->Add(
4618 factory()->NewExpressionStatement(yield, RelocInfo::kNoPosition),
4619 zone());
4620
4621 ParseStatementList(body, Token::RBRACE, CHECK_OK);
4622
4623 Statement* final_return = factory()->NewReturnStatement(
4624 BuildIteratorResult(nullptr, true), RelocInfo::kNoPosition);
4625 body->Add(final_return, zone());
4588 } else { 4626 } else {
4589 ParseStatementList(body, Token::RBRACE, CHECK_OK); 4627 ParseStatementList(body, Token::RBRACE, CHECK_OK);
4590 } 4628 }
4591 4629
4592 if (IsSubclassConstructor(kind)) { 4630 if (IsSubclassConstructor(kind)) {
4593 body->Add( 4631 body->Add(
4594 factory()->NewReturnStatement( 4632 factory()->NewReturnStatement(
4595 this->ThisExpression(scope_, factory(), RelocInfo::kNoPosition), 4633 this->ThisExpression(scope_, factory(), RelocInfo::kNoPosition),
4596 RelocInfo::kNoPosition), 4634 RelocInfo::kNoPosition),
4597 zone()); 4635 zone());
4598 } 4636 }
4599 } 4637 }
4600 4638
4601 Expect(Token::RBRACE, CHECK_OK); 4639 Expect(Token::RBRACE, CHECK_OK);
4602 scope_->set_end_position(scanner()->location().end_pos); 4640 scope_->set_end_position(scanner()->location().end_pos);
4603 4641
4604 if (!parameters.is_simple) { 4642 if (!parameters.is_simple) {
4605 DCHECK_NOT_NULL(inner_scope); 4643 DCHECK_NOT_NULL(inner_scope);
4606 DCHECK_EQ(body, inner_block->statements()); 4644 DCHECK_EQ(body, inner_block->statements());
4607 SetLanguageMode(scope_, inner_scope->language_mode()); 4645 SetLanguageMode(scope_, inner_scope->language_mode());
4608 Block* init_block = BuildParameterInitializationBlock(parameters, CHECK_OK); 4646 Block* init_block = BuildParameterInitializationBlock(parameters, CHECK_OK);
4647
4648 if (IsAsyncFunction(kind)) {
4649 // If an exception occurs during parameter initialization, return a
4650 // rejected promise.
4651 //
4652 // try { <init_block> } catch (error) { return Promise.reject(error); }
4653 Block* try_block = init_block;
4654 Scope* catch_scope = NewScope(scope_, CATCH_SCOPE);
4655 Variable* catch_variable =
4656 catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(),
4657 VAR, kCreatedInitialized, Variable::NORMAL);
4658 Block* catch_block =
4659 factory()->NewBlock(nullptr, 1, true, RelocInfo::kNoPosition);
4660
4661 ZoneList<Expression*>* promise_reject_arguments =
4662 new (zone()) ZoneList<Expression*>(1, zone());
4663 promise_reject_arguments->Add(factory()->NewVariableProxy(catch_variable),
4664 zone());
4665 CallRuntime* promise_reject = factory()->NewCallRuntime(
4666 Context::PROMISE_CREATE_REJECTED_INDEX, promise_reject_arguments,
4667 RelocInfo::kNoPosition);
4668 ReturnStatement* return_promise_reject =
4669 factory()->NewReturnStatement(promise_reject, RelocInfo::kNoPosition);
4670 catch_block->statements()->Add(return_promise_reject, zone());
4671 TryStatement* try_catch_statement = factory()->NewTryCatchStatement(
4672 try_block, catch_scope, catch_variable, catch_block,
4673 RelocInfo::kNoPosition);
4674
4675 init_block =
4676 factory()->NewBlock(nullptr, 1, true, RelocInfo::kNoPosition);
4677 init_block->statements()->Add(try_catch_statement, zone());
4678 }
4679
4609 DCHECK_NOT_NULL(init_block); 4680 DCHECK_NOT_NULL(init_block);
4610 4681
4611 inner_scope->set_end_position(scanner()->location().end_pos); 4682 inner_scope->set_end_position(scanner()->location().end_pos);
4612 inner_scope = inner_scope->FinalizeBlockScope(); 4683 inner_scope = inner_scope->FinalizeBlockScope();
4613 if (inner_scope != nullptr) { 4684 if (inner_scope != nullptr) {
4614 CheckConflictingVarDeclarations(inner_scope, CHECK_OK); 4685 CheckConflictingVarDeclarations(inner_scope, CHECK_OK);
4615 InsertShadowingVarBindingInitializers(inner_block); 4686 InsertShadowingVarBindingInitializers(inner_block);
4616 } 4687 }
4617 4688
4618 result->Add(init_block, zone()); 4689 result->Add(init_block, zone());
(...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after
5389 } 5460 }
5390 5461
5391 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier, 5462 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier,
5392 bool* ok) { 5463 bool* ok) {
5393 parser_->RewriteNonPattern(classifier, ok); 5464 parser_->RewriteNonPattern(classifier, ok);
5394 } 5465 }
5395 5466
5396 Expression* ParserTraits::RewriteAwaitExpression(Expression* value, int pos) { 5467 Expression* ParserTraits::RewriteAwaitExpression(Expression* value, int pos) {
5397 // TODO(caitp): Implement AsyncFunctionAwait() 5468 // TODO(caitp): Implement AsyncFunctionAwait()
5398 // per tc39.github.io/ecmascript-asyncawait/#abstract-ops-async-function-await 5469 // per tc39.github.io/ecmascript-asyncawait/#abstract-ops-async-function-await
5399 return value; 5470 Expression* generator_object = parser_->factory()->NewVariableProxy(
5471 parser_->function_state_->generator_object_variable());
5472 value = BuildIteratorResult(value, false);
5473 return parser_->factory()->NewYield(generator_object, value, pos);
5400 } 5474 }
5401 5475
5402 Zone* ParserTraits::zone() const { 5476 Zone* ParserTraits::zone() const {
5403 return parser_->function_state_->scope()->zone(); 5477 return parser_->function_state_->scope()->zone();
5404 } 5478 }
5405 5479
5406 5480
5407 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const { 5481 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const {
5408 return parser_->function_state_->non_patterns_to_rewrite(); 5482 return parser_->function_state_->non_patterns_to_rewrite();
5409 } 5483 }
(...skipping 1470 matching lines...) Expand 10 before | Expand all | Expand 10 after
6880 try_block, target); 6954 try_block, target);
6881 final_loop = target; 6955 final_loop = target;
6882 } 6956 }
6883 6957
6884 return final_loop; 6958 return final_loop;
6885 } 6959 }
6886 6960
6887 6961
6888 } // namespace internal 6962 } // namespace internal
6889 } // namespace v8 6963 } // namespace v8
OLDNEW
« src/js/promise.js ('K') | « src/objects-inl.h ('k') | src/parsing/parser-base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698