Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / const-redecl.js
1 // Copyright 2008 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 for const semantics.
29
30
31 function CheckException(e) {
32   var string = e.toString();
33   var index = string.indexOf(':');
34   assertTrue(index >= 0);
35   var name = string.slice(0, index);
36   assertTrue(string.indexOf("has already been declared") >= 0 ||
37              string.indexOf("redeclaration") >= 0);
38   if (name == 'SyntaxError') return 'TypeError';
39   return name;
40 }
41
42
43 function TestLocal(s,e) {
44   try {
45     return eval("(function(){" + s + ";return " + e + "})")();
46   } catch (x) {
47     return CheckException(x);
48   }
49 }
50
51
52 function TestContext(s,e) {
53   try {
54     // Use a with-statement to force the system to do dynamic
55     // declarations of the introduced variables or constants.
56     with ({}) {
57       return eval(s + ";" + e);
58     }
59   } catch (x) {
60     return CheckException(x);
61   }
62 }
63
64
65 function TestAll(expected,s,opt_e) {
66   var e = "";
67   var msg = s;
68   if (opt_e) { e = opt_e; msg += "; " + opt_e; }
69   assertEquals(expected, TestLocal(s,e), "local:'" + msg + "'");
70   assertEquals(expected, TestContext(s,e), "context:'" + msg + "'");
71 }
72
73
74 function TestConflict(def0, def1) {
75   // No eval.
76   TestAll("TypeError", def0 +'; ' + def1);
77   // Eval everything.
78   TestAll("TypeError", 'eval("' + def0 + '; ' + def1 + '")');
79   // Eval first definition.
80   TestAll("TypeError", 'eval("' + def0 +'"); ' + def1);
81   // Eval second definition.
82   TestAll("TypeError", def0 + '; eval("' + def1 +'")');
83   // Eval both definitions separately.
84   TestAll("TypeError", 'eval("' + def0 +'"); eval("' + def1 + '")');
85 }
86
87
88 // Test conflicting definitions.
89 TestConflict("const x", "var x");
90 TestConflict("const x = 0", "var x");
91 TestConflict("const x", "var x = 0");
92 TestConflict("const x = 0", "var x = 0");
93
94 TestConflict("var x", "const x");
95 TestConflict("var x = 0", "const x");
96 TestConflict("var x", "const x = 0");
97 TestConflict("var x = 0", "const x = 0");
98
99 TestConflict("const x = undefined", "var x");
100 TestConflict("const x", "var x = undefined");
101 TestConflict("const x = undefined", "var x = undefined");
102
103 TestConflict("var x = undefined", "const x");
104 TestConflict("var x", "const x = undefined");
105 TestConflict("var x = undefined", "const x = undefined");
106
107 TestConflict("const x = undefined", "var x = 0");
108 TestConflict("const x = 0", "var x = undefined");
109
110 TestConflict("var x = undefined", "const x = 0");
111 TestConflict("var x = 0", "const x = undefined");
112
113 TestConflict("const x", "function x() { }");
114 TestConflict("const x = 0", "function x() { }");
115 TestConflict("const x = undefined", "function x() { }");
116
117 TestConflict("function x() { }", "const x");
118 TestConflict("function x() { }", "const x = 0");
119 TestConflict("function x() { }", "const x = undefined");
120
121 TestConflict("const x, y", "var x");
122 TestConflict("const x, y", "var y");
123 TestConflict("const x = 0, y", "var x");
124 TestConflict("const x = 0, y", "var y");
125 TestConflict("const x, y = 0", "var x");
126 TestConflict("const x, y = 0", "var y");
127 TestConflict("const x = 0, y = 0", "var x");
128 TestConflict("const x = 0, y = 0", "var y");
129
130 TestConflict("var x", "const x, y");
131 TestConflict("var y", "const x, y");
132 TestConflict("var x", "const x = 0, y");
133 TestConflict("var y", "const x = 0, y");
134 TestConflict("var x", "const x, y = 0");
135 TestConflict("var y", "const x, y = 0");
136 TestConflict("var x", "const x = 0, y = 0");
137 TestConflict("var y", "const x = 0, y = 0");
138
139
140 // Test that multiple conflicts do not cause issues.
141 TestConflict("var x, y", "const x, y");
142
143
144 // Test that repeated const declarations throw redeclaration errors.
145 TestConflict("const x", "const x");
146 TestConflict("const x = 0", "const x");
147 TestConflict("const x", "const x = 0");
148 TestConflict("const x = 0", "const x = 0");
149
150 TestConflict("const x = undefined", "const x");
151 TestConflict("const x", "const x = undefined");
152 TestConflict("const x = undefined", "const x = undefined");
153
154 TestConflict("const x = undefined", "const x = 0");
155 TestConflict("const x = 0", "const x = undefined");
156
157 TestConflict("const x, y", "const x");
158 TestConflict("const x, y", "const y");
159 TestConflict("const x = 0, y", "const x");
160 TestConflict("const x = 0, y", "const y");
161 TestConflict("const x, y = 0", "const x");
162 TestConflict("const x, y = 0", "const y");
163 TestConflict("const x = 0, y = 0", "const x");
164 TestConflict("const x = 0, y = 0", "const y");
165
166 TestConflict("const x", "const x, y");
167 TestConflict("const y", "const x, y");
168 TestConflict("const x", "const x = 0, y");
169 TestConflict("const y", "const x = 0, y");
170 TestConflict("const x", "const x, y = 0");
171 TestConflict("const y", "const x, y = 0");
172 TestConflict("const x", "const x = 0, y = 0");
173 TestConflict("const y", "const x = 0, y = 0");
174
175
176 // Test that multiple const conflicts do not cause issues.
177 TestConflict("const x, y", "const x, y");
178
179
180 // Test that const inside loop behaves correctly.
181 var loop = "for (var i = 0; i < 3; i++) { const x = i; }";
182 TestAll(0, loop, "x");
183 TestAll(0, "var a,b,c,d,e,f,g,h; " + loop, "x");
184
185
186 // Test that const inside with behaves correctly.
187 TestAll(87, "with ({x:42}) { const x = 87; }", "x");
188 TestAll(undefined, "with ({x:42}) { const x; }", "x");
189
190
191 // Additional tests for how various combinations of re-declarations affect
192 // the values of the var/const in question.
193 try {
194   eval("var undefined;");
195 } catch (ex) {
196   assertUnreachable("undefined (1) has thrown");
197 }
198
199 var original_undef = undefined;
200 var undefined = 1;  // Should be silently ignored.
201 assertEquals(original_undef, undefined, "undefined got overwritten");
202 undefined = original_undef;
203
204 const e = 1; eval('var e = 2');
205 assertEquals(1, e, "e has wrong value");
206
207 const h; eval('var h = 1');
208 assertEquals(undefined, h, "h has wrong value");
209
210 eval("Object.defineProperty(this, 'i', { writable: true });"
211    + "const i = 7;"
212    + "assertEquals(7, i, \"i has wrong value\");");
213
214 var global = this;
215 Object.defineProperty(global, 'j', { value: 100, writable: true });
216 assertEquals(100, j);
217 // The const declaration stays configurable, so the declaration above goes
218 // through even though the const declaration is hoisted above.
219 const j = 2;
220 assertEquals(2, j, "j has wrong value");
221
222 var k = 1;
223 try { eval('const k'); } catch(e) { }
224 assertEquals(1, k, "k has wrong value");
225 try { eval('const k = 10'); } catch(e) { }
226 assertEquals(1, k, "k has wrong value");