test: remove obsolete harmony flags
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / harmony / block-const-assign.js
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 that we throw early syntax errors in harmony mode
31 // when using an immutable binding in an assigment or with
32 // prefix/postfix decrement/increment operators.
33
34 "use strict";
35
36 // Function local const.
37 function constDecl0(use) {
38   return "(function() { const constvar = 1; " + use + "; })();";
39 }
40
41
42 function constDecl1(use) {
43   return "(function() { " + use + "; const constvar = 1; })();";
44 }
45
46
47 // Function local const, assign from eval.
48 function constDecl2(use) {
49   use = "eval('(function() { " + use + " })')()";
50   return "(function() { const constvar = 1; " + use + "; })();";
51 }
52
53
54 function constDecl3(use) {
55   use = "eval('(function() { " + use + " })')()";
56   return "(function() { " + use + "; const constvar = 1; })();";
57 }
58
59
60 // Block local const.
61 function constDecl4(use) {
62   return "(function() { { const constvar = 1; " + use + "; } })();";
63 }
64
65
66 function constDecl5(use) {
67   return "(function() { { " + use + "; const constvar = 1; } })();";
68 }
69
70
71 // Block local const, assign from eval.
72 function constDecl6(use) {
73   use = "eval('(function() {" + use + "})')()";
74   return "(function() { { const constvar = 1; " + use + "; } })();";
75 }
76
77
78 function constDecl7(use) {
79   use = "eval('(function() {" + use + "})')()";
80   return "(function() { { " + use + "; const constvar = 1; } })();";
81 }
82
83
84 // Function expression name.
85 function constDecl8(use) {
86   return "(function constvar() { " + use + "; })();";
87 }
88
89
90 // Function expression name, assign from eval.
91 function constDecl9(use) {
92   use = "eval('(function(){" + use + "})')()";
93   return "(function constvar() { " + use + "; })();";
94 }
95
96 let decls = [ constDecl0,
97               constDecl1,
98               constDecl2,
99               constDecl3,
100               constDecl4,
101               constDecl5,
102               constDecl6,
103               constDecl7,
104               constDecl8,
105               constDecl9
106               ];
107 let declsForTDZ = new Set([constDecl1, constDecl3, constDecl5, constDecl7]);
108 let uses = [ 'constvar = 1;',
109              'constvar += 1;',
110              '++constvar;',
111              'constvar++;'
112              ];
113
114 function Test(d,u) {
115   'use strict';
116   try {
117     print(d(u));
118     eval(d(u));
119   } catch (e) {
120     if (declsForTDZ.has(d) && u !== uses[0]) {
121       // In these cases, read of a const variable occurs
122       // before a write to it, so TDZ kicks in before const check.
123       assertInstanceof(e, ReferenceError);
124       return;
125     }
126     assertInstanceof(e, TypeError);
127     assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0);
128     return;
129   }
130   assertUnreachable();
131 }
132
133 for (var d = 0; d < decls.length; ++d) {
134   for (var u = 0; u < uses.length; ++u) {
135     Test(decls[d], uses[u]);
136   }
137 }