OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/objects-inl.h" | 5 #include "src/objects-inl.h" |
6 #include "test/cctest/compiler/function-tester.h" | 6 #include "test/cctest/compiler/function-tester.h" |
7 | 7 |
8 namespace v8 { | 8 namespace v8 { |
9 namespace internal { | 9 namespace internal { |
10 namespace compiler { | 10 namespace compiler { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 97 |
98 TEST(ForStatement) { | 98 TEST(ForStatement) { |
99 FunctionTester T("(function(a,b) { for (; a < b; a+=23) {} return a; })"); | 99 FunctionTester T("(function(a,b) { for (; a < b; a+=23) {} return a; })"); |
100 | 100 |
101 T.CheckCall(T.Val(1), T.Val(1), T.Val(1)); | 101 T.CheckCall(T.Val(1), T.Val(1), T.Val(1)); |
102 T.CheckCall(T.Val(24), T.Val(1), T.Val(23)); | 102 T.CheckCall(T.Val(24), T.Val(1), T.Val(23)); |
103 T.CheckCall(T.Val(47), T.Val(1), T.Val(25)); | 103 T.CheckCall(T.Val(47), T.Val(1), T.Val(25)); |
104 T.CheckCall(T.Val("str"), T.Val("str"), T.Val("str")); | 104 T.CheckCall(T.Val("str"), T.Val("str"), T.Val("str")); |
105 } | 105 } |
106 | 106 |
107 | |
108 static void TestForIn(const char* code) { | |
109 FunctionTester T(code); | |
110 T.CheckCall(T.undefined(), T.undefined()); | |
111 T.CheckCall(T.undefined(), T.null()); | |
112 T.CheckCall(T.undefined(), T.NewObject("({})")); | |
113 T.CheckCall(T.undefined(), T.Val(1)); | |
114 T.CheckCall(T.Val("2"), T.Val("str")); | |
115 T.CheckCall(T.Val("a"), T.NewObject("({'a' : 1})")); | |
116 T.CheckCall(T.Val("2"), T.NewObject("([1, 2, 3])")); | |
117 T.CheckCall(T.Val("a"), T.NewObject("({'a' : 1, 'b' : 1})"), T.Val("b")); | |
118 T.CheckCall(T.Val("1"), T.NewObject("([1, 2, 3])"), T.Val("2")); | |
119 } | |
120 | |
121 | |
122 TEST(ForInStatement) { | |
123 // Variable assignment. | |
124 TestForIn( | |
125 "(function(a, b) {" | |
126 "var last;" | |
127 "for (var x in a) {" | |
128 " if (b) { delete a[b]; b = undefined; }" | |
129 " last = x;" | |
130 "}" | |
131 "return last;})"); | |
132 // Indexed assignment. | |
133 TestForIn( | |
134 "(function(a, b) {" | |
135 "var array = [0, 1, undefined];" | |
136 "for (array[2] in a) {" | |
137 " if (b) { delete a[b]; b = undefined; }" | |
138 "}" | |
139 "return array[2];})"); | |
140 // Named assignment. | |
141 TestForIn( | |
142 "(function(a, b) {" | |
143 "var obj = {'a' : undefined};" | |
144 "for (obj.a in a) {" | |
145 " if (b) { delete a[b]; b = undefined; }" | |
146 "}" | |
147 "return obj.a;})"); | |
148 } | |
149 | |
150 | |
151 TEST(ForInContinueStatement) { | |
152 const char* src = | |
153 "(function(a,b) {" | |
154 " var r = '-';" | |
155 " for (var x in a) {" | |
156 " r += 'A-';" | |
157 " if (b) continue;" | |
158 " r += 'B-';" | |
159 " }" | |
160 " return r;" | |
161 "})"; | |
162 FunctionTester T(src); | |
163 | |
164 T.CheckCall(T.Val("-A-B-"), T.NewObject("({x:1})"), T.false_value()); | |
165 T.CheckCall(T.Val("-A-B-A-B-"), T.NewObject("({x:1,y:2})"), T.false_value()); | |
166 T.CheckCall(T.Val("-A-"), T.NewObject("({x:1})"), T.true_value()); | |
167 T.CheckCall(T.Val("-A-A-"), T.NewObject("({x:1,y:2})"), T.true_value()); | |
168 } | |
169 | |
170 | |
171 TEST(ForOfContinueStatement) { | 107 TEST(ForOfContinueStatement) { |
172 const char* src = | 108 const char* src = |
173 "(function(a,b) {" | 109 "(function(a,b) {" |
174 " var r = '-';" | 110 " var r = '-';" |
175 " for (var x of a) {" | 111 " for (var x of a) {" |
176 " r += x + '-';" | 112 " r += x + '-';" |
177 " if (b) continue;" | 113 " if (b) continue;" |
178 " r += 'X-';" | 114 " r += 'X-';" |
179 " }" | 115 " }" |
180 " return r;" | 116 " return r;" |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 TEST(EmptyFor) { | 315 TEST(EmptyFor) { |
380 FunctionTester T("(function(a,b) { if (a) for(;;) ; return b; })"); | 316 FunctionTester T("(function(a,b) { if (a) for(;;) ; return b; })"); |
381 | 317 |
382 T.CheckCall(T.Val(8126.1), T.Val(0.0), T.Val(8126.1)); | 318 T.CheckCall(T.Val(8126.1), T.Val(0.0), T.Val(8126.1)); |
383 T.CheckCall(T.Val(1123.1), T.Val(0.0), T.Val(1123.1)); | 319 T.CheckCall(T.Val(1123.1), T.Val(0.0), T.Val(1123.1)); |
384 } | 320 } |
385 | 321 |
386 } // namespace compiler | 322 } // namespace compiler |
387 } // namespace internal | 323 } // namespace internal |
388 } // namespace v8 | 324 } // namespace v8 |
OLD | NEW |