Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/Bug708801Test.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /**
6 *
7 */
8 package org.mozilla.javascript.tests;
9
10 import static java.lang.String.format;
11 import static java.util.Arrays.asList;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertTrue;
17
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import org.junit.Test;
22 import org.mozilla.javascript.CompilerEnvirons;
23 import org.mozilla.javascript.Context;
24 import org.mozilla.javascript.ContextAction;
25 import org.mozilla.javascript.ContextFactory;
26 import org.mozilla.javascript.ErrorReporter;
27 import org.mozilla.javascript.IRFactory;
28 import org.mozilla.javascript.Parser;
29 import org.mozilla.javascript.ScriptableObject;
30 import org.mozilla.javascript.ast.AstRoot;
31 import org.mozilla.javascript.ast.FunctionNode;
32 import org.mozilla.javascript.ast.ScriptNode;
33 import org.mozilla.javascript.optimizer.Codegen;
34 import org.mozilla.javascript.optimizer.OptFunctionNode;
35
36 /**
37 * @author André Bargull
38 *
39 */
40 public class Bug708801Test {
41 private static final ContextFactory factory = new ContextFactory() {
42 static final int COMPILER_MODE = 9;
43
44 @Override
45 protected Context makeContext() {
46 Context cx = super.makeContext();
47 cx.setLanguageVersion(Context.VERSION_1_8);
48 cx.setOptimizationLevel(COMPILER_MODE);
49 return cx;
50 }
51 };
52
53 private static abstract class Action implements ContextAction {
54 protected Context cx;
55 protected ScriptableObject scope;
56
57 @SuppressWarnings("unused")
58 protected Object evaluate(String s) {
59 return cx.evaluateString(scope, s, "<eval>", 1, null);
60 }
61
62 /**
63 * Compiles {@code source} and returns the transformed and optimized
64 * {@link ScriptNode}
65 */
66 protected ScriptNode compile(CharSequence source) {
67 final String mainMethodClassName = "Main";
68 final String scriptClassName = "Main";
69
70 CompilerEnvirons compilerEnv = new CompilerEnvirons();
71 compilerEnv.initFromContext(cx);
72 ErrorReporter compilationErrorReporter = compilerEnv
73 .getErrorReporter();
74 Parser p = new Parser(compilerEnv, compilationErrorReporter);
75 AstRoot ast = p.parse(source.toString(), "<eval>", 1);
76 IRFactory irf = new IRFactory(compilerEnv);
77 ScriptNode tree = irf.transformTree(ast);
78
79 Codegen codegen = new Codegen();
80 codegen.setMainMethodClass(mainMethodClassName);
81 codegen.compileToClassFile(compilerEnv, scriptClassName, tree,
82 tree.getEncodedSource(), false);
83
84 return tree;
85 }
86
87 /**
88 * Checks every variable {@code v} in {@code source} is marked as a
89 * number-variable iff {@code numbers} contains {@code v}
90 */
91 protected void assertNumberVars(CharSequence source, String... numbers) {
92 // wrap source in function
93 ScriptNode tree = compile("function f(o, fn){" + source + "}");
94
95 FunctionNode fnode = tree.getFunctionNode(0);
96 assertNotNull(fnode);
97 OptFunctionNode opt = OptFunctionNode.get(fnode);
98 assertNotNull(opt);
99 assertSame(fnode, opt.fnode);
100
101 for (int i = 0, c = fnode.getParamCount(); i < c; ++i) {
102 assertTrue(opt.isParameter(i));
103 assertFalse(opt.isNumberVar(i));
104 }
105
106 Set<String> set = new HashSet<String>(asList(numbers));
107 for (int i = fnode.getParamCount(), c = fnode.getParamAndVarCount(); i < c; ++i) {
108 assertFalse(opt.isParameter(i));
109 String name = fnode.getParamOrVarName(i);
110 String msg = format("{%s -> number? = %b}", name, opt.isNumberVar(i));
111 assertEquals(msg, set.contains(name), opt.isNumberVar(i));
112 }
113 }
114
115 public final Object run(Context cx) {
116 this.cx = cx;
117 scope = cx.initStandardObjects(null, true);
118 return run();
119 }
120
121 protected abstract Object run();
122 }
123
124 @Test
125 public void testIncDec() {
126 factory.call(new Action() {
127 @Override
128 protected Object run() {
129 // pre-inc
130 assertNumberVars("var b; ++b");
131 assertNumberVars("var b=0; ++b", "b");
132 assertNumberVars("var b; ++[1][++b]");
133 assertNumberVars("var b=0; ++[1][++b]", "b");
134 assertNumberVars("var b; ++[1][b=0,++b]", "b");
135 // pre-dec
136 assertNumberVars("var b; --b");
137 assertNumberVars("var b=0; --b", "b");
138 assertNumberVars("var b; --[1][--b]");
139 assertNumberVars("var b=0; --[1][--b]", "b");
140 assertNumberVars("var b; --[1][b=0,--b]", "b");
141 // post-inc
142 assertNumberVars("var b; b++");
143 assertNumberVars("var b=0; b++", "b");
144 assertNumberVars("var b; [1][b++]++");
145 assertNumberVars("var b=0; [1][b++]++", "b");
146 assertNumberVars("var b; [1][b=0,b++]++", "b");
147 // post-dec
148 assertNumberVars("var b; b--");
149 assertNumberVars("var b=0; b--", "b");
150 assertNumberVars("var b; [1][b--]--");
151 assertNumberVars("var b=0; [1][b--]--", "b");
152 assertNumberVars("var b; [1][b=0,b--]--", "b");
153 return null;
154 }
155 });
156 }
157
158 @Test
159 public void testTypeofName() {
160 factory.call(new Action() {
161 @Override
162 protected Object run() {
163 // Token.TYPEOFNAME
164 assertNumberVars("var b; typeof b");
165 assertNumberVars("var b=0; typeof b", "b");
166 assertNumberVars("var b; if(new Date()<0){b=0} typeof b");
167 // Token.TYPEOF
168 assertNumberVars("var b; typeof (b,b)");
169 assertNumberVars("var b=0; typeof (b,b)", "b");
170 assertNumberVars("var b; if(new Date()<0){b=0} typeof (b,b)");
171
172 return null;
173 }
174 });
175 }
176
177 @Test
178 public void testEval() {
179 factory.call(new Action() {
180 @Override
181 protected Object run() {
182 // direct eval => requires activation
183 assertNumberVars("var b; eval('typeof b')");
184 assertNumberVars("var b=0; eval('typeof b')");
185 assertNumberVars("var b; if(new Date()<0){b=0} eval('typeof b')");
186
187 // indirect eval => requires no activation
188 assertNumberVars("var b; (1,eval)('typeof b');");
189 assertNumberVars("var b=0; (1,eval)('typeof b')", "b");
190 assertNumberVars(
191 "var b; if(new Date()<0){b=0} (1,eval)('typeof b')",
192 "b");
193
194 return null;
195 }
196 });
197 }
198
199 @Test
200 public void testRelOp() {
201 factory.call(new Action() {
202 @Override
203 protected Object run() {
204 // relational operators: <, <=, >, >=
205 assertNumberVars("var b = 1 < 1");
206 assertNumberVars("var b = 1 <= 1");
207 assertNumberVars("var b = 1 > 1");
208 assertNumberVars("var b = 1 >= 1");
209 // equality operators: ==, !=, ===, !==
210 assertNumberVars("var b = 1 == 1");
211 assertNumberVars("var b = 1 != 1");
212 assertNumberVars("var b = 1 === 1");
213 assertNumberVars("var b = 1 !== 1");
214
215 return null;
216 }
217 });
218 }
219
220 @Test
221 public void testMore() {
222 factory.call(new Action() {
223 @Override
224 protected Object run() {
225 // simple assignment:
226 assertNumberVars("var b");
227 assertNumberVars("var b = 1", "b");
228 assertNumberVars("var b = 'a'");
229 assertNumberVars("var b = true");
230 assertNumberVars("var b = /(?:)/");
231 assertNumberVars("var b = o");
232 assertNumberVars("var b = fn");
233 // use before assignment
234 assertNumberVars("b; var b = 1");
235 assertNumberVars("b || c; var b = 1, c = 2");
236 assertNumberVars("if(b) var b = 1");
237 assertNumberVars("typeof b; var b=1");
238 assertNumberVars("typeof (b,b); var b=1");
239 // relational operators: in, instanceof
240 assertNumberVars("var b = 1 instanceof 1");
241 assertNumberVars("var b = 1 in 1");
242 // other operators with nested comma expression:
243 assertNumberVars("var b = !(1,1)");
244 assertNumberVars("var b = typeof(1,1)");
245 assertNumberVars("var b = void(1,1)");
246 // nested assignment
247 assertNumberVars("var b = 1; var f = (b = 'a')");
248 // let expression:
249 assertNumberVars("var b = let(x=1) x", "b", "x");
250 assertNumberVars("var b = let(x=1,y=1) x,y", "b", "x", "y");
251 // conditional operator:
252 assertNumberVars("var b = 0 ? 1 : 2", "b");
253 assertNumberVars("var b = 'a' ? 1 : 2", "b");
254 // comma expression:
255 assertNumberVars("var b = (0,1)", "b");
256 assertNumberVars("var b = ('a',1)", "b");
257 // assignment operations:
258 assertNumberVars("var b; var c=0; b=c", "b", "c");
259 assertNumberVars("var b; var c=0; b=(c=1)", "b", "c");
260 assertNumberVars("var b; var c=0; b=(c='a')");
261 assertNumberVars("var b; var c=0; b=(c+=1)", "b", "c");
262 assertNumberVars("var b; var c=0; b=(c*=1)", "b", "c");
263 assertNumberVars("var b; var c=0; b=(c%=1)", "b", "c");
264 assertNumberVars("var b; var c=0; b=(c/=1)", "b", "c");
265 // property access:
266 assertNumberVars("var b; b=(o.p)");
267 assertNumberVars("var b; b=(o.p=1)", "b");
268 assertNumberVars("var b; b=(o.p+=1)");
269 assertNumberVars("var b; b=(o['p']=1)", "b");
270 assertNumberVars("var b; b=(o['p']+=1)");
271 assertNumberVars("var b; var o = {p:0}; b=(o.p=1)", "b");
272 assertNumberVars("var b; var o = {p:0}; b=(o.p+=1)");
273 assertNumberVars("var b; var o = {p:0}; b=(o['p']=1)", "b");
274 assertNumberVars("var b; var o = {p:0}; b=(o['p']+=1)");
275 assertNumberVars("var b = 1; b.p = true", "b");
276 assertNumberVars("var b = 1; b.p", "b");
277 assertNumberVars("var b = 1; b.p()", "b");
278 assertNumberVars("var b = 1; b[0] = true", "b");
279 assertNumberVars("var b = 1; b[0]", "b");
280 assertNumberVars("var b = 1; b[0]()", "b");
281 // assignment (global)
282 assertNumberVars("var b = foo");
283 assertNumberVars("var b = foo1=1", "b");
284 assertNumberVars("var b = foo2+=1");
285 // boolean operators:
286 assertNumberVars("var b = 1||2", "b");
287 assertNumberVars("var b; var c=1; b=c||2", "b", "c");
288 assertNumberVars("var b; var c=1; b=c||c||2", "b", "c");
289 assertNumberVars("var b = 1&&2", "b");
290 assertNumberVars("var b; var c=1; b=c&&2", "b", "c");
291 assertNumberVars("var b; var c=1; b=c&&c&&2", "b", "c");
292 // bit not:
293 assertNumberVars("var b = ~0", "b");
294 assertNumberVars("var b = ~o", "b");
295 assertNumberVars("var b; var c=1; b=~c", "b", "c");
296 // increment, function call:
297 assertNumberVars("var b; var g; b = (g=0,g++)", "b", "g");
298 assertNumberVars("var b; var x = fn(b=1)", "b");
299 assertNumberVars("var b; var x = fn(b=1).p++", "b", "x");
300 assertNumberVars("var b; ({1:{}})[b=1].p++", "b");
301 assertNumberVars("var b; o[b=1]++", "b");
302 // destructuring
303 assertNumberVars("var r,s; [r,s] = [1,1]");
304 assertNumberVars("var r=0, s=0; [r,s] = [1,1]");
305 assertNumberVars("var r,s; ({a: r, b: s}) = {a:1, b:1}");
306 assertNumberVars("var r=0, s=0; ({a: r, b: s}) = {a:1, b:1}");
307 // array comprehension
308 assertNumberVars("var b=[i*i for each (i in [1,2,3])]");
309 assertNumberVars("var b=[j*j for each (j in [1,2,3]) if (j>1)]");
310
311 return null;
312 }
313 });
314 }
315
316 }