OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 2618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2629 *visit_mode = ForEachStatement::ENUMERATE; | 2629 *visit_mode = ForEachStatement::ENUMERATE; |
2630 return true; | 2630 return true; |
2631 } else if (allow_for_of() && CheckContextualKeyword(CStrVector("of"))) { | 2631 } else if (allow_for_of() && CheckContextualKeyword(CStrVector("of"))) { |
2632 *visit_mode = ForEachStatement::ITERATE; | 2632 *visit_mode = ForEachStatement::ITERATE; |
2633 return true; | 2633 return true; |
2634 } | 2634 } |
2635 return false; | 2635 return false; |
2636 } | 2636 } |
2637 | 2637 |
2638 | 2638 |
| 2639 void Parser::InitializeForEachStatement(ForEachStatement* stmt, |
| 2640 Expression* each, |
| 2641 Expression* subject, |
| 2642 Statement* body) { |
| 2643 ForOfStatement* for_of = stmt->AsForOfStatement(); |
| 2644 |
| 2645 if (for_of != NULL) { |
| 2646 Factory* heap_factory = isolate()->factory(); |
| 2647 Handle<String> iterator_str = heap_factory->InternalizeOneByteString( |
| 2648 STATIC_ASCII_VECTOR(".iterator")); |
| 2649 Handle<String> result_str = heap_factory->InternalizeOneByteString( |
| 2650 STATIC_ASCII_VECTOR(".result")); |
| 2651 Variable* iterator = |
| 2652 top_scope_->DeclarationScope()->NewTemporary(iterator_str); |
| 2653 Variable* result = top_scope_->DeclarationScope()->NewTemporary(result_str); |
| 2654 |
| 2655 Expression* assign_iterator; |
| 2656 Expression* next_result; |
| 2657 Expression* result_done; |
| 2658 Expression* assign_each; |
| 2659 |
| 2660 // var iterator = iterable; |
| 2661 { |
| 2662 Expression* iterator_proxy = factory()->NewVariableProxy(iterator); |
| 2663 assign_iterator = factory()->NewAssignment( |
| 2664 Token::ASSIGN, iterator_proxy, subject, RelocInfo::kNoPosition); |
| 2665 } |
| 2666 |
| 2667 // var result = iterator.next(); |
| 2668 { |
| 2669 Expression* iterator_proxy = factory()->NewVariableProxy(iterator); |
| 2670 Expression* next_literal = |
| 2671 factory()->NewLiteral(heap_factory->next_string()); |
| 2672 Expression* next_property = factory()->NewProperty( |
| 2673 iterator_proxy, next_literal, RelocInfo::kNoPosition); |
| 2674 ZoneList<Expression*>* next_arguments = |
| 2675 new(zone()) ZoneList<Expression*>(0, zone()); |
| 2676 Expression* next_call = factory()->NewCall( |
| 2677 next_property, next_arguments, RelocInfo::kNoPosition); |
| 2678 Expression* result_proxy = factory()->NewVariableProxy(result); |
| 2679 next_result = factory()->NewAssignment( |
| 2680 Token::ASSIGN, result_proxy, next_call, RelocInfo::kNoPosition); |
| 2681 } |
| 2682 |
| 2683 // result.done |
| 2684 { |
| 2685 Expression* done_literal = |
| 2686 factory()->NewLiteral(heap_factory->done_string()); |
| 2687 Expression* result_proxy = factory()->NewVariableProxy(result); |
| 2688 result_done = factory()->NewProperty( |
| 2689 result_proxy, done_literal, RelocInfo::kNoPosition); |
| 2690 } |
| 2691 |
| 2692 // each = result.value |
| 2693 { |
| 2694 Expression* value_literal = |
| 2695 factory()->NewLiteral(heap_factory->value_string()); |
| 2696 Expression* result_proxy = factory()->NewVariableProxy(result); |
| 2697 Expression* result_value = factory()->NewProperty( |
| 2698 result_proxy, value_literal, RelocInfo::kNoPosition); |
| 2699 assign_each = factory()->NewAssignment( |
| 2700 Token::ASSIGN, each, result_value, RelocInfo::kNoPosition); |
| 2701 } |
| 2702 |
| 2703 for_of->Initialize(each, subject, body, |
| 2704 assign_iterator, next_result, result_done, assign_each); |
| 2705 } else { |
| 2706 stmt->Initialize(each, subject, body); |
| 2707 } |
| 2708 } |
| 2709 |
| 2710 |
2639 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) { | 2711 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) { |
2640 // ForStatement :: | 2712 // ForStatement :: |
2641 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement | 2713 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement |
2642 | 2714 |
2643 Statement* init = NULL; | 2715 Statement* init = NULL; |
2644 | 2716 |
2645 // Create an in-between scope for let-bound iteration variables. | 2717 // Create an in-between scope for let-bound iteration variables. |
2646 Scope* saved_scope = top_scope_; | 2718 Scope* saved_scope = top_scope_; |
2647 Scope* for_scope = NewScope(top_scope_, BLOCK_SCOPE); | 2719 Scope* for_scope = NewScope(top_scope_, BLOCK_SCOPE); |
2648 top_scope_ = for_scope; | 2720 top_scope_ = for_scope; |
(...skipping 14 matching lines...) Expand all Loading... |
2663 is_const ? Interface::NewConst() : Interface::NewValue(); | 2735 is_const ? Interface::NewConst() : Interface::NewValue(); |
2664 ForEachStatement* loop = factory()->NewForEachStatement(mode, labels); | 2736 ForEachStatement* loop = factory()->NewForEachStatement(mode, labels); |
2665 Target target(&this->target_stack_, loop); | 2737 Target target(&this->target_stack_, loop); |
2666 | 2738 |
2667 Expression* enumerable = ParseExpression(true, CHECK_OK); | 2739 Expression* enumerable = ParseExpression(true, CHECK_OK); |
2668 Expect(Token::RPAREN, CHECK_OK); | 2740 Expect(Token::RPAREN, CHECK_OK); |
2669 | 2741 |
2670 VariableProxy* each = | 2742 VariableProxy* each = |
2671 top_scope_->NewUnresolved(factory(), name, interface); | 2743 top_scope_->NewUnresolved(factory(), name, interface); |
2672 Statement* body = ParseStatement(NULL, CHECK_OK); | 2744 Statement* body = ParseStatement(NULL, CHECK_OK); |
2673 loop->Initialize(each, enumerable, body); | 2745 InitializeForEachStatement(loop, each, enumerable, body); |
2674 Block* result = factory()->NewBlock(NULL, 2, false); | 2746 Block* result = factory()->NewBlock(NULL, 2, false); |
2675 result->AddStatement(variable_statement, zone()); | 2747 result->AddStatement(variable_statement, zone()); |
2676 result->AddStatement(loop, zone()); | 2748 result->AddStatement(loop, zone()); |
2677 top_scope_ = saved_scope; | 2749 top_scope_ = saved_scope; |
2678 for_scope->set_end_position(scanner().location().end_pos); | 2750 for_scope->set_end_position(scanner().location().end_pos); |
2679 for_scope = for_scope->FinalizeBlockScope(); | 2751 for_scope = for_scope->FinalizeBlockScope(); |
2680 ASSERT(for_scope == NULL); | 2752 ASSERT(for_scope == NULL); |
2681 // Parsed for-in loop w/ variable/const declaration. | 2753 // Parsed for-in loop w/ variable/const declaration. |
2682 return result; | 2754 return result; |
2683 } else { | 2755 } else { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2727 top_scope_->NewUnresolved(factory(), name, Interface::NewValue()); | 2799 top_scope_->NewUnresolved(factory(), name, Interface::NewValue()); |
2728 Statement* body = ParseStatement(NULL, CHECK_OK); | 2800 Statement* body = ParseStatement(NULL, CHECK_OK); |
2729 Block* body_block = factory()->NewBlock(NULL, 3, false); | 2801 Block* body_block = factory()->NewBlock(NULL, 3, false); |
2730 Assignment* assignment = factory()->NewAssignment( | 2802 Assignment* assignment = factory()->NewAssignment( |
2731 Token::ASSIGN, each, temp_proxy, RelocInfo::kNoPosition); | 2803 Token::ASSIGN, each, temp_proxy, RelocInfo::kNoPosition); |
2732 Statement* assignment_statement = | 2804 Statement* assignment_statement = |
2733 factory()->NewExpressionStatement(assignment); | 2805 factory()->NewExpressionStatement(assignment); |
2734 body_block->AddStatement(variable_statement, zone()); | 2806 body_block->AddStatement(variable_statement, zone()); |
2735 body_block->AddStatement(assignment_statement, zone()); | 2807 body_block->AddStatement(assignment_statement, zone()); |
2736 body_block->AddStatement(body, zone()); | 2808 body_block->AddStatement(body, zone()); |
2737 loop->Initialize(temp_proxy, enumerable, body_block); | 2809 InitializeForEachStatement(loop, temp_proxy, enumerable, body_block); |
2738 top_scope_ = saved_scope; | 2810 top_scope_ = saved_scope; |
2739 for_scope->set_end_position(scanner().location().end_pos); | 2811 for_scope->set_end_position(scanner().location().end_pos); |
2740 for_scope = for_scope->FinalizeBlockScope(); | 2812 for_scope = for_scope->FinalizeBlockScope(); |
2741 body_block->set_scope(for_scope); | 2813 body_block->set_scope(for_scope); |
2742 // Parsed for-in loop w/ let declaration. | 2814 // Parsed for-in loop w/ let declaration. |
2743 return loop; | 2815 return loop; |
2744 | 2816 |
2745 } else { | 2817 } else { |
2746 init = variable_statement; | 2818 init = variable_statement; |
2747 } | 2819 } |
(...skipping 11 matching lines...) Expand all Loading... |
2759 isolate()->factory()->invalid_lhs_in_for_in_string(); | 2831 isolate()->factory()->invalid_lhs_in_for_in_string(); |
2760 expression = NewThrowReferenceError(message); | 2832 expression = NewThrowReferenceError(message); |
2761 } | 2833 } |
2762 ForEachStatement* loop = factory()->NewForEachStatement(mode, labels); | 2834 ForEachStatement* loop = factory()->NewForEachStatement(mode, labels); |
2763 Target target(&this->target_stack_, loop); | 2835 Target target(&this->target_stack_, loop); |
2764 | 2836 |
2765 Expression* enumerable = ParseExpression(true, CHECK_OK); | 2837 Expression* enumerable = ParseExpression(true, CHECK_OK); |
2766 Expect(Token::RPAREN, CHECK_OK); | 2838 Expect(Token::RPAREN, CHECK_OK); |
2767 | 2839 |
2768 Statement* body = ParseStatement(NULL, CHECK_OK); | 2840 Statement* body = ParseStatement(NULL, CHECK_OK); |
2769 loop->Initialize(expression, enumerable, body); | 2841 InitializeForEachStatement(loop, expression, enumerable, body); |
2770 top_scope_ = saved_scope; | 2842 top_scope_ = saved_scope; |
2771 for_scope->set_end_position(scanner().location().end_pos); | 2843 for_scope->set_end_position(scanner().location().end_pos); |
2772 for_scope = for_scope->FinalizeBlockScope(); | 2844 for_scope = for_scope->FinalizeBlockScope(); |
2773 ASSERT(for_scope == NULL); | 2845 ASSERT(for_scope == NULL); |
2774 // Parsed for-in loop. | 2846 // Parsed for-in loop. |
2775 return loop; | 2847 return loop; |
2776 | 2848 |
2777 } else { | 2849 } else { |
2778 init = factory()->NewExpressionStatement(expression); | 2850 init = factory()->NewExpressionStatement(expression); |
2779 } | 2851 } |
(...skipping 3078 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5858 ASSERT(info()->isolate()->has_pending_exception()); | 5930 ASSERT(info()->isolate()->has_pending_exception()); |
5859 } else { | 5931 } else { |
5860 result = ParseProgram(); | 5932 result = ParseProgram(); |
5861 } | 5933 } |
5862 } | 5934 } |
5863 info()->SetFunction(result); | 5935 info()->SetFunction(result); |
5864 return (result != NULL); | 5936 return (result != NULL); |
5865 } | 5937 } |
5866 | 5938 |
5867 } } // namespace v8::internal | 5939 } } // namespace v8::internal |
OLD | NEW |