OLD | NEW |
---|---|
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 // Flags: --no-legacy-const --harmony-sloppy --harmony-sloppy-let | 5 // Flags: --no-legacy-const --harmony-sloppy --harmony-sloppy-let |
6 // Flags: --harmony-sloppy-function --harmony-destructuring | 6 // Flags: --harmony-sloppy-function --harmony-destructuring |
7 // Flags: --harmony-rest-parameters | 7 // Flags: --harmony-rest-parameters |
8 | 8 |
9 // Test Annex B 3.3 semantics for functions declared in blocks in sloppy mode. | 9 // Test Annex B 3.3 semantics for functions declared in blocks in sloppy mode. |
10 // http://www.ecma-international.org/ecma-262/6.0/#sec-block-level-function-decl arations-web-legacy-compatibility-semantics | 10 // http://www.ecma-international.org/ecma-262/6.0/#sec-block-level-function-decl arations-web-legacy-compatibility-semantics |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 } | 139 } |
140 assertEquals(2, f()); | 140 assertEquals(2, f()); |
141 L: { | 141 L: { |
142 assertEquals(3, f()); | 142 assertEquals(3, f()); |
143 break L; | 143 break L; |
144 function f() { return 3; } | 144 function f() { return 3; } |
145 } | 145 } |
146 assertEquals(2, f()); | 146 assertEquals(2, f()); |
147 })(); | 147 })(); |
148 | 148 |
149 // Test that hoisting from blocks doesn't happen in global scope | |
150 function globalUnhoisted() { return 0; } | |
151 { | |
152 function globalUnhoisted() { return 1; } | |
153 } | |
154 assertEquals(0, globalUnhoisted()); | |
155 | |
156 // Test that shadowing arguments is fine | 149 // Test that shadowing arguments is fine |
157 (function shadowArguments(x) { | 150 (function shadowArguments(x) { |
158 assertArrayEquals([1], arguments); | 151 assertArrayEquals([1], arguments); |
159 { | 152 { |
160 assertEquals('function', typeof arguments); | 153 assertEquals('function', typeof arguments); |
161 function arguments() {} | 154 function arguments() {} |
162 assertEquals('function', typeof arguments); | 155 assertEquals('function', typeof arguments); |
163 } | 156 } |
164 assertEquals('function', typeof arguments); | 157 assertEquals('function', typeof arguments); |
165 })(1); | 158 })(1); |
(...skipping 28 matching lines...) Expand all Loading... | |
194 // assertEquals('function', typeof x); | 187 // assertEquals('function', typeof x); |
195 })(1); | 188 })(1); |
196 | 189 |
197 assertThrows(function notInDefaultScope(x = y) { | 190 assertThrows(function notInDefaultScope(x = y) { |
198 { | 191 { |
199 function y() {} | 192 function y() {} |
200 } | 193 } |
201 assertEquals('function', typeof y); | 194 assertEquals('function', typeof y); |
202 assertEquals(x, undefined); | 195 assertEquals(x, undefined); |
203 }, ReferenceError); | 196 }, ReferenceError); |
197 | |
198 // Test that hoisting from blocks does happen in global scope | |
199 function globalHoisted() { return 0; } | |
200 { | |
201 function globalHoisted() { return 1; } | |
202 } | |
203 assertEquals(1, globalHoisted()); | |
204 | |
205 // Also happens when not previously defined | |
206 assertEquals(undefined, globalUndefinedHoisted); | |
207 { | |
208 function globalUndefinedHoisted() { return 1; } | |
209 } | |
210 assertEquals(1, globalUndefinedHoisted()); | |
211 var globalUndefinedHoistedDescriptor = | |
212 Object.getOwnPropertyDescriptor(this, "globalUndefinedHoisted"); | |
213 assertFalse(globalUndefinedHoistedDescriptor.configurable); | |
214 assertTrue(globalUndefinedHoistedDescriptor.writable); | |
215 assertTrue(globalUndefinedHoistedDescriptor.enumerable); | |
216 assertEquals(1, globalUndefinedHoistedDescriptor.value()); | |
217 | |
218 // When a function property is hoisted, it should be | |
219 // made enumerable. | |
220 // BUG(v8:4451) | |
221 Object.defineProperty(this, "globalNonEnumerable", { | |
222 value: false, | |
223 configurable: true, | |
224 writable: true, | |
adamk
2015/09/30 23:13:25
One more test tweak: can you make this "writable:
Dan Ehrenberg
2015/09/30 23:27:21
Done (but in a separate test case)
| |
225 enumerable: false | |
226 }); | |
227 eval("{function globalNonEnumerable() { return 1; }}"); | |
228 var globalNonEnumerableDescriptor | |
229 = Object.getOwnPropertyDescriptor(this, "globalNonEnumerable"); | |
230 // BUG(v8:4451): Should be made non-configurable | |
231 assertTrue(globalNonEnumerableDescriptor.configurable); | |
232 assertTrue(globalNonEnumerableDescriptor.writable); | |
233 // BUG(v8:4451): Should be made enumerable | |
234 assertFalse(globalNonEnumerableDescriptor.enumerable); | |
235 assertEquals(1, globalNonEnumerableDescriptor.value()); | |
236 | |
237 // Test that hoisting from blocks does happen in an eval | |
238 eval(` | |
239 function evalHoisted() { return 0; } | |
240 { | |
241 function evalHoisted() { return 1; } | |
242 } | |
243 assertEquals(1, evalHoisted()); | |
244 `); | |
245 | |
246 // Test that hoisting from blocks happens from eval in a function | |
247 !function() { | |
248 eval(` | |
249 function evalInFunctionHoisted() { return 0; } | |
250 { | |
251 function evalInFunctionHoisted() { return 1; } | |
252 } | |
253 assertEquals(1, evalInFunctionHoisted()); | |
254 `); | |
255 }(); | |
256 | |
257 let dontHoistGlobal; | |
258 { function dontHoistGlobal() {} } | |
259 assertEquals(undefined, dontHoistGlobal); | |
260 | |
261 let dontHoistEval; | |
262 // BUG(v8:) This shouldn't hoist and shouldn't throw | |
263 var throws = false; | |
264 try { | |
265 eval("{ function dontHoistEval() {} }"); | |
266 } catch (e) { | |
267 throws = true; | |
268 } | |
269 assertTrue(throws); | |
270 | |
271 // When the global object is frozen, silently don't hoist | |
272 // Currently this actually throws BUG(v8:4452) | |
273 Object.freeze(this); | |
274 throws = false; | |
275 try { | |
276 eval('{ function hoistWhenFrozen() {} }'); | |
277 } catch (e) { | |
278 throws = true; | |
279 } | |
280 assertFalse(this.hasOwnProperty("hoistWhenFrozen")); | |
281 assertThrows(() => hoistWhenFrozen, ReferenceError); | |
282 // Should be assertFalse BUG(v8:4452) | |
283 assertTrue(throws); | |
OLD | NEW |