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

Unified Diff: src/parsing/parser.cc

Issue 1841543003: [esnext] implement frontend changes for async/await proposal (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: A bunch more tests, some fixes, ExpressionClassifier gets fatter :( Created 4 years, 7 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: src/parsing/parser.cc
diff --git a/src/parsing/parser.cc b/src/parsing/parser.cc
index ce3dd20d359310a1b89eda3d05c88551127537d8..f804a84368bd69da3548103d4bea113c108fd455 100644
--- a/src/parsing/parser.cc
+++ b/src/parsing/parser.cc
@@ -330,6 +330,10 @@ bool ParserTraits::IsUndefined(const AstRawString* identifier) const {
return identifier == parser_->ast_value_factory()->undefined_string();
}
+bool ParserTraits::IsAwait(const AstRawString* identifier) const {
+ return identifier == parser_->ast_value_factory()->await_string();
+}
+
bool ParserTraits::IsPrototype(const AstRawString* identifier) const {
return identifier == parser_->ast_value_factory()->prototype_string();
}
@@ -798,6 +802,7 @@ Parser::Parser(ParseInfo* info)
FLAG_harmony_restrictive_declarations);
set_allow_harmony_exponentiation_operator(
FLAG_harmony_exponentiation_operator);
+ set_allow_harmony_async_await(FLAG_harmony_async_await);
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
++feature) {
use_counts_[feature] = 0;
@@ -1073,6 +1078,16 @@ FunctionLiteral* Parser::ParseLazy(Isolate* isolate, ParseInfo* info,
bool ok = true;
if (shared_info->is_arrow()) {
+ bool is_async = allow_harmony_async_await() &&
+ peek() == Token::IDENTIFIER &&
+ PeekContextualKeyword(CStrVector("async")) &&
+ !scanner()->HasAnyLineTerminatorAfterNext();
+
+ if (is_async) {
+ Consume(Token::IDENTIFIER);
+ DCHECK(peek_any_identifier() || peek() == Token::LPAREN);
+ }
+
// TODO(adamk): We should construct this scope from the ScopeInfo.
Scope* scope =
NewScope(scope_, FUNCTION_SCOPE, FunctionKind::kArrowFunction);
@@ -1113,8 +1128,8 @@ FunctionLiteral* Parser::ParseLazy(Isolate* isolate, ParseInfo* info,
checkpoint.Restore(&formals.materialized_literals_count);
// Pass `accept_IN=true` to ParseArrowFunctionLiteral --- This should
// not be observable, or else the preparser would have failed.
- Expression* expression =
- ParseArrowFunctionLiteral(true, formals, formals_classifier, &ok);
+ Expression* expression = ParseArrowFunctionLiteral(
+ true, formals, is_async, formals_classifier, &ok);
if (ok) {
// Scanning must end at the same position that was recorded
// previously. If not, parsing has been interrupted due to a stack
@@ -1253,8 +1268,8 @@ Statement* Parser::ParseStatementListItem(bool* ok) {
// StatementListItem:
// Statement
// Declaration
-
- switch (peek()) {
+ const Token::Value peeked = peek();
+ switch (peeked) {
case Token::FUNCTION:
return ParseHoistableDeclaration(NULL, ok);
case Token::CLASS:
@@ -1269,6 +1284,15 @@ Statement* Parser::ParseStatementListItem(bool* ok) {
return ParseVariableStatement(kStatementListItem, NULL, ok);
}
break;
+ case Token::IDENTIFIER:
+ if (allow_harmony_async_await() &&
+ PeekContextualKeyword(CStrVector("async")) &&
+ PeekAhead() == Token::FUNCTION &&
+ !scanner()->HasAnyLineTerminatorAfterNext()) {
+ Consume(Token::IDENTIFIER);
+ return ParseAsyncFunctionDeclaration(NULL, ok);
+ }
+ /* falls through */
default:
break;
}
@@ -1558,7 +1582,10 @@ Statement* Parser::ParseExportDefault(bool* ok) {
pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK);
result = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
} else {
- result = ParseHoistableDeclaration(pos, is_generator, &names, CHECK_OK);
+ result = ParseHoistableDeclaration(
+ pos, is_generator ? ParseFunctionFlags::kIsGenerator
+ : ParseFunctionFlags::kIsNormal,
+ &names, CHECK_OK);
}
break;
}
@@ -2060,13 +2087,25 @@ Statement* Parser::ParseHoistableDeclaration(
ZoneList<const AstRawString*>* names, bool* ok) {
Expect(Token::FUNCTION, CHECK_OK);
int pos = position();
- bool is_generator = Check(Token::MUL);
- return ParseHoistableDeclaration(pos, is_generator, names, ok);
+ ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal;
+ if (Check(Token::MUL)) {
+ flags |= ParseFunctionFlags::kIsGenerator;
+ }
+ return ParseHoistableDeclaration(pos, flags, names, ok);
}
+Statement* Parser::ParseAsyncFunctionDeclaration(
+ ZoneList<const AstRawString*>* names, bool* ok) {
+ DCHECK_EQ(scanner()->current_token(), Token::IDENTIFIER);
+ DCHECK(scanner()->is_literal_contextual_keyword(CStrVector("async")));
+ int pos = position();
+ Expect(Token::FUNCTION, CHECK_OK);
+ ParseFunctionFlags flags = ParseFunctionFlags::kIsAsync;
+ return ParseHoistableDeclaration(pos, flags, names, ok);
+}
Statement* Parser::ParseHoistableDeclaration(
- int pos, bool is_generator, ZoneList<const AstRawString*>* names,
+ int pos, ParseFunctionFlags flags, ZoneList<const AstRawString*>* names,
bool* ok) {
// FunctionDeclaration ::
// 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
@@ -2074,10 +2113,21 @@ Statement* Parser::ParseHoistableDeclaration(
// 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
//
// 'function' and '*' (if present) have been consumed by the caller.
+ const bool is_generator = flags & ParseFunctionFlags::kIsGenerator;
+ const bool is_async = flags & ParseFunctionFlags::kIsAsync;
+ DCHECK(!is_generator || !is_async);
+
bool is_strict_reserved = false;
const AstRawString* name = ParseIdentifierOrStrictReservedWord(
&is_strict_reserved, CHECK_OK);
+ if (V8_UNLIKELY(is_async_function() && is_async && this->IsAwait(name))) {
+ ReportMessageAt(scanner()->location(),
+ MessageTemplate::kAwaitBindingIdentifier);
+ *ok = false;
+ return nullptr;
+ }
+
FuncNameInferrer::State fni_state(fni_);
if (fni_ != NULL) fni_->PushEnclosingName(name);
FunctionLiteral* fun = ParseFunctionLiteral(
@@ -2085,7 +2135,8 @@ Statement* Parser::ParseHoistableDeclaration(
is_strict_reserved ? kFunctionNameIsStrictReserved
: kFunctionNameValidityUnknown,
is_generator ? FunctionKind::kGeneratorFunction
- : FunctionKind::kNormalFunction,
+ : is_async ? FunctionKind::kAsyncFunction
+ : FunctionKind::kNormalFunction,
pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK);
// Even if we're not at the top-level of the global or a function
@@ -2398,15 +2449,18 @@ static bool ContainsLabel(ZoneList<const AstRawString*>* labels,
Statement* Parser::ParseFunctionDeclaration(bool* ok) {
Consume(Token::FUNCTION);
int pos = position();
- bool is_generator = Check(Token::MUL);
- if (allow_harmony_restrictive_declarations() && is_generator) {
- ParserTraits::ReportMessageAt(
- scanner()->location(),
- MessageTemplate::kGeneratorInLegacyContext);
- *ok = false;
- return nullptr;
+ ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal;
+ if (Check(Token::MUL)) {
+ flags |= ParseFunctionFlags::kIsGenerator;
+ if (allow_harmony_restrictive_declarations()) {
+ ParserTraits::ReportMessageAt(scanner()->location(),
+ MessageTemplate::kGeneratorInLegacyContext);
+ *ok = false;
+ return nullptr;
+ }
}
- return ParseHoistableDeclaration(pos, is_generator, nullptr, CHECK_OK);
+
+ return ParseHoistableDeclaration(pos, flags, nullptr, CHECK_OK);
}
Statement* Parser::ParseExpressionOrLabelledStatement(
@@ -4206,6 +4260,37 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
return function_literal;
}
+Expression* Parser::ParseAsyncFunctionExpression(bool* ok) {
+ // AsyncFunctionDeclaration ::
+ // async [no LineTerminator here] function ( FormalParameters[Await] )
+ // { AsyncFunctionBody }
+ //
+ // async [no LineTerminator here] function BindingIdentifier[Await]
+ // ( FormalParameters[Await] ) { AsyncFunctionBody }
+ DCHECK_EQ(scanner()->current_token(), Token::IDENTIFIER);
+ DCHECK(scanner()->is_literal_contextual_keyword(CStrVector("async")));
+ int pos = position();
+ Expect(Token::FUNCTION, CHECK_OK);
+ bool is_strict_reserved = false;
+ const AstRawString* name = nullptr;
+ FunctionLiteral::FunctionType type = FunctionLiteral::kAnonymousExpression;
+
+ if (peek_any_identifier()) {
+ type = FunctionLiteral::kNamedExpression;
+ name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
+ if (this->IsAwait(name)) {
+ ReportMessageAt(scanner()->location(),
+ MessageTemplate::kAwaitBindingIdentifier);
+ *ok = false;
+ return nullptr;
+ }
+ }
+ return ParseFunctionLiteral(name, scanner()->location(),
+ is_strict_reserved ? kFunctionNameIsStrictReserved
+ : kFunctionNameValidityUnknown,
+ FunctionKind::kAsyncFunction, pos, type,
+ language_mode(), CHECK_OK);
+}
void Parser::SkipLazyFunctionBody(int* materialized_literal_count,
int* expected_property_count, bool* ok,
@@ -4605,6 +4690,7 @@ PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser(
SET_ALLOW(harmony_function_sent);
SET_ALLOW(harmony_exponentiation_operator);
SET_ALLOW(harmony_restrictive_declarations);
+ SET_ALLOW(harmony_async_await);
#undef SET_ALLOW
}
PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
@@ -5312,6 +5398,17 @@ void Parser::MarkCollectedTailCallExpressions() {
}
}
+Expression* ParserTraits::ExpressionListToExpression(
+ ZoneList<Expression*>* args) {
+ AstNodeFactory* factory = parser_->factory();
+ Expression* expr = args->at(0);
+ for (int i = 1; i < args->length(); ++i) {
+ expr = factory->NewBinaryOperation(Token::COMMA, expr, args->at(i),
+ expr->position());
+ }
+ return expr;
+}
+
void ParserTraits::RewriteDestructuringAssignments() {
parser_->RewriteDestructuringAssignments();
}
@@ -5332,6 +5429,11 @@ void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier,
parser_->RewriteNonPattern(classifier, ok);
}
+Expression* ParserTraits::RewriteAwaitExpression(Expression* value, int pos) {
+ // TODO(caitp): Implement AsyncFunctionAwait()
+ // per tc39.github.io/ecmascript-asyncawait/#abstract-ops-async-function-await
+ return value;
+}
Zone* ParserTraits::zone() const {
return parser_->function_state_->scope()->zone();

Powered by Google App Engine
This is Rietveld 408576698