OLD | NEW |
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 Loading... |
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 Loading... |
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 Loading... |
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 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4578 args->Add(call_proxy, zone()); | 4579 args->Add(call_proxy, zone()); |
4579 Expression* call = factory()->NewCallRuntime( | 4580 Expression* call = factory()->NewCallRuntime( |
4580 Runtime::kGeneratorClose, args, RelocInfo::kNoPosition); | 4581 Runtime::kGeneratorClose, args, RelocInfo::kNoPosition); |
4581 finally_block->statements()->Add( | 4582 finally_block->statements()->Add( |
4582 factory()->NewExpressionStatement(call, RelocInfo::kNoPosition), | 4583 factory()->NewExpressionStatement(call, RelocInfo::kNoPosition), |
4583 zone()); | 4584 zone()); |
4584 | 4585 |
4585 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, | 4586 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, |
4586 RelocInfo::kNoPosition), | 4587 RelocInfo::kNoPosition), |
4587 zone()); | 4588 zone()); |
| 4589 } else if (IsAsyncFunction(kind)) { |
| 4590 ZoneList<Expression*>* arguments = |
| 4591 new (zone()) ZoneList<Expression*>(2, zone()); |
| 4592 arguments->Add(factory()->NewThisFunction(pos), zone()); |
| 4593 arguments->Add(ThisExpression(scope_, factory(), RelocInfo::kNoPosition), |
| 4594 zone()); |
| 4595 CallRuntime* allocation = factory()->NewCallRuntime( |
| 4596 Runtime::kCreateJSGeneratorObject, arguments, pos); |
| 4597 |
| 4598 VariableProxy* init_proxy = factory()->NewVariableProxy( |
| 4599 function_state_->generator_object_variable()); |
| 4600 Assignment* assignment = factory()->NewAssignment( |
| 4601 Token::INIT, init_proxy, allocation, RelocInfo::kNoPosition); |
| 4602 |
| 4603 VariableProxy* get_proxy = factory()->NewVariableProxy( |
| 4604 function_state_->generator_object_variable()); |
| 4605 |
| 4606 ZoneList<Expression*>* async_function_start_args = |
| 4607 new (zone()) ZoneList<Expression*>(1, zone()); |
| 4608 async_function_start_args->Add(get_proxy, zone()); |
| 4609 CallRuntime* async_function_start = factory()->NewCallRuntime( |
| 4610 Context::ASYNC_FUNCTION_START_INDEX, async_function_start_args, |
| 4611 RelocInfo::kNoPosition); |
| 4612 Yield* yield = factory()->NewAsyncFunctionStart( |
| 4613 assignment, async_function_start, RelocInfo::kNoPosition); |
| 4614 |
| 4615 body->Add( |
| 4616 factory()->NewExpressionStatement(yield, RelocInfo::kNoPosition), |
| 4617 zone()); |
| 4618 |
| 4619 ParseStatementList(body, Token::RBRACE, CHECK_OK); |
| 4620 |
| 4621 Statement* final_return = factory()->NewReturnStatement( |
| 4622 BuildIteratorResult(nullptr, true), RelocInfo::kNoPosition); |
| 4623 body->Add(final_return, zone()); |
4588 } else { | 4624 } else { |
4589 ParseStatementList(body, Token::RBRACE, CHECK_OK); | 4625 ParseStatementList(body, Token::RBRACE, CHECK_OK); |
4590 } | 4626 } |
4591 | 4627 |
4592 if (IsSubclassConstructor(kind)) { | 4628 if (IsSubclassConstructor(kind)) { |
4593 body->Add( | 4629 body->Add( |
4594 factory()->NewReturnStatement( | 4630 factory()->NewReturnStatement( |
4595 this->ThisExpression(scope_, factory(), RelocInfo::kNoPosition), | 4631 this->ThisExpression(scope_, factory(), RelocInfo::kNoPosition), |
4596 RelocInfo::kNoPosition), | 4632 RelocInfo::kNoPosition), |
4597 zone()); | 4633 zone()); |
4598 } | 4634 } |
4599 } | 4635 } |
4600 | 4636 |
4601 Expect(Token::RBRACE, CHECK_OK); | 4637 Expect(Token::RBRACE, CHECK_OK); |
4602 scope_->set_end_position(scanner()->location().end_pos); | 4638 scope_->set_end_position(scanner()->location().end_pos); |
4603 | 4639 |
4604 if (!parameters.is_simple) { | 4640 if (!parameters.is_simple) { |
4605 DCHECK_NOT_NULL(inner_scope); | 4641 DCHECK_NOT_NULL(inner_scope); |
4606 DCHECK_EQ(body, inner_block->statements()); | 4642 DCHECK_EQ(body, inner_block->statements()); |
4607 SetLanguageMode(scope_, inner_scope->language_mode()); | 4643 SetLanguageMode(scope_, inner_scope->language_mode()); |
4608 Block* init_block = BuildParameterInitializationBlock(parameters, CHECK_OK); | 4644 Block* init_block = BuildParameterInitializationBlock(parameters, CHECK_OK); |
| 4645 |
| 4646 if (IsAsyncFunction(kind)) { |
| 4647 // If an exception occurs during parameter initialization, return a |
| 4648 // rejected promise. |
| 4649 // |
| 4650 // try { <init_block> } catch (error) { return Promise.reject(error); } |
| 4651 Block* try_block = init_block; |
| 4652 Scope* catch_scope = NewScope(scope_, CATCH_SCOPE); |
| 4653 Variable* catch_variable = |
| 4654 catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(), |
| 4655 VAR, kCreatedInitialized, Variable::NORMAL); |
| 4656 Block* catch_block = |
| 4657 factory()->NewBlock(nullptr, 1, true, RelocInfo::kNoPosition); |
| 4658 |
| 4659 ZoneList<Expression*>* promise_reject_arguments = |
| 4660 new (zone()) ZoneList<Expression*>(1, zone()); |
| 4661 promise_reject_arguments->Add(factory()->NewVariableProxy(catch_variable), |
| 4662 zone()); |
| 4663 CallRuntime* promise_reject = factory()->NewCallRuntime( |
| 4664 Context::PROMISE_CREATE_REJECTED_INDEX, promise_reject_arguments, |
| 4665 RelocInfo::kNoPosition); |
| 4666 ReturnStatement* return_promise_reject = |
| 4667 factory()->NewReturnStatement(promise_reject, RelocInfo::kNoPosition); |
| 4668 catch_block->statements()->Add(return_promise_reject, zone()); |
| 4669 TryStatement* try_catch_statement = factory()->NewTryCatchStatement( |
| 4670 try_block, catch_scope, catch_variable, catch_block, |
| 4671 RelocInfo::kNoPosition); |
| 4672 |
| 4673 init_block = |
| 4674 factory()->NewBlock(nullptr, 1, true, RelocInfo::kNoPosition); |
| 4675 init_block->statements()->Add(try_catch_statement, zone()); |
| 4676 } |
| 4677 |
4609 DCHECK_NOT_NULL(init_block); | 4678 DCHECK_NOT_NULL(init_block); |
4610 | 4679 |
4611 inner_scope->set_end_position(scanner()->location().end_pos); | 4680 inner_scope->set_end_position(scanner()->location().end_pos); |
4612 inner_scope = inner_scope->FinalizeBlockScope(); | 4681 inner_scope = inner_scope->FinalizeBlockScope(); |
4613 if (inner_scope != nullptr) { | 4682 if (inner_scope != nullptr) { |
4614 CheckConflictingVarDeclarations(inner_scope, CHECK_OK); | 4683 CheckConflictingVarDeclarations(inner_scope, CHECK_OK); |
4615 InsertShadowingVarBindingInitializers(inner_block); | 4684 InsertShadowingVarBindingInitializers(inner_block); |
4616 } | 4685 } |
4617 | 4686 |
4618 result->Add(init_block, zone()); | 4687 result->Add(init_block, zone()); |
(...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5389 } | 5458 } |
5390 | 5459 |
5391 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier, | 5460 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier, |
5392 bool* ok) { | 5461 bool* ok) { |
5393 parser_->RewriteNonPattern(classifier, ok); | 5462 parser_->RewriteNonPattern(classifier, ok); |
5394 } | 5463 } |
5395 | 5464 |
5396 Expression* ParserTraits::RewriteAwaitExpression(Expression* value, int pos) { | 5465 Expression* ParserTraits::RewriteAwaitExpression(Expression* value, int pos) { |
5397 // TODO(caitp): Implement AsyncFunctionAwait() | 5466 // TODO(caitp): Implement AsyncFunctionAwait() |
5398 // per tc39.github.io/ecmascript-asyncawait/#abstract-ops-async-function-await | 5467 // per tc39.github.io/ecmascript-asyncawait/#abstract-ops-async-function-await |
5399 return value; | 5468 Expression* generator_object = parser_->factory()->NewVariableProxy( |
| 5469 parser_->function_state_->generator_object_variable()); |
| 5470 value = BuildIteratorResult(value, false); |
| 5471 return parser_->factory()->NewYield(generator_object, value, pos); |
5400 } | 5472 } |
5401 | 5473 |
5402 Zone* ParserTraits::zone() const { | 5474 Zone* ParserTraits::zone() const { |
5403 return parser_->function_state_->scope()->zone(); | 5475 return parser_->function_state_->scope()->zone(); |
5404 } | 5476 } |
5405 | 5477 |
5406 | 5478 |
5407 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const { | 5479 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const { |
5408 return parser_->function_state_->non_patterns_to_rewrite(); | 5480 return parser_->function_state_->non_patterns_to_rewrite(); |
5409 } | 5481 } |
(...skipping 1470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6880 try_block, target); | 6952 try_block, target); |
6881 final_loop = target; | 6953 final_loop = target; |
6882 } | 6954 } |
6883 | 6955 |
6884 return final_loop; | 6956 return final_loop; |
6885 } | 6957 } |
6886 | 6958 |
6887 | 6959 |
6888 } // namespace internal | 6960 } // namespace internal |
6889 } // namespace v8 | 6961 } // namespace v8 |
OLD | NEW |