Commit 8925b848 authored by dslomov's avatar dslomov Committed by Commit bot

[destructuring] Implement pattern matching in lexcal for-of/for-in.

R=arv@chromium.org,rossberg@chromium.org
BUG=v8:811
LOG=N

Review URL: https://codereview.chromium.org/1152503002

Cr-Commit-Position: refs/heads/master@{#28547}
parent 16484824
......@@ -3484,8 +3484,6 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
is_const = peek() == Token::CONST;
ParseVariableDeclarations(kForStatement, &parsing_result, CHECK_OK);
DCHECK(parsing_result.descriptor.pos != RelocInfo::kNoPosition);
Block* variable_statement =
parsing_result.BuildInitializationBlock(&lexical_bindings, CHECK_OK);
int num_decl = parsing_result.declarations.length();
bool accept_IN = num_decl >= 1;
......@@ -3546,19 +3544,23 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
scope_ = for_scope;
Expect(Token::RPAREN, CHECK_OK);
VariableProxy* each =
scope_->NewUnresolved(factory(), parsing_result.SingleName(),
Variable::NORMAL, each_end_pos);
Statement* body = ParseSubStatement(NULL, CHECK_OK);
Block* body_block =
factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition);
Token::Value init_op = is_const ? Token::INIT_CONST : Token::ASSIGN;
Assignment* assignment = factory()->NewAssignment(
init_op, each, temp_proxy, RelocInfo::kNoPosition);
Statement* assignment_statement = factory()->NewExpressionStatement(
assignment, RelocInfo::kNoPosition);
body_block->AddStatement(variable_statement, zone());
body_block->AddStatement(assignment_statement, zone());
auto each_initialization_block = factory()->NewBlock(
nullptr, 1, true, parsing_result.descriptor.pos);
{
DCHECK(parsing_result.declarations.length() == 1);
DeclarationParsingResult::Declaration decl =
parsing_result.declarations[0];
decl.initializer = temp_proxy;
PatternRewriter::DeclareAndInitializeVariables(
each_initialization_block, &parsing_result.descriptor, &decl,
&lexical_bindings, CHECK_OK);
}
body_block->AddStatement(each_initialization_block, zone());
body_block->AddStatement(body, zone());
InitializeForEachStatement(loop, temp_proxy, enumerable, body_block);
scope_ = saved_scope;
......@@ -3567,9 +3569,9 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
body_block->set_scope(for_scope);
// Parsed for-in loop w/ let declaration.
return loop;
} else {
init = variable_statement;
init = parsing_result.BuildInitializationBlock(&lexical_bindings,
CHECK_OK);
}
} else {
Scanner::Location lhs_location = scanner()->peek_location();
......
......@@ -623,3 +623,36 @@
assertArrayEquals(["1", "2"], log);
}());
}());
(function TestForEachLexical() {
'use strict';
let a = [{x:1, y:-1}, {x:2,y:-2}, {x:3,y:-3}];
let sumX = 0;
let sumY = 0;
let fs = [];
for (let {x,y} of a) {
sumX += x;
sumY += y;
fs.push({fx : function() { return x; }, fy : function() { return y }});
}
assertSame(6, sumX);
assertSame(-6, sumY);
assertSame(3, fs.length);
for (let i = 0; i < fs.length; i++) {
let {fx,fy} = fs[i];
assertSame(i+1, fx());
assertSame(-(i+1), fy());
}
var o = { 'a1':1, 'b2':2 };
o.__proto__ = null;
let sx = '';
let sy = '';
for (let [x,y] in o) {
sx += x;
sy += y;
}
assertEquals('ab', sx);
assertEquals('12', sy);
}());
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment