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

Unified Diff: src/pattern-rewriter.cc

Issue 1139603005: [destructuring] Implement BindingArrayPattern (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebased + feedback Created 5 years, 7 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 | « src/parser.cc ('k') | test/mjsunit/harmony/destructuring.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pattern-rewriter.cc
diff --git a/src/pattern-rewriter.cc b/src/pattern-rewriter.cc
index 5871740c14bf16a064c78f9c0c05fdddc228ff80..52ff8f60f10bca57bc487195f8107e0ddea3ad8e 100644
--- a/src/pattern-rewriter.cc
+++ b/src/pattern-rewriter.cc
@@ -211,13 +211,15 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) {
auto temp_scope = descriptor_->parser->scope_->DeclarationScope();
auto temp = temp_scope->NewTemporary(ast_value_factory()->empty_string());
- auto assignment =
- factory()->NewAssignment(Token::ASSIGN, factory()->NewVariableProxy(temp),
- value, RelocInfo::kNoPosition);
+ if (value != nullptr) {
+ auto assignment = factory()->NewAssignment(
+ Token::ASSIGN, factory()->NewVariableProxy(temp), value,
+ RelocInfo::kNoPosition);
- block_->AddStatement(
- factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
- zone());
+ block_->AddStatement(
+ factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
+ zone());
+ }
return temp;
}
@@ -238,7 +240,62 @@ void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) {
void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) {
- // TODO(dslomov): implement.
+ auto iterator = CreateTempVar(
+ descriptor_->parser->GetIterator(current_value_, factory()));
+ auto done = CreateTempVar(
+ factory()->NewBooleanLiteral(false, RelocInfo::kNoPosition));
+ auto result = CreateTempVar();
+ auto v = CreateTempVar();
+ for (Expression* value : *node->values()) {
+ // if (!done) {
+ // result = IteratorNext(iterator);
+ // v = (done = result.done) ? undefined : result.value;
+ // }
+ auto next_block =
+ factory()->NewBlock(nullptr, 2, true, RelocInfo::kNoPosition);
+ next_block->AddStatement(factory()->NewExpressionStatement(
+ descriptor_->parser->BuildIteratorNextResult(
+ factory()->NewVariableProxy(iterator),
+ result, RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition),
+ zone());
+
+ auto assign_to_done = factory()->NewAssignment(
+ Token::ASSIGN, factory()->NewVariableProxy(done),
+ factory()->NewProperty(
+ factory()->NewVariableProxy(result),
+ factory()->NewStringLiteral(ast_value_factory()->done_string(),
+ RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition);
+ auto next_value = factory()->NewConditional(
+ assign_to_done, factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
+ factory()->NewProperty(
+ factory()->NewVariableProxy(result),
+ factory()->NewStringLiteral(ast_value_factory()->value_string(),
+ RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition);
+ next_block->AddStatement(
+ factory()->NewExpressionStatement(
+ factory()->NewAssignment(Token::ASSIGN,
+ factory()->NewVariableProxy(v), next_value,
+ RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition),
+ zone());
+
+ auto if_statement = factory()->NewIfStatement(
+ factory()->NewUnaryOperation(Token::NOT,
+ factory()->NewVariableProxy(done),
+ RelocInfo::kNoPosition),
+ next_block, factory()->NewEmptyStatement(RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition);
+ block_->AddStatement(if_statement, zone());
+
+ if (!(value->IsLiteral() && value->AsLiteral()->raw_value()->IsTheHole())) {
+ RecurseIntoSubpattern(value, factory()->NewVariableProxy(v));
+ }
+ }
}
« no previous file with comments | « src/parser.cc ('k') | test/mjsunit/harmony/destructuring.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698