1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Flags: --no-harmony-restrictive-declarations 6 7 // ES#sec-functiondeclarations-in-ifstatement-statement-clauses 8 // Annex B 3.4 FunctionDeclarations in IfStatement Statement Clauses 9 // In sloppy mode, function declarations in if statements act like 10 // they have a block around them. Prohibited in strict mode. 11 (function() { 12 assertEquals(undefined, f); 13 if (false) function f() { }; 14 assertEquals(undefined, f); 15 })(); 16 17 (function() { 18 assertEquals(undefined, f); 19 if (true) function f() { }; 20 assertEquals('function', typeof f); 21 })(); 22 23 (function() { 24 assertEquals(undefined, f); 25 if (true) {} else function f() { }; 26 assertEquals(undefined, f); 27 })(); 28 29 (function() { 30 assertEquals(undefined, f); 31 if (false) {} else function f() { }; 32 assertEquals('function', typeof f); 33 })(); 34 35 // For legacy reasons, we also support these types of semantics as 36 // the body of a for or with statement. 37 (function() { 38 for (;false;) function f() { }; 39 assertEquals(undefined, f); 40 })(); 41 42 (function() { 43 for (var x in {}) function f() { }; 44 assertEquals(undefined, f); 45 })(); 46 47 (function() { 48 var x; 49 for (x in {}) function f() { }; 50 assertEquals(undefined, f); 51 })(); 52 53 (function() { 54 for (var i = 0; i < 1; i++) function f() { }; 55 assertEquals('function', typeof f); 56 })(); 57 58 (function() { 59 for (var x in {a: 1}) function f() { }; 60 assertEquals('function', typeof f); 61 })(); 62 63 (function() { 64 var x; 65 for (x in {a: 1}) function f() { }; 66 assertEquals('function', typeof f); 67 })(); 68 69 (function() { 70 with ({}) function f() { }; 71 assertEquals('function', typeof f); 72 })(); 73 74 (function() { 75 do function f() {} while (0); 76 assertEquals('function', typeof f); 77 })(); 78 79 // Labeled function declarations undergo the same hoisting/FiB semantics as if 80 // they were unalbeled. 81 (function() { 82 function bar() { 83 return f; 84 x: function f() {} 85 } 86 assertEquals('function', typeof bar()); 87 })(); 88 89 (function() { 90 function bar() { 91 return f; 92 { 93 x: function f() {} 94 } 95 } 96 assertEquals(undefined, bar()); 97 })(); 98