OLD | NEW |
| (Empty) |
1 // Copyright 2011 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // Test let declarations in various settings. | |
29 | |
30 // Flags: --harmony-sloppy --harmony-sloppy-let | |
31 | |
32 // Global | |
33 let x; | |
34 let y = 2; | |
35 const z = 4; | |
36 class c { static foo() { return 1; } } | |
37 | |
38 // Block local | |
39 { | |
40 let y; | |
41 let x = 3; | |
42 const z = 5; | |
43 class c { static foo() { return 2; } } | |
44 } | |
45 | |
46 assertEquals(undefined, x); | |
47 assertEquals(2,y); | |
48 assertEquals(4,z); | |
49 assertEquals(1, c.foo()); | |
50 | |
51 if (true) { | |
52 let y; | |
53 assertEquals(undefined, y); | |
54 } | |
55 | |
56 // Invalid declarations are early errors in harmony mode and thus should trigger | |
57 // an exception in eval code during parsing, before even compiling or executing | |
58 // the code. Thus the generated function is not called here. | |
59 function TestLocalThrows(str, expect) { | |
60 assertThrows("(function(arg){ 'use strict'; " + str + "})", expect); | |
61 } | |
62 | |
63 function TestLocalDoesNotThrow(str) { | |
64 assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()"); | |
65 } | |
66 | |
67 // Test let declarations in statement positions. | |
68 TestLocalThrows("if (true) let x;", SyntaxError); | |
69 TestLocalThrows("if (true) {} else let x;", SyntaxError); | |
70 TestLocalThrows("do let x; while (false)", SyntaxError); | |
71 TestLocalThrows("while (false) let x;", SyntaxError); | |
72 TestLocalThrows("label: let x;", SyntaxError); | |
73 TestLocalThrows("for (;false;) let x;", SyntaxError); | |
74 TestLocalDoesNotThrow("switch (true) { case true: let x; }"); | |
75 TestLocalDoesNotThrow("switch (true) { default: let x; }"); | |
76 | |
77 // Test const declarations with initialisers in statement positions. | |
78 TestLocalThrows("if (true) const x = 1;", SyntaxError); | |
79 TestLocalThrows("if (true) {} else const x = 1;", SyntaxError); | |
80 TestLocalThrows("do const x = 1; while (false)", SyntaxError); | |
81 TestLocalThrows("while (false) const x = 1;", SyntaxError); | |
82 TestLocalThrows("label: const x = 1;", SyntaxError); | |
83 TestLocalThrows("for (;false;) const x = 1;", SyntaxError); | |
84 TestLocalDoesNotThrow("switch (true) { case true: const x = 1; }"); | |
85 TestLocalDoesNotThrow("switch (true) { default: const x = 1; }"); | |
86 | |
87 // Test const declarations without initialisers. | |
88 TestLocalThrows("const x;", SyntaxError); | |
89 TestLocalThrows("const x = 1, y;", SyntaxError); | |
90 TestLocalThrows("const x, y = 1;", SyntaxError); | |
91 | |
92 // Test const declarations without initialisers in statement positions. | |
93 TestLocalThrows("if (true) const x;", SyntaxError); | |
94 TestLocalThrows("if (true) {} else const x;", SyntaxError); | |
95 TestLocalThrows("do const x; while (false)", SyntaxError); | |
96 TestLocalThrows("while (false) const x;", SyntaxError); | |
97 TestLocalThrows("label: const x;", SyntaxError); | |
98 TestLocalThrows("for (;false;) const x;", SyntaxError); | |
99 TestLocalThrows("switch (true) { case true: const x; }", SyntaxError); | |
100 TestLocalThrows("switch (true) { default: const x; }", SyntaxError); | |
101 | |
102 // Test var declarations in statement positions. | |
103 TestLocalDoesNotThrow("if (true) var x;"); | |
104 TestLocalDoesNotThrow("if (true) {} else var x;"); | |
105 TestLocalDoesNotThrow("do var x; while (false)"); | |
106 TestLocalDoesNotThrow("while (false) var x;"); | |
107 TestLocalDoesNotThrow("label: var x;"); | |
108 TestLocalDoesNotThrow("for (;false;) var x;"); | |
109 TestLocalDoesNotThrow("switch (true) { case true: var x; }"); | |
110 TestLocalDoesNotThrow("switch (true) { default: var x; }"); | |
111 | |
112 // Test class declarations with initialisers in statement positions. | |
113 TestLocalThrows("if (true) class x { };", SyntaxError); | |
114 TestLocalThrows("if (true) {} else class x { };", SyntaxError); | |
115 TestLocalThrows("do class x { }; while (false)", SyntaxError); | |
116 TestLocalThrows("while (false) class x { };", SyntaxError); | |
117 TestLocalThrows("label: class x { };", SyntaxError); | |
118 TestLocalThrows("for (;false;) class x { };", SyntaxError); | |
119 TestLocalDoesNotThrow("switch (true) { case true: class x { }; }"); | |
120 TestLocalDoesNotThrow("switch (true) { default: class x { }; }"); | |
121 | |
122 // Test that redeclarations of functions are only allowed in outermost scope. | |
123 TestLocalThrows("{ let f; var f; }"); | |
124 TestLocalThrows("{ var f; let f; }"); | |
125 TestLocalThrows("{ function f() {} let f; }"); | |
126 TestLocalThrows("{ let f; function f() {} }"); | |
127 TestLocalThrows("{ function f() {} var f; }"); | |
128 TestLocalThrows("{ var f; function f() {} }"); | |
129 TestLocalThrows("{ function f() {} class f {} }"); | |
130 TestLocalThrows("{ class f {}; function f() {} }"); | |
131 TestLocalThrows("{ function f() {} function f() {} }"); | |
132 TestLocalThrows("function f() {} let f;"); | |
133 TestLocalThrows("let f; function f() {}"); | |
134 TestLocalThrows("function f() {} class f {}"); | |
135 TestLocalThrows("class f {}; function f() {}"); | |
136 TestLocalDoesNotThrow("function arg() {}"); | |
137 TestLocalDoesNotThrow("function f() {} var f;"); | |
138 TestLocalDoesNotThrow("var f; function f() {}"); | |
139 TestLocalDoesNotThrow("function f() {} function f() {}"); | |
140 | |
141 function g(f) { | |
142 function f() { return 1 } | |
143 return f() | |
144 } | |
145 assertEquals(1, g(function() { return 2 })) | |
146 | |
147 | |
148 // Test function declarations in source element and | |
149 // sloppy statement positions. | |
150 function f() { | |
151 // Sloppy source element positions. | |
152 function g0() { | |
153 "use strict"; | |
154 // Strict source element positions. | |
155 function h() { } | |
156 { | |
157 function h1() { } | |
158 } | |
159 } | |
160 { | |
161 function g1() { } | |
162 } | |
163 } | |
164 f(); | |
165 | |
166 // Test function declarations in statement position in strict mode. | |
167 TestLocalThrows("function f() { if (true) function g() {} }", SyntaxError); | |
168 TestLocalThrows("function f() { if (true) {} else function g() {} }", SyntaxErro
r); | |
169 TestLocalThrows("function f() { do function g() {} while (false) }", SyntaxError
); | |
170 TestLocalThrows("function f() { while (false) function g() {} }", SyntaxError); | |
171 TestLocalThrows("function f() { label: function g() {} }", SyntaxError); | |
172 TestLocalThrows("function f() { for (;false;) function g() {} }", SyntaxError); | |
173 TestLocalDoesNotThrow("function f() { switch (true) { case true: function g() {}
} }"); | |
174 TestLocalDoesNotThrow("function f() { switch (true) { default: function g() {} }
}"); | |
OLD | NEW |