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 // Flags: --harmony-scoping | |
29 | |
30 // Test let declarations in various settings. | |
31 | |
32 "use strict"; | |
33 | |
34 // Global | |
35 let x; | |
36 let y = 2; | |
37 const z = 4; | |
38 | |
39 // Block local | |
40 { | |
41 let y; | |
42 let x = 3; | |
43 const z = 5; | |
44 } | |
45 | |
46 assertEquals(undefined, x); | |
47 assertEquals(2,y); | |
48 assertEquals(4,z); | |
49 | |
50 if (true) { | |
51 let y; | |
52 assertEquals(undefined, y); | |
53 } | |
54 | |
55 // Invalid declarations are early errors in harmony mode and thus should trigger | |
56 // an exception in eval code during parsing, before even compiling or executing | |
57 // the code. Thus the generated function is not called here. | |
58 function TestLocalThrows(str, expect) { | |
59 assertThrows("(function(arg){ 'use strict'; " + str + "})", expect); | |
60 } | |
61 | |
62 function TestLocalDoesNotThrow(str) { | |
63 assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()"); | |
64 } | |
65 | |
66 // Test let declarations in statement positions. | |
67 TestLocalThrows("if (true) let x;", SyntaxError); | |
68 TestLocalThrows("if (true) {} else let x;", SyntaxError); | |
69 TestLocalThrows("do let x; while (false)", SyntaxError); | |
70 TestLocalThrows("while (false) let x;", SyntaxError); | |
71 TestLocalThrows("label: let x;", SyntaxError); | |
72 TestLocalThrows("for (;false;) let x;", SyntaxError); | |
73 TestLocalThrows("switch (true) { case true: let x; }", SyntaxError); | |
74 TestLocalThrows("switch (true) { default: let x; }", SyntaxError); | |
75 | |
76 // Test const declarations with initialisers in statement positions. | |
77 TestLocalThrows("if (true) const x = 1;", SyntaxError); | |
78 TestLocalThrows("if (true) {} else const x = 1;", SyntaxError); | |
79 TestLocalThrows("do const x = 1; while (false)", SyntaxError); | |
80 TestLocalThrows("while (false) const x = 1;", SyntaxError); | |
81 TestLocalThrows("label: const x = 1;", SyntaxError); | |
82 TestLocalThrows("for (;false;) const x = 1;", SyntaxError); | |
83 TestLocalThrows("switch (true) { case true: const x = 1; }", SyntaxError); | |
84 TestLocalThrows("switch (true) { default: const x = 1; }", SyntaxError); | |
85 | |
86 // Test const declarations without initialisers. | |
87 TestLocalThrows("const x;", SyntaxError); | |
88 TestLocalThrows("const x = 1, y;", SyntaxError); | |
89 TestLocalThrows("const x, y = 1;", SyntaxError); | |
90 | |
91 // Test const declarations without initialisers in statement positions. | |
92 TestLocalThrows("if (true) const x;", SyntaxError); | |
93 TestLocalThrows("if (true) {} else const x;", SyntaxError); | |
94 TestLocalThrows("do const x; while (false)", SyntaxError); | |
95 TestLocalThrows("while (false) const x;", SyntaxError); | |
96 TestLocalThrows("label: const x;", SyntaxError); | |
97 TestLocalThrows("for (;false;) const x;", SyntaxError); | |
98 TestLocalThrows("switch (true) { case true: const x; }", SyntaxError); | |
99 TestLocalThrows("switch (true) { default: const x; }", SyntaxError); | |
100 | |
101 // Test var declarations in statement positions. | |
102 TestLocalDoesNotThrow("if (true) var x;"); | |
103 TestLocalDoesNotThrow("if (true) {} else var x;"); | |
104 TestLocalDoesNotThrow("do var x; while (false)"); | |
105 TestLocalDoesNotThrow("while (false) var x;"); | |
106 TestLocalDoesNotThrow("label: var x;"); | |
107 TestLocalDoesNotThrow("for (;false;) var x;"); | |
108 TestLocalDoesNotThrow("switch (true) { case true: var x; }"); | |
109 TestLocalDoesNotThrow("switch (true) { default: var x; }"); | |
110 | |
111 // Test that redeclarations of functions are only allowed in outermost scope. | |
112 TestLocalThrows("{ let f; var f; }"); | |
113 TestLocalThrows("{ var f; let f; }"); | |
114 TestLocalThrows("{ function f() {} let f; }"); | |
115 TestLocalThrows("{ let f; function f() {} }"); | |
116 TestLocalThrows("{ function f() {} var f; }"); | |
117 TestLocalThrows("{ var f; function f() {} }"); | |
118 TestLocalThrows("{ function f() {} function f() {} }"); | |
119 TestLocalThrows("function f() {} let f;"); | |
120 TestLocalThrows("let f; function f() {}"); | |
121 TestLocalDoesNotThrow("function arg() {}"); | |
122 TestLocalDoesNotThrow("function f() {} var f;"); | |
123 TestLocalDoesNotThrow("var f; function f() {}"); | |
124 TestLocalDoesNotThrow("function f() {} function f() {}"); | |
125 | |
126 function g(f) { | |
127 function f() { return 1 } | |
128 return f() | |
129 } | |
130 assertEquals(1, g(function() { return 2 })) | |
131 | |
132 | |
133 // Test function declarations in source element and | |
134 // sloppy statement positions. | |
135 function f() { | |
136 // Sloppy source element positions. | |
137 function g0() { | |
138 "use strict"; | |
139 // Strict source element positions. | |
140 function h() { } | |
141 { | |
142 function h1() { } | |
143 } | |
144 } | |
145 { | |
146 function g1() { } | |
147 } | |
148 } | |
149 f(); | |
150 | |
151 // Test function declarations in statement position in strict mode. | |
152 TestLocalThrows("function f() { if (true) function g() {}", SyntaxError); | |
153 TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError)
; | |
154 TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError); | |
155 TestLocalThrows("function f() { while (false) function g() {}", SyntaxError); | |
156 TestLocalThrows("function f() { label: function g() {}", SyntaxError); | |
157 TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError); | |
158 TestLocalThrows("function f() { switch (true) { case true: function g() {} }", S
yntaxError); | |
159 TestLocalThrows("function f() { switch (true) { default: function g() {} }", Syn
taxError); | |
OLD | NEW |