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

Side by Side Diff: src/parser.cc

Issue 292743009: Make let variables fresh in each iteration of a for-loop. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address comments Created 6 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 | Annotate | Revision Log
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/harmony/block-for.js » ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 "v8.h" 5 #include "v8.h"
6 6
7 #include "api.h" 7 #include "api.h"
8 #include "ast.h" 8 #include "ast.h"
9 #include "bootstrapper.h" 9 #include "bootstrapper.h"
10 #include "char-predicates-inl.h" 10 #include "char-predicates-inl.h"
(...skipping 2819 matching lines...) Expand 10 before | Expand all | Expand 10 after
2830 } 2830 }
2831 2831
2832 for_of->Initialize(each, subject, body, 2832 for_of->Initialize(each, subject, body,
2833 assign_iterator, next_result, result_done, assign_each); 2833 assign_iterator, next_result, result_done, assign_each);
2834 } else { 2834 } else {
2835 stmt->Initialize(each, subject, body); 2835 stmt->Initialize(each, subject, body);
2836 } 2836 }
2837 } 2837 }
2838 2838
2839 2839
2840
2841 Statement* Parser::DesugarLetBindingsInForStatement(
2842 Scope* inner_scope, ZoneStringList* names, ForStatement* loop,
2843 Statement* init, Expression* cond, Statement* next, Statement* body,
2844 bool* ok) {
2845 // ES6 13.6.3.4 specifies that on each loop iteration the let variables are
2846 // copied into a new environment. After copying, the "next" statement of the
2847 // loop is executed to update the loop variables. The loop condition is
2848 // checked and the loop body is executed.
2849 //
2850 // We rewrite a for statement of the form
2851 //
2852 // for (let x = i; cond; next) body
2853 //
2854 // into
2855 //
2856 // {
2857 // let x = i;
2858 // temp_x = x;
2859 // flag = 1;
2860 // for (;;) {
2861 // let x = temp_x;
2862 // if (flag == 1) {
2863 // flag = 0;
2864 // } else {
2865 // next;
2866 // }
2867 // if (cond) {
2868 // <empty>
2869 // } else {
2870 // break;
2871 // }
2872 // b
2873 // temp_x = x;
2874 // }
2875 // }
2876
2877 ASSERT(names->length() > 0);
2878 Scope* for_scope = scope_;
2879 ZoneList<Variable*> temps(names->length(), zone());
2880
2881 Block* outer_block = factory()->NewBlock(NULL, names->length() + 3, false,
2882 RelocInfo::kNoPosition);
2883 outer_block->AddStatement(init, zone());
2884
2885 Handle<String> temp_name = isolate()->factory()->dot_for_string();
2886 Handle<Smi> smi0 = handle(Smi::FromInt(0), isolate());
2887 Handle<Smi> smi1 = handle(Smi::FromInt(1), isolate());
2888
2889
2890 // For each let variable x:
2891 // make statement: temp_x = x.
2892 for (int i = 0; i < names->length(); i++) {
2893 VariableProxy* proxy =
2894 NewUnresolved(names->at(i), LET, Interface::NewValue());
2895 Variable* temp = scope_->DeclarationScope()->NewTemporary(temp_name);
2896 VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
2897 Assignment* assignment = factory()->NewAssignment(
2898 Token::ASSIGN, temp_proxy, proxy, RelocInfo::kNoPosition);
2899 Statement* assignment_statement = factory()->NewExpressionStatement(
2900 assignment, RelocInfo::kNoPosition);
2901 outer_block->AddStatement(assignment_statement, zone());
2902 temps.Add(temp, zone());
2903 }
2904
2905 Variable* flag = scope_->DeclarationScope()->NewTemporary(temp_name);
2906 // Make statement: flag = 1.
2907 {
2908 VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2909 Expression* const1 = factory()->NewLiteral(smi1, RelocInfo::kNoPosition);
2910 Assignment* assignment = factory()->NewAssignment(
2911 Token::ASSIGN, flag_proxy, const1, RelocInfo::kNoPosition);
2912 Statement* assignment_statement = factory()->NewExpressionStatement(
2913 assignment, RelocInfo::kNoPosition);
2914 outer_block->AddStatement(assignment_statement, zone());
2915 }
2916
2917 outer_block->AddStatement(loop, zone());
2918 outer_block->set_scope(for_scope);
2919 scope_ = inner_scope;
2920
2921 Block* inner_block = factory()->NewBlock(NULL, 2 * names->length() + 3,
2922 false, RelocInfo::kNoPosition);
2923 int pos = scanner()->location().beg_pos;
2924 ZoneList<Variable*> inner_vars(names->length(), zone());
2925
2926 // For each let variable x:
2927 // make statement: let x = temp_x.
2928 for (int i = 0; i < names->length(); i++) {
2929 VariableProxy* proxy =
2930 NewUnresolved(names->at(i), LET, Interface::NewValue());
2931 Declaration* declaration =
2932 factory()->NewVariableDeclaration(proxy, LET, scope_, pos);
2933 Declare(declaration, true, CHECK_OK);
2934 inner_vars.Add(declaration->proxy()->var(), zone());
2935 VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
2936 Assignment* assignment = factory()->NewAssignment(
2937 Token::INIT_LET, proxy, temp_proxy, pos);
2938 Statement* assignment_statement = factory()->NewExpressionStatement(
2939 assignment, pos);
2940 proxy->var()->set_initializer_position(pos);
2941 inner_block->AddStatement(assignment_statement, zone());
2942 }
2943
2944 // Make statement: if (flag == 1) { flag = 0; } else { next; }.
2945 {
2946 Expression* compare = NULL;
2947 // Make compare expresion: flag == 1.
2948 {
2949 Expression* const1 = factory()->NewLiteral(smi1, RelocInfo::kNoPosition);
2950 VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2951 compare = factory()->NewCompareOperation(
2952 Token::EQ, flag_proxy, const1, RelocInfo::kNoPosition);
2953 }
2954 Statement* clear_flag = NULL;
2955 // Make statement: flag = 0.
2956 {
2957 VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2958 Expression* const0 = factory()->NewLiteral(smi0, RelocInfo::kNoPosition);
2959 Assignment* assignment = factory()->NewAssignment(
2960 Token::ASSIGN, flag_proxy, const0, RelocInfo::kNoPosition);
2961 clear_flag = factory()->NewExpressionStatement(assignment, pos);
2962 }
2963 Statement* clear_flag_or_next = factory()->NewIfStatement(
2964 compare, clear_flag, next, RelocInfo::kNoPosition);
2965 inner_block->AddStatement(clear_flag_or_next, zone());
2966 }
2967
2968
2969 // Make statement: if (cond) { } else { break; }.
2970 {
2971 Statement* empty = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
2972 BreakableStatement* t = LookupBreakTarget(Handle<String>(), CHECK_OK);
2973 Statement* stop = factory()->NewBreakStatement(t, RelocInfo::kNoPosition);
2974 Statement* if_not_cond_break = factory()->NewIfStatement(
2975 cond, empty, stop, RelocInfo::kNoPosition);
2976 inner_block->AddStatement(if_not_cond_break, zone());
2977 }
2978
2979 inner_block->AddStatement(body, zone());
2980
2981 // For each let variable x:
2982 // make statement: temp_x = x;
2983 for (int i = 0; i < names->length(); i++) {
2984 VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
2985 int pos = scanner()->location().end_pos;
2986 VariableProxy* proxy = factory()->NewVariableProxy(inner_vars.at(i), pos);
2987 Assignment* assignment = factory()->NewAssignment(
2988 Token::ASSIGN, temp_proxy, proxy, RelocInfo::kNoPosition);
2989 Statement* assignment_statement = factory()->NewExpressionStatement(
2990 assignment, RelocInfo::kNoPosition);
2991 inner_block->AddStatement(assignment_statement, zone());
2992 }
2993
2994 inner_scope->set_end_position(scanner()->location().end_pos);
2995 inner_block->set_scope(inner_scope);
2996 scope_ = for_scope;
2997
2998 loop->Initialize(NULL, NULL, NULL, inner_block);
2999 return outer_block;
3000 }
3001
3002
2840 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) { 3003 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
2841 // ForStatement :: 3004 // ForStatement ::
2842 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement 3005 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
2843 3006
2844 int pos = peek_position(); 3007 int pos = peek_position();
2845 Statement* init = NULL; 3008 Statement* init = NULL;
3009 ZoneStringList let_bindings(1, zone());
2846 3010
2847 // Create an in-between scope for let-bound iteration variables. 3011 // Create an in-between scope for let-bound iteration variables.
2848 Scope* saved_scope = scope_; 3012 Scope* saved_scope = scope_;
2849 Scope* for_scope = NewScope(scope_, BLOCK_SCOPE); 3013 Scope* for_scope = NewScope(scope_, BLOCK_SCOPE);
2850 scope_ = for_scope; 3014 scope_ = for_scope;
2851 3015
2852 Expect(Token::FOR, CHECK_OK); 3016 Expect(Token::FOR, CHECK_OK);
2853 Expect(Token::LPAREN, CHECK_OK); 3017 Expect(Token::LPAREN, CHECK_OK);
2854 for_scope->set_start_position(scanner()->location().beg_pos); 3018 for_scope->set_start_position(scanner()->location().beg_pos);
2855 if (peek() != Token::SEMICOLON) { 3019 if (peek() != Token::SEMICOLON) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2887 ASSERT(for_scope == NULL); 3051 ASSERT(for_scope == NULL);
2888 // Parsed for-in loop w/ variable/const declaration. 3052 // Parsed for-in loop w/ variable/const declaration.
2889 return result; 3053 return result;
2890 } else { 3054 } else {
2891 init = variable_statement; 3055 init = variable_statement;
2892 } 3056 }
2893 } else if (peek() == Token::LET) { 3057 } else if (peek() == Token::LET) {
2894 Handle<String> name; 3058 Handle<String> name;
2895 VariableDeclarationProperties decl_props = kHasNoInitializers; 3059 VariableDeclarationProperties decl_props = kHasNoInitializers;
2896 Block* variable_statement = 3060 Block* variable_statement =
2897 ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name, 3061 ParseVariableDeclarations(kForStatement, &decl_props, &let_bindings,
2898 CHECK_OK); 3062 &name, CHECK_OK);
2899 bool accept_IN = !name.is_null() && decl_props != kHasInitializers; 3063 bool accept_IN = !name.is_null() && decl_props != kHasInitializers;
2900 bool accept_OF = decl_props == kHasNoInitializers; 3064 bool accept_OF = decl_props == kHasNoInitializers;
2901 ForEachStatement::VisitMode mode; 3065 ForEachStatement::VisitMode mode;
2902 3066
2903 if (accept_IN && CheckInOrOf(accept_OF, &mode)) { 3067 if (accept_IN && CheckInOrOf(accept_OF, &mode)) {
2904 // Rewrite a for-in statement of the form 3068 // Rewrite a for-in statement of the form
2905 // 3069 //
2906 // for (let x in e) b 3070 // for (let x in e) b
2907 // 3071 //
2908 // into 3072 // into
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
2991 } 3155 }
2992 } 3156 }
2993 3157
2994 // Standard 'for' loop 3158 // Standard 'for' loop
2995 ForStatement* loop = factory()->NewForStatement(labels, pos); 3159 ForStatement* loop = factory()->NewForStatement(labels, pos);
2996 Target target(&this->target_stack_, loop); 3160 Target target(&this->target_stack_, loop);
2997 3161
2998 // Parsed initializer at this point. 3162 // Parsed initializer at this point.
2999 Expect(Token::SEMICOLON, CHECK_OK); 3163 Expect(Token::SEMICOLON, CHECK_OK);
3000 3164
3165 // If there are let bindings, then condition and the next statement of the
3166 // for loop must be parsed in a new scope.
3167 Scope* inner_scope = NULL;
3168 if (let_bindings.length() > 0) {
3169 inner_scope = NewScope(for_scope, BLOCK_SCOPE);
3170 inner_scope->set_start_position(scanner()->location().beg_pos);
3171 scope_ = inner_scope;
3172 }
3173
3001 Expression* cond = NULL; 3174 Expression* cond = NULL;
3002 if (peek() != Token::SEMICOLON) { 3175 if (peek() != Token::SEMICOLON) {
3003 cond = ParseExpression(true, CHECK_OK); 3176 cond = ParseExpression(true, CHECK_OK);
3004 } 3177 }
3005 Expect(Token::SEMICOLON, CHECK_OK); 3178 Expect(Token::SEMICOLON, CHECK_OK);
3006 3179
3007 Statement* next = NULL; 3180 Statement* next = NULL;
3008 if (peek() != Token::RPAREN) { 3181 if (peek() != Token::RPAREN) {
3009 Expression* exp = ParseExpression(true, CHECK_OK); 3182 Expression* exp = ParseExpression(true, CHECK_OK);
3010 next = factory()->NewExpressionStatement(exp, RelocInfo::kNoPosition); 3183 next = factory()->NewExpressionStatement(exp, RelocInfo::kNoPosition);
3011 } 3184 }
3012 Expect(Token::RPAREN, CHECK_OK); 3185 Expect(Token::RPAREN, CHECK_OK);
3013 3186
3014 Statement* body = ParseStatement(NULL, CHECK_OK); 3187 Statement* body = ParseStatement(NULL, CHECK_OK);
3015 scope_ = saved_scope; 3188
3016 for_scope->set_end_position(scanner()->location().end_pos); 3189 Statement* result = NULL;
3017 for_scope = for_scope->FinalizeBlockScope(); 3190 if (let_bindings.length() > 0) {
3018 if (for_scope != NULL) { 3191 scope_ = for_scope;
3019 // Rewrite a for statement of the form 3192 result = DesugarLetBindingsInForStatement(inner_scope, &let_bindings, loop,
3020 // 3193 init, cond, next, body, CHECK_OK);
3021 // for (let x = i; c; n) b 3194 scope_ = saved_scope;
3022 // 3195 for_scope->set_end_position(scanner()->location().end_pos);
3023 // into
3024 //
3025 // {
3026 // let x = i;
3027 // for (; c; n) b
3028 // }
3029 ASSERT(init != NULL);
3030 Block* result = factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
3031 result->AddStatement(init, zone());
3032 result->AddStatement(loop, zone());
3033 result->set_scope(for_scope);
3034 loop->Initialize(NULL, cond, next, body);
3035 return result;
3036 } else { 3196 } else {
3037 loop->Initialize(init, cond, next, body); 3197 loop->Initialize(init, cond, next, body);
3038 return loop; 3198 result = loop;
3199 scope_ = saved_scope;
3200 for_scope->set_end_position(scanner()->location().end_pos);
3201 for_scope->FinalizeBlockScope();
3039 } 3202 }
3203 return result;
3040 } 3204 }
3041 3205
3042 3206
3043 DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) { 3207 DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
3044 // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser 3208 // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
3045 // contexts this is used as a statement which invokes the debugger as i a 3209 // contexts this is used as a statement which invokes the debugger as i a
3046 // break point is present. 3210 // break point is present.
3047 // DebuggerStatement :: 3211 // DebuggerStatement ::
3048 // 'debugger' ';' 3212 // 'debugger' ';'
3049 3213
(...skipping 1568 matching lines...) Expand 10 before | Expand all | Expand 10 after
4618 ASSERT(info()->isolate()->has_pending_exception()); 4782 ASSERT(info()->isolate()->has_pending_exception());
4619 } else { 4783 } else {
4620 result = ParseProgram(); 4784 result = ParseProgram();
4621 } 4785 }
4622 } 4786 }
4623 info()->SetFunction(result); 4787 info()->SetFunction(result);
4624 return (result != NULL); 4788 return (result != NULL);
4625 } 4789 }
4626 4790
4627 } } // namespace v8::internal 4791 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/harmony/block-for.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698