Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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 import com.google.common.collect.ImmutableSet;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.jscomp.SpecializeModule.SpecializationState;
22 import com.google.javascript.rhino.Node;
23
24 /**
25 * Tests for {@link SpecializeModule}.
26 *
27 * @author [email protected] (Devin Coughlin)
28 */
29 public class SpecializeModuleTest extends CompilerTestCase {
30
31 private static final String SHARED_EXTERNS = "var alert = function() {}";
32
33 public SpecializeModuleTest() {
34 super(SHARED_EXTERNS);
35 }
36
37 private PassFactory inlineFunctions =
38 new PassFactory("inlineFunctions", true) {
39 @Override
40 protected CompilerPass create(AbstractCompiler compiler) {
41 return new InlineFunctions(compiler,
42 compiler.getUniqueNameIdSupplier(), true, false, true, true, true);
43 }
44 };
45
46 private PassFactory removeUnusedPrototypeProperties =
47 new PassFactory("removeUnusedPrototypeProperties", true) {
48 @Override
49 protected CompilerPass create(AbstractCompiler compiler) {
50 return new RemoveUnusedPrototypeProperties(compiler, false, false);
51 }
52 };
53
54 private PassFactory devirtualizePrototypeMethods =
55 new PassFactory("devirtualizePrototypeMethods", true) {
56 @Override
57 protected CompilerPass create(AbstractCompiler compiler) {
58 return new DevirtualizePrototypeMethods(compiler);
59 }
60 };
61
62 @Override
63 protected CompilerPass getProcessor(final Compiler compiler) {
64 final SpecializeModule specializeModule = new SpecializeModule(compiler,
65 devirtualizePrototypeMethods, inlineFunctions,
66 removeUnusedPrototypeProperties);
67
68 return new CompilerPass() {
69 @Override
70 public void process(Node externs, Node root) {
71 specializeModule.process(externs, root);
72
73 /* Make sure variables are declared before used */
74 new VarCheck(compiler).process(externs, root);
75 }
76 };
77 }
78
79 @Override
80 public void setUp() throws Exception {
81 super.setUp();
82
83 enableNormalize();
84 }
85
86 public void testSpecializeInline() {
87 JSModule[] modules = createModuleStar(
88 // m1
89 /* Recursion in A() prevents inline of A*/
90 "var A = function() {alert(B());A()};" +
91 "var B = function() {return 6};" +
92 "A();",
93 // m2
94 "A();" +
95 "B();" +
96 "B = function() {return 7};" +
97 "A();" +
98 "B();"
99 );
100
101 test(modules, new String[] {
102 // m1
103 "var A = function() {alert(6);A()};" + /* Specialized A */
104 "A();" +
105 "var B;",
106 // m2
107 "A = function() {alert(B());A()};" + /* Unspecialized A */
108 "B = function() {return 6};" + /* Removed from m1, so add to m2 */
109 "A();" +
110 "B();" +
111 "B = function() {return 7};" +
112 "A();" +
113 "B();"
114 });
115 }
116
117 public void testSpecializeCascadedInline() {
118 JSModule[] modules = createModuleStar(
119 // m1
120 /* Recursion in A() prevents inline of A*/
121 "var A = function() {alert(B());A()};" +
122 "var B = function() {return C()};" +
123 "var C = function() {return 6};" +
124 "A();",
125 // m2
126 "B = function() {return 7};" +
127 "A();");
128
129 test(modules, new String[] {
130 // m1
131 "var A = function() {alert(6);A()};" + /* Specialized A */
132 "A();" +
133 "var B, C;",
134 // m2
135 "A = function() {alert(B());A()};" + /* Unspecialized A */
136 "B = function() {return C()};" + /* Removed from m1, so add to m2 */
137 "C = function() {return 6};" + /* Removed from m1, so add to m2 */
138 "B = function() {return 7};" +
139 "A();"
140 });
141 }
142
143 public void testSpecializeInlineWithMultipleDependents() {
144 JSModule[] modules = createModuleStar(
145 // m1
146 /* Recursion in A() prevents inline of A*/
147 "var A = function() {alert(B());A()};" +
148 "var B = function() {return 6};" +
149 "A();",
150 // m2
151 "B = function() {return 7};" +
152 "A();",
153 // m3
154 "A();"
155 );
156
157 test(modules, new String[] {
158 // m1
159 "var A = function() {alert(6);A()};" + /* Specialized A */
160 "A();" +
161 "var B;",
162 // m2
163 "A = function() {alert(B());A()};" + /* Unspecialized A */
164 "B = function() {return 6};" + /* Removed from m1, so add to m2 */
165 "B = function() {return 7};" +
166 "A();",
167 "A = function() {alert(B());A()};" + /* Unspecialized A */
168 "B = function() {return 6};" + /* Removed from m1, so add to m2 */
169 "A();",
170
171 });
172 }
173
174 public void testSpecializeInlineWithNamespaces() {
175 JSModule[] modules = createModuleStar(
176 // m1
177 "var ns = {};" +
178 /* Recursion in A() prevents inline of A*/
179 "ns.A = function() {alert(B());ns.A()};" +
180 "var B = function() {return 6};" +
181 "ns.A();",
182 // m2
183 "B = function() {return 7};" +
184 "ns.A();");
185
186 test(modules, new String[] {
187 // m1
188 "var ns = {};" +
189 "ns.A = function() {alert(6);ns.A()};" + /* Specialized A */
190 "ns.A();" +
191 "var B;",
192 // m2
193 "ns.A = function() {alert(B());ns.A()};" + /* Unspecialized A */
194 "B = function() {return 6};" + /* Removed from m1, so add to m2 */
195 "B = function() {return 7};" +
196 "ns.A();"
197 });
198 }
199
200 public void testSpecializeInlineWithRegularFunctions() {
201 JSModule[] modules = createModuleStar(
202 // m1
203 /* Recursion in A() prevents inline of A*/
204 "function A() {alert(B());A()}" +
205 "function B() {return 6}" +
206 "A();",
207 // m2
208 "B = function() {return 7};" +
209 "A();");
210
211 test(modules, new String[] {
212 // m1
213 "function A() {alert(6);A()}" + /* Specialized A */
214 "A();" +
215 "var B;",
216 // m2
217 "A = function() {alert(B());A()};" + /* Unspecialized A */
218 "B = function() {return 6};" + /* Removed from m1, so add to m2 */
219 /* Start of original m2 */
220 "B = function() {return 7};" +
221 "A();"
222 });
223 }
224
225 public void testDontSpecializeLocalNonAnonymousFunctions() {
226 /* normalize result, but not expected */
227 enableNormalize(false);
228
229 JSModule[] modules = createModuleStar(
230 // m1
231 "(function(){var noSpecialize = " +
232 "function() {alert(6)};noSpecialize()})()",
233 // m2
234 "");
235
236 test(modules, new String[] {
237 // m1
238 "(function(){var noSpecialize = " +
239 "function() {alert(6)};noSpecialize()})()",
240 // m2
241 ""
242 });
243 }
244
245 public void testAddDummyVarsForRemovedFunctions() {
246 JSModule[] modules = createModuleStar(
247 // m1
248 /* Recursion in A() prevents inline of A*/
249 "var A = function() {alert(B() + C());A()};" +
250 "var B = function() {return 6};" +
251 "var C = function() {return 8};" +
252 "A();",
253 // m2
254 "" +
255 "A();");
256
257 test(modules, new String[] {
258 // m1
259 "var A = function() {alert(6 + 8);A()};" + /* Specialized A */
260 "A();" +
261 "var B, C;",
262 // m2
263 "A = function() {alert(B() + C());A()};" + /* Unspecialized A */
264 "B = function() {return 6};" + /* Removed from m1, so add to m2 */
265 "C = function() {return 8};" + /* Removed from m1, so add to m2 */
266 "A();"
267 });
268 }
269
270 public void testSpecializeRemoveUnusedProperties() {
271 JSModule[] modules = createModuleStar(
272 // m1
273 /* Recursion in A() prevents inline of A*/
274 "var Foo = function(){};" + /* constructor */
275 "Foo.prototype.a = function() {this.a()};" +
276 "Foo.prototype.b = function() {return 6};" +
277 "Foo.prototype.c = function() {return 7};" +
278 "var aliasA = Foo.prototype.a;" + // Prevents devirtualization of a
279 "var x = new Foo();" +
280 "x.a();",
281 // m2
282 "");
283
284 test(modules, new String[] {
285 // m1
286 "var Foo = function(){};" + /* constructor */
287 "Foo.prototype.a = function() {this.a()};" +
288 "var aliasA = Foo.prototype.a;" +
289 "var x = new Foo();" +
290 "x.a();",
291 // m2
292 "Foo.prototype.b = function() {return 6};" +
293 "Foo.prototype.c = function() {return 7};"
294 });
295 }
296
297 public void testDontSpecializeAliasedFunctions_inline() {
298 JSModule[] modules = createModuleStar(
299 // m1
300 /* Recursion in A() prevents inline of A*/
301 "function A() {alert(B());A()}" +
302 "function B() {return 6}" +
303 "var aliasA = A;" +
304 "A();",
305 // m2
306 "B = function() {return 7};" +
307 "B();");
308
309 test(modules, new String[] {
310 // m1
311 /* Recursion in A() prevents inline of A*/
312 "function A() {alert(B());A()}" +
313 "function B() {return 6}" +
314 "var aliasA = A;" +
315 "A();",
316 // m2
317 "B = function() {return 7};" +
318 "B();"
319 });
320 }
321
322 public void testDontSpecializeAliasedFunctions_remove_unused_properties() {
323 JSModule[] modules = createModuleStar(
324 // m1
325 "var Foo = function(){};" + /* constructor */
326 "Foo.prototype.a = function() {this.a()};" +
327 "Foo.prototype.b = function() {return 6};" +
328 "var aliasB = Foo.prototype.b;" +
329 "Foo.prototype.c = function() {return 7};" +
330 "Foo.prototype.d = function() {return 7};" +
331 "var aliasA = Foo.prototype.a;" + // Prevents devirtualization of a
332 "var x = new Foo();" +
333 "x.a();" +
334 "var aliasC = (new Foo).c",
335 // m2
336 "");
337
338 test(modules, new String[] {
339 // m1
340 "var Foo = function(){};" + /* constructor */
341 "Foo.prototype.a = function() {this.a()};" +
342 "Foo.prototype.b = function() {return 6};" +
343 "var aliasB = Foo.prototype.b;" +
344 "Foo.prototype.c = function() {return 7};" +
345 "var aliasA = Foo.prototype.a;" + // Prevents devirtualization of a
346 "var x = new Foo();" +
347 "x.a();" +
348 "var aliasC = (new Foo).c",
349 // m2
350 "Foo.prototype.d = function() {return 7};"
351 });
352 }
353
354 public void testSpecializeDevirtualizePrototypeMethods() {
355 JSModule[] modules = createModuleStar(
356 // m1
357 "/** @constructor */" +
358 "var Foo = function(){};" + /* constructor */
359 "Foo.prototype.a = function() {this.a();return 7};" +
360 "Foo.prototype.b = function() {this.a()};" +
361 "var x = new Foo();" +
362 "x.a();",
363 // m2
364 "");
365
366 test(modules, new String[] {
367 // m1
368 "var Foo = function(){};" + /* constructor */
369 "var JSCompiler_StaticMethods_a =" +
370 "function(JSCompiler_StaticMethods_a$self) {" +
371 "JSCompiler_StaticMethods_a(JSCompiler_StaticMethods_a$self);" +
372 "return 7" +
373 "};" +
374 "var x = new Foo();" +
375 "JSCompiler_StaticMethods_a(x);",
376 // m2
377 "Foo.prototype.a = function() {this.a();return 7};" +
378 "Foo.prototype.b = function() {this.a()};"
379 });
380 }
381
382 public void testSpecializeDevirtualizePrototypeMethodsWithInline() {
383 JSModule[] modules = createModuleStar(
384 // m1
385 "/** @constructor */" +
386 "var Foo = function(){};" + /* constructor */
387 "Foo.prototype.a = function() {return 7};" +
388 "var x = new Foo();" +
389 "var z = x.a();",
390 // m2
391 "");
392
393 test(modules, new String[] {
394 // m1
395 "var Foo = function(){};" + /* constructor */
396 "var x = new Foo();" +
397 "var z = 7;",
398 // m2
399 "Foo.prototype.a = function() {return 7};"
400 });
401 }
402
403 /**
404 * Tests for {@link SpecializeModule.SpecializationState}.
405 */
406 public static class SpecializeModuleSpecializationStateTest
407 extends CompilerTestCase {
408
409 Compiler lastCompiler;
410
411 SpecializationState lastState;
412
413 @Override
414 public CompilerPass getProcessor(final Compiler compiler) {
415 lastCompiler = compiler;
416
417 return new CompilerPass() {
418
419 @Override
420 public void process(Node externs, Node root) {
421 SimpleDefinitionFinder defFinder =
422 new SimpleDefinitionFinder(compiler);
423
424 defFinder.process(externs, root);
425
426 SimpleFunctionAliasAnalysis functionAliasAnalysis =
427 new SimpleFunctionAliasAnalysis();
428
429 functionAliasAnalysis.analyze(defFinder);
430
431 lastState = new SpecializationState(functionAliasAnalysis);
432 }
433 };
434 }
435
436 public void testRemovedFunctions() {
437 testSame("function F(){}\nvar G = function(a){};");
438
439 assertEquals(ImmutableSet.of(), lastState.getRemovedFunctions());
440
441 Node functionF = findFunction("F");
442
443 lastState.reportRemovedFunction(functionF, functionF.getParent());
444 assertEquals(ImmutableSet.of(functionF), lastState.getRemovedFunctions());
445
446 Node functionG = findFunction("F");
447
448 lastState.reportRemovedFunction(functionG, functionF.getParent());
449 assertEquals(ImmutableSet.of(functionF, functionG),
450 lastState.getRemovedFunctions());
451
452 assertEquals(ImmutableSet.of(), lastState.getSpecializedFunctions());
453 }
454
455 public void testSpecializedFunctions() {
456 testSame("function F(){}\nvar G = function(a){};");
457
458 assertEquals(ImmutableSet.of(), lastState.getSpecializedFunctions());
459
460 Node functionF = findFunction("F");
461
462 lastState.reportSpecializedFunction(functionF);
463 assertEquals(ImmutableSet.of(functionF),
464 lastState.getSpecializedFunctions());
465
466 Node functionG = findFunction("F");
467
468 lastState.reportSpecializedFunction(functionG);
469 assertEquals(ImmutableSet.of(functionF, functionG),
470 lastState.getSpecializedFunctions());
471
472 assertEquals(ImmutableSet.of(), lastState.getRemovedFunctions());
473 }
474
475 public void testCanFixupFunction() {
476 testSame("function F(){}\n" +
477 "var G = function(a){};\n" +
478 "var ns = {};" +
479 "ns.H = function(){};" +
480 "var ns2 = {I : function anon1(){}};" +
481 "(function anon2(){})();");
482
483 assertTrue(lastState.canFixupFunction(findFunction("F")));
484 assertTrue(lastState.canFixupFunction(findFunction("G")));
485 assertTrue(lastState.canFixupFunction(findFunction("ns.H")));
486 assertFalse(lastState.canFixupFunction(findFunction("anon1")));
487 assertFalse(lastState.canFixupFunction(findFunction("anon2")));
488
489 // Can't guarantee safe fixup for aliased functions
490 testSame("function A(){}\n" +
491 "var aliasA = A;\n");
492
493 assertFalse(lastState.canFixupFunction(findFunction("A")));
494 }
495
496 private Node findFunction(String name) {
497 FunctionFinder f = new FunctionFinder(name);
498 new NodeTraversal(lastCompiler, f).traverse(lastCompiler.jsRoot);
499 assertNotNull("Couldn't find " + name, f.found);
500 return f.found;
501 }
502
503 /**
504 * Quick Traversal to find a given function in the AST.
505 */
506 private class FunctionFinder extends AbstractPostOrderCallback {
507 Node found = null;
508 final String target;
509
510 FunctionFinder(String target) {
511 this.target = target;
512 }
513
514 @Override
515 public void visit(NodeTraversal t, Node n, Node parent) {
516 if (n.isFunction()
517 && target.equals(NodeUtil.getFunctionName(n))) {
518 found = n;
519 }
520 }
521 }
522 }
523 }