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

Unified Diff: src/parser.cc

Issue 1024063002: [strong] checking of this & super in constructors (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Typo Created 5 years, 9 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
« no previous file with comments | « src/messages.js ('k') | src/preparser.h » ('j') | test/cctest/test-parsing.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index 77c79e378999080a761130690133d76e24109769..0222ae7a7436500cc0b4440d8b61ca30894e14af 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1208,25 +1208,28 @@ void* Parser::ParseStatementList(ZoneList<Statement*>* body, int end_token,
directive_prologue = false;
}
- Token::Value token = peek();
Scanner::Location token_loc = scanner()->peek_location();
- Scanner::Location old_super_loc = function_state_->super_call_location();
+ Scanner::Location old_this_loc = function_state_->this_location();
+ Scanner::Location old_super_loc = function_state_->super_location();
Statement* stat = ParseStatementListItem(CHECK_OK);
- Scanner::Location super_loc = function_state_->super_call_location();
if (is_strong(language_mode()) &&
- i::IsConstructor(function_state_->kind()) &&
- !old_super_loc.IsValid() && super_loc.IsValid() &&
- token != Token::SUPER) {
- // TODO(rossberg): This is more permissive than spec'ed, it allows e.g.
- // super(), 1;
- // super() + "";
- // super() = 0;
- // That should still be safe, though, thanks to left-to-right evaluation.
- // The proper check would be difficult to implement in the preparser.
- ReportMessageAt(super_loc, "strong_super_call_nested");
- *ok = false;
- return NULL;
+ scope_->is_function_scope() &&
+ i::IsConstructor(function_state_->kind())) {
+ Scanner::Location this_loc = function_state_->this_location();
+ Scanner::Location super_loc = function_state_->super_location();
+ if (this_loc.beg_pos != old_this_loc.beg_pos &&
+ this_loc.beg_pos != token_loc.beg_pos) {
+ ReportMessageAt(this_loc, "strong_constructor_this");
+ *ok = false;
+ return nullptr;
+ }
+ if (super_loc.beg_pos != old_super_loc.beg_pos &&
+ super_loc.beg_pos != token_loc.beg_pos) {
+ ReportMessageAt(super_loc, "strong_constructor_super");
+ *ok = false;
+ return nullptr;
+ }
}
if (stat == NULL || stat->IsEmpty()) {
@@ -2551,6 +2554,8 @@ Statement* Parser::ParseExpressionOrLabelledStatement(
// ExpressionStatement[Yield] :
// [lookahead ∉ {{, function, class, let [}] Expression[In, ?Yield] ;
+ int pos = peek_position();
+
switch (peek()) {
case Token::FUNCTION:
case Token::LBRACE:
@@ -2560,6 +2565,37 @@ Statement* Parser::ParseExpressionOrLabelledStatement(
*ok = false;
return nullptr;
+ case Token::THIS:
+ case Token::SUPER:
+ if (is_strong(language_mode()) &&
+ i::IsConstructor(function_state_->kind())) {
+ bool is_this = peek() == Token::THIS;
+ Expression* expr;
+ if (is_this) {
+ expr = ParseStrongInitializationExpression(CHECK_OK);
+ } else {
+ expr = ParseStrongSuperCallExpression(CHECK_OK);
+ }
+ switch (peek()) {
+ case Token::SEMICOLON:
+ Consume(Token::SEMICOLON);
+ break;
+ case Token::RBRACE:
+ case Token::EOS:
+ break;
+ default:
+ if (!scanner()->HasAnyLineTerminatorBeforeNext()) {
+ ReportMessageAt(function_state_->this_location(),
+ is_this ? "strong_constructor_this"
+ : "strong_constructor_super");
+ *ok = false;
+ return nullptr;
+ }
+ }
+ return factory()->NewExpressionStatement(expr, pos);
+ }
+ break;
+
// TODO(arv): Handle `let [`
// https://code.google.com/p/v8/issues/detail?id=3847
@@ -2567,7 +2603,6 @@ Statement* Parser::ParseExpressionOrLabelledStatement(
break;
}
- int pos = peek_position();
bool starts_with_idenfifier = peek_any_identifier();
Expression* expr = ParseExpression(true, CHECK_OK);
if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL &&
@@ -3961,7 +3996,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
CheckConflictingVarDeclarations(scope, CHECK_OK);
}
if (is_strong(language_mode()) && IsSubclassConstructor(kind)) {
- if (!function_state.super_call_location().IsValid()) {
+ if (!function_state.super_location().IsValid()) {
ReportMessageAt(function_name_location, "strong_super_call_missing",
kReferenceError);
*ok = false;
« no previous file with comments | « src/messages.js ('k') | src/preparser.h » ('j') | test/cctest/test-parsing.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698