1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_cifunction check_syntax_error (code) 17425bb815Sopenharmony_ci{ 18425bb815Sopenharmony_ci try { 19425bb815Sopenharmony_ci eval (code) 20425bb815Sopenharmony_ci assert (false) 21425bb815Sopenharmony_ci } catch (e) { 22425bb815Sopenharmony_ci assert (e instanceof SyntaxError) 23425bb815Sopenharmony_ci } 24425bb815Sopenharmony_ci} 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_cicheck_syntax_error ("'use strict'; if (true) function f() {}") 27425bb815Sopenharmony_cicheck_syntax_error ("'use strict'; if (true) ; else function f() {}") 28425bb815Sopenharmony_cicheck_syntax_error ("'use strict'; a: function f() {}") 29425bb815Sopenharmony_cicheck_syntax_error ("if (true) async function f() {}") 30425bb815Sopenharmony_cicheck_syntax_error ("if (true) ; else async function f() {}") 31425bb815Sopenharmony_cicheck_syntax_error ("if (true) a: function f() {}") 32425bb815Sopenharmony_cicheck_syntax_error ("if (true) ; else a: function f() {}") 33425bb815Sopenharmony_ci 34425bb815Sopenharmony_civar g = 1 35425bb815Sopenharmony_civar h = 1 36425bb815Sopenharmony_ci 37425bb815Sopenharmony_cifunction f1(p) 38425bb815Sopenharmony_ci{ 39425bb815Sopenharmony_ci assert(g === undefined) 40425bb815Sopenharmony_ci assert(h === undefined) 41425bb815Sopenharmony_ci 42425bb815Sopenharmony_ci if (p) 43425bb815Sopenharmony_ci // Same as: { function g() { return 3 } } 44425bb815Sopenharmony_ci function g() { return 3 } 45425bb815Sopenharmony_ci else 46425bb815Sopenharmony_ci // Same as: { function h() { return 4 } } 47425bb815Sopenharmony_ci function h() { return 4 } 48425bb815Sopenharmony_ci 49425bb815Sopenharmony_ci if (p) { 50425bb815Sopenharmony_ci assert(g() === 3) 51425bb815Sopenharmony_ci assert(h === undefined) 52425bb815Sopenharmony_ci } else { 53425bb815Sopenharmony_ci assert(g === undefined) 54425bb815Sopenharmony_ci assert(h() === 4) 55425bb815Sopenharmony_ci } 56425bb815Sopenharmony_ci} 57425bb815Sopenharmony_ci 58425bb815Sopenharmony_cif1(true) 59425bb815Sopenharmony_cif1(false) 60425bb815Sopenharmony_ci 61425bb815Sopenharmony_cifunction f2() 62425bb815Sopenharmony_ci{ 63425bb815Sopenharmony_ci assert(g() === 2) 64425bb815Sopenharmony_ci a: b: c: d: function g() { return 2 } 65425bb815Sopenharmony_ci 66425bb815Sopenharmony_ci assert(h === undefined) 67425bb815Sopenharmony_ci 68425bb815Sopenharmony_ci { 69425bb815Sopenharmony_ci assert(h() === 3) 70425bb815Sopenharmony_ci a: b: c: d: function h() { return 3 } 71425bb815Sopenharmony_ci } 72425bb815Sopenharmony_ci 73425bb815Sopenharmony_ci assert(h() === 3) 74425bb815Sopenharmony_ci 75425bb815Sopenharmony_ci try { 76425bb815Sopenharmony_ci assert(h() === 4) 77425bb815Sopenharmony_ci a: b: c: d: function h() { return 4 } 78425bb815Sopenharmony_ci throw 1 79425bb815Sopenharmony_ci } catch (e) { 80425bb815Sopenharmony_ci assert(h() === 5) 81425bb815Sopenharmony_ci a: b: c: d: function h() { return 5 } 82425bb815Sopenharmony_ci } finally { 83425bb815Sopenharmony_ci assert(h() === 6) 84425bb815Sopenharmony_ci a: b: c: d: function h() { return 6 } 85425bb815Sopenharmony_ci } 86425bb815Sopenharmony_ci 87425bb815Sopenharmony_ci assert(h() === 6) 88425bb815Sopenharmony_ci 89425bb815Sopenharmony_ci switch (1) { 90425bb815Sopenharmony_ci default: 91425bb815Sopenharmony_ci assert(h() === 7) 92425bb815Sopenharmony_ci a: b: c: d: function h() { return 7 } 93425bb815Sopenharmony_ci } 94425bb815Sopenharmony_ci 95425bb815Sopenharmony_ci assert(h() === 7) 96425bb815Sopenharmony_ci} 97425bb815Sopenharmony_cif2() 98425bb815Sopenharmony_ci 99425bb815Sopenharmony_cifunction f3() 100425bb815Sopenharmony_ci{ 101425bb815Sopenharmony_ci assert(h === undefined) 102425bb815Sopenharmony_ci 103425bb815Sopenharmony_ci { 104425bb815Sopenharmony_ci let a = eval("1") 105425bb815Sopenharmony_ci assert(a === 1) 106425bb815Sopenharmony_ci assert(h() === 1) 107425bb815Sopenharmony_ci a: b: c: d: function h() { return 1 } 108425bb815Sopenharmony_ci } 109425bb815Sopenharmony_ci 110425bb815Sopenharmony_ci assert(h() === 1) 111425bb815Sopenharmony_ci 112425bb815Sopenharmony_ci { 113425bb815Sopenharmony_ci eval("a: b: c: d: function h() { return 2 }") 114425bb815Sopenharmony_ci assert(h() === 2) 115425bb815Sopenharmony_ci } 116425bb815Sopenharmony_ci 117425bb815Sopenharmony_ci assert(h() === 2) 118425bb815Sopenharmony_ci} 119425bb815Sopenharmony_cif3() 120