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

Unified Diff: src/parsing/preparser.cc

Issue 1900033003: Disallow generator declarations in certain locations (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix from review comments 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 side-by-side diff with in-line comments
Download patch
Index: src/parsing/preparser.cc
diff --git a/src/parsing/preparser.cc b/src/parsing/preparser.cc
index e4c30841edc144f301e20b1fc10db46a00ba7123..393461512194dcfe2dac25d36bea7f4318560ff0 100644
--- a/src/parsing/preparser.cc
+++ b/src/parsing/preparser.cc
@@ -178,7 +178,7 @@ PreParser::Statement PreParser::ParseStatementListItem(bool* ok) {
switch (peek()) {
case Token::FUNCTION:
- return ParseFunctionDeclaration(ok);
+ return ParseHoistableDeclaration(ok);
case Token::CLASS:
return ParseClassDeclaration(ok);
case Token::CONST:
@@ -278,7 +278,7 @@ PreParser::Statement PreParser::ParseScopedStatement(bool legacy, bool* ok) {
(legacy && allow_harmony_restrictive_declarations())) {
return ParseSubStatement(kDisallowLabelledFunctionStatement, ok);
} else {
- return ParseFunctionDeclaration(CHECK_OK);
+ return ParseFunctionDeclaration(ok);
}
}
@@ -375,15 +375,8 @@ PreParser::Statement PreParser::ParseSubStatement(
}
-PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
- // FunctionDeclaration ::
- // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
- // GeneratorDeclaration ::
- // 'function' '*' Identifier '(' FormalParameterListopt ')'
- // '{' FunctionBody '}'
- Expect(Token::FUNCTION, CHECK_OK);
- int pos = position();
- bool is_generator = Check(Token::MUL);
+PreParser::Statement PreParser::ParseHoistableDeclaration(
+ int pos, bool is_generator, bool* ok) {
bool is_strict_reserved = false;
Identifier name = ParseIdentifierOrStrictReservedWord(
&is_strict_reserved, CHECK_OK);
@@ -398,6 +391,19 @@ PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
}
+PreParser::Statement PreParser::ParseHoistableDeclaration(bool* ok) {
+ // FunctionDeclaration ::
+ // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
+ // GeneratorDeclaration ::
+ // 'function' '*' Identifier '(' FormalParameterListopt ')'
+ // '{' FunctionBody '}'
+ Expect(Token::FUNCTION, CHECK_OK);
+ int pos = position();
+ bool is_generator = Check(Token::MUL);
+ return ParseHoistableDeclaration(pos, is_generator, CHECK_OK);
+}
+
+
PreParser::Statement PreParser::ParseClassDeclaration(bool* ok) {
Expect(Token::CLASS, CHECK_OK);
@@ -546,6 +552,20 @@ PreParser::Statement PreParser::ParseVariableDeclarations(
return Statement::Default();
}
+PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
+ Consume(Token::FUNCTION);
+ int pos = position();
+ bool is_generator = Check(Token::MUL);
+ if (allow_harmony_restrictive_declarations() && is_generator) {
+ PreParserTraits::ReportMessageAt(
+ scanner()->location(),
+ MessageTemplate::kGeneratorInLegacyContext);
+ *ok = false;
+ return Statement::Default();
+ }
+ return ParseHoistableDeclaration(pos, is_generator, ok);
+}
+
PreParser::Statement PreParser::ParseExpressionOrLabelledStatement(
AllowLabelledFunctionStatement allow_function, bool* ok) {
// ExpressionStatement | LabelledStatement ::

Powered by Google App Engine
This is Rietveld 408576698