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

Side by Side Diff: src/parser.cc

Issue 1300103005: [parser] disallow language mode directive in body of function with non-simple parameters (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: minor test cleanup Created 5 years, 4 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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 "src/parser.h" 5 #include "src/parser.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/ast-literal-reindexer.h" 9 #include "src/ast-literal-reindexer.h"
10 #include "src/bailout-reason.h" 10 #include "src/bailout-reason.h"
(...skipping 1183 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 // scope for formal parameter parsing. 1194 // scope for formal parameter parsing.
1195 BlockState block_state(&scope_, scope); 1195 BlockState block_state(&scope_, scope);
1196 if (Check(Token::LPAREN)) { 1196 if (Check(Token::LPAREN)) {
1197 // '(' StrictFormalParameters ')' 1197 // '(' StrictFormalParameters ')'
1198 ParseFormalParameterList(&formals, &formals_classifier, &ok); 1198 ParseFormalParameterList(&formals, &formals_classifier, &ok);
1199 if (ok) ok = Check(Token::RPAREN); 1199 if (ok) ok = Check(Token::RPAREN);
1200 } else { 1200 } else {
1201 // BindingIdentifier 1201 // BindingIdentifier
1202 ParseFormalParameter(&formals, &formals_classifier, &ok); 1202 ParseFormalParameter(&formals, &formals_classifier, &ok);
1203 if (ok) { 1203 if (ok) {
1204 DeclareFormalParameter( 1204 DeclareFormalParameter(formals.scope, formals.at(0),
1205 formals.scope, formals.at(0), formals.is_simple, 1205 &formals_classifier);
1206 &formals_classifier);
1207 } 1206 }
1208 } 1207 }
1209 } 1208 }
1210 1209
1211 if (ok) { 1210 if (ok) {
1212 checkpoint.Restore(&formals.materialized_literals_count); 1211 checkpoint.Restore(&formals.materialized_literals_count);
1213 Expression* expression = 1212 Expression* expression =
1214 ParseArrowFunctionLiteral(formals, formals_classifier, &ok); 1213 ParseArrowFunctionLiteral(formals, formals_classifier, &ok);
1215 if (ok) { 1214 if (ok) {
1216 // Scanning must end at the same position that was recorded 1215 // Scanning must end at the same position that was recorded
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1318 ast_value_factory()->use_strict_string() && 1317 ast_value_factory()->use_strict_string() &&
1319 token_loc.end_pos - token_loc.beg_pos == 1318 token_loc.end_pos - token_loc.beg_pos ==
1320 ast_value_factory()->use_strict_string()->length() + 2; 1319 ast_value_factory()->use_strict_string()->length() + 2;
1321 bool use_strong_found = 1320 bool use_strong_found =
1322 allow_strong_mode() && 1321 allow_strong_mode() &&
1323 literal->raw_value()->AsString() == 1322 literal->raw_value()->AsString() ==
1324 ast_value_factory()->use_strong_string() && 1323 ast_value_factory()->use_strong_string() &&
1325 token_loc.end_pos - token_loc.beg_pos == 1324 token_loc.end_pos - token_loc.beg_pos ==
1326 ast_value_factory()->use_strong_string()->length() + 2; 1325 ast_value_factory()->use_strong_string()->length() + 2;
1327 if (use_strict_found || use_strong_found) { 1326 if (use_strict_found || use_strong_found) {
1327 if (!scope_->HasSimpleParameters()) {
1328 // A block declaration scope as a child scope of a function scope
1329 // indicates that a function has a non-simple parameter list.
1330 // TC39 deemed "use strict" directives to be an error in this case,
1331 // on 29/7/2015. https://goo.gl/ueA7Ln
1332 //
1333 // In V8, this also applies to "use strong " directives.
1334 const AstRawString* string = literal->raw_value()->AsString();
1335 ParserTraits::ReportMessageAt(
1336 token_loc, MessageTemplate::kIllegalLanguageModeDirective,
1337 string);
1338 *ok = false;
1339 return nullptr;
1340 }
1341
1328 // Strong mode implies strict mode. If there are several "use strict" 1342 // Strong mode implies strict mode. If there are several "use strict"
1329 // / "use strong" directives, do the strict mode changes only once. 1343 // / "use strong" directives, do the strict mode changes only once.
1330 if (is_sloppy(scope_->language_mode())) { 1344 if (is_sloppy(scope_->language_mode())) {
1331 scope_->SetLanguageMode(static_cast<LanguageMode>( 1345 scope_->SetLanguageMode(static_cast<LanguageMode>(
1332 scope_->language_mode() | STRICT)); 1346 scope_->language_mode() | STRICT));
1333 } 1347 }
1334 1348
1335 if (use_strong_found) { 1349 if (use_strong_found) {
1336 scope_->SetLanguageMode(static_cast<LanguageMode>( 1350 scope_->SetLanguageMode(static_cast<LanguageMode>(
1337 scope_->language_mode() | STRONG)); 1351 scope_->language_mode() | STRONG));
(...skipping 2513 matching lines...) Expand 10 before | Expand all | Expand 10 after
3851 } 3865 }
3852 3866
3853 3867
3854 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { 3868 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3855 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); 3869 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3856 } 3870 }
3857 3871
3858 3872
3859 void ParserTraits::ParseArrowFunctionFormalParameters( 3873 void ParserTraits::ParseArrowFunctionFormalParameters(
3860 ParserFormalParameters* parameters, Expression* expr, 3874 ParserFormalParameters* parameters, Expression* expr,
3861 const Scanner::Location& params_loc, 3875 const Scanner::Location& params_loc, bool* ok) {
3862 Scanner::Location* duplicate_loc, bool* ok) {
3863 if (parameters->Arity() >= Code::kMaxArguments) { 3876 if (parameters->Arity() >= Code::kMaxArguments) {
3864 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); 3877 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
3865 *ok = false; 3878 *ok = false;
3866 return; 3879 return;
3867 } 3880 }
3868 3881
3869 // ArrowFunctionFormals :: 3882 // ArrowFunctionFormals ::
3870 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail) 3883 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail)
3871 // Tail 3884 // Tail
3872 // NonTailArrowFunctionFormals :: 3885 // NonTailArrowFunctionFormals ::
3873 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy) 3886 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy)
3874 // VariableProxy 3887 // VariableProxy
3875 // Tail :: 3888 // Tail ::
3876 // VariableProxy 3889 // VariableProxy
3877 // Spread(VariableProxy) 3890 // Spread(VariableProxy)
3878 // 3891 //
3879 // As we need to visit the parameters in left-to-right order, we recurse on 3892 // As we need to visit the parameters in left-to-right order, we recurse on
3880 // the left-hand side of comma expressions. 3893 // the left-hand side of comma expressions.
3881 // 3894 //
3882 if (expr->IsBinaryOperation()) { 3895 if (expr->IsBinaryOperation()) {
3883 BinaryOperation* binop = expr->AsBinaryOperation(); 3896 BinaryOperation* binop = expr->AsBinaryOperation();
3884 // The classifier has already run, so we know that the expression is a valid 3897 // The classifier has already run, so we know that the expression is a valid
3885 // arrow function formals production. 3898 // arrow function formals production.
3886 DCHECK_EQ(binop->op(), Token::COMMA); 3899 DCHECK_EQ(binop->op(), Token::COMMA);
3887 Expression* left = binop->left(); 3900 Expression* left = binop->left();
3888 Expression* right = binop->right(); 3901 Expression* right = binop->right();
3889 ParseArrowFunctionFormalParameters(parameters, left, params_loc, 3902 ParseArrowFunctionFormalParameters(parameters, left, params_loc, ok);
3890 duplicate_loc, ok);
3891 if (!*ok) return; 3903 if (!*ok) return;
3892 // LHS of comma expression should be unparenthesized. 3904 // LHS of comma expression should be unparenthesized.
3893 expr = right; 3905 expr = right;
3894 } 3906 }
3895 3907
3896 // Only the right-most expression may be a rest parameter. 3908 // Only the right-most expression may be a rest parameter.
3897 DCHECK(!parameters->has_rest); 3909 DCHECK(!parameters->has_rest);
3898 3910
3899 bool is_rest = expr->IsSpread(); 3911 bool is_rest = expr->IsSpread();
3900 if (is_rest) { 3912 if (is_rest) {
(...skipping 28 matching lines...) Expand all
3929 AddFormalParameter(parameters, expr, initializer, is_rest); 3941 AddFormalParameter(parameters, expr, initializer, is_rest);
3930 } 3942 }
3931 3943
3932 3944
3933 void ParserTraits::ParseArrowFunctionFormalParameterList( 3945 void ParserTraits::ParseArrowFunctionFormalParameterList(
3934 ParserFormalParameters* parameters, Expression* expr, 3946 ParserFormalParameters* parameters, Expression* expr,
3935 const Scanner::Location& params_loc, 3947 const Scanner::Location& params_loc,
3936 Scanner::Location* duplicate_loc, bool* ok) { 3948 Scanner::Location* duplicate_loc, bool* ok) {
3937 if (expr->IsEmptyParentheses()) return; 3949 if (expr->IsEmptyParentheses()) return;
3938 3950
3939 ParseArrowFunctionFormalParameters(parameters, expr, params_loc, 3951 ParseArrowFunctionFormalParameters(parameters, expr, params_loc, ok);
3940 duplicate_loc, ok);
3941 if (!*ok) return; 3952 if (!*ok) return;
3942 3953
3954 ExpressionClassifier classifier;
3955 if (!parameters->is_simple) {
3956 classifier.RecordNonSimpleParameter();
3957 }
3943 for (int i = 0; i < parameters->Arity(); ++i) { 3958 for (int i = 0; i < parameters->Arity(); ++i) {
3944 auto parameter = parameters->at(i); 3959 auto parameter = parameters->at(i);
3945 ExpressionClassifier classifier; 3960 DeclareFormalParameter(parameters->scope, parameter, &classifier);
3946 DeclareFormalParameter(
3947 parameters->scope, parameter, parameters->is_simple, &classifier);
3948 if (!duplicate_loc->IsValid()) { 3961 if (!duplicate_loc->IsValid()) {
3949 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; 3962 *duplicate_loc = classifier.duplicate_formal_parameter_error().location;
3950 } 3963 }
3951 } 3964 }
3952 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters()); 3965 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters());
3953 } 3966 }
3954 3967
3955 3968
3956 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) { 3969 void ParserTraits::ReindexLiterals(const ParserFormalParameters& parameters) {
3957 if (parser_->function_state_->materialized_literal_count() > 0) { 3970 if (parser_->function_state_->materialized_literal_count() > 0) {
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after
4556 SET_ALLOW(harmony_rest_parameters); 4569 SET_ALLOW(harmony_rest_parameters);
4557 SET_ALLOW(harmony_default_parameters); 4570 SET_ALLOW(harmony_default_parameters);
4558 SET_ALLOW(harmony_spreadcalls); 4571 SET_ALLOW(harmony_spreadcalls);
4559 SET_ALLOW(harmony_destructuring); 4572 SET_ALLOW(harmony_destructuring);
4560 SET_ALLOW(harmony_spread_arrays); 4573 SET_ALLOW(harmony_spread_arrays);
4561 SET_ALLOW(harmony_new_target); 4574 SET_ALLOW(harmony_new_target);
4562 SET_ALLOW(strong_mode); 4575 SET_ALLOW(strong_mode);
4563 #undef SET_ALLOW 4576 #undef SET_ALLOW
4564 } 4577 }
4565 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( 4578 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
4566 language_mode(), function_state_->kind(), logger, bookmark); 4579 language_mode(), function_state_->kind(), scope_->has_simple_parameters(),
4580 logger, bookmark);
4567 if (pre_parse_timer_ != NULL) { 4581 if (pre_parse_timer_ != NULL) {
4568 pre_parse_timer_->Stop(); 4582 pre_parse_timer_->Stop();
4569 } 4583 }
4570 return result; 4584 return result;
4571 } 4585 }
4572 4586
4573 4587
4574 ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name, 4588 ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name,
4575 Scanner::Location class_name_location, 4589 Scanner::Location class_name_location,
4576 bool name_is_strict_reserved, int pos, 4590 bool name_is_strict_reserved, int pos,
(...skipping 1496 matching lines...) Expand 10 before | Expand all | Expand 10 after
6073 Expression* Parser::SpreadCallNew(Expression* function, 6087 Expression* Parser::SpreadCallNew(Expression* function,
6074 ZoneList<v8::internal::Expression*>* args, 6088 ZoneList<v8::internal::Expression*>* args,
6075 int pos) { 6089 int pos) {
6076 args->InsertAt(0, function, zone()); 6090 args->InsertAt(0, function, zone());
6077 6091
6078 return factory()->NewCallRuntime( 6092 return factory()->NewCallRuntime(
6079 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 6093 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
6080 } 6094 }
6081 } // namespace internal 6095 } // namespace internal
6082 } // namespace v8 6096 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698