Debian Codegraph @

test/com/google/javascript/jscomp/AliasExternalsTest.java

raw symbols SCIP
1 /*
2 * Copyright 2006 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 /**
20 * Tests for {@link AliasExternals}.
21 *
22 */
23 public class AliasExternalsTest extends CompilerTestCase {
24
25 private static String EXTERNS =
26 // Globals
27 "/** @const */ var window;" +
28 "/** @const */ var document;" +
29 "var arguments;var _USER_ID;var ActiveXObject;" +
30 "function eval(x) {}" +
31 // Properties
32 "window.setTimeout;" +
33 "window.eval;" +
34 "props.window;props.innerHTML;props.length;props.prototype;props.length;" +
35 // More globals
36 "/** @noalias */ var RangeObject; " +
37 "var /** @noalias */ RuntimeObject, SelectionObject;" +
38 "/** @noalias */ function NoAliasFunction() {};";
39
40 // Blacklist and whitelist of globals. Assign to these before running test
41 // if you want to factor them in to the test, otherwise they will be null.
42 private String unaliasableGlobals;
43 private String aliasableGlobals;
44
45 public AliasExternalsTest() {
46 super(EXTERNS);
47 }
48
49 @Override
50 protected int getNumRepetitions() {
51 // This pass only runs once.
52 return 1;
53 }
54
55 @Override
56 public void setUp() {
57 super.enableLineNumberCheck(false);
58 super.enableNormalize();
59 unaliasableGlobals = null;
60 aliasableGlobals = null;
61 }
62
63 /**
64 * Test standard global aliasing.
65 */
66 public void testGlobalAlias() {
67 test("window.setTimeout(function() {}, 0);" +
68 "var doc=window.document;" +
69 "window.alert(\"foo\");" +
70 "window.eval(\"1\");" +
71 "window.location.href=\"http://www.example.com\";" +
72 "function foo() {var window = \"bar\"; return window}foo();",
73
74 "var GLOBAL_window=window;" +
75 formatPropNameDecl("setTimeout") +
76 "GLOBAL_window[$$PROP_setTimeout](function() {}, 0);" +
77 "var doc=GLOBAL_window.document;" +
78 "GLOBAL_window.alert(\"foo\");" +
79 "GLOBAL_window.eval(\"1\");" +
80 "GLOBAL_window.location.href=\"http://www.example.com\";" +
81 "function foo() {var window = \"bar\"; return window}foo();");
82 }
83
84 /**
85 * Some globals should not be aliased because they have special meaning
86 * within the language (like arguments).
87 */
88 public void testUnaliasable() {
89 test("function foo() {" +
90 "var x=arguments.length;" +
91 "var y=arguments.length;" +
92 "var z=arguments.length;" +
93 "var w=arguments.length;" +
94 "return x + y + z + w" +
95 "};foo();",
96
97 formatPropNameDecl("length") +
98 "function foo() {" +
99 "var x=arguments[$$PROP_length];" +
100 "var y=arguments[$$PROP_length];" +
101 "var z=arguments[$$PROP_length];" +
102 "var w=arguments[$$PROP_length];" +
103 "return x + y + z + w" +
104 "};foo();");
105
106 test("var x=new ActiveXObject();" +
107 "x.foo=\"bar\";" +
108 "var y=new ActiveXObject();" +
109 "y.foo=\"bar\";" +
110 "var z=new ActiveXObject();" +
111 "z.foo=\"bar\";",
112
113 "var x=new ActiveXObject();" +
114 "x.foo=\"bar\";" +
115 "var y=new ActiveXObject();" +
116 "y.foo=\"bar\";" +
117 "var z=new ActiveXObject();" +
118 "z.foo=\"bar\";");
119
120 test("var _a=eval('foo'),_b=eval('foo'),_c=eval('foo'),_d=eval('foo')," +
121 "_e=eval('foo'),_f=eval('foo'),_g=eval('foo');",
122 "var _a=eval('foo'),_b=eval('foo'),_c=eval('foo'),_d=eval('foo')," +
123 "_e=eval('foo'),_f=eval('foo'),_g=eval('foo');");
124 }
125
126 /**
127 * Test using a whitelist to explicitly alias only specific
128 * identifiers.
129 */
130 public void testAliasableGlobals() {
131 aliasableGlobals = "notused,length";
132 test("function foo() {" +
133 "var x=arguments.length;" +
134 "var y=arguments.length;" +
135 "var z=arguments.length;" +
136 "var w=arguments.length;" +
137 "return x + y + z + w" +
138 "};foo();",
139
140 formatPropNameDecl("length") +
141 "function foo() {" +
142 "var x=arguments[$$PROP_length];" +
143 "var y=arguments[$$PROP_length];" +
144 "var z=arguments[$$PROP_length];" +
145 "var w=arguments[$$PROP_length];" +
146 "return x + y + z + w" +
147 "};foo();");
148
149 aliasableGlobals = "notused,notlength";
150 test("function foo() {" +
151 "var x=arguments.length;" +
152 "var y=arguments.length;" +
153 "var z=arguments.length;" +
154 "var w=arguments.length;" +
155 "return x + y + z + w" +
156 "};foo();",
157
158 "function foo() {" +
159 "var x=arguments.length;" +
160 "var y=arguments.length;" +
161 "var z=arguments.length;" +
162 "var w=arguments.length;" +
163 "return x + y + z + w" +
164 "};foo();");
165 }
166
167 /**
168 * Test combined usage of aliasable and unaliasable global lists.
169 */
170 public void testAliasableAndUnaliasableGlobals() {
171 // Only aliasable provided - OK
172 aliasableGlobals = "foo,bar";
173 unaliasableGlobals = "";
174 test("var x;", "var x;");
175
176 // Only unaliasable provided - OK
177 aliasableGlobals = "";
178 unaliasableGlobals = "baz,qux";
179 test("var x;", "var x;");
180
181 // Both provided - bad
182 aliasableGlobals = "foo,bar";
183 unaliasableGlobals = "baz,qux";
184 try {
185 test("var x;", "var x;");
186 fail("Expected an IllegalArgumentException");
187 } catch (IllegalArgumentException ex) {
188 // pass
189 }
190 }
191
192 /**
193 * Global variables that get re-assigned should not be aliased.
194 */
195 public void testGlobalAssigment() {
196 test("var x=_USER_ID+window;" +
197 "var y=_USER_ID+window;" +
198 "var z=_USER_ID+window;" +
199 "var w=x+y+z;" +
200 "_USER_ID = \"foo\";" +
201 "window++;",
202
203 "var x=_USER_ID+window;" +
204 "var y=_USER_ID+window;" +
205 "var z=_USER_ID+window;" +
206 "var w=x+y+z;" +
207 "_USER_ID = \"foo\";" +
208 "window++");
209 }
210
211 public void testNewOperator() {
212 test("var x;new x(window);window;window;window;window;window",
213
214 "var GLOBAL_window=window; var x;" +
215 " new x(GLOBAL_window);GLOBAL_window;GLOBAL_window;" +
216 " GLOBAL_window;GLOBAL_window;GLOBAL_window");
217 }
218
219 /**
220 * Test the standard replacement for GETPROP
221 */
222 public void testGetProp() {
223 test("function foo(a,b){return a.length > b.length;}",
224 formatPropNameDecl("length") +
225 "function foo(a, b){return a[$$PROP_length] > b[$$PROP_length];}");
226 test("Foo.prototype.bar = function() { return 'foo'; }",
227 formatPropNameDecl("prototype") +
228 "Foo[$$PROP_prototype].bar = function() { return 'foo'; }");
229 test("Foo.notreplaced = 5", "Foo.notreplaced=5");
230 }
231
232 /**
233 * Ops that should be ignored
234 */
235 public void testIgnoredOps() {
236 testSame("function foo() { this.length-- }");
237 testSame("function foo() { this.length++ }");
238 testSame("function foo() { this.length+=5 }");
239 testSame("function foo() { this.length-=5 }");
240 }
241
242 /**
243 * Test property setting
244 */
245 public void testSetProp() {
246 test("function foo() { this.innerHTML = 'hello!'; }",
247 formatSetPropFn("innerHTML")
248 + "function foo() { SETPROP_innerHTML(this, 'hello!'); }");
249 }
250
251 /**
252 * Test for modifying both parent and child, as all replacements
253 * are on a single pass and modifying both involves being careful with
254 * references.
255 */
256 public void testParentChild() {
257 test("a.length = b.length = c.length;", formatSetPropFn("length")
258 + formatPropNameDecl("length")
259 + "SETPROP_length(a, SETPROP_length(b, c[$$PROP_length]))");
260 }
261
262 private static final String MODULE_SRC_ONE =
263 "a=b.length;a=b.length;a=b.length;";
264 private static final String MODULE_SRC_TWO = "c=d.length;";
265
266 /**
267 * Test that the code is placed in the first module when there are no
268 * dependencies.
269 */
270 public void testModulesWithoutDependencies() {
271 test(createModules(MODULE_SRC_ONE, MODULE_SRC_TWO),
272 new String[] {
273 "var $$PROP_length=\"length\";a=b[$$PROP_length];" +
274 "a=b[$$PROP_length];a=b[$$PROP_length];",
275 "c=d[$$PROP_length];"});
276 }
277
278 /**
279 * Test that the code is placed in the first module when the second module
280 * depends on the first.
281 */
282 public void testModulesWithDependencies() {
283 test(createModuleChain(MODULE_SRC_ONE, MODULE_SRC_TWO),
284 new String[] {
285 "var $$PROP_length=\"length\";a=b[$$PROP_length];" +
286 "a=b[$$PROP_length];a=b[$$PROP_length];",
287 "c=d[$$PROP_length];"});
288 }
289
290 public void testPropAccessorPushedDeeper1() {
291 test(createModuleChain("var a = \"foo\";", "var b = a.length;"),
292 new String[] {
293 "var a = \"foo\";",
294 formatPropNameDecl("length") + "var b = a[$$PROP_length]" });
295 }
296
297 public void testPropAccessorPushedDeeper2() {
298 test(createModuleChain(
299 "var a = \"foo\";", "var b = a.length;", "var c = a.length;"),
300 new String[] {
301 "var a = \"foo\";",
302 formatPropNameDecl("length") + "var b = a[$$PROP_length]",
303 "var c = a[$$PROP_length]" });
304 }
305
306 public void testPropAccessorPushedDeeper3() {
307 test(createModuleStar(
308 "var a = \"foo\";", "var b = a.length;", "var c = a.length;"),
309 new String[] {
310 formatPropNameDecl("length") + "var a = \"foo\";",
311 "var b = a[$$PROP_length]",
312 "var c = a[$$PROP_length]" });
313 }
314
315 public void testPropAccessorNotPushedDeeper() {
316 test(createModuleChain("var a = \"foo\"; var b = a.length;",
317 "var c = a.length;"),
318 new String[] {
319 formatPropNameDecl("length") +
320 "var a = \"foo\"; var b = a[$$PROP_length]",
321 "var c = a[$$PROP_length]" });
322 }
323
324 public void testPropMutatorPushedDeeper() {
325 test(createModuleChain("var a = [1];", "a.length = 0;"),
326 new String[] {
327 "var a = [1];",
328 formatSetPropFn("length") + "SETPROP_length(a, 0);" });
329 }
330
331 public void testPropMutatorNotPushedDeeper() {
332 test(createModuleChain(
333 "var a = [1]; a.length = 1;", "a.length = 0;"),
334 new String[] {
335 formatSetPropFn("length") + "var a = [1]; SETPROP_length(a, 1);",
336 "SETPROP_length(a, 0);" });
337 }
338
339 public void testGlobalAliasPushedDeeper() {
340 test(createModuleChain(
341 "var a = 1;",
342 "var b = window, c = window, d = window, e = window;"),
343 new String[] { "var a = 1;",
344 "var GLOBAL_window = window;" +
345 "var b = GLOBAL_window, c = GLOBAL_window, " +
346 " d = GLOBAL_window, e = GLOBAL_window;" });
347 }
348
349 public void testGlobalAliasNotPushedDeeper() {
350 test(createModuleChain(
351 "var a = 1, b = window;",
352 "var c = window, d = window, e = window;"),
353 new String[] { "var GLOBAL_window = window;" +
354 "var a = 1, b = GLOBAL_window;",
355 "var c = GLOBAL_window, " +
356 " d = GLOBAL_window, e = GLOBAL_window;" });
357 }
358
359 public void testNoAliasAnnotationForSingleVar() {
360 testSame("[RangeObject, RangeObject, RangeObject]");
361 }
362
363 public void testNoAliasAnnotationForMultiVarDeclaration() {
364 test("[RuntimeObject, RuntimeObject, RuntimeObject," +
365 " SelectionObject, SelectionObject, SelectionObject]",
366 "var GLOBAL_SelectionObject = SelectionObject;" +
367 "[RuntimeObject, RuntimeObject, RuntimeObject," +
368 " GLOBAL_SelectionObject, GLOBAL_SelectionObject," +
369 " GLOBAL_SelectionObject]");
370 }
371
372 public void testNoAliasAnnotationForFunction() {
373 testSame("[NoAliasFunction(), NoAliasFunction(), NoAliasFunction()]");
374 }
375
376 private String formatPropNameDecl(String prop) {
377 return "var $$PROP_" + prop + "='" + prop + "';";
378 }
379
380 private String formatSetPropFn(String prop) {
381 String mutatorName = "SETPROP_" + prop;
382 String arg1 = mutatorName + "$a";
383 String arg2 = mutatorName + "$b";
384 return "function " + mutatorName + "(" + arg1 + "," + arg2 + ") {" +
385 "return " + arg1 + "." + prop + "=" + arg2 + ";}";
386 }
387
388 @Override
389 protected CompilerPass getProcessor(Compiler compiler) {
390 AliasExternals ae = new AliasExternals(
391 compiler, compiler.getModuleGraph(),
392 unaliasableGlobals, aliasableGlobals);
393 ae.setRequiredUsage(1);
394 return ae;
395 }
396 }