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

Side by Side Diff: src/parser.cc

Issue 898983002: Add strong mode. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Code review + fixes Created 5 years, 10 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
« no previous file with comments | « src/objects-inl.h ('k') | src/preparser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/v8.h" 5 #include "src/v8.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/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 int pos, int end_pos) { 282 int pos, int end_pos) {
283 int materialized_literal_count = -1; 283 int materialized_literal_count = -1;
284 int expected_property_count = -1; 284 int expected_property_count = -1;
285 int handler_count = 0; 285 int handler_count = 0;
286 int parameter_count = 0; 286 int parameter_count = 0;
287 const AstRawString* name = ast_value_factory()->empty_string(); 287 const AstRawString* name = ast_value_factory()->empty_string();
288 288
289 Scope* function_scope = 289 Scope* function_scope =
290 NewScope(scope, FUNCTION_SCOPE, FunctionKind::kDefaultConstructor); 290 NewScope(scope, FUNCTION_SCOPE, FunctionKind::kDefaultConstructor);
291 function_scope->SetLanguageMode( 291 function_scope->SetLanguageMode(
292 static_cast<LanguageMode>(scope->language_mode() | STRICT)); 292 static_cast<LanguageMode>(scope->language_mode() | STRICT_BIT));
293 // Set start and end position to the same value 293 // Set start and end position to the same value
294 function_scope->set_start_position(pos); 294 function_scope->set_start_position(pos);
295 function_scope->set_end_position(pos); 295 function_scope->set_end_position(pos);
296 ZoneList<Statement*>* body = NULL; 296 ZoneList<Statement*>* body = NULL;
297 297
298 { 298 {
299 AstNodeFactory function_factory(ast_value_factory()); 299 AstNodeFactory function_factory(ast_value_factory());
300 FunctionState function_state(&function_state_, &scope_, function_scope, 300 FunctionState function_state(&function_state_, &scope_, function_scope,
301 &function_factory); 301 &function_factory);
302 302
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 set_allow_harmony_arrow_functions(FLAG_harmony_arrow_functions); 817 set_allow_harmony_arrow_functions(FLAG_harmony_arrow_functions);
818 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 818 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
819 set_allow_harmony_classes(FLAG_harmony_classes); 819 set_allow_harmony_classes(FLAG_harmony_classes);
820 set_allow_harmony_object_literals(FLAG_harmony_object_literals); 820 set_allow_harmony_object_literals(FLAG_harmony_object_literals);
821 set_allow_harmony_templates(FLAG_harmony_templates); 821 set_allow_harmony_templates(FLAG_harmony_templates);
822 set_allow_harmony_sloppy(FLAG_harmony_sloppy); 822 set_allow_harmony_sloppy(FLAG_harmony_sloppy);
823 set_allow_harmony_unicode(FLAG_harmony_unicode); 823 set_allow_harmony_unicode(FLAG_harmony_unicode);
824 set_allow_harmony_computed_property_names( 824 set_allow_harmony_computed_property_names(
825 FLAG_harmony_computed_property_names); 825 FLAG_harmony_computed_property_names);
826 set_allow_harmony_rest_params(FLAG_harmony_rest_parameters); 826 set_allow_harmony_rest_params(FLAG_harmony_rest_parameters);
827 set_allow_strong_mode(FLAG_strong_mode);
827 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 828 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
828 ++feature) { 829 ++feature) {
829 use_counts_[feature] = 0; 830 use_counts_[feature] = 0;
830 } 831 }
831 if (info->ast_value_factory() == NULL) { 832 if (info->ast_value_factory() == NULL) {
832 // info takes ownership of AstValueFactory. 833 // info takes ownership of AstValueFactory.
833 info->SetAstValueFactory( 834 info->SetAstValueFactory(
834 new AstValueFactory(zone(), parse_info->hash_seed)); 835 new AstValueFactory(zone(), parse_info->hash_seed));
835 } 836 }
836 } 837 }
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 } 1139 }
1139 1140
1140 if (directive_prologue) { 1141 if (directive_prologue) {
1141 // A shot at a directive. 1142 // A shot at a directive.
1142 ExpressionStatement* e_stat; 1143 ExpressionStatement* e_stat;
1143 Literal* literal; 1144 Literal* literal;
1144 // Still processing directive prologue? 1145 // Still processing directive prologue?
1145 if ((e_stat = stat->AsExpressionStatement()) != NULL && 1146 if ((e_stat = stat->AsExpressionStatement()) != NULL &&
1146 (literal = e_stat->expression()->AsLiteral()) != NULL && 1147 (literal = e_stat->expression()->AsLiteral()) != NULL &&
1147 literal->raw_value()->IsString()) { 1148 literal->raw_value()->IsString()) {
1148 // Check "use strict" directive (ES5 14.1) and "use asm" directive. Only 1149 // Check "use strict" directive (ES5 14.1), "use asm" directive, and
1149 // one can be present. 1150 // "use strong" directive (experimental).
1150 if (is_sloppy(language_mode()) && 1151 bool use_strict_found =
1151 literal->raw_value()->AsString() == 1152 literal->raw_value()->AsString() ==
1152 ast_value_factory()->use_strict_string() && 1153 ast_value_factory()->use_strict_string() &&
1153 token_loc.end_pos - token_loc.beg_pos == 1154 token_loc.end_pos - token_loc.beg_pos ==
1154 ast_value_factory()->use_strict_string()->length() + 2) { 1155 ast_value_factory()->use_strict_string()->length() + 2;
1155 // TODO(mstarzinger): Global strict eval calls, need their own scope 1156 bool use_strong_found =
1156 // as specified in ES5 10.4.2(3). The correct fix would be to always 1157 allow_strong_mode() &&
1157 // add this scope in DoParseProgram(), but that requires adaptations 1158 literal->raw_value()->AsString() ==
1158 // all over the code base, so we go with a quick-fix for now. 1159 ast_value_factory()->use_strong_string() &&
1159 // In the same manner, we have to patch the parsing mode. 1160 token_loc.end_pos - token_loc.beg_pos ==
1160 if (is_eval && !scope_->is_eval_scope()) { 1161 ast_value_factory()->use_strong_string()->length() + 2;
1161 DCHECK(scope_->is_script_scope()); 1162 if (use_strict_found || use_strong_found) {
1162 Scope* scope = NewScope(scope_, EVAL_SCOPE); 1163 // Strong mode implies strict mode. If there are several "use strict"
1163 scope->set_start_position(scope_->start_position()); 1164 // / "use strong" directives, do the strict mode changes only once.
1164 scope->set_end_position(scope_->end_position()); 1165 if (is_sloppy(scope_->language_mode())) {
1165 scope_ = scope; 1166 // TODO(mstarzinger): Global strict eval calls, need their own scope
1166 if (eval_scope != NULL) { 1167 // as specified in ES5 10.4.2(3). The correct fix would be to always
1167 // Caller will correct the positions of the ad hoc eval scope. 1168 // add this scope in DoParseProgram(), but that requires adaptations
1168 *eval_scope = scope; 1169 // all over the code base, so we go with a quick-fix for now.
1170 // In the same manner, we have to patch the parsing mode.
1171 if (is_eval && !scope_->is_eval_scope()) {
1172 DCHECK(scope_->is_script_scope());
1173 Scope* scope = NewScope(scope_, EVAL_SCOPE);
1174 scope->set_start_position(scope_->start_position());
1175 scope->set_end_position(scope_->end_position());
1176 scope_ = scope;
1177 if (eval_scope != NULL) {
1178 // Caller will correct the positions of the ad hoc eval scope.
1179 *eval_scope = scope;
1180 }
1181 mode_ = PARSE_EAGERLY;
1169 } 1182 }
1170 mode_ = PARSE_EAGERLY; 1183 scope_->SetLanguageMode(static_cast<LanguageMode>(
1184 scope_->language_mode() | STRICT_BIT));
1171 } 1185 }
1172 scope_->SetLanguageMode( 1186
1173 static_cast<LanguageMode>(scope_->language_mode() | STRICT)); 1187 if (use_strong_found) {
1174 // "use strict" is the only directive for now. 1188 scope_->SetLanguageMode(static_cast<LanguageMode>(
1175 directive_prologue = false; 1189 scope_->language_mode() | STRONG_BIT));
1190 }
1176 } else if (literal->raw_value()->AsString() == 1191 } else if (literal->raw_value()->AsString() ==
1177 ast_value_factory()->use_asm_string() && 1192 ast_value_factory()->use_asm_string() &&
1178 token_loc.end_pos - token_loc.beg_pos == 1193 token_loc.end_pos - token_loc.beg_pos ==
1179 ast_value_factory()->use_asm_string()->length() + 2) { 1194 ast_value_factory()->use_asm_string()->length() + 2) {
1180 // Store the usage count; The actual use counter on the isolate is 1195 // Store the usage count; The actual use counter on the isolate is
1181 // incremented after parsing is done. 1196 // incremented after parsing is done.
1182 ++use_counts_[v8::Isolate::kUseAsm]; 1197 ++use_counts_[v8::Isolate::kUseAsm];
1183 scope_->SetAsmModule(); 1198 scope_->SetAsmModule();
1184 } 1199 }
1185 } else { 1200 } else {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1250 int pos = peek_position(); 1265 int pos = peek_position();
1251 // Construct block expecting 16 statements. 1266 // Construct block expecting 16 statements.
1252 Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition); 1267 Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition);
1253 #ifdef DEBUG 1268 #ifdef DEBUG
1254 if (FLAG_print_interface_details) PrintF("# Literal "); 1269 if (FLAG_print_interface_details) PrintF("# Literal ");
1255 #endif 1270 #endif
1256 Scope* scope = NewScope(scope_, MODULE_SCOPE); 1271 Scope* scope = NewScope(scope_, MODULE_SCOPE);
1257 1272
1258 scope->set_start_position(scanner()->location().beg_pos); 1273 scope->set_start_position(scanner()->location().beg_pos);
1259 scope->SetLanguageMode( 1274 scope->SetLanguageMode(
1260 static_cast<LanguageMode>(scope->language_mode() | STRICT)); 1275 static_cast<LanguageMode>(scope->language_mode() | STRICT_BIT));
1261 1276
1262 { 1277 {
1263 BlockState block_state(&scope_, scope); 1278 BlockState block_state(&scope_, scope);
1264 Target target(&this->target_stack_, body); 1279 Target target(&this->target_stack_, body);
1265 1280
1266 while (peek() != Token::EOS) { 1281 while (peek() != Token::EOS) {
1267 Statement* stat = ParseModuleItem(CHECK_OK); 1282 Statement* stat = ParseModuleItem(CHECK_OK);
1268 if (stat && !stat->IsEmpty()) { 1283 if (stat && !stat->IsEmpty()) {
1269 body->AddStatement(stat, zone()); 1284 body->AddStatement(stat, zone());
1270 } 1285 }
(...skipping 2729 matching lines...) Expand 10 before | Expand all | Expand 10 after
4000 reusable_preparser_->set_allow_harmony_classes(allow_harmony_classes()); 4015 reusable_preparser_->set_allow_harmony_classes(allow_harmony_classes());
4001 reusable_preparser_->set_allow_harmony_object_literals( 4016 reusable_preparser_->set_allow_harmony_object_literals(
4002 allow_harmony_object_literals()); 4017 allow_harmony_object_literals());
4003 reusable_preparser_->set_allow_harmony_templates(allow_harmony_templates()); 4018 reusable_preparser_->set_allow_harmony_templates(allow_harmony_templates());
4004 reusable_preparser_->set_allow_harmony_sloppy(allow_harmony_sloppy()); 4019 reusable_preparser_->set_allow_harmony_sloppy(allow_harmony_sloppy());
4005 reusable_preparser_->set_allow_harmony_unicode(allow_harmony_unicode()); 4020 reusable_preparser_->set_allow_harmony_unicode(allow_harmony_unicode());
4006 reusable_preparser_->set_allow_harmony_computed_property_names( 4021 reusable_preparser_->set_allow_harmony_computed_property_names(
4007 allow_harmony_computed_property_names()); 4022 allow_harmony_computed_property_names());
4008 reusable_preparser_->set_allow_harmony_rest_params( 4023 reusable_preparser_->set_allow_harmony_rest_params(
4009 allow_harmony_rest_params()); 4024 allow_harmony_rest_params());
4025 reusable_preparser_->set_allow_strong_mode(allow_strong_mode());
4010 } 4026 }
4011 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( 4027 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
4012 language_mode(), is_generator(), logger); 4028 language_mode(), is_generator(), logger);
4013 if (pre_parse_timer_ != NULL) { 4029 if (pre_parse_timer_ != NULL) {
4014 pre_parse_timer_->Stop(); 4030 pre_parse_timer_->Stop();
4015 } 4031 }
4016 return result; 4032 return result;
4017 } 4033 }
4018 4034
4019 4035
4020 ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name, 4036 ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name,
4021 Scanner::Location class_name_location, 4037 Scanner::Location class_name_location,
4022 bool name_is_strict_reserved, int pos, 4038 bool name_is_strict_reserved, int pos,
4023 bool* ok) { 4039 bool* ok) {
4024 // All parts of a ClassDeclaration and ClassExpression are strict code. 4040 // All parts of a ClassDeclaration and ClassExpression are strict code.
4025 if (name_is_strict_reserved) { 4041 if (name_is_strict_reserved) {
4026 ReportMessageAt(class_name_location, "unexpected_strict_reserved"); 4042 ReportMessageAt(class_name_location, "unexpected_strict_reserved");
4027 *ok = false; 4043 *ok = false;
4028 return NULL; 4044 return NULL;
4029 } 4045 }
4030 if (IsEvalOrArguments(name)) { 4046 if (IsEvalOrArguments(name)) {
4031 ReportMessageAt(class_name_location, "strict_eval_arguments"); 4047 ReportMessageAt(class_name_location, "strict_eval_arguments");
4032 *ok = false; 4048 *ok = false;
4033 return NULL; 4049 return NULL;
4034 } 4050 }
4035 4051
4036 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE); 4052 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
4037 BlockState block_state(&scope_, block_scope); 4053 BlockState block_state(&scope_, block_scope);
4038 scope_->SetLanguageMode( 4054 scope_->SetLanguageMode(
4039 static_cast<LanguageMode>(scope_->language_mode() | STRICT)); 4055 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
4040 scope_->SetScopeName(name); 4056 scope_->SetScopeName(name);
4041 4057
4042 VariableProxy* proxy = NULL; 4058 VariableProxy* proxy = NULL;
4043 if (name != NULL) { 4059 if (name != NULL) {
4044 proxy = NewUnresolved(name, CONST, Interface::NewConst()); 4060 proxy = NewUnresolved(name, CONST, Interface::NewConst());
4045 Declaration* declaration = 4061 Declaration* declaration =
4046 factory()->NewVariableDeclaration(proxy, CONST, block_scope, pos); 4062 factory()->NewVariableDeclaration(proxy, CONST, block_scope, pos);
4047 Declare(declaration, true, CHECK_OK); 4063 Declare(declaration, true, CHECK_OK);
4048 } 4064 }
4049 4065
(...skipping 1362 matching lines...) Expand 10 before | Expand all | Expand 10 after
5412 } else { 5428 } else {
5413 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data()); 5429 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data());
5414 running_hash = StringHasher::ComputeRunningHash(running_hash, data, 5430 running_hash = StringHasher::ComputeRunningHash(running_hash, data,
5415 raw_string->length()); 5431 raw_string->length());
5416 } 5432 }
5417 } 5433 }
5418 5434
5419 return running_hash; 5435 return running_hash;
5420 } 5436 }
5421 } } // namespace v8::internal 5437 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects-inl.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698