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 3 // found in the LICENSE file. 4 5 // Test for conflicting variable bindings. 6 7 function AssertEqualsStrictAndSloppy(value, code) { 8 assertEquals(value, eval("(function() {" + code + "})()")); 9 assertEquals(value, eval("(function() { 'use strict'; " + code + "})()")); 10 assertEquals(value, eval("(function() { var x = 0; {" + code + "} })()")); 11 assertEquals(value, eval("(function() { 'use strict'; var x = 0; {" 12 + code + "} })()")); 13 } 14 15 function AssertThrowsStrictAndSloppy(code, error) { 16 assertThrows("(function() {" + code + "})()", error); 17 assertThrows("(function() { 'use strict'; " + code + "})()", error); 18 assertThrows("(function() { var x = 0; { " + code + "} })()", error); 19 assertThrows("(function() { 'use strict'; var x = 0; {" + code + "} })()", 20 error); 21 } 22 23 (function TestClassTDZ() { 24 AssertEqualsStrictAndSloppy( 25 "x", "function f() { return x; }; class x { }; return f().name;"); 26 AssertEqualsStrictAndSloppy 27 ("x", "class x { }; function f() { return x; }; return f().name;"); 28 AssertEqualsStrictAndSloppy( 29 "x", "class x { }; var result = f().name; " + 30 "function f() { return x; }; return result;"); 31 AssertThrowsStrictAndSloppy( 32 "function f() { return x; }; f(); class x { };", ReferenceError); 33 AssertThrowsStrictAndSloppy( 34 "f(); function f() { return x; }; class x { };", ReferenceError); 35 AssertThrowsStrictAndSloppy( 36 "f(); class x { }; function f() { return x; };", ReferenceError); 37 AssertThrowsStrictAndSloppy( 38 "var x = 1; { f(); class x { }; function f() { return x; }; }", 39 ReferenceError); 40 AssertThrowsStrictAndSloppy("x = 3; class x { };", ReferenceError) 41 })(); 42 43 (function TestClassNameConflict() { 44 AssertThrowsStrictAndSloppy("class x { }; var x;", SyntaxError); 45 AssertThrowsStrictAndSloppy("var x; class x { };", SyntaxError); 46 AssertThrowsStrictAndSloppy("class x { }; function x() { };", SyntaxError); 47 AssertThrowsStrictAndSloppy("function x() { }; class x { };", SyntaxError); 48 AssertThrowsStrictAndSloppy("class x { }; for (var x = 0; false;) { };", 49 SyntaxError); 50 AssertThrowsStrictAndSloppy("for (var x = 0; false;) { }; class x { };", 51 SyntaxError); 52 })(); 53 54 (function TestClassMutableBinding() { 55 AssertEqualsStrictAndSloppy( 56 "x3", "class x { }; var y = x.name; x = 3; return y + x;") 57 })(); 58