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

Side by Side Diff: src/parser.cc

Issue 953983002: Fix up ParseProgram and ParseModule to do something sane with module scopes (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Stop using CHECK_EQ for AstRawStrings, clang does not like it 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 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/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 910 matching lines...) Expand 10 before | Expand all | Expand 10 after
921 DCHECK(parsing_on_main_thread_); 921 DCHECK(parsing_on_main_thread_);
922 ast_value_factory()->Internalize(info->isolate()); 922 ast_value_factory()->Internalize(info->isolate());
923 } 923 }
924 original_scope_ = *scope; 924 original_scope_ = *scope;
925 if (info->is_eval()) { 925 if (info->is_eval()) {
926 if (!(*scope)->is_script_scope() || is_strict(info->language_mode())) { 926 if (!(*scope)->is_script_scope() || is_strict(info->language_mode())) {
927 *scope = NewScope(*scope, EVAL_SCOPE); 927 *scope = NewScope(*scope, EVAL_SCOPE);
928 } 928 }
929 } else if (info->is_global()) { 929 } else if (info->is_global()) {
930 *scope = NewScope(*scope, SCRIPT_SCOPE); 930 *scope = NewScope(*scope, SCRIPT_SCOPE);
931 } else if (info->is_module()) {
932 *scope = NewScope(*scope, MODULE_SCOPE);
931 } 933 }
932 (*scope)->set_start_position(0); 934 (*scope)->set_start_position(0);
933 // End position will be set by the caller. 935 // End position will be set by the caller.
934 936
935 // Compute the parsing mode. 937 // Compute the parsing mode.
936 Mode mode = (FLAG_lazy && allow_lazy()) ? PARSE_LAZILY : PARSE_EAGERLY; 938 Mode mode = (FLAG_lazy && allow_lazy()) ? PARSE_LAZILY : PARSE_EAGERLY;
937 if (allow_natives() || extension_ != NULL || 939 if (allow_natives() || extension_ != NULL ||
938 (*scope)->is_eval_scope()) { 940 (*scope)->is_eval_scope()) {
939 mode = PARSE_EAGERLY; 941 mode = PARSE_EAGERLY;
940 } 942 }
941 ParsingModeScope parsing_mode(this, mode); 943 ParsingModeScope parsing_mode(this, mode);
942 944
943 // Enters 'scope'. 945 // Enters 'scope'.
944 AstNodeFactory function_factory(ast_value_factory()); 946 AstNodeFactory function_factory(ast_value_factory());
945 FunctionState function_state(&function_state_, &scope_, *scope, 947 FunctionState function_state(&function_state_, &scope_, *scope,
946 kNormalFunction, &function_factory); 948 kNormalFunction, &function_factory);
947 949
948 scope_->SetLanguageMode(info->language_mode()); 950 scope_->SetLanguageMode(info->language_mode());
949 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); 951 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
950 bool ok = true; 952 bool ok = true;
951 int beg_pos = scanner()->location().beg_pos; 953 int beg_pos = scanner()->location().beg_pos;
952 if (info->is_module()) { 954 if (info->is_module()) {
953 DCHECK(allow_harmony_modules()); 955 DCHECK(allow_harmony_modules());
954 Statement* stmt = ParseModule(&ok); 956 ParseModule(body, &ok);
955 if (ok) {
956 body->Add(stmt, zone());
957 }
958 } else { 957 } else {
959 ParseStatementList(body, Token::EOS, info->is_eval(), eval_scope, &ok); 958 ParseStatementList(body, Token::EOS, info->is_eval(), eval_scope, &ok);
960 } 959 }
961 960
962 if (ok && is_strict(language_mode())) { 961 if (ok && is_strict(language_mode())) {
963 CheckStrictOctalLiteral(beg_pos, scanner()->location().end_pos, &ok); 962 CheckStrictOctalLiteral(beg_pos, scanner()->location().end_pos, &ok);
964 } 963 }
965 964
966 if (ok && allow_harmony_scoping() && is_strict(language_mode())) { 965 if (ok && allow_harmony_scoping() && is_strict(language_mode())) {
967 CheckConflictingVarDeclarations(scope_, &ok); 966 CheckConflictingVarDeclarations(scope_, &ok);
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
1247 case Token::IMPORT: 1246 case Token::IMPORT:
1248 return ParseImportDeclaration(ok); 1247 return ParseImportDeclaration(ok);
1249 case Token::EXPORT: 1248 case Token::EXPORT:
1250 return ParseExportDeclaration(ok); 1249 return ParseExportDeclaration(ok);
1251 default: 1250 default:
1252 return ParseStatementListItem(ok); 1251 return ParseStatementListItem(ok);
1253 } 1252 }
1254 } 1253 }
1255 1254
1256 1255
1257 Statement* Parser::ParseModule(bool* ok) { 1256 void* Parser::ParseModule(ZoneList<Statement*>* body, bool* ok) {
1258 // (Ecma 262 6th Edition, 15.2): 1257 // (Ecma 262 6th Edition, 15.2):
1259 // Module : 1258 // Module :
1260 // ModuleBody? 1259 // ModuleBody?
1261 // 1260 //
1262 // ModuleBody : 1261 // ModuleBody :
1263 // ModuleItem* 1262 // ModuleItem*
1264 1263
1265 Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition); 1264 DCHECK(scope_->is_module_scope());
1266 Scope* scope = NewScope(scope_, MODULE_SCOPE); 1265 scope_->SetLanguageMode(
1267 scope->set_start_position(scanner()->location().beg_pos); 1266 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
1268 scope->SetLanguageMode(
1269 static_cast<LanguageMode>(scope->language_mode() | STRICT_BIT));
1270 1267
1271 { 1268 while (peek() != Token::EOS) {
1272 BlockState block_state(&scope_, scope); 1269 Statement* stat = ParseModuleItem(CHECK_OK);
1273 1270 if (stat && !stat->IsEmpty()) {
1274 while (peek() != Token::EOS) { 1271 body->Add(stat, zone());
1275 Statement* stat = ParseModuleItem(CHECK_OK);
1276 if (stat && !stat->IsEmpty()) {
1277 body->AddStatement(stat, zone());
1278 }
1279 } 1272 }
1280 } 1273 }
1281 1274
1282 scope->set_end_position(scanner()->location().end_pos);
1283 body->set_scope(scope);
1284
1285 // Check that all exports are bound. 1275 // Check that all exports are bound.
1286 ModuleDescriptor* descriptor = scope->module(); 1276 ModuleDescriptor* descriptor = scope_->module();
1287 for (ModuleDescriptor::Iterator it = descriptor->iterator(); !it.done(); 1277 for (ModuleDescriptor::Iterator it = descriptor->iterator(); !it.done();
1288 it.Advance()) { 1278 it.Advance()) {
1289 if (scope->LookupLocal(it.local_name()) == NULL) { 1279 if (scope_->LookupLocal(it.local_name()) == NULL) {
1290 // TODO(adamk): Pass both local_name and export_name once ParserTraits 1280 // TODO(adamk): Pass both local_name and export_name once ParserTraits
1291 // supports multiple arg error messages. 1281 // supports multiple arg error messages.
1292 // Also try to report this at a better location. 1282 // Also try to report this at a better location.
1293 ParserTraits::ReportMessage("module_export_undefined", it.local_name()); 1283 ParserTraits::ReportMessage("module_export_undefined", it.local_name());
1294 *ok = false; 1284 *ok = false;
1295 return NULL; 1285 return NULL;
1296 } 1286 }
1297 } 1287 }
1298 1288
1299 scope->module()->Freeze(); 1289 scope_->module()->Freeze();
1300 return body; 1290 return NULL;
1301 } 1291 }
1302 1292
1303 1293
1304 Literal* Parser::ParseModuleSpecifier(bool* ok) { 1294 Literal* Parser::ParseModuleSpecifier(bool* ok) {
1305 // ModuleSpecifier : 1295 // ModuleSpecifier :
1306 // StringLiteral 1296 // StringLiteral
1307 1297
1308 int pos = peek_position(); 1298 int pos = peek_position();
1309 Expect(Token::STRING, CHECK_OK); 1299 Expect(Token::STRING, CHECK_OK);
1310 return factory()->NewStringLiteral(GetSymbol(scanner()), pos); 1300 return factory()->NewStringLiteral(GetSymbol(scanner()), pos);
(...skipping 4174 matching lines...) Expand 10 before | Expand all | Expand 10 after
5485 } else { 5475 } else {
5486 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data()); 5476 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data());
5487 running_hash = StringHasher::ComputeRunningHash(running_hash, data, 5477 running_hash = StringHasher::ComputeRunningHash(running_hash, data,
5488 raw_string->length()); 5478 raw_string->length());
5489 } 5479 }
5490 } 5480 }
5491 5481
5492 return running_hash; 5482 return running_hash;
5493 } 5483 }
5494 } } // namespace v8::internal 5484 } } // namespace v8::internal
OLDNEW
« src/parser.h ('K') | « src/parser.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698