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

Side by Side Diff: src/pattern-rewriter.cc

Issue 1146683002: [destructuring] Implement initializers in patterns. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebased onto ToT 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 unified diff | Download patch
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 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/ast.h" 5 #include "src/ast.h"
6 #include "src/messages.h" 6 #include "src/messages.h"
7 #include "src/parser.h" 7 #include "src/parser.h"
8 8
9 namespace v8 { 9 namespace v8 {
10 10
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 VariableProxy* proxy = initialization_scope->NewUnresolved(factory(), name); 201 VariableProxy* proxy = initialization_scope->NewUnresolved(factory(), name);
202 Assignment* assignment = factory()->NewAssignment( 202 Assignment* assignment = factory()->NewAssignment(
203 descriptor_->init_op, proxy, value, descriptor_->pos); 203 descriptor_->init_op, proxy, value, descriptor_->pos);
204 block_->AddStatement( 204 block_->AddStatement(
205 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), 205 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
206 zone()); 206 zone());
207 } 207 }
208 } 208 }
209 209
210 210
211 Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) {
212 auto temp_scope = descriptor_->parser->scope_->DeclarationScope();
213 auto temp = temp_scope->NewTemporary(ast_value_factory()->empty_string());
214 auto assignment =
215 factory()->NewAssignment(Token::ASSIGN, factory()->NewVariableProxy(temp),
216 value, RelocInfo::kNoPosition);
217
218 block_->AddStatement(
219 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
220 zone());
221 return temp;
222 }
223
224
211 void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) { 225 void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) {
212 auto temp = TemporaryDeclarationScope()->NewTemporary( 226 auto temp = CreateTempVar(current_value_);
213 ast_value_factory()->empty_string());
214 auto assignment =
215 factory()->NewAssignment(Token::ASSIGN, factory()->NewVariableProxy(temp),
216 current_value_, RelocInfo::kNoPosition);
217
218 block_->AddStatement(
219 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
220 zone());
221
222 if (pattern->properties()->length() == 0) { 227 if (pattern->properties()->length() == 0) {
223 block_->AddStatement(descriptor_->parser->BuildAssertIsCoercible(temp), 228 block_->AddStatement(descriptor_->parser->BuildAssertIsCoercible(temp),
224 zone()); 229 zone());
225 } 230 }
226 231
227 for (ObjectLiteralProperty* property : *pattern->properties()) { 232 for (ObjectLiteralProperty* property : *pattern->properties()) {
228 // TODO(dslomov): computed property names. 233 // TODO(dslomov): computed property names.
229 RecurseIntoSubpattern( 234 RecurseIntoSubpattern(
230 property->value(), 235 property->value(),
231 factory()->NewProperty(factory()->NewVariableProxy(temp), 236 factory()->NewProperty(factory()->NewVariableProxy(temp),
232 property->key(), RelocInfo::kNoPosition)); 237 property->key(), RelocInfo::kNoPosition));
233 } 238 }
234 } 239 }
235 240
236 241
237 void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { 242 void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) {
238 // TODO(dslomov): implement. 243 // TODO(dslomov): implement.
239 } 244 }
240 245
241 246
242 void Parser::PatternRewriter::VisitAssignment(Assignment* node) { 247 void Parser::PatternRewriter::VisitAssignment(Assignment* node) {
243 // TODO(dslomov): implement. 248 // let {<pattern> = <init>} = <value>
249 // becomes
250 // temp = <value>;
251 // <pattern> = temp === undefined ? <init> : temp;
252 DCHECK(node->op() == Token::ASSIGN);
253 auto temp = CreateTempVar(current_value_);
254 Expression* is_undefined = factory()->NewCompareOperation(
255 Token::EQ_STRICT, factory()->NewVariableProxy(temp),
256 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
257 RelocInfo::kNoPosition);
258 Expression* value = factory()->NewConditional(
259 is_undefined, node->value(), factory()->NewVariableProxy(temp),
260 RelocInfo::kNoPosition);
261 RecurseIntoSubpattern(node->target(), value);
244 } 262 }
245 263
246 264
247 void Parser::PatternRewriter::VisitSpread(Spread* node) { 265 void Parser::PatternRewriter::VisitSpread(Spread* node) {
248 // TODO(dslomov): implement. 266 // TODO(dslomov): implement.
249 } 267 }
250 268
251 269
252 // =============== UNREACHABLE ============================= 270 // =============== UNREACHABLE =============================
253 271
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 NOT_A_PATTERN(TryFinallyStatement) 313 NOT_A_PATTERN(TryFinallyStatement)
296 NOT_A_PATTERN(UnaryOperation) 314 NOT_A_PATTERN(UnaryOperation)
297 NOT_A_PATTERN(VariableDeclaration) 315 NOT_A_PATTERN(VariableDeclaration)
298 NOT_A_PATTERN(WhileStatement) 316 NOT_A_PATTERN(WhileStatement)
299 NOT_A_PATTERN(WithStatement) 317 NOT_A_PATTERN(WithStatement)
300 NOT_A_PATTERN(Yield) 318 NOT_A_PATTERN(Yield)
301 319
302 #undef NOT_A_PATTERN 320 #undef NOT_A_PATTERN
303 } 321 }
304 } // namespace v8::internal 322 } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698