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, |
| 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 // When a function property is hoisted, it should be overwritten and |
| 238 // made writable and overwritten, even if the property was non-writable. |
| 239 Object.defineProperty(this, "globalNonWritable", { |
| 240 value: false, |
| 241 configurable: true, |
| 242 writable: false, |
| 243 enumerable: true |
| 244 }); |
| 245 eval("{function globalNonWritable() { return 1; }}"); |
| 246 var globalNonWritableDescriptor |
| 247 = Object.getOwnPropertyDescriptor(this, "globalNonWritable"); |
| 248 // BUG(v8:4451): Should be made non-configurable |
| 249 assertTrue(globalNonWritableDescriptor.configurable); |
| 250 // BUG(v8:4451): Should be made writable |
| 251 assertFalse(globalNonWritableDescriptor.writable); |
| 252 assertFalse(globalNonEnumerableDescriptor.enumerable); |
| 253 // BUG(v8:4451): Should be overwritten |
| 254 assertEquals(false, globalNonWritableDescriptor.value); |
| 255 |
| 256 // Test that hoisting from blocks does happen in an eval |
| 257 eval(` |
| 258 function evalHoisted() { return 0; } |
| 259 { |
| 260 function evalHoisted() { return 1; } |
| 261 } |
| 262 assertEquals(1, evalHoisted()); |
| 263 `); |
| 264 |
| 265 // Test that hoisting from blocks happens from eval in a function |
| 266 !function() { |
| 267 eval(` |
| 268 function evalInFunctionHoisted() { return 0; } |
| 269 { |
| 270 function evalInFunctionHoisted() { return 1; } |
| 271 } |
| 272 assertEquals(1, evalInFunctionHoisted()); |
| 273 `); |
| 274 }(); |
| 275 |
| 276 let dontHoistGlobal; |
| 277 { function dontHoistGlobal() {} } |
| 278 assertEquals(undefined, dontHoistGlobal); |
| 279 |
| 280 let dontHoistEval; |
| 281 // BUG(v8:) This shouldn't hoist and shouldn't throw |
| 282 var throws = false; |
| 283 try { |
| 284 eval("{ function dontHoistEval() {} }"); |
| 285 } catch (e) { |
| 286 throws = true; |
| 287 } |
| 288 assertTrue(throws); |
| 289 |
| 290 // When the global object is frozen, silently don't hoist |
| 291 // Currently this actually throws BUG(v8:4452) |
| 292 Object.freeze(this); |
| 293 throws = false; |
| 294 try { |
| 295 eval('{ function hoistWhenFrozen() {} }'); |
| 296 } catch (e) { |
| 297 throws = true; |
| 298 } |
| 299 assertFalse(this.hasOwnProperty("hoistWhenFrozen")); |
| 300 assertThrows(() => hoistWhenFrozen, ReferenceError); |
| 301 // Should be assertFalse BUG(v8:4452) |
| 302 assertTrue(throws); |
OLD | NEW |