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

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

Issue 1151503002: [destructuring] Implement spread binding patterns. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: 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
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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 } 239 }
240 240
241 241
242 void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { 242 void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) {
243 auto iterator = CreateTempVar( 243 auto iterator = CreateTempVar(
244 descriptor_->parser->GetIterator(current_value_, factory())); 244 descriptor_->parser->GetIterator(current_value_, factory()));
245 auto done = CreateTempVar( 245 auto done = CreateTempVar(
246 factory()->NewBooleanLiteral(false, RelocInfo::kNoPosition)); 246 factory()->NewBooleanLiteral(false, RelocInfo::kNoPosition));
247 auto result = CreateTempVar(); 247 auto result = CreateTempVar();
248 auto v = CreateTempVar(); 248 auto v = CreateTempVar();
249
250 Spread* spread = nullptr;
249 for (Expression* value : *node->values()) { 251 for (Expression* value : *node->values()) {
252 if (value->IsSpread()) {
253 spread = value->AsSpread();
254 break;
255 }
256
250 // if (!done) { 257 // if (!done) {
251 // result = IteratorNext(iterator); 258 // result = IteratorNext(iterator);
252 // v = (done = result.done) ? undefined : result.value; 259 // v = (done = result.done) ? undefined : result.value;
253 // } 260 // }
254 auto next_block = 261 auto next_block =
255 factory()->NewBlock(nullptr, 2, true, RelocInfo::kNoPosition); 262 factory()->NewBlock(nullptr, 2, true, RelocInfo::kNoPosition);
256 next_block->AddStatement(factory()->NewExpressionStatement( 263 next_block->AddStatement(factory()->NewExpressionStatement(
257 descriptor_->parser->BuildIteratorNextResult( 264 descriptor_->parser->BuildIteratorNextResult(
258 factory()->NewVariableProxy(iterator), 265 factory()->NewVariableProxy(iterator),
259 result, RelocInfo::kNoPosition), 266 result, RelocInfo::kNoPosition),
(...skipping 29 matching lines...) Expand all
289 factory()->NewVariableProxy(done), 296 factory()->NewVariableProxy(done),
290 RelocInfo::kNoPosition), 297 RelocInfo::kNoPosition),
291 next_block, factory()->NewEmptyStatement(RelocInfo::kNoPosition), 298 next_block, factory()->NewEmptyStatement(RelocInfo::kNoPosition),
292 RelocInfo::kNoPosition); 299 RelocInfo::kNoPosition);
293 block_->AddStatement(if_statement, zone()); 300 block_->AddStatement(if_statement, zone());
294 301
295 if (!(value->IsLiteral() && value->AsLiteral()->raw_value()->IsTheHole())) { 302 if (!(value->IsLiteral() && value->AsLiteral()->raw_value()->IsTheHole())) {
296 RecurseIntoSubpattern(value, factory()->NewVariableProxy(v)); 303 RecurseIntoSubpattern(value, factory()->NewVariableProxy(v));
297 } 304 }
298 } 305 }
306
307 if (spread != nullptr) {
308 // array = [];
arv (Not doing code reviews) 2015/05/20 13:58:05 Lets change this to not create the array here and
Dmitry Lomov (no reviews) 2015/05/20 14:07:19 We definitely want to create an array here, to ben
309 // if (!done) $spreadIntoArray(array, iterator);
310 auto empty_exprs = new (zone()) ZoneList<Expression*>(0, zone());
311 auto array = CreateTempVar(factory()->NewArrayLiteral(
312 empty_exprs, spread->literal_index(),
313 is_strong(descriptor_->parser->language_mode()),
314 RelocInfo::kNoPosition));
315
316 auto arguments = new (zone()) ZoneList<Expression*>(2, zone());
317 arguments->Add(factory()->NewVariableProxy(array), zone());
318 arguments->Add(factory()->NewVariableProxy(iterator), zone());
319 auto spread_into_array_call = factory()->NewCallRuntime(
320 ast_value_factory()->spread_into_array_string(), nullptr, arguments,
321 RelocInfo::kNoPosition);
322
323 auto if_statement = factory()->NewIfStatement(
324 factory()->NewUnaryOperation(Token::NOT,
325 factory()->NewVariableProxy(done),
326 RelocInfo::kNoPosition),
327 factory()->NewExpressionStatement(spread_into_array_call,
328 RelocInfo::kNoPosition),
329 factory()->NewEmptyStatement(RelocInfo::kNoPosition),
330 RelocInfo::kNoPosition);
331 block_->AddStatement(if_statement, zone());
332
333
334 RecurseIntoSubpattern(spread->expression(),
335 factory()->NewVariableProxy(array));
336 }
299 } 337 }
300 338
301 339
302 void Parser::PatternRewriter::VisitAssignment(Assignment* node) { 340 void Parser::PatternRewriter::VisitAssignment(Assignment* node) {
303 // let {<pattern> = <init>} = <value> 341 // let {<pattern> = <init>} = <value>
304 // becomes 342 // becomes
305 // temp = <value>; 343 // temp = <value>;
306 // <pattern> = temp === undefined ? <init> : temp; 344 // <pattern> = temp === undefined ? <init> : temp;
307 DCHECK(node->op() == Token::ASSIGN); 345 DCHECK(node->op() == Token::ASSIGN);
308 auto temp = CreateTempVar(current_value_); 346 auto temp = CreateTempVar(current_value_);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 NOT_A_PATTERN(TryFinallyStatement) 406 NOT_A_PATTERN(TryFinallyStatement)
369 NOT_A_PATTERN(UnaryOperation) 407 NOT_A_PATTERN(UnaryOperation)
370 NOT_A_PATTERN(VariableDeclaration) 408 NOT_A_PATTERN(VariableDeclaration)
371 NOT_A_PATTERN(WhileStatement) 409 NOT_A_PATTERN(WhileStatement)
372 NOT_A_PATTERN(WithStatement) 410 NOT_A_PATTERN(WithStatement)
373 NOT_A_PATTERN(Yield) 411 NOT_A_PATTERN(Yield)
374 412
375 #undef NOT_A_PATTERN 413 #undef NOT_A_PATTERN
376 } 414 }
377 } // namespace v8::internal 415 } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698