1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is JavaScript Engine testing utilities.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s): Jesse Ruderman
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 //-----------------------------------------------------------------------------
39 var BUGNUMBER = 380237;
40 var summary = 'Generator expressions parenthesization test';
47 Given that parentheization seems so fragile *and* the rules for where
48 genexps are allowed keep changing, I thought it would be good to have
51 1) unparenthesized genexps are allowed in some places and the
52 decompilation is sane and not over-parenthesized
54 2) unparenthesized genexps are disallowed in many places and when
55 there are parens, the decompilation is sane and not over-parenthesized
59 // |genexp| must have the exact same whitespace the decompiler uses
60 genexp = "x * x for (x in [])";
61 genexpParened = "(" + genexp + ")";
62 genexpParenedTwice = "(" + genexpParened + ")";
64 // Warning: be careful not to put [] around stuff, because that would
65 // cause it to be treated as an array comprehension instead of a
66 // generator expression!
69 doesNotNeedParens(1, "if (xx) { }");
70 needParens(2, "if (1, xx) { }");
71 needParens(3, "if (xx, 1) { }");
72 doesNotNeedParens(4, "do { } while (xx);");
73 doesNotNeedParens(5, "while (xx) { }");
74 doesNotNeedParens(6, "switch (xx) { }");
75 doesNotNeedParens(7, "with (xx) { }");
76 needParens(8, "switch (x) { case xx: }");
77 needParens(9, "return xx;");
78 needParens(10, "yield xx;");
79 needParens(11, "for (xx;;) { }");
80 needParens(12, "for (;xx;) { }", "function anonymous() {\n for (;;) {\n }\n}");
81 needParens(13, "for (;;xx) { }");
82 needParens(14, "for (i in xx) { }");
83 needParens(15, "throw xx");
84 needParens(16, "try { } catch (e if xx) { }");
85 needParens(17, "let (x=3) xx");
86 needParens(18, "let (x=xx) 3");
89 doesNotNeedParens(19, "f(xx);");
90 needParens(20, "f(xx, 1);");
91 needParens(21, "f(1, xx);");
92 doesNotNeedParens(22, "/x/(xx);");
93 needParens(23, "/x/(xx, 1);");
94 needParens(24, "/x/(1, xx);");
96 // eval is special and often confuses the decompiler.
97 doesNotNeedParens(25, "eval(xx);");
98 needParens(26, "eval(xx, 1);");
99 needParens(27, "eval(1, xx);");
102 needParens(28, "xx;"); // ???
103 needParens(29, "var g = xx;"); // ???
104 needParens(30, "g += xx;");
105 needParens(31, "xx();");
106 needParens(32, "xx() = 3;");
107 needParens(33, "a ? xx : c");
108 needParens(34, "xx ? b : c");
109 needParens(35, "a ? b : xx");
110 needParens(36, "1 ? xx : c");
111 needParens(37, "0 ? b : xx");
112 needParens(38, "1 + xx");
113 needParens(39, "xx + 1");
114 needParens(40, "1, xx");
115 doesNotNeedParens(41, "+(xx)");
116 doesNotNeedParens(42, "!(xx)");
117 needParens(43, "xx, 1");
118 needParens(44, "[1, xx]");
119 needParens(45, "[xx, 1]");
120 needParens(46, "[#1=xx,3]");
121 needParens(47, "[#1=xx,#1#]");
122 needParens(48, "xx.p");
123 needParens(49, "xx.@p");
124 needParens(50, "typeof xx;");
125 needParens(51, "void xx;");
126 needParens(52, "({ a: xx })");
127 needParens(53, "({ a: 1, b: xx })");
128 needParens(54, "({ a: xx, b: 1 })");
129 needParens(55, "({ a getter: xx })");
130 needParens(56, "<x a={xx}/>");
131 doesNotNeedParens(57, "new (xx);");
132 doesNotNeedParens(58, "new a(xx);");
135 // Generator expressions cannot be used as LHS, even though they're syntactic
136 // sugar for something that looks a lot like an "lvalue return": (f() = 3).
138 rejectLHS(59, "++ (xx);");
139 rejectLHS(60, "delete xx;");
140 rejectLHS(61, "delete (xx);");
141 rejectLHS(62, "for (xx in []) { }");
142 rejectLHS(63, "for ((xx) in []) { }");
143 rejectLHS(64, "try { } catch(xx) { }");
144 rejectLHS(65, "try { } catch([(xx)]) { }");
145 rejectLHS(66, "xx += 3;");
146 rejectLHS(67, "(xx) += 3;");
147 rejectLHS(68, "xx = 3;");
150 rejectLHS(69, " (xx) = 3;");
151 rejectLHS(70, "var (xx) = 3;");
152 rejectLHS(71, "const (xx) = 3;");
153 rejectLHS(72, "let (xx) = 3;");
155 // Destructuring assignment
156 rejectLHS(73, " [(xx)] = 3;");
157 rejectLHS(74, "var [(xx)] = 3;");
158 rejectLHS(75, "const [(xx)] = 3;");
159 rejectLHS(76, "let [(xx)] = 3;");
161 // Group assignment (Spidermonkey optimization for certain
162 // destructuring assignments)
163 rejectLHS(77, " [(xx)] = [3];");
164 rejectLHS(78, "var [(xx)] = [3];");
165 rejectLHS(79, "const [(xx)] = [3];");
166 rejectLHS(80, "let [(xx)] = [3];");
168 // Destructuring & group assignment for array comprehensions, just for kicks.
169 rejectLHS(81, " [xx] = [3];");
170 rejectLHS(82, "var [xx] = [3];");
171 rejectLHS(83, "const [xx] = [3];");
172 rejectLHS(84, "let [xx] = 3;");
173 rejectLHS(85, " [xx] = 3;");
174 rejectLHS(86, "var [xx] = 3;");
175 rejectLHS(87, "const [xx] = 3;");
176 rejectLHS(88, "let [xx] = 3;");
178 // This is crazy, ambiguous, and/or buggy.
179 // See https://bugzilla.mozilla.org/show_bug.cgi?id=380237#c23 et seq.
180 //doesNotNeedParens("(yield xx);");
184 function doesNotNeedParens(section, pat)
186 print("Testing section " + section + " pattern " + pat);
189 sanityCheck(section, pat);
193 ft = pat.replace(/xx/, genexp);
195 f = new Function(ft);
198 print("Unparenthesized genexp SHOULD have been accepted here!");
201 reportCompare(expect, actual, summary + ': doesNotNeedParens section ' + section + ' pattern ' + pat);
203 roundTripTest(section, f);
205 // Make sure the decompilation is not over-parenthesized.
207 if (pat.indexOf("(xx)") != -1)
208 overParenTest(section, f);
210 // print("Skipping the over-parenthesization test, because I don't know how to test for over-parenthesization when the pattern doesn't have parens snugly around it.")
213 function needParens(section, pat, exp)
215 print("Testing section " + section + " pattern " + pat);
218 sanityCheck(section, pat);
220 expect = 'SyntaxError';
222 ft = pat.replace(/xx/, genexp);
224 f = new Function(ft);
225 print("Unparenthesized genexp should NOT have been accepted here!");
227 /* expected to throw */
230 reportCompare(expect, actual, summary + ': needParens section ' + section + ' pattern ' + pat);
234 ft = pat.replace(/xx/, genexpParened);
236 f = new Function(ft);
242 reportCompare(expect, actual, summary + ': needParens section ' + section + ' ft ' + ft);
244 roundTripTest(section, f, exp);
245 overParenTest(section, f, exp);
248 function rejectLHS(section, pat)
250 print("Testing section " + section + " pattern " + pat);
252 // sanityCheck(pat); // because 'z' should be accepted as an LHS or binding
256 expect = 'SyntaxError';
258 ft = pat.replace(/xx/, genexp)
261 print("That should have been a syntax error!");
266 reportCompare(expect, actual, summary + ': rejectLHS section ' + section);
270 function overParenTest(section, f, exp)
276 reportCompare(false, uf.indexOf(genexpParened) == -1, summary +
277 ': overParenTest genexp snugly in parentheses: section ' + section + ' uf ' + uf);
279 if (uf.indexOf(genexpParened) != -1) {
280 reportCompare(true, uf.indexOf(genexpParenedTwice) == -1, summary +
281 ': overParensTest decompilation should not be over-parenthesized: section ' + ' uf ' + uf);
285 function sanityCheck(section, pat)
290 if (pat.indexOf("xx") == -1)
292 actual += "No 'xx' in this pattern? ";
296 ft = pat.replace(/xx/, "z");
298 f = new Function(ft);
300 actual += "Yowzers! Probably a bogus test!";
302 reportCompare(expect, actual, summary + ': sanityCheck section ' + section + ' pattern ' + pat);
305 function roundTripTest(section, f, exp)
315 euf = eval("(" + uf + ")");
317 reportCompare(expect, actual, summary + ': roundTripTest: section ' + section + ' uf ' + uf);
320 reportCompare(expect, actual, summary + ': roundTripTest: section ' + section + ' uf ' + uf);
324 // Decompile again and make sure the decompilations match exactly.
327 reportCompare(expect, actual, summary + ': roundTripTest no round-trip change: section ' + section);