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

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

Issue 1852703002: Fix treatment of rest pattern in array destructuring. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase Created 4 years, 8 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/js/runtime.js ('k') | test/cctest/interpreter/bytecode_expectations/CallRuntime.golden » ('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/ast.h" 5 #include "src/ast/ast.h"
6 #include "src/messages.h" 6 #include "src/messages.h"
7 #include "src/parsing/parameter-initializer-rewriter.h" 7 #include "src/parsing/parameter-initializer-rewriter.h"
8 #include "src/parsing/parser.h" 8 #include "src/parsing/parser.h"
9 9
10 namespace v8 { 10 namespace v8 {
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 } 540 }
541 } 541 }
542 set_context(context); 542 set_context(context);
543 } 543 }
544 544
545 if (spread != nullptr) { 545 if (spread != nullptr) {
546 // A spread can only occur as the last component. It is not handled by 546 // A spread can only occur as the last component. It is not handled by
547 // RecurseIntoSubpattern above. 547 // RecurseIntoSubpattern above.
548 548
549 // let array = []; 549 // let array = [];
550 // if (!done) %concat_iterable_to_array(array, iterator); 550 // while (!done) {
551 // result = IteratorNext(iterator);
552 // if (result.done) {
553 // done = true;
554 // } else {
555 // %AppendElement(array, result.value);
556 // }
557 // }
558
559 // let array = [];
560 Variable* array;
561 {
562 auto empty_exprs = new (zone()) ZoneList<Expression*>(0, zone());
563 array = CreateTempVar(factory()->NewArrayLiteral(
564 empty_exprs,
565 // Reuse pattern's literal index - it is unused since there is no
566 // actual literal allocated.
567 node->literal_index(), RelocInfo::kNoPosition));
568 }
569
570 // result = IteratorNext(iterator);
571 Statement* get_next = factory()->NewExpressionStatement(
572 parser_->BuildIteratorNextResult(factory()->NewVariableProxy(iterator),
573 result, nopos),
574 nopos);
575
551 // done = true; 576 // done = true;
577 Statement* set_done = factory()->NewExpressionStatement(
578 factory()->NewAssignment(
579 Token::ASSIGN, factory()->NewVariableProxy(done),
580 factory()->NewBooleanLiteral(true, nopos), nopos),
581 nopos);
552 582
553 auto empty_exprs = new (zone()) ZoneList<Expression*>(0, zone()); 583 // %AppendElement(array, result.value);
554 auto array = CreateTempVar(factory()->NewArrayLiteral( 584 Statement* append_element;
555 empty_exprs, 585 {
556 // Reuse pattern's literal index - it is unused since there is no 586 auto args = new (zone()) ZoneList<Expression*>(2, zone());
557 // actual literal allocated. 587 args->Add(factory()->NewVariableProxy(array), zone());
558 node->literal_index(), RelocInfo::kNoPosition)); 588 args->Add(factory()->NewProperty(
589 factory()->NewVariableProxy(result),
590 factory()->NewStringLiteral(
591 ast_value_factory()->value_string(), nopos),
592 nopos),
593 zone());
594 append_element = factory()->NewExpressionStatement(
595 factory()->NewCallRuntime(Runtime::kAppendElement, args, nopos),
596 nopos);
597 }
559 598
560 auto arguments = new (zone()) ZoneList<Expression*>(2, zone()); 599 // if (result.done) { #set_done } else { #append_element }
561 arguments->Add(factory()->NewVariableProxy(array), zone()); 600 Statement* set_done_or_append;
562 arguments->Add(factory()->NewVariableProxy(iterator), zone()); 601 {
563 auto spread_into_array_call = 602 Expression* result_done =
564 factory()->NewCallRuntime(Context::CONCAT_ITERABLE_TO_ARRAY_INDEX, 603 factory()->NewProperty(factory()->NewVariableProxy(result),
565 arguments, RelocInfo::kNoPosition); 604 factory()->NewStringLiteral(
605 ast_value_factory()->done_string(), nopos),
606 nopos);
607 set_done_or_append = factory()->NewIfStatement(result_done, set_done,
608 append_element, nopos);
609 }
566 610
567 auto if_statement = factory()->NewIfStatement( 611 // while (!done) {
568 factory()->NewUnaryOperation(Token::NOT, 612 // #get_next;
569 factory()->NewVariableProxy(done), 613 // #set_done_or_append;
570 RelocInfo::kNoPosition), 614 // }
571 factory()->NewExpressionStatement(spread_into_array_call, 615 WhileStatement* loop = factory()->NewWhileStatement(nullptr, nopos);
572 RelocInfo::kNoPosition), 616 {
573 factory()->NewEmptyStatement(RelocInfo::kNoPosition), 617 Expression* condition = factory()->NewUnaryOperation(
574 RelocInfo::kNoPosition); 618 Token::NOT, factory()->NewVariableProxy(done), nopos);
575 block_->statements()->Add(if_statement, zone()); 619 Block* body = factory()->NewBlock(nullptr, 2, true, nopos);
620 body->statements()->Add(get_next, zone());
621 body->statements()->Add(set_done_or_append, zone());
622 loop->Initialize(condition, body);
623 }
576 624
577 auto set_done = factory()->NewAssignment( 625 block_->statements()->Add(loop, zone());
578 Token::ASSIGN, factory()->NewVariableProxy(done),
579 factory()->NewBooleanLiteral(true, nopos), nopos);
580 block_->statements()->Add(
581 factory()->NewExpressionStatement(set_done, nopos), zone());
582
583 RecurseIntoSubpattern(spread->expression(), 626 RecurseIntoSubpattern(spread->expression(),
584 factory()->NewVariableProxy(array)); 627 factory()->NewVariableProxy(array));
585 } 628 }
586 629
587 if (FLAG_harmony_iterator_close) { 630 if (FLAG_harmony_iterator_close) {
588 Expression* closing_condition = factory()->NewUnaryOperation( 631 Expression* closing_condition = factory()->NewUnaryOperation(
589 Token::NOT, factory()->NewVariableProxy(done), nopos); 632 Token::NOT, factory()->NewVariableProxy(done), nopos);
590 parser_->FinalizeIteratorUse(completion, closing_condition, iterator, 633 parser_->FinalizeIteratorUse(completion, closing_condition, iterator,
591 block_, target); 634 block_, target);
592 block_ = target; 635 block_ = target;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 NOT_A_PATTERN(TryFinallyStatement) 742 NOT_A_PATTERN(TryFinallyStatement)
700 NOT_A_PATTERN(UnaryOperation) 743 NOT_A_PATTERN(UnaryOperation)
701 NOT_A_PATTERN(VariableDeclaration) 744 NOT_A_PATTERN(VariableDeclaration)
702 NOT_A_PATTERN(WhileStatement) 745 NOT_A_PATTERN(WhileStatement)
703 NOT_A_PATTERN(WithStatement) 746 NOT_A_PATTERN(WithStatement)
704 NOT_A_PATTERN(Yield) 747 NOT_A_PATTERN(Yield)
705 748
706 #undef NOT_A_PATTERN 749 #undef NOT_A_PATTERN
707 } // namespace internal 750 } // namespace internal
708 } // namespace v8 751 } // namespace v8
OLDNEW
« no previous file with comments | « src/js/runtime.js ('k') | test/cctest/interpreter/bytecode_expectations/CallRuntime.golden » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698