1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_8/genexps/regress-380237-04.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,297 @@ 1.4 +// |reftest| skip -- obsolete test, need to remove minor failures to reenable. 1.5 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +//----------------------------------------------------------------------------- 1.11 +var BUGNUMBER = 380237; 1.12 +var summary = 'Generator expressions parenthesization test'; 1.13 +var actual = ''; 1.14 +var expect = ''; 1.15 + 1.16 + 1.17 +/* 1.18 + 1.19 +Given that parentheization seems so fragile *and* the rules for where 1.20 +genexps are allowed keep changing, I thought it would be good to have 1.21 +a way to test that: 1.22 + 1.23 +1) unparenthesized genexps are allowed in some places and the 1.24 +decompilation is sane and not over-parenthesized 1.25 + 1.26 +2) unparenthesized genexps are disallowed in many places and when 1.27 +there are parens, the decompilation is sane and not over-parenthesized 1.28 + 1.29 +*/ 1.30 + 1.31 +// |genexp| must have the exact same whitespace the decompiler uses 1.32 +genexp = "x * x for (x in [])"; 1.33 +genexpParened = "(" + genexp + ")"; 1.34 +genexpParenedTwice = "(" + genexpParened + ")"; 1.35 + 1.36 +// Warning: be careful not to put [] around stuff, because that would 1.37 +// cause it to be treated as an array comprehension instead of a 1.38 +// generator expression! 1.39 + 1.40 +// Statements 1.41 +doesNotNeedParens(1, "if (xx) { }"); 1.42 +needParens(2, "if (1, xx) { }"); 1.43 +needParens(3, "if (xx, 1) { }"); 1.44 +doesNotNeedParens(4, "do { } while (xx);"); 1.45 +doesNotNeedParens(5, "while (xx) { }"); 1.46 +doesNotNeedParens(6, "switch (xx) { }"); 1.47 +doesNotNeedParens(7, "with (xx) { }"); 1.48 +needParens(8, "switch (x) { case xx: }"); 1.49 +needParens(9, "return xx;"); 1.50 +needParens(10, "yield xx;"); 1.51 +needParens(11, "for (xx;;) { }"); 1.52 +needParens(12, "for (;xx;) { }", "function anonymous() {\n for (;;) {\n }\n}"); 1.53 +needParens(13, "for (;;xx) { }"); 1.54 +needParens(14, "for (i in xx) { }"); 1.55 +needParens(15, "throw xx"); 1.56 +needParens(16, "try { } catch (e if xx) { }"); 1.57 +needParens(17, "let (x=3) xx"); 1.58 +needParens(18, "let (x=xx) 3"); 1.59 + 1.60 +// Function calls 1.61 +doesNotNeedParens(19, "f(xx);"); 1.62 +needParens(20, "f(xx, 1);"); 1.63 +needParens(21, "f(1, xx);"); 1.64 +doesNotNeedParens(22, "/x/(xx);"); 1.65 +needParens(23, "/x/(xx, 1);"); 1.66 +needParens(24, "/x/(1, xx);"); 1.67 + 1.68 +// eval is special and often confuses the decompiler. 1.69 +doesNotNeedParens(25, "eval(xx);"); 1.70 +needParens(26, "eval(xx, 1);"); 1.71 +needParens(27, "eval(1, xx);"); 1.72 + 1.73 +// Expressions 1.74 +needParens(28, "xx;"); // ??? 1.75 +needParens(29, "var g = xx;"); // ??? 1.76 +needParens(30, "g += xx;"); 1.77 +needParens(31, "xx();"); 1.78 +needParens(32, "xx() = 3;"); 1.79 +needParens(33, "a ? xx : c"); 1.80 +needParens(34, "xx ? b : c"); 1.81 +needParens(35, "a ? b : xx"); 1.82 +needParens(36, "1 ? xx : c"); 1.83 +needParens(37, "0 ? b : xx"); 1.84 +needParens(38, "1 + xx"); 1.85 +needParens(39, "xx + 1"); 1.86 +needParens(40, "1, xx"); 1.87 +doesNotNeedParens(41, "+(xx)"); 1.88 +doesNotNeedParens(42, "!(xx)"); 1.89 +needParens(43, "xx, 1"); 1.90 +needParens(44, "[1, xx]"); 1.91 +needParens(45, "[xx, 1]"); 1.92 +needParens(46, "[xx,3]"); 1.93 +needParens(47, "[xx,null]"); 1.94 +needParens(48, "xx.p"); 1.95 +needParens(49, "xx.@p"); 1.96 +needParens(50, "typeof xx;"); 1.97 +needParens(51, "void xx;"); 1.98 +needParens(52, "({ a: xx })"); 1.99 +needParens(53, "({ a: 1, b: xx })"); 1.100 +needParens(54, "({ a: xx, b: 1 })"); 1.101 +needParens(55, "({ a getter: xx })"); 1.102 +needParens(56, "<x a={xx}/>"); 1.103 +doesNotNeedParens(57, "new (xx);"); 1.104 +doesNotNeedParens(58, "new a(xx);"); 1.105 + 1.106 + 1.107 +// Generator expressions cannot be used as LHS, even though they're syntactic 1.108 +// sugar for something that looks a lot like an "lvalue return": (f() = 3). 1.109 + 1.110 +rejectLHS(59, "++ (xx);"); 1.111 +rejectLHS(60, "delete xx;"); 1.112 +rejectLHS(61, "delete (xx);"); 1.113 +rejectLHS(62, "for (xx in []) { }"); 1.114 +rejectLHS(63, "for ((xx) in []) { }"); 1.115 +rejectLHS(64, "try { } catch(xx) { }"); 1.116 +rejectLHS(65, "try { } catch([(xx)]) { }"); 1.117 +rejectLHS(66, "xx += 3;"); 1.118 +rejectLHS(67, "(xx) += 3;"); 1.119 +rejectLHS(68, "xx = 3;"); 1.120 + 1.121 +// Assignment 1.122 +rejectLHS(69, " (xx) = 3;"); 1.123 +rejectLHS(70, "var (xx) = 3;"); 1.124 +rejectLHS(71, "const (xx) = 3;"); 1.125 +rejectLHS(72, "let (xx) = 3;"); 1.126 + 1.127 +// Destructuring assignment 1.128 +rejectLHS(73, " [(xx)] = 3;"); 1.129 +rejectLHS(74, "var [(xx)] = 3;"); 1.130 +rejectLHS(75, "const [(xx)] = 3;"); 1.131 +rejectLHS(76, "let [(xx)] = 3;"); 1.132 + 1.133 +// Group assignment (Spidermonkey optimization for certain 1.134 +// destructuring assignments) 1.135 +rejectLHS(77, " [(xx)] = [3];"); 1.136 +rejectLHS(78, "var [(xx)] = [3];"); 1.137 +rejectLHS(79, "const [(xx)] = [3];"); 1.138 +rejectLHS(80, "let [(xx)] = [3];"); 1.139 + 1.140 +// Destructuring & group assignment for array comprehensions, just for kicks. 1.141 +rejectLHS(81, " [xx] = [3];"); 1.142 +rejectLHS(82, "var [xx] = [3];"); 1.143 +rejectLHS(83, "const [xx] = [3];"); 1.144 +rejectLHS(84, "let [xx] = 3;"); 1.145 +rejectLHS(85, " [xx] = 3;"); 1.146 +rejectLHS(86, "var [xx] = 3;"); 1.147 +rejectLHS(87, "const [xx] = 3;"); 1.148 +rejectLHS(88, "let [xx] = 3;"); 1.149 + 1.150 +// This is crazy, ambiguous, and/or buggy. 1.151 +// See https://bugzilla.mozilla.org/show_bug.cgi?id=380237#c23 et seq. 1.152 +//doesNotNeedParens("(yield xx);"); 1.153 + 1.154 +print("Done!"); 1.155 + 1.156 +function doesNotNeedParens(section, pat) 1.157 +{ 1.158 + print("Testing section " + section + " pattern " + pat); 1.159 + 1.160 + var f, ft; 1.161 + sanityCheck(section, pat); 1.162 + 1.163 + expect = 'No Error'; 1.164 + actual = ''; 1.165 + ft = pat.replace(/xx/, genexp); 1.166 + try { 1.167 + f = new Function(ft); 1.168 + actual = 'No Error'; 1.169 + } catch(e) { 1.170 + print("Unparenthesized genexp SHOULD have been accepted here!"); 1.171 + actual = e + ''; 1.172 + } 1.173 + reportCompare(expect, actual, summary + ': doesNotNeedParens section ' + section + ' pattern ' + pat); 1.174 + 1.175 + roundTripTest(section, f); 1.176 + 1.177 + // Make sure the decompilation is not over-parenthesized. 1.178 + var uf = "" + f; 1.179 + if (pat.indexOf("(xx)") != -1) 1.180 + overParenTest(section, f); 1.181 + // else 1.182 + // 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.") 1.183 +} 1.184 + 1.185 +function needParens(section, pat, exp) 1.186 +{ 1.187 + print("Testing section " + section + " pattern " + pat); 1.188 + 1.189 + var f, ft; 1.190 + sanityCheck(section, pat); 1.191 + 1.192 + expect = 'SyntaxError'; 1.193 + actual = ''; 1.194 + ft = pat.replace(/xx/, genexp); 1.195 + try { 1.196 + f = new Function(ft); 1.197 + print("Unparenthesized genexp should NOT have been accepted here!"); 1.198 + } catch(e) { 1.199 + /* expected to throw */ 1.200 + actual = e.name; 1.201 + } 1.202 + reportCompare(expect, actual, summary + ': needParens section ' + section + ' pattern ' + pat); 1.203 + 1.204 + expect = 'No Error'; 1.205 + actual = ''; 1.206 + ft = pat.replace(/xx/, genexpParened); 1.207 + try { 1.208 + f = new Function(ft); 1.209 + actual = 'No Error'; 1.210 + } catch(e) { 1.211 + print("Yikes!"); 1.212 + actual = e + ''; 1.213 + } 1.214 + reportCompare(expect, actual, summary + ': needParens section ' + section + ' ft ' + ft); 1.215 + 1.216 + roundTripTest(section, f, exp); 1.217 + overParenTest(section, f, exp); 1.218 +} 1.219 + 1.220 +function rejectLHS(section, pat) 1.221 +{ 1.222 + print("Testing section " + section + " pattern " + pat); 1.223 + 1.224 + // sanityCheck(pat); // because 'z' should be accepted as an LHS or binding 1.225 + 1.226 + var ft; 1.227 + 1.228 + expect = 'SyntaxError'; 1.229 + actual = ''; 1.230 + ft = pat.replace(/xx/, genexp) 1.231 + try { 1.232 + new Function(ft); 1.233 + print("That should have been a syntax error!"); 1.234 + actual = 'No Error'; 1.235 + } catch(e) { 1.236 + actual = e.name; 1.237 + } 1.238 + reportCompare(expect, actual, summary + ': rejectLHS section ' + section); 1.239 +} 1.240 + 1.241 + 1.242 +function overParenTest(section, f, exp) 1.243 +{ 1.244 + var uf = "" + f; 1.245 + if (uf == exp) 1.246 + return; 1.247 + 1.248 + reportCompare(false, uf.indexOf(genexpParened) == -1, summary + 1.249 + ': overParenTest genexp snugly in parentheses: section ' + section + ' uf ' + uf); 1.250 + 1.251 + if (uf.indexOf(genexpParened) != -1) { 1.252 + reportCompare(true, uf.indexOf(genexpParenedTwice) == -1, summary + 1.253 + ': overParensTest decompilation should not be over-parenthesized: section ' + ' uf ' + uf); 1.254 + } 1.255 +} 1.256 + 1.257 +function sanityCheck(section, pat) 1.258 +{ 1.259 + expect = ''; 1.260 + actual = ''; 1.261 + 1.262 + if (pat.indexOf("xx") == -1) 1.263 + { 1.264 + actual += "No 'xx' in this pattern? "; 1.265 + } 1.266 + 1.267 + var f, ft; 1.268 + ft = pat.replace(/xx/, "z"); 1.269 + try { 1.270 + f = new Function(ft); 1.271 + } catch(e) { 1.272 + actual += "Yowzers! Probably a bogus test!"; 1.273 + } 1.274 + reportCompare(expect, actual, summary + ': sanityCheck section ' + section + ' pattern ' + pat); 1.275 +} 1.276 + 1.277 +function roundTripTest(section, f, exp) 1.278 +{ 1.279 + // Decompile 1.280 + var uf = "" + f; 1.281 + 1.282 + // Recompile 1.283 + expect = 'No Error'; 1.284 + actual = ''; 1.285 + var euf; 1.286 + try { 1.287 + euf = eval("(" + uf + ")"); 1.288 + actual = 'No Error'; 1.289 + reportCompare(expect, actual, summary + ': roundTripTest: section ' + section + ' uf ' + uf); 1.290 + } catch(e) { 1.291 + actual = e + ''; 1.292 + reportCompare(expect, actual, summary + ': roundTripTest: section ' + section + ' uf ' + uf); 1.293 + return; 1.294 + } 1.295 + 1.296 + // Decompile again and make sure the decompilations match exactly. 1.297 + expect = exp || uf; 1.298 + actual = "" + euf; 1.299 + reportCompare(expect, actual, summary + ': roundTripTest no round-trip change: section ' + section); 1.300 +}