Index: src/pattern-rewriter.cc |
diff --git a/src/pattern-rewriter.cc b/src/pattern-rewriter.cc |
index e96aef8ba2ffbad5031d69ea2c749e1394493f83..93bd2d0ee5f94e68954c28b222f95b11ec58da97 100644 |
--- a/src/pattern-rewriter.cc |
+++ b/src/pattern-rewriter.cc |
@@ -18,6 +18,9 @@ void Parser::PatternRewriter::DeclareAndInitializeVariables( |
ZoneList<const AstRawString*>* names, bool* ok) { |
PatternRewriter rewriter; |
+ rewriter.scope_ = declaration_descriptor->parser->scope_; |
+ rewriter.parser_ = declaration_descriptor->parser; |
+ rewriter.context_ = BINDING; |
rewriter.pattern_ = declaration->pattern; |
rewriter.initializer_position_ = declaration->initializer_position; |
rewriter.block_ = block; |
@@ -29,8 +32,39 @@ void Parser::PatternRewriter::DeclareAndInitializeVariables( |
} |
+void Parser::PatternRewriter::RewriteDestructuringAssignment( |
+ Parser* parser, Assignment* assignment, Scope* scope, bool* ok) { |
+ PatternRewriter rewriter; |
+ |
+ DCHECK(assignment->target()->IsAssignmentPattern()); |
+ DCHECK_EQ(assignment->op(), Token::ASSIGN); |
+ |
+ rewriter.scope_ = scope; |
+ rewriter.parser_ = parser; |
+ rewriter.context_ = ASSIGNMENT; |
+ rewriter.pattern_ = assignment->target(); |
+ rewriter.block_ = nullptr; |
+ rewriter.descriptor_ = nullptr; |
+ rewriter.names_ = nullptr; |
+ rewriter.ok_ = ok; |
+ |
+ rewriter.RecurseIntoSubpattern(rewriter.pattern_, assignment->value()); |
+} |
+ |
+ |
void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) { |
Expression* value = current_value_; |
+ |
+ if (context() == ASSIGNMENT) { |
+ // In an assignment context, simply perform the assignment |
+ Assignment* assignment = factory()->NewAssignment( |
+ Token::ASSIGN, pattern, value, pattern->position()); |
+ block_->statements()->Add( |
+ factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), |
+ zone()); |
+ return; |
+ } |
+ |
descriptor_->scope->RemoveUnresolved(pattern); |
// Declare variable. |
@@ -48,15 +82,14 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) { |
// For let/const declarations in harmony mode, we can also immediately |
// pre-resolve the proxy because it resides in the same scope as the |
// declaration. |
- Parser* parser = descriptor_->parser; |
const AstRawString* name = pattern->raw_name(); |
- VariableProxy* proxy = parser->NewUnresolved(name, descriptor_->mode); |
+ VariableProxy* proxy = parser_->NewUnresolved(name, descriptor_->mode); |
Declaration* declaration = factory()->NewVariableDeclaration( |
proxy, descriptor_->mode, descriptor_->scope, |
descriptor_->declaration_pos); |
- Variable* var = parser->Declare(declaration, descriptor_->declaration_kind, |
- descriptor_->mode != VAR, ok_, |
- descriptor_->hoist_scope); |
+ Variable* var = |
+ parser_->Declare(declaration, descriptor_->declaration_kind, |
+ descriptor_->mode != VAR, ok_, descriptor_->hoist_scope); |
if (!*ok_) return; |
DCHECK_NOT_NULL(var); |
DCHECK(!proxy->is_resolved() || proxy->var() == var); |
@@ -66,7 +99,7 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) { |
if (descriptor_->declaration_scope->num_var_or_const() > |
kMaxNumFunctionLocals) { |
- parser->ReportMessage(MessageTemplate::kTooManyVariables); |
+ parser_->ReportMessage(MessageTemplate::kTooManyVariables); |
*ok_ = false; |
return; |
} |
@@ -214,8 +247,7 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) { |
Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) { |
- auto temp = descriptor_->parser->scope_->NewTemporary( |
- ast_value_factory()->empty_string()); |
+ auto temp = scope()->NewTemporary(ast_value_factory()->empty_string()); |
if (value != nullptr) { |
auto assignment = factory()->NewAssignment( |
Token::ASSIGN, factory()->NewVariableProxy(temp), value, |
@@ -229,11 +261,11 @@ Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) { |
} |
-void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) { |
- auto temp = CreateTempVar(current_value_); |
+void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern, |
+ Variable** temp_var) { |
+ auto temp = *temp_var = CreateTempVar(current_value_); |
- block_->statements()->Add(descriptor_->parser->BuildAssertIsCoercible(temp), |
- zone()); |
+ block_->statements()->Add(parser_->BuildAssertIsCoercible(temp), zone()); |
for (ObjectLiteralProperty* property : *pattern->properties()) { |
RecurseIntoSubpattern( |
@@ -244,14 +276,14 @@ void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) { |
} |
-void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { |
- auto temp = CreateTempVar(current_value_); |
+void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node, |
+ Variable** temp_var) { |
+ auto temp = *temp_var = CreateTempVar(current_value_); |
- block_->statements()->Add(descriptor_->parser->BuildAssertIsCoercible(temp), |
- zone()); |
+ block_->statements()->Add(parser_->BuildAssertIsCoercible(temp), zone()); |
- auto iterator = CreateTempVar(descriptor_->parser->GetIterator( |
- factory()->NewVariableProxy(temp), factory())); |
+ auto iterator = CreateTempVar( |
+ parser_->GetIterator(factory()->NewVariableProxy(temp), factory())); |
auto done = CreateTempVar( |
factory()->NewBooleanLiteral(false, RelocInfo::kNoPosition)); |
auto result = CreateTempVar(); |
@@ -270,13 +302,12 @@ void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { |
// } |
auto next_block = |
factory()->NewBlock(nullptr, 2, true, RelocInfo::kNoPosition); |
- next_block->statements()->Add( |
- factory()->NewExpressionStatement( |
- descriptor_->parser->BuildIteratorNextResult( |
- factory()->NewVariableProxy(iterator), result, |
- RelocInfo::kNoPosition), |
- RelocInfo::kNoPosition), |
- zone()); |
+ next_block->statements()->Add(factory()->NewExpressionStatement( |
+ parser_->BuildIteratorNextResult( |
+ factory()->NewVariableProxy(iterator), |
+ result, RelocInfo::kNoPosition), |
+ RelocInfo::kNoPosition), |
+ zone()); |
auto assign_to_done = factory()->NewAssignment( |
Token::ASSIGN, factory()->NewVariableProxy(done), |
@@ -323,7 +354,7 @@ void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { |
empty_exprs, |
// Reuse pattern's literal index - it is unused since there is no |
// actual literal allocated. |
- node->literal_index(), is_strong(descriptor_->parser->language_mode()), |
+ node->literal_index(), is_strong(scope_->language_mode()), |
RelocInfo::kNoPosition)); |
auto arguments = new (zone()) ZoneList<Expression*>(2, zone()); |
@@ -354,6 +385,15 @@ void Parser::PatternRewriter::VisitAssignment(Assignment* node) { |
// becomes |
// temp = <value>; |
// <pattern> = temp === undefined ? <init> : temp; |
+ PatternContext old_context = context(); |
+ if (old_context == BINDING) { |
+ set_context(INITIALIZER); |
+ } else { |
+ set_context(ASSIGNMENT); |
+ RecurseIntoSubpattern(node->target(), node->value()); |
+ return set_context(old_context); |
+ } |
+ |
DCHECK(node->op() == Token::ASSIGN); |
auto temp = CreateTempVar(current_value_); |
Expression* is_undefined = factory()->NewCompareOperation( |
@@ -372,8 +412,43 @@ void Parser::PatternRewriter::VisitAssignment(Assignment* node) { |
is_undefined, initializer, factory()->NewVariableProxy(temp), |
RelocInfo::kNoPosition); |
RecurseIntoSubpattern(node->target(), value); |
+ set_context(old_context); |
+} |
+ |
+ |
+void Parser::PatternRewriter::VisitAssignmentPattern(AssignmentPattern* node) { |
+ if (node->is_rewritten()) return; |
+ |
+ Block* old_block = block_; |
+ int pos = node->position(); |
+ block_ = factory()->NewBlock(nullptr, 8, false, pos); |
+ Variable* temp = nullptr; |
+ if (node->pattern()->IsObjectLiteral()) { |
+ VisitObjectLiteral(node->pattern()->AsObjectLiteral(), &temp); |
+ } else if (node->pattern()->IsArrayLiteral()) { |
+ VisitArrayLiteral(node->pattern()->AsArrayLiteral(), &temp); |
+ } |
+ DCHECK_NOT_NULL(temp); |
+ DoExpression* expr = factory()->NewDoExpression(block_, temp, pos); |
adamk
2015/11/20 22:42:59
Looks like this creates a DoExpression even for de
|
+ node->set_expression(expr); |
+ block_ = old_block; |
+ if (block_) { |
+ block_->statements()->Add(factory()->NewExpressionStatement(expr, pos), |
+ zone()); |
+ } |
} |
+// =============== AssignmentPattern only ================== |
+ |
+void Parser::PatternRewriter::VisitProperty(v8::internal::Property* node) { |
+ DCHECK_EQ(context(), ASSIGNMENT); |
+ Expression* value = current_value_; |
+ Assignment* assignment = |
+ factory()->NewAssignment(Token::ASSIGN, node, value, node->position()); |
+ block_->statements()->Add( |
+ factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), |
+ zone()); |
+} |
// =============== UNREACHABLE ============================= |
@@ -412,7 +487,6 @@ NOT_A_PATTERN(IfStatement) |
NOT_A_PATTERN(ImportDeclaration) |
NOT_A_PATTERN(Literal) |
NOT_A_PATTERN(NativeFunctionLiteral) |
-NOT_A_PATTERN(Property) |
NOT_A_PATTERN(RegExpLiteral) |
NOT_A_PATTERN(ReturnStatement) |
NOT_A_PATTERN(SloppyBlockFunctionStatement) |