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

Side by Side Diff: src/preparser.cc

Issue 1024063002: [strong] checking of this & super in constructors (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Comments Created 5 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 unified diff | Download patch
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 <cmath> 5 #include <cmath>
6 6
7 #include "src/allocation.h" 7 #include "src/allocation.h"
8 #include "src/base/logging.h" 8 #include "src/base/logging.h"
9 #include "src/conversions-inl.h" 9 #include "src/conversions-inl.h"
10 #include "src/conversions.h" 10 #include "src/conversions.h"
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 int start_position = peek_position(); 113 int start_position = peek_position();
114 ParseLazyFunctionLiteralBody(&ok); 114 ParseLazyFunctionLiteralBody(&ok);
115 if (stack_overflow()) return kPreParseStackOverflow; 115 if (stack_overflow()) return kPreParseStackOverflow;
116 if (!ok) { 116 if (!ok) {
117 ReportUnexpectedToken(scanner()->current_token()); 117 ReportUnexpectedToken(scanner()->current_token());
118 } else { 118 } else {
119 DCHECK_EQ(Token::RBRACE, scanner()->peek()); 119 DCHECK_EQ(Token::RBRACE, scanner()->peek());
120 if (is_strict(scope_->language_mode())) { 120 if (is_strict(scope_->language_mode())) {
121 int end_pos = scanner()->location().end_pos; 121 int end_pos = scanner()->location().end_pos;
122 CheckStrictOctalLiteral(start_position, end_pos, &ok); 122 CheckStrictOctalLiteral(start_position, end_pos, &ok);
123 if (!ok) return kPreParseSuccess;
124
125 if (is_strong(scope_->language_mode()) && IsSubclassConstructor(kind)) {
126 if (!function_state.super_location().IsValid()) {
127 ReportMessageAt(Scanner::Location(start_position, start_position + 1),
128 "strong_super_call_missing", kReferenceError);
129 return kPreParseSuccess;
130 }
131 }
123 } 132 }
124 } 133 }
125 return kPreParseSuccess; 134 return kPreParseSuccess;
126 } 135 }
127 136
128 137
129 PreParserExpression PreParserTraits::ParseClassLiteral( 138 PreParserExpression PreParserTraits::ParseClassLiteral(
130 PreParserIdentifier name, Scanner::Location class_name_location, 139 PreParserIdentifier name, Scanner::Location class_name_location,
131 bool name_is_strict_reserved, int pos, bool* ok) { 140 bool name_is_strict_reserved, int pos, bool* ok) {
132 return pre_parser_->ParseClassLiteral(name, class_name_location, 141 return pre_parser_->ParseClassLiteral(name, class_name_location,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 195
187 void PreParser::ParseStatementList(int end_token, bool* ok) { 196 void PreParser::ParseStatementList(int end_token, bool* ok) {
188 // SourceElements :: 197 // SourceElements ::
189 // (Statement)* <end_token> 198 // (Statement)* <end_token>
190 199
191 bool directive_prologue = true; 200 bool directive_prologue = true;
192 while (peek() != end_token) { 201 while (peek() != end_token) {
193 if (directive_prologue && peek() != Token::STRING) { 202 if (directive_prologue && peek() != Token::STRING) {
194 directive_prologue = false; 203 directive_prologue = false;
195 } 204 }
196 Token::Value token = peek(); 205 Scanner::Location token_loc = scanner()->peek_location();
197 Scanner::Location old_super_loc = function_state_->super_call_location(); 206 Scanner::Location old_this_loc = function_state_->this_location();
207 Scanner::Location old_super_loc = function_state_->super_location();
198 Statement statement = ParseStatementListItem(ok); 208 Statement statement = ParseStatementListItem(ok);
199 if (!*ok) return; 209 if (!*ok) return;
200 Scanner::Location super_loc = function_state_->super_call_location(); 210
201 if (is_strong(language_mode()) && 211 if (is_strong(language_mode()) &&
202 i::IsConstructor(function_state_->kind()) && 212 scope_->is_function_scope() &&
203 !old_super_loc.IsValid() && super_loc.IsValid() && 213 i::IsConstructor(function_state_->kind())) {
204 token != Token::SUPER) { 214 Scanner::Location this_loc = function_state_->this_location();
205 ReportMessageAt(super_loc, "strong_super_call_nested"); 215 Scanner::Location super_loc = function_state_->super_location();
206 *ok = false; 216 if (this_loc.beg_pos != old_this_loc.beg_pos &&
207 return; 217 this_loc.beg_pos != token_loc.beg_pos) {
218 ReportMessageAt(this_loc, "strong_constructor_this");
219 *ok = false;
220 return;
221 }
222 if (super_loc.beg_pos != old_super_loc.beg_pos &&
223 super_loc.beg_pos != token_loc.beg_pos) {
224 ReportMessageAt(super_loc, "strong_constructor_super");
225 *ok = false;
226 return;
227 }
208 } 228 }
229
209 if (directive_prologue) { 230 if (directive_prologue) {
210 if (statement.IsUseStrictLiteral()) { 231 if (statement.IsUseStrictLiteral()) {
211 scope_->SetLanguageMode( 232 scope_->SetLanguageMode(
212 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT)); 233 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
213 } else if (statement.IsUseStrongLiteral() && allow_strong_mode()) { 234 } else if (statement.IsUseStrongLiteral() && allow_strong_mode()) {
214 scope_->SetLanguageMode(static_cast<LanguageMode>( 235 scope_->SetLanguageMode(static_cast<LanguageMode>(
215 scope_->language_mode() | STRICT_BIT | STRONG_BIT)); 236 scope_->language_mode() | STRICT_BIT | STRONG_BIT));
216 } else if (!statement.IsStringLiteral()) { 237 } else if (!statement.IsStringLiteral()) {
217 directive_prologue = false; 238 directive_prologue = false;
218 } 239 }
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 529
509 switch (peek()) { 530 switch (peek()) {
510 case Token::FUNCTION: 531 case Token::FUNCTION:
511 case Token::LBRACE: 532 case Token::LBRACE:
512 UNREACHABLE(); // Always handled by the callers. 533 UNREACHABLE(); // Always handled by the callers.
513 case Token::CLASS: 534 case Token::CLASS:
514 ReportUnexpectedToken(Next()); 535 ReportUnexpectedToken(Next());
515 *ok = false; 536 *ok = false;
516 return Statement::Default(); 537 return Statement::Default();
517 538
539 case Token::THIS:
540 case Token::SUPER:
541 if (is_strong(language_mode()) &&
542 i::IsConstructor(function_state_->kind())) {
543 bool is_this = peek() == Token::THIS;
544 Expression expr = Expression::Default();
545 if (is_this) {
546 expr = ParseStrongInitializationExpression(CHECK_OK);
547 } else {
548 expr = ParseStrongSuperCallExpression(CHECK_OK);
549 }
550 switch (peek()) {
551 case Token::SEMICOLON:
552 Consume(Token::SEMICOLON);
553 break;
554 case Token::RBRACE:
555 case Token::EOS:
556 break;
557 default:
558 if (!scanner()->HasAnyLineTerminatorBeforeNext()) {
559 ReportMessageAt(function_state_->this_location(),
560 is_this ? "strong_constructor_this"
561 : "strong_constructor_super");
562 *ok = false;
563 return Statement::Default();
564 }
565 }
566 return Statement::ExpressionStatement(expr);
567 }
568 break;
569
518 // TODO(arv): Handle `let [` 570 // TODO(arv): Handle `let [`
519 // https://code.google.com/p/v8/issues/detail?id=3847 571 // https://code.google.com/p/v8/issues/detail?id=3847
520 572
521 default: 573 default:
522 break; 574 break;
523 } 575 }
524 576
525 bool starts_with_identifier = peek_any_identifier(); 577 bool starts_with_identifier = peek_any_identifier();
526 Expression expr = ParseExpression(true, CHECK_OK); 578 Expression expr = ParseExpression(true, CHECK_OK);
527 // Even if the expression starts with an identifier, it is not necessarily an 579 // Even if the expression starts with an identifier, it is not necessarily an
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 CheckFunctionParameterNames(language_mode(), use_strict_params, 1004 CheckFunctionParameterNames(language_mode(), use_strict_params,
953 eval_args_error_loc, dupe_error_loc, 1005 eval_args_error_loc, dupe_error_loc,
954 reserved_error_loc, CHECK_OK); 1006 reserved_error_loc, CHECK_OK);
955 1007
956 if (is_strict(language_mode())) { 1008 if (is_strict(language_mode())) {
957 int end_position = scanner()->location().end_pos; 1009 int end_position = scanner()->location().end_pos;
958 CheckStrictOctalLiteral(start_position, end_position, CHECK_OK); 1010 CheckStrictOctalLiteral(start_position, end_position, CHECK_OK);
959 } 1011 }
960 1012
961 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) { 1013 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) {
962 if (!function_state.super_call_location().IsValid()) { 1014 if (!function_state.super_location().IsValid()) {
963 ReportMessageAt(function_name_location, "strong_super_call_missing", 1015 ReportMessageAt(function_name_location, "strong_super_call_missing",
964 kReferenceError); 1016 kReferenceError);
965 *ok = false; 1017 *ok = false;
966 return Expression::Default(); 1018 return Expression::Default();
967 } 1019 }
968 } 1020 }
969 1021
970 return Expression::Default(); 1022 return Expression::Default();
971 } 1023 }
972 1024
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); 1097 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
1046 ParseArguments(ok); 1098 ParseArguments(ok);
1047 1099
1048 return Expression::Default(); 1100 return Expression::Default();
1049 } 1101 }
1050 1102
1051 #undef CHECK_OK 1103 #undef CHECK_OK
1052 1104
1053 1105
1054 } } // v8::internal 1106 } } // v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698