diff --git a/src/parsing/parser-base.h b/src/parsing/parser-base.h index 61ecbf15c9..ea550de3c6 100644 --- a/src/parsing/parser-base.h +++ b/src/parsing/parser-base.h @@ -337,8 +337,6 @@ class ParserBase { kForStatement }; - enum class FunctionBodyType { kNormal, kSingleExpression }; - class ClassLiteralChecker; class ObjectLiteralChecker; @@ -1116,9 +1114,9 @@ class ParserBase { ExpressionT ParseArrowFunctionLiteral(bool accept_IN, const FormalParametersT& parameters, int rewritable_length, bool* ok); - void ParseAsyncFunctionBody(Scope* scope, StatementListT body, - FunctionKind kind, FunctionBodyType type, - bool accept_IN, int pos, bool* ok); + void ParseSingleExpressionFunctionBody(StatementListT body, bool is_async, + bool accept_IN, bool* ok); + void ParseAsyncFunctionBody(Scope* scope, StatementListT body, bool* ok); ExpressionT ParseAsyncFunctionLiteral(bool* ok); ExpressionT ParseClassLiteral(IdentifierT name, Scanner::Location class_name_location, @@ -4053,14 +4051,14 @@ void ParserBase::ParseFunctionBody( { BlockState block_state(&scope_, inner_scope); + if (IsResumableFunction(kind)) impl()->PrepareGeneratorVariables(); + if (IsAsyncGeneratorFunction(kind)) { impl()->ParseAndRewriteAsyncGeneratorFunctionBody(pos, kind, body, ok); } else if (IsGeneratorFunction(kind)) { impl()->ParseAndRewriteGeneratorFunctionBody(pos, kind, body, ok); } else if (IsAsyncFunction(kind)) { - const bool accept_IN = true; - ParseAsyncFunctionBody(inner_scope, body, kind, FunctionBodyType::kNormal, - accept_IN, pos, CHECK_OK_VOID); + ParseAsyncFunctionBody(inner_scope, body, CHECK_OK_VOID); } else { ParseStatementList(body, Token::RBRACE, CHECK_OK_VOID); } @@ -4264,31 +4262,19 @@ ParserBase::ParseArrowFunctionLiteral( } else { Consume(Token::LBRACE); body = impl()->NewStatementList(8); - impl()->ParseFunctionBody(body, impl()->EmptyIdentifier(), - kNoSourcePosition, formal_parameters, kind, - FunctionLiteral::kAnonymousExpression, - CHECK_OK); + ParseFunctionBody(body, impl()->EmptyIdentifier(), kNoSourcePosition, + formal_parameters, kind, + FunctionLiteral::kAnonymousExpression, CHECK_OK); expected_property_count = function_state.expected_property_count(); } } else { // Single-expression body has_braces = false; - int pos = position(); + const bool is_async = IsAsyncFunction(kind); body = impl()->NewStatementList(1); - impl()->AddParameterInitializationBlock( - formal_parameters, body, kind == kAsyncArrowFunction, CHECK_OK); - ExpressionClassifier classifier(this); - if (kind == kAsyncArrowFunction) { - ParseAsyncFunctionBody(scope(), body, kAsyncArrowFunction, - FunctionBodyType::kSingleExpression, accept_IN, - pos, CHECK_OK); - impl()->RewriteNonPattern(CHECK_OK); - } else { - ExpressionT expression = ParseAssignmentExpression(accept_IN, CHECK_OK); - impl()->RewriteNonPattern(CHECK_OK); - body->Add(BuildReturnStatement(expression, expression->position()), - zone()); - } + impl()->AddParameterInitializationBlock(formal_parameters, body, is_async, + CHECK_OK); + ParseSingleExpressionFunctionBody(body, is_async, accept_IN, CHECK_OK); expected_property_count = function_state.expected_property_count(); } @@ -4425,27 +4411,33 @@ typename ParserBase::ExpressionT ParserBase::ParseClassLiteral( } template -void ParserBase::ParseAsyncFunctionBody(Scope* scope, StatementListT body, - FunctionKind kind, - FunctionBodyType body_type, - bool accept_IN, int pos, - bool* ok) { - impl()->PrepareAsyncFunctionBody(body, kind, pos); +void ParserBase::ParseSingleExpressionFunctionBody(StatementListT body, + bool is_async, + bool accept_IN, + bool* ok) { + if (is_async) impl()->PrepareGeneratorVariables(); + ExpressionClassifier classifier(this); + ExpressionT expression = ParseAssignmentExpression(accept_IN, CHECK_OK_VOID); + impl()->RewriteNonPattern(CHECK_OK_VOID); + + if (is_async) { + BlockT block = factory()->NewBlock(nullptr, 1, true, kNoSourcePosition); + impl()->RewriteAsyncFunctionBody(body, block, expression, CHECK_OK_VOID); + } else { + body->Add(BuildReturnStatement(expression, expression->position()), zone()); + } +} + +template +void ParserBase::ParseAsyncFunctionBody(Scope* scope, StatementListT body, + bool* ok) { BlockT block = factory()->NewBlock(nullptr, 8, true, kNoSourcePosition); - ExpressionT return_value = impl()->EmptyExpression(); - if (body_type == FunctionBodyType::kNormal) { - ParseStatementList(block->statements(), Token::RBRACE, - CHECK_OK_CUSTOM(Void)); - return_value = factory()->NewUndefinedLiteral(kNoSourcePosition); - } else { - return_value = ParseAssignmentExpression(accept_IN, CHECK_OK_CUSTOM(Void)); - impl()->RewriteNonPattern(CHECK_OK_CUSTOM(Void)); - } - - impl()->RewriteAsyncFunctionBody(body, block, return_value, - CHECK_OK_CUSTOM(Void)); + ParseStatementList(block->statements(), Token::RBRACE, CHECK_OK_VOID); + impl()->RewriteAsyncFunctionBody( + body, block, factory()->NewUndefinedLiteral(kNoSourcePosition), + CHECK_OK_VOID); scope->set_end_position(scanner()->location().end_pos); } diff --git a/src/parsing/parser.cc b/src/parsing/parser.cc index 67a57cea19..30180a5989 100644 --- a/src/parsing/parser.cc +++ b/src/parsing/parser.cc @@ -3194,8 +3194,6 @@ ZoneList* Parser::ParseFunction( DuplicateFinder duplicate_finder; ExpressionClassifier formals_classifier(this, &duplicate_finder); - if (IsResumableFunction(kind)) PrepareGeneratorVariables(); - int expected_parameters_end_pos = parameters_end_pos_; if (expected_parameters_end_pos != kNoSourcePosition) { // This is the first function encountered in a CreateDynamicFunction eval. @@ -3820,17 +3818,6 @@ Expression* Parser::ExpressionListToExpression(ZoneList* args) { return expr; } -// This method intoduces the line initializing the generator object -// when desugaring the body of async_function. -void Parser::PrepareAsyncFunctionBody(ZoneList* body, - FunctionKind kind, int pos) { - // When parsing an async arrow function, we get here without having called - // PrepareGeneratorVariables yet, so do it now. - if (function_state_->scope()->generator_object_var() == nullptr) { - PrepareGeneratorVariables(); - } -} - // This method completes the desugaring of the body of async_function. void Parser::RewriteAsyncFunctionBody(ZoneList* body, Block* block, Expression* return_value, bool* ok) { diff --git a/src/parsing/parser.h b/src/parsing/parser.h index 7a5ae5854d..d3bd979680 100644 --- a/src/parsing/parser.h +++ b/src/parsing/parser.h @@ -685,8 +685,6 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase) { IteratorType type); Statement* CheckCallable(Variable* var, Expression* error, int pos); - V8_INLINE void PrepareAsyncFunctionBody(ZoneList* body, - FunctionKind kind, int pos); V8_INLINE void RewriteAsyncFunctionBody(ZoneList* body, Block* block, Expression* return_value, bool* ok); diff --git a/src/parsing/preparser.h b/src/parsing/preparser.h index 30899824e2..fa163daf05 100644 --- a/src/parsing/preparser.h +++ b/src/parsing/preparser.h @@ -1026,8 +1026,7 @@ class PreParser : public ParserBase { return left; } - V8_INLINE void PrepareAsyncFunctionBody(PreParserStatementList body, - FunctionKind kind, int pos) {} + V8_INLINE void PrepareGeneratorVariables() {} V8_INLINE void RewriteAsyncFunctionBody(PreParserStatementList body, PreParserStatement block, PreParserExpression return_value,