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

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: Comments addressed 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/ast-value-factory.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 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 = [];
309 // if (!done) $concatIterableToArray(array, iterator);
310 auto empty_exprs = new (zone()) ZoneList<Expression*>(0, zone());
311 auto array = CreateTempVar(factory()->NewArrayLiteral(
312 empty_exprs,
313 // Reuse pattern's literal index - it is unused since there is no
arv (Not doing code reviews) 2015/05/20 14:40:50 nice.
314 // actual literal allocated.
315 node->literal_index(), is_strong(descriptor_->parser->language_mode()),
316 RelocInfo::kNoPosition));
317
318 auto arguments = new (zone()) ZoneList<Expression*>(2, zone());
319 arguments->Add(factory()->NewVariableProxy(array), zone());
320 arguments->Add(factory()->NewVariableProxy(iterator), zone());
321 auto spread_into_array_call = factory()->NewCallRuntime(
322 ast_value_factory()->concat_iterable_to_array_string(), nullptr,
323 arguments, RelocInfo::kNoPosition);
324
325 auto if_statement = factory()->NewIfStatement(
326 factory()->NewUnaryOperation(Token::NOT,
327 factory()->NewVariableProxy(done),
328 RelocInfo::kNoPosition),
329 factory()->NewExpressionStatement(spread_into_array_call,
330 RelocInfo::kNoPosition),
331 factory()->NewEmptyStatement(RelocInfo::kNoPosition),
332 RelocInfo::kNoPosition);
333 block_->AddStatement(if_statement, zone());
334
335
336 RecurseIntoSubpattern(spread->expression(),
337 factory()->NewVariableProxy(array));
338 }
299 } 339 }
300 340
301 341
302 void Parser::PatternRewriter::VisitAssignment(Assignment* node) { 342 void Parser::PatternRewriter::VisitAssignment(Assignment* node) {
303 // let {<pattern> = <init>} = <value> 343 // let {<pattern> = <init>} = <value>
304 // becomes 344 // becomes
305 // temp = <value>; 345 // temp = <value>;
306 // <pattern> = temp === undefined ? <init> : temp; 346 // <pattern> = temp === undefined ? <init> : temp;
307 DCHECK(node->op() == Token::ASSIGN); 347 DCHECK(node->op() == Token::ASSIGN);
308 auto temp = CreateTempVar(current_value_); 348 auto temp = CreateTempVar(current_value_);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 NOT_A_PATTERN(TryFinallyStatement) 408 NOT_A_PATTERN(TryFinallyStatement)
369 NOT_A_PATTERN(UnaryOperation) 409 NOT_A_PATTERN(UnaryOperation)
370 NOT_A_PATTERN(VariableDeclaration) 410 NOT_A_PATTERN(VariableDeclaration)
371 NOT_A_PATTERN(WhileStatement) 411 NOT_A_PATTERN(WhileStatement)
372 NOT_A_PATTERN(WithStatement) 412 NOT_A_PATTERN(WithStatement)
373 NOT_A_PATTERN(Yield) 413 NOT_A_PATTERN(Yield)
374 414
375 #undef NOT_A_PATTERN 415 #undef NOT_A_PATTERN
376 } 416 }
377 } // namespace v8::internal 417 } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/ast-value-factory.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698