1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16function check_syntax_error (code) 17{ 18 try { 19 eval (code) 20 assert (false) 21 } catch (e) { 22 assert (e instanceof SyntaxError) 23 } 24} 25 26check_syntax_error ("'use strict'; if (true) function f() {}") 27check_syntax_error ("'use strict'; if (true) ; else function f() {}") 28check_syntax_error ("'use strict'; a: function f() {}") 29check_syntax_error ("if (true) async function f() {}") 30check_syntax_error ("if (true) ; else async function f() {}") 31check_syntax_error ("if (true) a: function f() {}") 32check_syntax_error ("if (true) ; else a: function f() {}") 33 34var g = 1 35var h = 1 36 37function f1(p) 38{ 39 assert(g === undefined) 40 assert(h === undefined) 41 42 if (p) 43 // Same as: { function g() { return 3 } } 44 function g() { return 3 } 45 else 46 // Same as: { function h() { return 4 } } 47 function h() { return 4 } 48 49 if (p) { 50 assert(g() === 3) 51 assert(h === undefined) 52 } else { 53 assert(g === undefined) 54 assert(h() === 4) 55 } 56} 57 58f1(true) 59f1(false) 60 61function f2() 62{ 63 assert(g() === 2) 64 a: b: c: d: function g() { return 2 } 65 66 assert(h === undefined) 67 68 { 69 assert(h() === 3) 70 a: b: c: d: function h() { return 3 } 71 } 72 73 assert(h() === 3) 74 75 try { 76 assert(h() === 4) 77 a: b: c: d: function h() { return 4 } 78 throw 1 79 } catch (e) { 80 assert(h() === 5) 81 a: b: c: d: function h() { return 5 } 82 } finally { 83 assert(h() === 6) 84 a: b: c: d: function h() { return 6 } 85 } 86 87 assert(h() === 6) 88 89 switch (1) { 90 default: 91 assert(h() === 7) 92 a: b: c: d: function h() { return 7 } 93 } 94 95 assert(h() === 7) 96} 97f2() 98 99function f3() 100{ 101 assert(h === undefined) 102 103 { 104 let a = eval("1") 105 assert(a === 1) 106 assert(h() === 1) 107 a: b: c: d: function h() { return 1 } 108 } 109 110 assert(h() === 1) 111 112 { 113 eval("a: b: c: d: function h() { return 2 }") 114 assert(h() === 2) 115 } 116 117 assert(h() === 2) 118} 119f3() 120