Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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 CrossModuleMethodMotion}.
21 *
22 * @author [email protected] (Nick Santos)
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 // Module 2
54 "(new Foo).bar()"));
55
56 canMoveExterns = true;
57 test(createModuleChain(
58 "function Foo() {}" +
59 "Foo.prototype.bar = function() {};",
60 // Module 2
61 "(new Foo).bar()"),
62 new String[] {
63 STUB_DECLARATIONS +
64 "function Foo() {}" +
65 "Foo.prototype.bar = JSCompiler_stubMethod(0);",
66 // Module 2
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 // Module 2
77 "(new Foo).method()"),
78 new String[] {
79 STUB_DECLARATIONS +
80 "function Foo() {}" +
81 "Foo.prototype = { method: JSCompiler_stubMethod(0) };",
82 // Module 2
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 // Module 2
94 "(new Foo).method()"));
95 }
96
97 public void testMovePrototypeRecursiveMethod() {
98 test(createModuleChain(
99 "function Foo() {}" +
100 "Foo.prototype.baz = function() { this.baz(); };",
101 // Module 2
102 "(new Foo).baz()"),
103 new String[] {
104 STUB_DECLARATIONS +
105 "function Foo() {}" +
106 "Foo.prototype.baz = JSCompiler_stubMethod(0);",
107 // Module 2
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 // Module 2
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 // Module 2
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 // Module 2
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 // Module 2, depends on 1
150 "",
151 // Module 3, depends on 2
152 "(new Foo).baz()",
153 // Module 4, depends on 3
154 "",
155 // Module 5, depends on 3
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 // Module 2
171 "",
172 // Module 3
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 // Module 4
179 "",
180 // Module 5
181 "(new Goo).baz()"
182 });
183 }
184
185 public void testMoveMethodsUsedInTwoModules() {
186 testSame(createModuleStar(
187 "function Foo() {}" +
188 "Foo.prototype.baz = function() {};",
189 // Module 2
190 "(new Foo).baz()",
191 // Module 3
192 "(new Foo).baz()"));
193 }
194
195 public void testMoveMethodsUsedInTwoModules2() {
196 JSModule[] modules = createModules(
197 "function Foo() {}" +
198 "Foo.prototype.baz = function() {};",
199 // Module 2
200 "", // a blank module in the middle
201 // Module 3
202 "(new Foo).baz() + 1",
203 // Module 4
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 // Module 2
215 "Foo.prototype.baz = JSCompiler_unstubMethod(0, function() {});",
216 // Module 3
217 "(new Foo).baz() + 1",
218 // Module 4
219 "(new Foo).baz() + 2"
220 });
221 }
222
223 public void testTwoMethods() {
224 test(createModuleChain(
225 "function Foo() {}" +
226 "Foo.prototype.baz = function() {};",
227 // Module 2
228 "Foo.prototype.callBaz = function() { this.baz(); }",
229 // Module 3
230 "(new Foo).callBaz()"),
231 new String[] {
232 STUB_DECLARATIONS +
233 "function Foo() {}" +
234 "Foo.prototype.baz = JSCompiler_stubMethod(1);",
235 // Module 2
236 "Foo.prototype.callBaz = JSCompiler_stubMethod(0);",
237 // Module 3
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 // if the programmer screws up the module order, we don't try to correct
247 // the mistake.
248 test(createModuleChain(
249 "function Foo() {}" +
250 "Foo.prototype.baz = function() {};",
251 // Module 2
252 "(new Foo).callBaz()",
253 // Module 3
254 "Foo.prototype.callBaz = function() { this.baz(); }"),
255 new String[] {
256 STUB_DECLARATIONS +
257 "function Foo() {}" +
258 "Foo.prototype.baz = JSCompiler_stubMethod(0);",
259 // Module 2
260 "(new Foo).callBaz()",
261 // Module 3
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 // Module 2
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 // Module 2
280 "Foo.prototype.baz = JSCompiler_unstubMethod(0, function() {});" +
281 "x();"
282 });
283 }
284
285 // Read of closure variable disables method motions.
286 public void testClosureVariableReads1() {
287 testSame(createModuleChain(
288 "function Foo() {}" +
289 "(function() {" +
290 "var x = 'x';" +
291 "Foo.prototype.baz = function() {x};" +
292 "})();",
293 // Module 2
294 "var y = new Foo(); y.baz();"));
295 }
296
297 // Read of global variable is fine.
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 // Module 2
310 "var y = new Foo(); y.b1();",
311 // Module 3
312 "y = new Foo(); z.b2();",
313 // Module 4
314 "y = new Foo(); z.b3();"
315 ),
316 new String[] {
317 STUB_DECLARATIONS +
318 "function Foo() {}" +
319 "Foo.prototype.b1 = JSCompiler_stubMethod(0);",
320 // Module 2
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 // Module 3
331 "y = new Foo(); z.b2();",
332 // Module 4
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 // Module 2
349 "var y = new Foo(); y.b1();",
350 // Module 3
351 "y = new Foo(); z.b2();",
352 // Module 4
353 "y = new Foo(); z.b3();"
354 ),
355 new String[] {
356 STUB_DECLARATIONS +
357 "function Foo() {}" +
358 "Foo.prototype.b1 = JSCompiler_stubMethod(0);",
359 // Module 2
360 "Foo.prototype.b1 = JSCompiler_unstubMethod(0, function() {" +
361 " Foo.prototype.b2 = JSCompiler_stubMethod(1);" +
362 "});" +
363 "var y = new Foo(); y.b1();",
364 // Module 3
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 // Module 4
373 "y = new Foo(); z.b3();"
374 });
375 }
376
377 // Read of global variable is fine.
378 public void testNoClosureVariableReads1() {
379 test(createModuleChain(
380 "function Foo() {}" +
381 "var x = 'x';" +
382 "Foo.prototype.baz = function(){x};",
383 // Module 2
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 // Module 2
391 "Foo.prototype.baz = JSCompiler_unstubMethod(0, function(){x});" +
392 "var y = new Foo(); y.baz();"
393 });
394 }
395
396 // Read of a local is fine.
397 public void testNoClosureVariableReads2() {
398 test(createModuleChain(
399 "function Foo() {}" +
400 "Foo.prototype.baz = function(){var x = 1;x};",
401 // Module 2
402 "var y = new Foo(); y.baz();"),
403 new String[] {
404 STUB_DECLARATIONS +
405 "function Foo() {}" +
406 "Foo.prototype.baz = JSCompiler_stubMethod(0);",
407 // Module 2
408 "Foo.prototype.baz = JSCompiler_unstubMethod(" +
409 " 0, function(){var x = 1; x});" +
410 "var y = new Foo(); y.baz();"
411 });
412 }
413
414 // An anonymous inner function reading a closure variable is fine.
415 public void testInnerFunctionClosureVariableReads() {
416 test(createModuleChain(
417 "function Foo() {}" +
418 "Foo.prototype.baz = function(){var x = 1;" +
419 " return function(){x}};",
420 // Module 2
421 "var y = new Foo(); y.baz();"),
422 new String[] {
423 STUB_DECLARATIONS +
424 "function Foo() {}" +
425 "Foo.prototype.baz = JSCompiler_stubMethod(0);",
426 // Module 2
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 }