OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // in a for-in statement with let variables. | 95 // in a for-in statement with let variables. |
96 // TODO(ES6): properly activate extended mode | 96 // TODO(ES6): properly activate extended mode |
97 assertThrows("function foo() { 'use strict'; for (let in {}) { } }", SyntaxError
); | 97 assertThrows("function foo() { 'use strict'; for (let in {}) { } }", SyntaxError
); |
98 assertThrows("function foo() { 'use strict'; for (let x = 3 in {}) { } }", Synta
xError); | 98 assertThrows("function foo() { 'use strict'; for (let x = 3 in {}) { } }", Synta
xError); |
99 assertThrows("function foo() { 'use strict'; for (let x, y in {}) { } }", Syntax
Error); | 99 assertThrows("function foo() { 'use strict'; for (let x, y in {}) { } }", Syntax
Error); |
100 assertThrows("function foo() { 'use strict'; for (let x = 3, y in {}) { } }", Sy
ntaxError); | 100 assertThrows("function foo() { 'use strict'; for (let x = 3, y in {}) { } }", Sy
ntaxError); |
101 assertThrows("function foo() { 'use strict'; for (let x, y = 4 in {}) { } }", Sy
ntaxError); | 101 assertThrows("function foo() { 'use strict'; for (let x, y = 4 in {}) { } }", Sy
ntaxError); |
102 assertThrows("function foo() { 'use strict'; for (let x = 3, y = 4 in {}) { } }"
, SyntaxError); | 102 assertThrows("function foo() { 'use strict'; for (let x = 3, y = 4 in {}) { } }"
, SyntaxError); |
103 | 103 |
104 | 104 |
105 // In a normal for statement the iteration variable is not | 105 // In a normal for statement the iteration variable is |
106 // freshly allocated for each iteration. | 106 // freshly allocated for each iteration. |
107 function closures1() { | 107 function closures1() { |
108 let a = []; | 108 let a = []; |
109 for (let i = 0; i < 5; ++i) { | 109 for (let i = 0; i < 5; ++i) { |
110 a.push(function () { return i; }); | 110 a.push(function () { return i; }); |
111 } | 111 } |
112 for (let j = 0; j < 5; ++j) { | 112 for (let j = 0; j < 5; ++j) { |
113 assertEquals(5, a[j]()); | 113 assertEquals(j, a[j]()); |
114 } | 114 } |
115 } | 115 } |
116 closures1(); | 116 closures1(); |
117 | 117 |
118 | 118 |
119 function closures2() { | 119 function closures2() { |
120 let a = [], b = []; | 120 let a = [], b = []; |
121 for (let i = 0, j = 10; i < 5; ++i, ++j) { | 121 for (let i = 0, j = 10; i < 5; ++i, ++j) { |
122 a.push(function () { return i; }); | 122 a.push(function () { return i; }); |
123 b.push(function () { return j; }); | 123 b.push(function () { return j; }); |
124 } | 124 } |
125 for (let k = 0; k < 5; ++k) { | 125 for (let k = 0; k < 5; ++k) { |
126 assertEquals(5, a[k]()); | 126 assertEquals(k, a[k]()); |
127 assertEquals(15, b[k]()); | 127 assertEquals(k + 10, b[k]()); |
128 } | 128 } |
129 } | 129 } |
130 closures2(); | 130 closures2(); |
131 | 131 |
132 | 132 |
| 133 function closure_in_for_init() { |
| 134 let a = []; |
| 135 for (let i = 0, f = function() { return i }; i < 5; ++i) { |
| 136 a.push(f); |
| 137 } |
| 138 for (let k = 0; k < 5; ++k) { |
| 139 assertEquals(0, a[k]()); |
| 140 } |
| 141 } |
| 142 closure_in_for_init(); |
| 143 |
| 144 |
| 145 function closure_in_for_cond() { |
| 146 let a = []; |
| 147 for (let i = 0; a.push(function () { return i; }), i < 5; ++i) { } |
| 148 for (let k = 0; k < 5; ++k) { |
| 149 assertEquals(k, a[k]()); |
| 150 } |
| 151 } |
| 152 closure_in_for_next(); |
| 153 |
| 154 |
| 155 function closure_in_for_next() { |
| 156 let a = []; |
| 157 for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { } |
| 158 for (let k = 0; k < 5; ++k) { |
| 159 assertEquals(k + 1, a[k]()); |
| 160 } |
| 161 } |
| 162 closure_in_for_next(); |
| 163 |
| 164 |
133 // In a for-in statement the iteration variable is fresh | 165 // In a for-in statement the iteration variable is fresh |
134 // for earch iteration. | 166 // for earch iteration. |
135 function closures3(x) { | 167 function closures3(x) { |
136 let a = []; | 168 let a = []; |
137 for (let p in x) { | 169 for (let p in x) { |
138 a.push(function () { return p; }); | 170 a.push(function () { return p; }); |
139 } | 171 } |
140 let k = 0; | 172 let k = 0; |
141 for (let q in x) { | 173 for (let q in x) { |
142 assertEquals(q, a[k]()); | 174 assertEquals(q, a[k]()); |
143 ++k; | 175 ++k; |
144 } | 176 } |
145 } | 177 } |
146 closures3({a : [0], b : 1, c : {v : 1}, get d() {}, set e(x) {}}); | 178 closures3({a : [0], b : 1, c : {v : 1}, get d() {}, set e(x) {}}); |
OLD | NEW |