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

Unified Diff: src/parser.cc

Issue 1382123002: Ensure scopes are backed by blocks in the body of for loops (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: tests Created 5 years, 3 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 | « no previous file | test/mjsunit/regress/regress-536751.js » ('j') | no next file with comments »
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 f122d9aa5e4b57ff1459cfa48e060c666bd407ff..04ce30f4f383733d32efe87786838ade2e3d0431 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -3786,12 +3786,19 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
Expression* enumerable = ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
+ // Make a block around the statement in case a lexical binding
+ // is introduced, e.g. by a FunctionDeclaration.
+ Block* block =
+ factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
Statement* body = ParseSubStatement(NULL, CHECK_OK);
- InitializeForEachStatement(loop, expression, enumerable, body);
+ block->statements()->Add(body, zone());
+ InitializeForEachStatement(loop, expression, enumerable, block);
scope_ = saved_scope;
for_scope->set_end_position(scanner()->location().end_pos);
for_scope = for_scope->FinalizeBlockScope();
- DCHECK(for_scope == NULL);
+ if (for_scope != nullptr) {
+ block->set_scope(for_scope);
+ }
// Parsed for-in loop.
return loop;
@@ -3861,10 +3868,20 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
// const x = i;
// for (; c; n) b
// }
- DCHECK(init != NULL);
+ //
+ // or, desugar
+ // for (; c; n) b
+ // into
+ // {
+ // for (; c; n) b
+ // }
+ // just in case b introduces a lexical binding some other way, e.g., if b
+ // is a FunctionDeclaration.
Block* block =
factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
- block->statements()->Add(init, zone());
+ if (init != nullptr) {
+ block->statements()->Add(init, zone());
+ }
block->statements()->Add(loop, zone());
block->set_scope(for_scope);
loop->Initialize(NULL, cond, next, body);
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-536751.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698