| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 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 |
|
| 89 |
|
| 90 |
"var A = function() {alert(B());A()};" + |
| 91 |
"var B = function() {return 6};" + |
| 92 |
"A();", |
| 93 |
|
| 94 |
"A();" + |
| 95 |
"B();" + |
| 96 |
"B = function() {return 7};" + |
| 97 |
"A();" + |
| 98 |
"B();" |
| 99 |
); |
| 100 |
|
| 101 |
test(modules, new String[] { |
| 102 |
|
| 103 |
"var A = function() {alert(6);A()};" + |
| 104 |
"A();" + |
| 105 |
"var B;", |
| 106 |
|
| 107 |
"A = function() {alert(B());A()};" + |
| 108 |
"B = function() {return 6};" + |
| 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 |
|
| 120 |
|
| 121 |
"var A = function() {alert(B());A()};" + |
| 122 |
"var B = function() {return C()};" + |
| 123 |
"var C = function() {return 6};" + |
| 124 |
"A();", |
| 125 |
|
| 126 |
"B = function() {return 7};" + |
| 127 |
"A();"); |
| 128 |
|
| 129 |
test(modules, new String[] { |
| 130 |
|
| 131 |
"var A = function() {alert(6);A()};" + |
| 132 |
"A();" + |
| 133 |
"var B, C;", |
| 134 |
|
| 135 |
"A = function() {alert(B());A()};" + |
| 136 |
"B = function() {return C()};" + |
| 137 |
"C = function() {return 6};" + |
| 138 |
"B = function() {return 7};" + |
| 139 |
"A();" |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
public void testSpecializeInlineWithMultipleDependents() { |
| 144 |
JSModule[] modules = createModuleStar( |
| 145 |
|
| 146 |
|
| 147 |
"var A = function() {alert(B());A()};" + |
| 148 |
"var B = function() {return 6};" + |
| 149 |
"A();", |
| 150 |
|
| 151 |
"B = function() {return 7};" + |
| 152 |
"A();", |
| 153 |
|
| 154 |
"A();" |
| 155 |
); |
| 156 |
|
| 157 |
test(modules, new String[] { |
| 158 |
|
| 159 |
"var A = function() {alert(6);A()};" + |
| 160 |
"A();" + |
| 161 |
"var B;", |
| 162 |
|
| 163 |
"A = function() {alert(B());A()};" + |
| 164 |
"B = function() {return 6};" + |
| 165 |
"B = function() {return 7};" + |
| 166 |
"A();", |
| 167 |
"A = function() {alert(B());A()};" + |
| 168 |
"B = function() {return 6};" + |
| 169 |
"A();", |
| 170 |
|
| 171 |
}); |
| 172 |
} |
| 173 |
|
| 174 |
public void testSpecializeInlineWithNamespaces() { |
| 175 |
JSModule[] modules = createModuleStar( |
| 176 |
|
| 177 |
"var ns = {};" + |
| 178 |
|
| 179 |
"ns.A = function() {alert(B());ns.A()};" + |
| 180 |
"var B = function() {return 6};" + |
| 181 |
"ns.A();", |
| 182 |
|
| 183 |
"B = function() {return 7};" + |
| 184 |
"ns.A();"); |
| 185 |
|
| 186 |
test(modules, new String[] { |
| 187 |
|
| 188 |
"var ns = {};" + |
| 189 |
"ns.A = function() {alert(6);ns.A()};" + |
| 190 |
"ns.A();" + |
| 191 |
"var B;", |
| 192 |
|
| 193 |
"ns.A = function() {alert(B());ns.A()};" + |
| 194 |
"B = function() {return 6};" + |
| 195 |
"B = function() {return 7};" + |
| 196 |
"ns.A();" |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
public void testSpecializeInlineWithRegularFunctions() { |
| 201 |
JSModule[] modules = createModuleStar( |
| 202 |
|
| 203 |
|
| 204 |
"function A() {alert(B());A()}" + |
| 205 |
"function B() {return 6}" + |
| 206 |
"A();", |
| 207 |
|
| 208 |
"B = function() {return 7};" + |
| 209 |
"A();"); |
| 210 |
|
| 211 |
test(modules, new String[] { |
| 212 |
|
| 213 |
"function A() {alert(6);A()}" + |
| 214 |
"A();" + |
| 215 |
"var B;", |
| 216 |
|
| 217 |
"A = function() {alert(B());A()};" + |
| 218 |
"B = function() {return 6};" + |
| 219 |
|
| 220 |
"B = function() {return 7};" + |
| 221 |
"A();" |
| 222 |
}); |
| 223 |
} |
| 224 |
|
| 225 |
public void testDontSpecializeLocalNonAnonymousFunctions() { |
| 226 |
|
| 227 |
enableNormalize(false); |
| 228 |
|
| 229 |
JSModule[] modules = createModuleStar( |
| 230 |
|
| 231 |
"(function(){var noSpecialize = " + |
| 232 |
"function() {alert(6)};noSpecialize()})()", |
| 233 |
|
| 234 |
""); |
| 235 |
|
| 236 |
test(modules, new String[] { |
| 237 |
|
| 238 |
"(function(){var noSpecialize = " + |
| 239 |
"function() {alert(6)};noSpecialize()})()", |
| 240 |
|
| 241 |
"" |
| 242 |
}); |
| 243 |
} |
| 244 |
|
| 245 |
public void testAddDummyVarsForRemovedFunctions() { |
| 246 |
JSModule[] modules = createModuleStar( |
| 247 |
|
| 248 |
|
| 249 |
"var A = function() {alert(B() + C());A()};" + |
| 250 |
"var B = function() {return 6};" + |
| 251 |
"var C = function() {return 8};" + |
| 252 |
"A();", |
| 253 |
|
| 254 |
"" + |
| 255 |
"A();"); |
| 256 |
|
| 257 |
test(modules, new String[] { |
| 258 |
|
| 259 |
"var A = function() {alert(6 + 8);A()};" + |
| 260 |
"A();" + |
| 261 |
"var B, C;", |
| 262 |
|
| 263 |
"A = function() {alert(B() + C());A()};" + |
| 264 |
"B = function() {return 6};" + |
| 265 |
"C = function() {return 8};" + |
| 266 |
"A();" |
| 267 |
}); |
| 268 |
} |
| 269 |
|
| 270 |
public void testSpecializeRemoveUnusedProperties() { |
| 271 |
JSModule[] modules = createModuleStar( |
| 272 |
|
| 273 |
|
| 274 |
"var Foo = function(){};" + |
| 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;" + |
| 279 |
"var x = new Foo();" + |
| 280 |
"x.a();", |
| 281 |
|
| 282 |
""); |
| 283 |
|
| 284 |
test(modules, new String[] { |
| 285 |
|
| 286 |
"var Foo = function(){};" + |
| 287 |
"Foo.prototype.a = function() {this.a()};" + |
| 288 |
"var aliasA = Foo.prototype.a;" + |
| 289 |
"var x = new Foo();" + |
| 290 |
"x.a();", |
| 291 |
|
| 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 |
|
| 300 |
|
| 301 |
"function A() {alert(B());A()}" + |
| 302 |
"function B() {return 6}" + |
| 303 |
"var aliasA = A;" + |
| 304 |
"A();", |
| 305 |
|
| 306 |
"B = function() {return 7};" + |
| 307 |
"B();"); |
| 308 |
|
| 309 |
test(modules, new String[] { |
| 310 |
|
| 311 |
|
| 312 |
"function A() {alert(B());A()}" + |
| 313 |
"function B() {return 6}" + |
| 314 |
"var aliasA = A;" + |
| 315 |
"A();", |
| 316 |
|
| 317 |
"B = function() {return 7};" + |
| 318 |
"B();" |
| 319 |
}); |
| 320 |
} |
| 321 |
|
| 322 |
public void testDontSpecializeAliasedFunctions_remove_unused_properties() { |
| 323 |
JSModule[] modules = createModuleStar( |
| 324 |
|
| 325 |
"var Foo = function(){};" + |
| 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;" + |
| 332 |
"var x = new Foo();" + |
| 333 |
"x.a();" + |
| 334 |
"var aliasC = (new Foo).c", |
| 335 |
|
| 336 |
""); |
| 337 |
|
| 338 |
test(modules, new String[] { |
| 339 |
|
| 340 |
"var Foo = function(){};" + |
| 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;" + |
| 346 |
"var x = new Foo();" + |
| 347 |
"x.a();" + |
| 348 |
"var aliasC = (new Foo).c", |
| 349 |
|
| 350 |
"Foo.prototype.d = function() {return 7};" |
| 351 |
}); |
| 352 |
} |
| 353 |
|
| 354 |
public void testSpecializeDevirtualizePrototypeMethods() { |
| 355 |
JSModule[] modules = createModuleStar( |
| 356 |
|
| 357 |
"/** @constructor */" + |
| 358 |
"var Foo = function(){};" + |
| 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 |
|
| 364 |
""); |
| 365 |
|
| 366 |
test(modules, new String[] { |
| 367 |
|
| 368 |
"var Foo = function(){};" + |
| 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 |
|
| 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 |
|
| 385 |
"/** @constructor */" + |
| 386 |
"var Foo = function(){};" + |
| 387 |
"Foo.prototype.a = function() {return 7};" + |
| 388 |
"var x = new Foo();" + |
| 389 |
"var z = x.a();", |
| 390 |
|
| 391 |
""); |
| 392 |
|
| 393 |
test(modules, new String[] { |
| 394 |
|
| 395 |
"var Foo = function(){};" + |
| 396 |
"var x = new Foo();" + |
| 397 |
"var z = 7;", |
| 398 |
|
| 399 |
"Foo.prototype.a = function() {return 7};" |
| 400 |
}); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 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 |
|
| 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 |
|
| 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 |
} |