| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class CrossModuleMethodMotionTest extends CompilerTestCase { |
| 25 |
private static final String EXTERNS = |
| 26 |
"IFoo.prototype.bar; var mExtern; mExtern.bExtern; mExtern['cExtern'];"; |
| 27 |
|
| 28 |
private boolean canMoveExterns = false; |
| 29 |
|
| 30 |
private final String STUB_DECLARATIONS = |
| 31 |
CrossModuleMethodMotion.STUB_DECLARATIONS; |
| 32 |
|
| 33 |
public CrossModuleMethodMotionTest() { |
| 34 |
super(EXTERNS); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 39 |
return new CrossModuleMethodMotion( |
| 40 |
compiler, new CrossModuleMethodMotion.IdGenerator(), canMoveExterns); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void setUp() { |
| 45 |
super.enableLineNumberCheck(true); |
| 46 |
canMoveExterns = false; |
| 47 |
} |
| 48 |
|
| 49 |
public void testMovePrototypeMethod1() { |
| 50 |
testSame(createModuleChain( |
| 51 |
"function Foo() {}" + |
| 52 |
"Foo.prototype.bar = function() {};", |
| 53 |
|
| 54 |
"(new Foo).bar()")); |
| 55 |
|
| 56 |
canMoveExterns = true; |
| 57 |
test(createModuleChain( |
| 58 |
"function Foo() {}" + |
| 59 |
"Foo.prototype.bar = function() {};", |
| 60 |
|
| 61 |
"(new Foo).bar()"), |
| 62 |
new String[] { |
| 63 |
STUB_DECLARATIONS + |
| 64 |
"function Foo() {}" + |
| 65 |
"Foo.prototype.bar = JSCompiler_stubMethod(0);", |
| 66 |
|
| 67 |
"Foo.prototype.bar = JSCompiler_unstubMethod(0, function() {});" + |
| 68 |
"(new Foo).bar()" |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
public void testMovePrototypeMethod2() { |
| 73 |
test(createModuleChain( |
| 74 |
"function Foo() {}" + |
| 75 |
"Foo.prototype = { method: function() {} };", |
| 76 |
|
| 77 |
"(new Foo).method()"), |
| 78 |
new String[] { |
| 79 |
STUB_DECLARATIONS + |
| 80 |
"function Foo() {}" + |
| 81 |
"Foo.prototype = { method: JSCompiler_stubMethod(0) };", |
| 82 |
|
| 83 |
"Foo.prototype.method = " + |
| 84 |
" JSCompiler_unstubMethod(0, function() {});" + |
| 85 |
"(new Foo).method()" |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
public void testMovePrototypeMethod3() { |
| 90 |
testSame(createModuleChain( |
| 91 |
"function Foo() {}" + |
| 92 |
"Foo.prototype = { get method() {} };", |
| 93 |
|
| 94 |
"(new Foo).method()")); |
| 95 |
} |
| 96 |
|
| 97 |
public void testMovePrototypeRecursiveMethod() { |
| 98 |
test(createModuleChain( |
| 99 |
"function Foo() {}" + |
| 100 |
"Foo.prototype.baz = function() { this.baz(); };", |
| 101 |
|
| 102 |
"(new Foo).baz()"), |
| 103 |
new String[] { |
| 104 |
STUB_DECLARATIONS + |
| 105 |
"function Foo() {}" + |
| 106 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);", |
| 107 |
|
| 108 |
"Foo.prototype.baz = JSCompiler_unstubMethod(0, " + |
| 109 |
" function() { this.baz(); });" + |
| 110 |
"(new Foo).baz()" |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
public void testCantMovePrototypeProp() { |
| 115 |
testSame(createModuleChain( |
| 116 |
"function Foo() {}" + |
| 117 |
"Foo.prototype.baz = goog.nullFunction;", |
| 118 |
|
| 119 |
"(new Foo).baz()")); |
| 120 |
} |
| 121 |
|
| 122 |
public void testMoveMethodsInRightOrder() { |
| 123 |
test(createModuleChain( |
| 124 |
"function Foo() {}" + |
| 125 |
"Foo.prototype.baz = function() { return 1; };" + |
| 126 |
"Foo.prototype.baz = function() { return 2; };", |
| 127 |
|
| 128 |
"(new Foo).baz()"), |
| 129 |
new String[] { |
| 130 |
STUB_DECLARATIONS + |
| 131 |
"function Foo() {}" + |
| 132 |
"Foo.prototype.baz = JSCompiler_stubMethod(1);" + |
| 133 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);", |
| 134 |
|
| 135 |
"Foo.prototype.baz = " + |
| 136 |
"JSCompiler_unstubMethod(1, function() { return 1; });" + |
| 137 |
"Foo.prototype.baz = " + |
| 138 |
"JSCompiler_unstubMethod(0, function() { return 2; });" + |
| 139 |
"(new Foo).baz()" |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
public void testMoveMethodsInRightOrder2() { |
| 144 |
JSModule[] m = createModules( |
| 145 |
"function Foo() {}" + |
| 146 |
"Foo.prototype.baz = function() { return 1; };" + |
| 147 |
"function Goo() {}" + |
| 148 |
"Goo.prototype.baz = function() { return 2; };", |
| 149 |
|
| 150 |
"", |
| 151 |
|
| 152 |
"(new Foo).baz()", |
| 153 |
|
| 154 |
"", |
| 155 |
|
| 156 |
"(new Goo).baz()"); |
| 157 |
|
| 158 |
m[1].addDependency(m[0]); |
| 159 |
m[2].addDependency(m[1]); |
| 160 |
m[3].addDependency(m[2]); |
| 161 |
m[4].addDependency(m[2]); |
| 162 |
|
| 163 |
test(m, |
| 164 |
new String[] { |
| 165 |
STUB_DECLARATIONS + |
| 166 |
"function Foo() {}" + |
| 167 |
"Foo.prototype.baz = JSCompiler_stubMethod(1);" + |
| 168 |
"function Goo() {}" + |
| 169 |
"Goo.prototype.baz = JSCompiler_stubMethod(0);", |
| 170 |
|
| 171 |
"", |
| 172 |
|
| 173 |
"Foo.prototype.baz = " + |
| 174 |
"JSCompiler_unstubMethod(1, function() { return 1; });" + |
| 175 |
"Goo.prototype.baz = " + |
| 176 |
"JSCompiler_unstubMethod(0, function() { return 2; });" + |
| 177 |
"(new Foo).baz()", |
| 178 |
|
| 179 |
"", |
| 180 |
|
| 181 |
"(new Goo).baz()" |
| 182 |
}); |
| 183 |
} |
| 184 |
|
| 185 |
public void testMoveMethodsUsedInTwoModules() { |
| 186 |
testSame(createModuleStar( |
| 187 |
"function Foo() {}" + |
| 188 |
"Foo.prototype.baz = function() {};", |
| 189 |
|
| 190 |
"(new Foo).baz()", |
| 191 |
|
| 192 |
"(new Foo).baz()")); |
| 193 |
} |
| 194 |
|
| 195 |
public void testMoveMethodsUsedInTwoModules2() { |
| 196 |
JSModule[] modules = createModules( |
| 197 |
"function Foo() {}" + |
| 198 |
"Foo.prototype.baz = function() {};", |
| 199 |
|
| 200 |
"", |
| 201 |
|
| 202 |
"(new Foo).baz() + 1", |
| 203 |
|
| 204 |
"(new Foo).baz() + 2"); |
| 205 |
|
| 206 |
modules[1].addDependency(modules[0]); |
| 207 |
modules[2].addDependency(modules[1]); |
| 208 |
modules[3].addDependency(modules[1]); |
| 209 |
test(modules, |
| 210 |
new String[] { |
| 211 |
STUB_DECLARATIONS + |
| 212 |
"function Foo() {}" + |
| 213 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);", |
| 214 |
|
| 215 |
"Foo.prototype.baz = JSCompiler_unstubMethod(0, function() {});", |
| 216 |
|
| 217 |
"(new Foo).baz() + 1", |
| 218 |
|
| 219 |
"(new Foo).baz() + 2" |
| 220 |
}); |
| 221 |
} |
| 222 |
|
| 223 |
public void testTwoMethods() { |
| 224 |
test(createModuleChain( |
| 225 |
"function Foo() {}" + |
| 226 |
"Foo.prototype.baz = function() {};", |
| 227 |
|
| 228 |
"Foo.prototype.callBaz = function() { this.baz(); }", |
| 229 |
|
| 230 |
"(new Foo).callBaz()"), |
| 231 |
new String[] { |
| 232 |
STUB_DECLARATIONS + |
| 233 |
"function Foo() {}" + |
| 234 |
"Foo.prototype.baz = JSCompiler_stubMethod(1);", |
| 235 |
|
| 236 |
"Foo.prototype.callBaz = JSCompiler_stubMethod(0);", |
| 237 |
|
| 238 |
"Foo.prototype.baz = JSCompiler_unstubMethod(1, function() {});" + |
| 239 |
"Foo.prototype.callBaz = " + |
| 240 |
" JSCompiler_unstubMethod(0, function() { this.baz(); });" + |
| 241 |
"(new Foo).callBaz()" |
| 242 |
}); |
| 243 |
} |
| 244 |
|
| 245 |
public void testTwoMethods2() { |
| 246 |
|
| 247 |
|
| 248 |
test(createModuleChain( |
| 249 |
"function Foo() {}" + |
| 250 |
"Foo.prototype.baz = function() {};", |
| 251 |
|
| 252 |
"(new Foo).callBaz()", |
| 253 |
|
| 254 |
"Foo.prototype.callBaz = function() { this.baz(); }"), |
| 255 |
new String[] { |
| 256 |
STUB_DECLARATIONS + |
| 257 |
"function Foo() {}" + |
| 258 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);", |
| 259 |
|
| 260 |
"(new Foo).callBaz()", |
| 261 |
|
| 262 |
"Foo.prototype.baz = JSCompiler_unstubMethod(0, function() {});" + |
| 263 |
"Foo.prototype.callBaz = function() { this.baz(); };" |
| 264 |
}); |
| 265 |
} |
| 266 |
|
| 267 |
public void testGlobalFunctionsInGraph() { |
| 268 |
test(createModuleChain( |
| 269 |
"function Foo() {}" + |
| 270 |
"Foo.prototype.baz = function() {};" + |
| 271 |
"function x() { return (new Foo).baz(); }", |
| 272 |
|
| 273 |
"x();"), |
| 274 |
new String[] { |
| 275 |
STUB_DECLARATIONS + |
| 276 |
"function Foo() {}" + |
| 277 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);" + |
| 278 |
"function x() { return (new Foo).baz(); }", |
| 279 |
|
| 280 |
"Foo.prototype.baz = JSCompiler_unstubMethod(0, function() {});" + |
| 281 |
"x();" |
| 282 |
}); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
public void testClosureVariableReads1() { |
| 287 |
testSame(createModuleChain( |
| 288 |
"function Foo() {}" + |
| 289 |
"(function() {" + |
| 290 |
"var x = 'x';" + |
| 291 |
"Foo.prototype.baz = function() {x};" + |
| 292 |
"})();", |
| 293 |
|
| 294 |
"var y = new Foo(); y.baz();")); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
public void testClosureVariableReads2() { |
| 299 |
test(createModuleChain( |
| 300 |
"function Foo() {}" + |
| 301 |
"Foo.prototype.b1 = function() {" + |
| 302 |
" var x = 1;" + |
| 303 |
" Foo.prototype.b2 = function() {" + |
| 304 |
" Foo.prototype.b3 = function() {" + |
| 305 |
" x;" + |
| 306 |
" }" + |
| 307 |
" }" + |
| 308 |
"};", |
| 309 |
|
| 310 |
"var y = new Foo(); y.b1();", |
| 311 |
|
| 312 |
"y = new Foo(); z.b2();", |
| 313 |
|
| 314 |
"y = new Foo(); z.b3();" |
| 315 |
), |
| 316 |
new String[] { |
| 317 |
STUB_DECLARATIONS + |
| 318 |
"function Foo() {}" + |
| 319 |
"Foo.prototype.b1 = JSCompiler_stubMethod(0);", |
| 320 |
|
| 321 |
"Foo.prototype.b1 = JSCompiler_unstubMethod(0, function() {" + |
| 322 |
" var x = 1;" + |
| 323 |
" Foo.prototype.b2 = function() {" + |
| 324 |
" Foo.prototype.b3 = function() {" + |
| 325 |
" x;" + |
| 326 |
" }" + |
| 327 |
" }" + |
| 328 |
"});" + |
| 329 |
"var y = new Foo(); y.b1();", |
| 330 |
|
| 331 |
"y = new Foo(); z.b2();", |
| 332 |
|
| 333 |
"y = new Foo(); z.b3();" |
| 334 |
}); |
| 335 |
} |
| 336 |
|
| 337 |
public void testClosureVariableReads3() { |
| 338 |
test(createModuleChain( |
| 339 |
"function Foo() {}" + |
| 340 |
"Foo.prototype.b1 = function() {" + |
| 341 |
" Foo.prototype.b2 = function() {" + |
| 342 |
" var x = 1;" + |
| 343 |
" Foo.prototype.b3 = function() {" + |
| 344 |
" x;" + |
| 345 |
" }" + |
| 346 |
" }" + |
| 347 |
"};", |
| 348 |
|
| 349 |
"var y = new Foo(); y.b1();", |
| 350 |
|
| 351 |
"y = new Foo(); z.b2();", |
| 352 |
|
| 353 |
"y = new Foo(); z.b3();" |
| 354 |
), |
| 355 |
new String[] { |
| 356 |
STUB_DECLARATIONS + |
| 357 |
"function Foo() {}" + |
| 358 |
"Foo.prototype.b1 = JSCompiler_stubMethod(0);", |
| 359 |
|
| 360 |
"Foo.prototype.b1 = JSCompiler_unstubMethod(0, function() {" + |
| 361 |
" Foo.prototype.b2 = JSCompiler_stubMethod(1);" + |
| 362 |
"});" + |
| 363 |
"var y = new Foo(); y.b1();", |
| 364 |
|
| 365 |
"Foo.prototype.b2 = JSCompiler_unstubMethod(1, function() {" + |
| 366 |
" var x = 1;" + |
| 367 |
" Foo.prototype.b3 = function() {" + |
| 368 |
" x;" + |
| 369 |
" }" + |
| 370 |
"});" + |
| 371 |
"y = new Foo(); z.b2();", |
| 372 |
|
| 373 |
"y = new Foo(); z.b3();" |
| 374 |
}); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
public void testNoClosureVariableReads1() { |
| 379 |
test(createModuleChain( |
| 380 |
"function Foo() {}" + |
| 381 |
"var x = 'x';" + |
| 382 |
"Foo.prototype.baz = function(){x};", |
| 383 |
|
| 384 |
"var y = new Foo(); y.baz();"), |
| 385 |
new String[] { |
| 386 |
STUB_DECLARATIONS + |
| 387 |
"function Foo() {}" + |
| 388 |
"var x = 'x';" + |
| 389 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);", |
| 390 |
|
| 391 |
"Foo.prototype.baz = JSCompiler_unstubMethod(0, function(){x});" + |
| 392 |
"var y = new Foo(); y.baz();" |
| 393 |
}); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
public void testNoClosureVariableReads2() { |
| 398 |
test(createModuleChain( |
| 399 |
"function Foo() {}" + |
| 400 |
"Foo.prototype.baz = function(){var x = 1;x};", |
| 401 |
|
| 402 |
"var y = new Foo(); y.baz();"), |
| 403 |
new String[] { |
| 404 |
STUB_DECLARATIONS + |
| 405 |
"function Foo() {}" + |
| 406 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);", |
| 407 |
|
| 408 |
"Foo.prototype.baz = JSCompiler_unstubMethod(" + |
| 409 |
" 0, function(){var x = 1; x});" + |
| 410 |
"var y = new Foo(); y.baz();" |
| 411 |
}); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
public void testInnerFunctionClosureVariableReads() { |
| 416 |
test(createModuleChain( |
| 417 |
"function Foo() {}" + |
| 418 |
"Foo.prototype.baz = function(){var x = 1;" + |
| 419 |
" return function(){x}};", |
| 420 |
|
| 421 |
"var y = new Foo(); y.baz();"), |
| 422 |
new String[] { |
| 423 |
STUB_DECLARATIONS + |
| 424 |
"function Foo() {}" + |
| 425 |
"Foo.prototype.baz = JSCompiler_stubMethod(0);", |
| 426 |
|
| 427 |
"Foo.prototype.baz = JSCompiler_unstubMethod(" + |
| 428 |
" 0, function(){var x = 1; return function(){x}});" + |
| 429 |
"var y = new Foo(); y.baz();" |
| 430 |
}); |
| 431 |
} |
| 432 |
|
| 433 |
public void testIssue600() { |
| 434 |
testSame( |
| 435 |
createModuleChain( |
| 436 |
"var jQuery1 = (function() {\n" + |
| 437 |
" var jQuery2 = function() {};\n" + |
| 438 |
" var theLoneliestNumber = 1;\n" + |
| 439 |
" jQuery2.prototype = {\n" + |
| 440 |
" size: function() {\n" + |
| 441 |
" return theLoneliestNumber;\n" + |
| 442 |
" }\n" + |
| 443 |
" };\n" + |
| 444 |
" return jQuery2;\n" + |
| 445 |
"})();\n", |
| 446 |
|
| 447 |
"(function() {" + |
| 448 |
" var div = jQuery1('div');" + |
| 449 |
" div.size();" + |
| 450 |
"})();")); |
| 451 |
} |
| 452 |
|
| 453 |
public void testIssue600b() { |
| 454 |
testSame( |
| 455 |
createModuleChain( |
| 456 |
"var jQuery1 = (function() {\n" + |
| 457 |
" var jQuery2 = function() {};\n" + |
| 458 |
" jQuery2.prototype = {\n" + |
| 459 |
" size: function() {\n" + |
| 460 |
" return 1;\n" + |
| 461 |
" }\n" + |
| 462 |
" };\n" + |
| 463 |
" return jQuery2;\n" + |
| 464 |
"})();\n", |
| 465 |
|
| 466 |
"(function() {" + |
| 467 |
" var div = jQuery1('div');" + |
| 468 |
" div.size();" + |
| 469 |
"})();")); |
| 470 |
} |
| 471 |
|
| 472 |
public void testIssue600c() { |
| 473 |
test( |
| 474 |
createModuleChain( |
| 475 |
"var jQuery2 = function() {};\n" + |
| 476 |
"jQuery2.prototype = {\n" + |
| 477 |
" size: function() {\n" + |
| 478 |
" return 1;\n" + |
| 479 |
" }\n" + |
| 480 |
"};\n", |
| 481 |
|
| 482 |
"(function() {" + |
| 483 |
" var div = jQuery2('div');" + |
| 484 |
" div.size();" + |
| 485 |
"})();"), |
| 486 |
new String[] { |
| 487 |
STUB_DECLARATIONS + |
| 488 |
"var jQuery2 = function() {};\n" + |
| 489 |
"jQuery2.prototype = {\n" + |
| 490 |
" size: JSCompiler_stubMethod(0)\n" + |
| 491 |
"};\n", |
| 492 |
"jQuery2.prototype.size=" + |
| 493 |
" JSCompiler_unstubMethod(0,function(){return 1});" + |
| 494 |
"(function() {" + |
| 495 |
" var div = jQuery2('div');" + |
| 496 |
" div.size();" + |
| 497 |
"})();" |
| 498 |
}); |
| 499 |
} |
| 500 |
|
| 501 |
public void testIssue600d() { |
| 502 |
test( |
| 503 |
createModuleChain( |
| 504 |
"var jQuery2 = function() {};\n" + |
| 505 |
"(function() {" + |
| 506 |
" jQuery2.prototype = {\n" + |
| 507 |
" size: function() {\n" + |
| 508 |
" return 1;\n" + |
| 509 |
" }\n" + |
| 510 |
" };\n" + |
| 511 |
"})();", |
| 512 |
|
| 513 |
"(function() {" + |
| 514 |
" var div = jQuery2('div');" + |
| 515 |
" div.size();" + |
| 516 |
"})();"), |
| 517 |
new String[] { |
| 518 |
STUB_DECLARATIONS + |
| 519 |
"var jQuery2 = function() {};\n" + |
| 520 |
"(function() {" + |
| 521 |
" jQuery2.prototype = {\n" + |
| 522 |
" size: JSCompiler_stubMethod(0)\n" + |
| 523 |
" };\n" + |
| 524 |
"})();", |
| 525 |
"jQuery2.prototype.size=" + |
| 526 |
" JSCompiler_unstubMethod(0,function(){return 1});" + |
| 527 |
"(function() {" + |
| 528 |
" var div = jQuery2('div');" + |
| 529 |
" div.size();" + |
| 530 |
"})();" |
| 531 |
}); |
| 532 |
} |
| 533 |
|
| 534 |
public void testIssue600e() { |
| 535 |
testSame( |
| 536 |
createModuleChain( |
| 537 |
"var jQuery2 = function() {};\n" + |
| 538 |
"(function() {" + |
| 539 |
" var theLoneliestNumber = 1;\n" + |
| 540 |
" jQuery2.prototype = {\n" + |
| 541 |
" size: function() {\n" + |
| 542 |
" return theLoneliestNumber;\n" + |
| 543 |
" }\n" + |
| 544 |
" };\n" + |
| 545 |
"})();", |
| 546 |
|
| 547 |
"(function() {" + |
| 548 |
" var div = jQuery2('div');" + |
| 549 |
" div.size();" + |
| 550 |
"})();")); |
| 551 |
} |
| 552 |
|
| 553 |
public void testPrototypeOfThisAssign() { |
| 554 |
testSame( |
| 555 |
createModuleChain( |
| 556 |
"/** @constructor */" + |
| 557 |
"function F() {}" + |
| 558 |
"this.prototype.foo = function() {};", |
| 559 |
"(new F()).foo();")); |
| 560 |
} |
| 561 |
} |