blob: b68dbdd12ddafb138f9d8aebda1757154f2ac4ef [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005// Var-let conflict in a function throws, even if the var is in an eval
6
7// Throws at the top level of a function
8assertThrows(function() {
9 let x = 1;
10 eval('var x');
Ben Murdoch61f157c2016-09-16 13:49:30 +010011}, SyntaxError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012
13// If the eval is in its own block scope, throws
14assertThrows(function() {
15 let y = 1;
16 { eval('var y'); }
Ben Murdoch61f157c2016-09-16 13:49:30 +010017}, SyntaxError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018
19// If the let is in its own block scope, with the eval, throws
20assertThrows(function() {
21 {
22 let x = 1;
23 eval('var x');
24 }
Ben Murdoch61f157c2016-09-16 13:49:30 +010025}, SyntaxError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026
27// Legal if the let is no longer visible
28assertDoesNotThrow(function() {
29 {
30 let x = 1;
31 }
32 eval('var x');
33});
34
35// All the same works for const:
36// Throws at the top level of a function
37assertThrows(function() {
38 const x = 1;
39 eval('var x');
Ben Murdoch61f157c2016-09-16 13:49:30 +010040}, SyntaxError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041
42// If the eval is in its own block scope, throws
43assertThrows(function() {
44 const y = 1;
45 { eval('var y'); }
Ben Murdoch61f157c2016-09-16 13:49:30 +010046}, SyntaxError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047
48// If the const is in its own block scope, with the eval, throws
49assertThrows(function() {
50 {
51 const x = 1;
52 eval('var x');
53 }
Ben Murdoch61f157c2016-09-16 13:49:30 +010054}, SyntaxError);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055
56// Legal if the const is no longer visible
57assertDoesNotThrow(function() {
58 {
59 const x = 1;
60 }
61 eval('var x');
62});
63
64// In global scope
65let caught = false;
66try {
67 let z = 1;
68 eval('var z');
69} catch (e) {
70 caught = true;
71}
72assertTrue(caught);
73
74// Let declarations beyond a function boundary don't conflict
75caught = false;
76try {
77 let a = 1;
78 (function() {
79 eval('var a');
80 })();
81} catch (e) {
82 caught = true;
83}
84assertFalse(caught);
85
86// var across with doesn't conflict
87caught = false;
88try {
89 (function() {
90 with ({x: 1}) {
91 eval("var x");
92 }
93 })();
94} catch (e) {
95 caught = true;
96}
97assertFalse(caught);
98
99// var can still conflict with let across a with
100caught = false;
101try {
102 (function() {
103 let x;
104 with ({x: 1}) {
105 eval("var x");
106 }
107 })();
108} catch (e) {
109 caught = true;
110}
111assertTrue(caught);
112
113// Functions declared in eval also conflict
114caught = false
115try {
116 (function() {
117 {
118 let x = 1;
119 eval('function x() {}');
120 }
121 })();
122} catch (e) {
123 caught = true;
124}
125assertTrue(caught);
126
127// TODO(littledan): Hoisting x out of the block should be
128// prevented in this case BUG(v8:4479)
129caught = false
130try {
131 (function() {
132 {
133 let x = 1;
134 eval('{ function x() {} }');
135 }
136 })();
137} catch (e) {
138 caught = true;
139}
140// TODO(littledan): switch to assertTrue when bug is fixed
141assertTrue(caught);