Debian Codegraph @

test/com/google/javascript/jscomp/InlineFunctionsTest.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 /**
21 * Inline function tests.
22 * @author [email protected] (john lenz)
23 */
24 public class InlineFunctionsTest extends CompilerTestCase {
25 boolean allowGlobalFunctionInlining = true;
26 boolean allowBlockInlining = true;
27 final boolean allowExpressionDecomposition = true;
28 final boolean allowFunctionExpressionInlining = true;
29 final boolean allowLocalFunctionInlining = true;
30 boolean assumeStrictThis = false;
31 boolean assumeMinimumCapture = false;
32
33 public InlineFunctionsTest() {
34 this.enableNormalize();
35 this.enableMarkNoSideEffects();
36 }
37
38 @Override
39 protected void setUp() throws Exception {
40 super.setUp();
41 super.enableLineNumberCheck(true);
42 allowGlobalFunctionInlining = true;
43 allowBlockInlining = true;
44 assumeStrictThis = false;
45 assumeMinimumCapture = false;
46 }
47
48 @Override
49 protected CompilerPass getProcessor(Compiler compiler) {
50 compiler.resetUniqueNameId();
51 return new InlineFunctions(
52 compiler,
53 compiler.getUniqueNameIdSupplier(),
54 allowGlobalFunctionInlining,
55 allowLocalFunctionInlining,
56 allowBlockInlining,
57 assumeStrictThis,
58 assumeMinimumCapture);
59 }
60
61 /**
62 * Returns the number of times the pass should be run before results are
63 * verified.
64 */
65 @Override
66 protected int getNumRepetitions() {
67 // Some inlining can only be done in multiple passes.
68 return 3;
69 }
70
71 public void testInlineEmptyFunction1() {
72 // Empty function, no params.
73 test("function foo(){}" +
74 "foo();",
75 "void 0;");
76 }
77
78 public void testInlineEmptyFunction2() {
79 // Empty function, params with no side-effects.
80 test("function foo(){}" +
81 "foo(1, new Date, function(){});",
82 "void 0;");
83 }
84
85 public void testInlineEmptyFunction3() {
86 // Empty function, multiple references.
87 test("function foo(){}" +
88 "foo();foo();foo();",
89 "void 0;void 0;void 0");
90 }
91
92 public void testInlineEmptyFunction4() {
93 // Empty function, params with side-effects forces block inlining.
94 test("function foo(){}" +
95 "foo(x());",
96 "{var JSCompiler_inline_anon_param_0=x();}");
97 }
98
99 public void testInlineEmptyFunction5() {
100 // Empty function, call params with side-effects in expression can not
101 // be inlined.
102 allowBlockInlining = false;
103 testSame("function foo(){}" +
104 "foo(x());");
105 }
106
107 public void testInlineFunctions1() {
108 // As simple a test as we can get.
109 test("function foo(){ return 4 }" +
110 "foo();",
111 "4");
112 }
113
114 public void testInlineFunctions2() {
115 // inline simple constants
116 // NOTE: CD is not inlined.
117 test("var t;var AB=function(){return 4};" +
118 "function BC(){return 6;}" +
119 "CD=function(x){return x + 5};x=CD(3);y=AB();z=BC();",
120 "var t;CD=function(x){return x+5};x=CD(3);y=4;z=6"
121 );
122 }
123
124 public void testInlineFunctions3() {
125 // inline simple constants
126 test("var t;var AB=function(){return 4};" +
127 "function BC(){return 6;}" +
128 "var CD=function(x){return x + 5};x=CD(3);y=AB();z=BC();",
129 "var t;x=3+5;y=4;z=6");
130 }
131
132 public void testInlineFunctions4() {
133 // don't inline if there are multiple definitions (need DFA for that).
134 test("var t; var AB = function() { return 4 }; " +
135 "function BC() { return 6; }" +
136 "CD = 0;" +
137 "CD = function(x) { return x + 5 }; x = CD(3); y = AB(); z = BC();",
138
139 "var t;CD=0;CD=function(x){return x+5};x=CD(3);y=4;z=6");
140 }
141
142 public void testInlineFunctions5() {
143 // inline additions
144 test("var FOO_FN=function(x,y) { return \"de\" + x + \"nu\" + y };" +
145 "var a = FOO_FN(\"ez\", \"ts\")",
146
147 "var a=\"de\"+\"ez\"+\"nu\"+\"ts\"");
148 }
149
150 public void testInlineFunctions6() {
151 // more complex inlines
152 test("function BAR_FN(x, y, z) { return z(foo(x + y)) }" +
153 "alert(BAR_FN(1, 2, baz))",
154
155 "alert(baz(foo(1+2)))");
156 }
157
158 public void testInlineFunctions7() {
159 // inlines appearing multiple times
160 test("function FN(x,y,z){return x+x+y}" +
161 "var b=FN(1,2,3)",
162
163 "var b=1+1+2");
164 }
165
166 public void testInlineFunctions8() {
167 // check correct parenthesization
168 test("function MUL(x,y){return x*y}function ADD(x,y){return x+y}" +
169 "var a=1+MUL(2,3);var b=2*ADD(3,4)",
170
171 "var a=1+2*3;var b=2*(3+4)");
172 }
173
174 public void testInlineFunctions9() {
175 // don't inline if the input parameter is modified.
176 test("function INC(x){return x++}" +
177 "var y=INC(i)",
178 "var y;{var x$$inline_0=i;" +
179 "y=x$$inline_0++}");
180 }
181
182 public void testInlineFunctions10() {
183 test("function INC(x){return x++}" +
184 "var y=INC(i);y=INC(i)",
185 "var y;" +
186 "{var x$$inline_0=i;" +
187 "y=x$$inline_0++}" +
188 "{var x$$inline_2=i;" +
189 "y=x$$inline_2++}");
190 }
191
192 public void testInlineFunctions11() {
193 test("function f(x){return x}" +
194 "var y=f(i)",
195 "var y=i");
196 }
197
198 public void testInlineFunctions12() {
199 // don't inline if the input parameter has side-effects.
200 allowBlockInlining = false;
201 test("function f(x){return x}" +
202 "var y=f(i)",
203 "var y=i");
204 testSame("function f(x){return x}" +
205 "var y=f(i++)");
206 }
207
208 public void testInlineFunctions13() {
209 // inline as block if the input parameter has side-effects.
210 test("function f(x){return x}" +
211 "var y=f(i++)",
212 "var y;{var x$$inline_0=i++;y=x$$inline_0}");
213 }
214
215 public void testInlineFunctions14() {
216 // don't remove functions that are referenced on other ways
217 test("function FOO(x){return x}var BAR=function(y){return y}" +
218 ";b=FOO;a(BAR);x=FOO(1);y=BAR(2)",
219
220 "function FOO(x){return x}var BAR=function(y){return y}" +
221 ";b=FOO;a(BAR);x=1;y=2");
222 }
223
224 public void testInlineFunctions15a() {
225 // closure factories: do inline into global scope.
226 test("function foo(){return function(a){return a+1}}" +
227 "var b=function(){return c};" +
228 "var d=b()+foo()",
229
230 "var d=c+function(a){return a+1}");
231 }
232
233 public void testInlineFunctions15b() {
234 assumeMinimumCapture = false;
235
236 // closure factories: don't inline closure with locals into global scope.
237 test("function foo(){var x;return function(a){return a+1}}" +
238 "var b=function(){return c};" +
239 "var d=b()+foo()",
240
241 "function foo(){var x;return function(a){return a+1}}" +
242 "var d=c+foo()");
243
244 assumeMinimumCapture = true;
245
246 test("function foo(){var x;return function(a){return a+1}}" +
247 "var b=function(){return c};" +
248 "var d=b()+foo()",
249
250 "var JSCompiler_temp_const$$0 = c;\n" +
251 "var JSCompiler_inline_result$$1;\n" +
252 "{\n" +
253 "var x$$inline_2;\n" +
254 "JSCompiler_inline_result$$1 = " +
255 " function(a$$inline_3){ return a$$inline_3+1 };\n" +
256 "}" +
257 "var d=JSCompiler_temp_const$$0 + JSCompiler_inline_result$$1");
258 }
259
260 public void testInlineFunctions15c() {
261 assumeMinimumCapture = false;
262
263 // closure factories: don't inline into non-global scope.
264 test("function foo(){return function(a){return a+1}}" +
265 "var b=function(){return c};" +
266 "function _x(){ var d=b()+foo() }",
267
268 "function foo(){return function(a){return a+1}}" +
269 "function _x(){ var d=c+foo() }");
270
271 assumeMinimumCapture = true;
272
273 // closure factories: don't inline into non-global scope.
274 test("function foo(){return function(a){return a+1}}" +
275 "var b=function(){return c};" +
276 "function _x(){ var d=b()+foo() }",
277
278 "function _x(){var d=c+function(a){return a+1}}");
279
280 }
281
282 public void testInlineFunctions15d() {
283 assumeMinimumCapture = false;
284
285 // closure factories: don't inline functions with vars.
286 test("function foo(){var x; return function(a){return a+1}}" +
287 "var b=function(){return c};" +
288 "function _x(){ var d=b()+foo() }",
289
290 "function foo(){var x; return function(a){return a+1}}" +
291 "function _x(){ var d=c+foo() }");
292
293 assumeMinimumCapture = true;
294
295 // closure factories: don't inline functions with vars.
296 test("function foo(){var x; return function(a){return a+1}}" +
297 "var b=function(){return c};" +
298 "function _x(){ var d=b()+foo() }",
299
300 "function _x() { \n" +
301 " var JSCompiler_temp_const$$0 = c;\n" +
302 " var JSCompiler_inline_result$$1;\n" +
303 " {\n" +
304 " var x$$inline_2;\n" +
305 " JSCompiler_inline_result$$1 = " +
306 " function(a$$inline_3) {return a$$inline_3+1};\n" +
307 " }\n" +
308 " var d = JSCompiler_temp_const$$0+JSCompiler_inline_result$$1\n" +
309 "}");
310 }
311
312 public void testInlineFunctions16a() {
313 assumeMinimumCapture = false;
314
315 testSame("function foo(b){return window.bar(function(){c(b)})}" +
316 "var d=foo(e)");
317
318 assumeMinimumCapture = true;
319
320 test(
321 "function foo(b){return window.bar(function(){c(b)})}" +
322 "var d=foo(e)",
323 "var d;{var b$$inline_0=e;" +
324 "d=window.bar(function(){c(b$$inline_0)})}");
325 }
326
327 public void testInlineFunctions16b() {
328 test("function foo(){return window.bar(function(){c()})}" +
329 "var d=foo(e)",
330 "var d=window.bar(function(){c()})");
331 }
332
333 public void testInlineFunctions17() {
334 // don't inline recursive functions
335 testSame("function foo(x){return x*x+foo(3)}var bar=foo(4)");
336 }
337
338 public void testInlineFunctions18() {
339 // TRICKY ... test nested inlines
340 allowBlockInlining = false;
341 test("function foo(a, b){return a+b}" +
342 "function bar(d){return c}" +
343 "var d=foo(bar(1),e)",
344 "var d=c+e");
345 }
346
347 public void testInlineFunctions19() {
348 // TRICKY ... test nested inlines
349 // with block inlining possible
350 test("function foo(a, b){return a+b}" +
351 "function bar(d){return c}" +
352 "var d=foo(bar(1),e)",
353 "var d;{d=c+e}");
354 }
355
356 public void testInlineFunctions20() {
357 // Make sure both orderings work
358 allowBlockInlining = false;
359 test("function foo(a, b){return a+b}" +
360 "function bar(d){return c}" +
361 "var d=bar(foo(1,e));",
362 "var d=c");
363 }
364
365 public void testInlineFunctions21() {
366 // with block inlining possible
367 test("function foo(a, b){return a+b}" +
368 "function bar(d){return c}" +
369 "var d=bar(foo(1,e))",
370 "var d;{d=c}");
371 }
372
373 public void testInlineFunctions22() {
374 // Another tricky case ... test nested compiler inlines
375 test("function plex(a){if(a) return 0;else return 1;}" +
376 "function foo(a, b){return bar(a+b)}" +
377 "function bar(d){return plex(d)}" +
378 "var d=foo(1,2)",
379
380 "var d;{JSCompiler_inline_label_plex_1:{" +
381 "if(1+2){" +
382 "d=0;break JSCompiler_inline_label_plex_1}" +
383 "else{" +
384 "d=1;break JSCompiler_inline_label_plex_1}d=void 0}}");
385 }
386
387 public void testInlineFunctions23() {
388 // Test both orderings again
389 test("function complex(a){if(a) return 0;else return 1;}" +
390 "function bar(d){return complex(d)}" +
391 "function foo(a, b){return bar(a+b)}" +
392 "var d=foo(1,2)",
393
394 "var d;{JSCompiler_inline_label_complex_1:{" +
395 "if(1+2){" +
396 "d=0;break JSCompiler_inline_label_complex_1" +
397 "}else{" +
398 "d=1;break JSCompiler_inline_label_complex_1" +
399 "}d=void 0}}");
400 }
401
402 public void testInlineFunctions24() {
403 // Don't inline functions with 'arguments' or 'this'
404 testSame("function foo(x){return this}foo(1)");
405 }
406
407 public void testInlineFunctions25() {
408 testSame("function foo(){return arguments[0]}foo()");
409 }
410
411 public void testInlineFunctions26() {
412 // Don't inline external functions
413 testSame("function _foo(x){return x}_foo(1)");
414 }
415
416 public void testInlineFunctions27() {
417 test("var window = {}; function foo(){window.bar++; return 3;}" +
418 "var x = {y: 1, z: foo(2)};",
419 "var window={};" +
420 "var JSCompiler_inline_result$$0;" +
421 "{" +
422 " window.bar++;" +
423 " JSCompiler_inline_result$$0 = 3;" +
424 "}" +
425 "var x = {y: 1, z: JSCompiler_inline_result$$0};");
426 }
427
428 public void testInlineFunctions28() {
429 test("var window = {}; function foo(){window.bar++; return 3;}" +
430 "var x = {y: alert(), z: foo(2)};",
431 "var window = {};" +
432 "var JSCompiler_temp_const$$0 = alert();" +
433 "var JSCompiler_inline_result$$1;" +
434 "{" +
435 " window.bar++;" +
436 " JSCompiler_inline_result$$1 = 3;}" +
437 "var x = {" +
438 " y: JSCompiler_temp_const$$0," +
439 " z: JSCompiler_inline_result$$1" +
440 "};");
441 }
442
443 public void testInlineFunctions29() {
444 test("var window = {}; function foo(){window.bar++; return 3;}" +
445 "var x = {a: alert(), b: alert2(), c: foo(2)};",
446 "var window = {};" +
447 "var JSCompiler_temp_const$$1 = alert();" +
448 "var JSCompiler_temp_const$$0 = alert2();" +
449 "var JSCompiler_inline_result$$2;" +
450 "{" +
451 " window.bar++;" +
452 " JSCompiler_inline_result$$2 = 3;}" +
453 "var x = {" +
454 " a: JSCompiler_temp_const$$1," +
455 " b: JSCompiler_temp_const$$0," +
456 " c: JSCompiler_inline_result$$2" +
457 "};");
458 }
459
460 public void testInlineFunctions30() {
461 // As simple a test as we can get.
462 testSame("function foo(){ return eval() }" +
463 "foo();");
464 }
465
466 public void testInlineFunctions31() {
467 // Don't introduce a duplicate label in the same scope
468 test("function foo(){ lab:{4;} }" +
469 "lab:{foo();}",
470 "lab:{{JSCompiler_inline_label_0:{4}}}");
471 }
472
473 public void testMixedModeInlining1() {
474 // Base line tests, direct inlining
475 test("function foo(){return 1}" +
476 "foo();",
477 "1;");
478 }
479
480 public void testMixedModeInlining2() {
481 // Base line tests, block inlining. Block inlining is needed by
482 // possible-side-effect parameter.
483 test("function foo(){return 1}" +
484 "foo(x());",
485 "{var JSCompiler_inline_anon_param_0=x();1}");
486 }
487
488 public void testMixedModeInlining3() {
489 // Inline using both modes.
490 test("function foo(){return 1}" +
491 "foo();foo(x());",
492 "1;{var JSCompiler_inline_anon_param_0=x();1}");
493 }
494
495 public void testMixedModeInlining4() {
496 // Inline using both modes. Alternating. Second call of each type has
497 // side-effect-less parameter, this is thrown away.
498 test("function foo(){return 1}" +
499 "foo();foo(x());" +
500 "foo(1);foo(1,x());",
501 "1;{var JSCompiler_inline_anon_param_0=x();1}" +
502 "1;{var JSCompiler_inline_anon_param_4=x();1}");
503 }
504
505 public void testMixedModeInliningCosting1() {
506 // Inline using both modes. Costing estimates.
507
508 // Base line.
509 test(
510 "function foo(a,b){return a+b+a+b+4+5+6+7+8+9+1+2+3+4+5}" +
511 "foo(1,2);" +
512 "foo(2,3)",
513
514 "1+2+1+2+4+5+6+7+8+9+1+2+3+4+5;" +
515 "2+3+2+3+4+5+6+7+8+9+1+2+3+4+5");
516 }
517
518 public void testMixedModeInliningCosting2() {
519 // Don't inline here because the function definition can not be eliminated.
520 // TODO(johnlenz): Should we add constant removing to the unit test?
521 testSame(
522 "function foo(a,b){return a+b+a+b+4+5+6+7+8+9+1+2+3+4+5}" +
523 "foo(1,2);" +
524 "foo(2,3,x())");
525 }
526
527 public void testMixedModeInliningCosting3() {
528 // Do inline here because the function definition can be eliminated.
529 test(
530 "function foo(a,b){return a+b+a+b+4+5+6+7+8+9+1+2+3+10}" +
531 "foo(1,2);" +
532 "foo(2,3,x())",
533
534 "1+2+1+2+4+5+6+7+8+9+1+2+3+10;" +
535 "{var JSCompiler_inline_anon_param_2=x();" +
536 "2+3+2+3+4+5+6+7+8+9+1+2+3+10}");
537 }
538
539 public void testMixedModeInliningCosting4() {
540 // Threshold test.
541 testSame(
542 "function foo(a,b){return a+b+a+b+4+5+6+7+8+9+1+2+3+4+101}" +
543 "foo(1,2);" +
544 "foo(2,3,x())");
545 }
546
547 public void testNoInlineIfParametersModified1() {
548 // Assignment
549 test("function f(x){return x=1}f(undefined)",
550 "{var x$$inline_0=undefined;" +
551 "x$$inline_0=1}");
552 }
553
554 public void testNoInlineIfParametersModified2() {
555 test("function f(x){return (x)=1;}f(2)",
556 "{var x$$inline_0=2;" +
557 "x$$inline_0=1}");
558 }
559
560 public void testNoInlineIfParametersModified3() {
561 // Assignment variant.
562 test("function f(x){return x*=2}f(2)",
563 "{var x$$inline_0=2;" +
564 "x$$inline_0*=2}");
565 }
566
567 public void testNoInlineIfParametersModified4() {
568 // Assignment in if.
569 test("function f(x){return x?(x=2):0}f(2)",
570 "{var x$$inline_0=2;" +
571 "x$$inline_0?(" +
572 "x$$inline_0=2):0}");
573 }
574
575 public void testNoInlineIfParametersModified5() {
576 // Assignment in if, multiple params
577 test("function f(x,y){return x?(y=2):0}f(2,undefined)",
578 "{var y$$inline_1=undefined;2?(" +
579 "y$$inline_1=2):0}");
580 }
581
582 public void testNoInlineIfParametersModified6() {
583 test("function f(x,y){return x?(y=2):0}f(2)",
584 "{var y$$inline_1=void 0;2?(" +
585 "y$$inline_1=2):0}");
586 }
587
588 public void testNoInlineIfParametersModified7() {
589 // Increment
590 test("function f(a){return++a<++a}f(1)",
591 "{var a$$inline_0=1;" +
592 "++a$$inline_0<" +
593 "++a$$inline_0}");
594 }
595
596 public void testNoInlineIfParametersModified8() {
597 // OK, object parameter modified.
598 test("function f(a){return a.x=2}f(o)", "o.x=2");
599 }
600
601 public void testNoInlineIfParametersModified9() {
602 // OK, array parameter modified.
603 test("function f(a){return a[2]=2}f(o)", "o[2]=2");
604 }
605
606 public void testInlineNeverPartialSubtitution1() {
607 test("function f(z){return x.y.z;}f(1)",
608 "x.y.z");
609 }
610
611 public void testInlineNeverPartialSubtitution2() {
612 test("function f(z){return x.y[z];}f(a)",
613 "x.y[a]");
614 }
615
616 public void testInlineNeverMutateConstants() {
617 test("function f(x){return x=1}f(undefined)",
618 "{var x$$inline_0=undefined;" +
619 "x$$inline_0=1}");
620 }
621
622 public void testInlineNeverOverrideNewValues() {
623 test("function f(a){return++a<++a}f(1)",
624 "{var a$$inline_0=1;" +
625 "++a$$inline_0<++a$$inline_0}");
626 }
627
628 public void testInlineMutableArgsReferencedOnce() {
629 test("function foo(x){return x;}foo([])", "[]");
630 }
631
632 public void testNoInlineMutableArgs1() {
633 allowBlockInlining = false;
634 testSame("function foo(x){return x+x} foo([])");
635 }
636
637 public void testNoInlineMutableArgs2() {
638 allowBlockInlining = false;
639 testSame("function foo(x){return x+x} foo(new Date)");
640 }
641
642 public void testNoInlineMutableArgs3() {
643 allowBlockInlining = false;
644 testSame("function foo(x){return x+x} foo(true&&new Date)");
645 }
646
647 public void testNoInlineMutableArgs4() {
648 allowBlockInlining = false;
649 testSame("function foo(x){return x+x} foo({})");
650 }
651
652 public void testInlineBlockMutableArgs1() {
653 test("function foo(x){x+x}foo([])",
654 "{var x$$inline_0=[];" +
655 "x$$inline_0+x$$inline_0}");
656 }
657
658 public void testInlineBlockMutableArgs2() {
659 test("function foo(x){x+x}foo(new Date)",
660 "{var x$$inline_0=new Date;" +
661 "x$$inline_0+x$$inline_0}");
662 }
663
664 public void testInlineBlockMutableArgs3() {
665 test("function foo(x){x+x}foo(true&&new Date)",
666 "{var x$$inline_0=true&&new Date;" +
667 "x$$inline_0+x$$inline_0}");
668 }
669
670 public void testInlineBlockMutableArgs4() {
671 test("function foo(x){x+x}foo({})",
672 "{var x$$inline_0={};" +
673 "x$$inline_0+x$$inline_0}");
674 }
675
676 public void testShadowVariables1() {
677 // The Normalize pass now guarantees that that globals are never shadowed
678 // by locals.
679
680 // "foo" is inlined here as its parameter "a" doesn't conflict.
681 // "bar" is assigned a new name.
682 test("var a=0;" +
683 "function foo(a){return 3+a}" +
684 "function bar(){var a=foo(4)}" +
685 "bar();",
686
687 "var a=0;" +
688 "{var a$$inline_0=3+4}");
689 }
690
691 public void testShadowVariables2() {
692 // "foo" is inlined here as its parameter "a" doesn't conflict.
693 // "bar" is inlined as its uses global "a", and does introduce any new
694 // globals.
695 test("var a=0;" +
696 "function foo(a){return 3+a}" +
697 "function bar(){a=foo(4)}" +
698 "bar()",
699
700 "var a=0;" +
701 "{a=3+4}");
702 }
703
704 public void testShadowVariables3() {
705 // "foo" is inlined into exported "_bar", aliasing foo's "a".
706 test("var a=0;" +
707 "function foo(){var a=2;return 3+a}" +
708 "function _bar(){a=foo()}",
709
710 "var a=0;" +
711 "function _bar(){{var a$$inline_0=2;" +
712 "a=3+a$$inline_0}}");
713 }
714
715 public void testShadowVariables4() {
716 // "foo" is inlined.
717 // block access to global "a".
718 test("var a=0;" +
719 "function foo(){return 3+a}" +
720 "function _bar(a){a=foo(4)+a}",
721
722 "var a=0;function _bar(a$$1){" +
723 "a$$1=" +
724 "3+a+a$$1}");
725 }
726
727 public void testShadowVariables5() {
728 // Can't yet inline multiple statements functions into expressions
729 // (though some are possible using the COMMA operator).
730 allowBlockInlining = false;
731 testSame("var a=0;" +
732 "function foo(){var a=4;return 3+a}" +
733 "function _bar(a){a=foo(4)+a}");
734 }
735
736 public void testShadowVariables6() {
737 test("var a=0;" +
738 "function foo(){var a=4;return 3+a}" +
739 "function _bar(a){a=foo(4)}",
740
741 "var a=0;function _bar(a$$2){{" +
742 "var a$$inline_0=4;" +
743 "a$$2=3+a$$inline_0}}");
744 }
745
746 public void testShadowVariables7() {
747 assumeMinimumCapture = false;
748 test("var a=3;" +
749 "function foo(){return a}" +
750 "(function(){var a=5;(function(){foo()})()})()",
751 "var a=3;" +
752 "{var a$$inline_0=5;{a}}"
753 );
754
755 assumeMinimumCapture = true;
756 test("var a=3;" +
757 "function foo(){return a}" +
758 "(function(){var a=5;(function(){foo()})()})()",
759 "var a=3;" +
760 "{var a$$inline_1=5;{a}}"
761 );
762 }
763
764 public void testShadowVariables8() {
765 // this should be inlined
766 test("var a=0;" +
767 "function foo(){return 3}" +
768 "function _bar(){var a=foo()}",
769
770 "var a=0;" +
771 "function _bar(){var a=3}");
772 }
773
774 public void testShadowVariables9() {
775 // this should be inlined too [even if the global is not declared]
776 test("function foo(){return 3}" +
777 "function _bar(){var a=foo()}",
778
779 "function _bar(){var a=3}");
780 }
781
782 public void testShadowVariables10() {
783 // callee var must be renamed.
784 test("var a;function foo(){return a}" +
785 "function _bar(){var a=foo()}",
786 "var a;function _bar(){var a$$1=a}");
787 }
788
789 public void testShadowVariables11() {
790 // The call has a local variable
791 // which collides with the function being inlined
792 test("var a=0;var b=1;" +
793 "function foo(){return a+a}" +
794 "function _bar(){var a=foo();alert(a)}",
795 "var a=0;var b=1;" +
796 "function _bar(){var a$$1=a+a;" +
797 "alert(a$$1)}"
798 );
799 }
800
801 public void testShadowVariables12() {
802 // 2 globals colliding
803 test("var a=0;var b=1;" +
804 "function foo(){return a+b}" +
805 "function _bar(){var a=foo(),b;alert(a)}",
806 "var a=0;var b=1;" +
807 "function _bar(){var a$$1=a+b," +
808 "b$$1;" +
809 "alert(a$$1)}");
810 }
811
812 public void testShadowVariables13() {
813 // The only change is to remove the collision
814 test("var a=0;var b=1;" +
815 "function foo(){return a+a}" +
816 "function _bar(){var c=foo();alert(c)}",
817
818 "var a=0;var b=1;" +
819 "function _bar(){var c=a+a;alert(c)}");
820 }
821
822 public void testShadowVariables14() {
823 // There is a collision even though it is not read.
824 test("var a=0;var b=1;" +
825 "function foo(){return a+b}" +
826 "function _bar(){var c=foo(),b;alert(c)}",
827 "var a=0;var b=1;" +
828 "function _bar(){var c=a+b," +
829 "b$$1;alert(c)}");
830 }
831
832 public void testShadowVariables15() {
833 // Both parent and child reference a global
834 test("var a=0;var b=1;" +
835 "function foo(){return a+a}" +
836 "function _bar(){var c=foo();alert(c+a)}",
837
838 "var a=0;var b=1;" +
839 "function _bar(){var c=a+a;alert(c+a)}");
840 }
841
842 public void testShadowVariables16() {
843 assumeMinimumCapture = false;
844 // Inline functions defined as a child of the CALL node.
845 test("var a=3;" +
846 "function foo(){return a}" +
847 "(function(){var a=5;(function(){foo()})()})()",
848 "var a=3;" +
849 "{var a$$inline_0=5;{a}}"
850 );
851
852 assumeMinimumCapture = true;
853 // Inline functions defined as a child of the CALL node.
854 test("var a=3;" +
855 "function foo(){return a}" +
856 "(function(){var a=5;(function(){foo()})()})()",
857 "var a=3;" +
858 "{var a$$inline_1=5;{a}}"
859 );
860
861 }
862
863 public void testShadowVariables17() {
864 test("var a=0;" +
865 "function bar(){return a+a}" +
866 "function foo(){return bar()}" +
867 "function _goo(){var a=2;var x=foo();}",
868
869 "var a=0;" +
870 "function _goo(){var a$$1=2;var x=a+a}");
871 }
872
873 public void testShadowVariables18() {
874 test("var a=0;" +
875 "function bar(){return a+a}" +
876 "function foo(){var a=3;return bar()}" +
877 "function _goo(){var a=2;var x=foo();}",
878
879 "var a=0;" +
880 "function _goo(){var a$$2=2;var x;" +
881 "{var a$$inline_0=3;x=a+a}}");
882 }
883
884 public void testCostBasedInlining1() {
885 testSame(
886 "function foo(a){return a}" +
887 "foo=new Function(\"return 1\");" +
888 "foo(1)");
889 }
890
891 public void testCostBasedInlining2() {
892 // Baseline complexity tests.
893 // Single call, function not removed.
894 test(
895 "function foo(a){return a}" +
896 "var b=foo;" +
897 "function _t1(){return foo(1)}",
898
899 "function foo(a){return a}" +
900 "var b=foo;" +
901 "function _t1(){return 1}");
902 }
903
904 public void testCostBasedInlining3() {
905 // Two calls, function not removed.
906 test(
907 "function foo(a,b){return a+b}" +
908 "var b=foo;" +
909 "function _t1(){return foo(1,2)}" +
910 "function _t2(){return foo(2,3)}",
911
912 "function foo(a,b){return a+b}" +
913 "var b=foo;" +
914 "function _t1(){return 1+2}" +
915 "function _t2(){return 2+3}");
916 }
917
918 public void testCostBasedInlining4() {
919 // Two calls, function not removed.
920 // Here there isn't enough savings to justify inlining.
921 testSame(
922 "function foo(a,b){return a+b+a+b}" +
923 "var b=foo;" +
924 "function _t1(){return foo(1,2)}" +
925 "function _t2(){return foo(2,3)}");
926 }
927
928 public void testCostBasedInlining5() {
929 // Here there is enough savings to justify inlining.
930 test(
931 "function foo(a,b){return a+b+a+b}" +
932 "function _t1(){return foo(1,2)}" +
933 "function _t2(){return foo(2,3)}",
934
935 "function _t1(){return 1+2+1+2}" +
936 "function _t2(){return 2+3+2+3}");
937 }
938
939 public void testCostBasedInlining6() {
940 // Here we have a threshold test.
941 // Do inline here:
942 test(
943 "function foo(a,b){return a+b+a+b+a+b+a+b+4+5+6+7+8+9+1+2+3+4+5}" +
944 "function _t1(){return foo(1,2)}" +
945 "function _t2(){return foo(2,3)}",
946
947 "function _t1(){return 1+2+1+2+1+2+1+2+4+5+6+7+8+9+1+2+3+4+5}" +
948 "function _t2(){return 2+3+2+3+2+3+2+3+4+5+6+7+8+9+1+2+3+4+5}");
949 }
950
951 public void testCostBasedInlining7() {
952 // Don't inline here (not enough savings):
953 testSame(
954 "function foo(a,b){" +
955 " return a+b+a+b+a+b+a+b+4+5+6+7+8+9+1+2+3+4+5+6}" +
956 "function _t1(){return foo(1,2)}" +
957 "function _t2(){return foo(2,3)}");
958 }
959
960 public void testCostBasedInlining8() {
961 // Verify multiple references in the same statement:
962 // Here "f" is not known to be removable, as it is a used as parameter
963 // and is not known to be side-effect free. The first call to f() can
964 // not be inlined on the first pass (as the call to f() as a parameter
965 // prevents this). However, the call to f() would be inlinable, if it
966 // is small enough to be inlined without removing the function declaration.
967 // but it is not in this first test.
968 allowBlockInlining = false;
969 testSame("function f(a){return 1 + a + a;}" +
970 "var a = f(f(1));");
971 }
972
973 public void testCostBasedInlining9() {
974 // Here both direct and block inlining is used. The call to f as a
975 // parameter is inlined directly, which the call to f with f as a parameter
976 // is inlined using block inlining.
977 test("function f(a){return 1 + a + a;}" +
978 "var a = f(f(1));",
979 "var a;" +
980 "{var a$$inline_0=1+1+1;" +
981 "a=1+a$$inline_0+a$$inline_0}");
982 }
983
984 public void testCostBasedInlining10() {
985 // But it is small enough here, and on the second iteration, the remaining
986 // call to f() is inlined, as there is no longer a possible side-effect-ing
987 // parameter.
988 allowBlockInlining = false;
989 test("function f(a){return a + a;}" +
990 "var a = f(f(1));",
991 "var a= 1+1+(1+1);");
992 }
993
994 public void testCostBasedInlining11() {
995 // With block inlining
996 test("function f(a){return a + a;}" +
997 "var a = f(f(1))",
998 "var a;" +
999 "{var a$$inline_0=1+1;" +
1000 "a=a$$inline_0+a$$inline_0}");
1001 }
1002
1003 public void testCostBasedInlining12() {
1004 test("function f(a){return 1 + a + a;}" +
1005 "var a = f(1) + f(2);",
1006
1007 "var a=1+1+1+(1+2+2)");
1008 }
1009
1010 public void testCostBasedInliningComplex1() {
1011 testSame(
1012 "function foo(a){a()}" +
1013 "foo=new Function(\"return 1\");" +
1014 "foo(1)");
1015 }
1016
1017 public void testCostBasedInliningComplex2() {
1018 // Baseline complexity tests.
1019 // Single call, function not removed.
1020 test(
1021 "function foo(a){a()}" +
1022 "var b=foo;" +
1023 "function _t1(){foo(x)}",
1024
1025 "function foo(a){a()}" +
1026 "var b=foo;" +
1027 "function _t1(){{x()}}");
1028 }
1029
1030 public void testCostBasedInliningComplex3() {
1031 // Two calls, function not removed.
1032 test(
1033 "function foo(a,b){a+b}" +
1034 "var b=foo;" +
1035 "function _t1(){foo(1,2)}" +
1036 "function _t2(){foo(2,3)}",
1037
1038 "function foo(a,b){a+b}" +
1039 "var b=foo;" +
1040 "function _t1(){{1+2}}" +
1041 "function _t2(){{2+3}}");
1042 }
1043
1044 public void testCostBasedInliningComplex4() {
1045 // Two calls, function not removed.
1046 // Here there isn't enough savings to justify inlining.
1047 testSame(
1048 "function foo(a,b){a+b+a+b}" +
1049 "var b=foo;" +
1050 "function _t1(){foo(1,2)}" +
1051 "function _t2(){foo(2,3)}");
1052 }
1053
1054 public void testCostBasedInliningComplex5() {
1055 // Here there is enough savings to justify inlining.
1056 test(
1057 "function foo(a,b){a+b+a+b}" +
1058 "function _t1(){foo(1,2)}" +
1059 "function _t2(){foo(2,3)}",
1060
1061 "function _t1(){{1+2+1+2}}" +
1062 "function _t2(){{2+3+2+3}}");
1063 }
1064
1065 public void testCostBasedInliningComplex6() {
1066 // Here we have a threshold test.
1067 // Do inline here:
1068 test(
1069 "function foo(a,b){a+b+a+b+a+b+a+b+4+5+6+7+8+9+1}" +
1070 "function _t1(){foo(1,2)}" +
1071 "function _t2(){foo(2,3)}",
1072
1073 "function _t1(){{1+2+1+2+1+2+1+2+4+5+6+7+8+9+1}}" +
1074 "function _t2(){{2+3+2+3+2+3+2+3+4+5+6+7+8+9+1}}");
1075 }
1076
1077 public void testCostBasedInliningComplex7() {
1078 // Don't inline here (not enough savings):
1079 testSame(
1080 "function foo(a,b){a+b+a+b+a+b+a+b+4+5+6+7+8+9+1+2}" +
1081 "function _t1(){foo(1,2)}" +
1082 "function _t2(){foo(2,3)}");
1083 }
1084
1085 public void testCostBasedInliningComplex8() {
1086 // Verify multiple references in the same statement.
1087 testSame("function _f(a){1+a+a}" +
1088 "a=_f(1)+_f(1)");
1089 }
1090
1091 public void testCostBasedInliningComplex9() {
1092 test("function f(a){1 + a + a;}" +
1093 "f(1);f(2);",
1094 "{1+1+1}{1+2+2}");
1095 }
1096
1097 public void testDoubleInlining1() {
1098 allowBlockInlining = false;
1099 test("var foo = function(a) { return getWindow(a); };" +
1100 "var bar = function(b) { return b; };" +
1101 "foo(bar(x));",
1102 "getWindow(x)");
1103 }
1104
1105 public void testDoubleInlining2() {
1106 test("var foo = function(a) { return getWindow(a); };" +
1107 "var bar = function(b) { return b; };" +
1108 "foo(bar(x));",
1109 "{getWindow(x)}");
1110 }
1111
1112 public void testNoInlineOfNonGlobalFunction1() {
1113 test("var g;function _f(){function g(){return 0}}" +
1114 "function _h(){return g()}",
1115 "var g;function _f(){}" +
1116 "function _h(){return g()}");
1117 }
1118
1119 public void testNoInlineOfNonGlobalFunction2() {
1120 test("var g;function _f(){var g=function(){return 0}}" +
1121 "function _h(){return g()}",
1122 "var g;function _f(){}" +
1123 "function _h(){return g()}");
1124 }
1125
1126 public void testNoInlineOfNonGlobalFunction3() {
1127 test("var g;function _f(){var g=function(){return 0}}" +
1128 "function _h(){return g()}",
1129 "var g;function _f(){}" +
1130 "function _h(){return g()}");
1131 }
1132
1133 public void testNoInlineOfNonGlobalFunction4() {
1134 test("var g;function _f(){function g(){return 0}}" +
1135 "function _h(){return g()}",
1136 "var g;function _f(){}" +
1137 "function _h(){return g()}");
1138
1139 }
1140
1141 public void testNoInlineMaskedFunction() {
1142 // Normalization makes this test of marginal value.
1143 // The unreferenced function is removed.
1144 test("var g=function(){return 0};" +
1145 "function _f(g){return g()}",
1146 "function _f(g$$1){return g$$1()}");
1147 }
1148
1149 public void testNoInlineNonFunction() {
1150 testSame("var g=3;function _f(){return g()}");
1151 }
1152
1153 public void testInlineCall() {
1154 test("function f(g) { return g.h(); } f('x');",
1155 "\"x\".h()");
1156 }
1157
1158 public void testInlineFunctionWithArgsMismatch1() {
1159 test("function f(g) { return g; } f();",
1160 "void 0");
1161 }
1162
1163 public void testInlineFunctionWithArgsMismatch2() {
1164 test("function f() { return 0; } f(1);",
1165 "0");
1166 }
1167
1168 public void testInlineFunctionWithArgsMismatch3() {
1169 test("function f(one, two, three) { return one + two + three; } f(1);",
1170 "1+void 0+void 0");
1171 }
1172
1173 public void testInlineFunctionWithArgsMismatch4() {
1174 test("function f(one, two, three) { return one + two + three; }" +
1175 "f(1,2,3,4,5);",
1176 "1+2+3");
1177 }
1178
1179 public void testArgumentsWithSideEffectsNeverInlined1() {
1180 allowBlockInlining = false;
1181 testSame("function f(){return 0} f(new goo());");
1182 }
1183
1184 public void testArgumentsWithSideEffectsNeverInlined2() {
1185 allowBlockInlining = false;
1186 testSame("function f(g,h){return h+g}f(g(),h());");
1187 }
1188
1189 public void testOneSideEffectCallDoesNotRuinOthers() {
1190 allowBlockInlining = false;
1191 test("function f(){return 0}f(new goo());f()",
1192 "function f(){return 0}f(new goo());0");
1193 }
1194
1195 public void testComplexInlineNoResultNoParamCall1() {
1196 test("function f(){a()}f()",
1197 "{a()}");
1198 }
1199
1200 public void testComplexInlineNoResultNoParamCall2() {
1201 test("function f(){if (true){return;}else;} f();",
1202 "{JSCompiler_inline_label_f_0:{" +
1203 "if(true)break JSCompiler_inline_label_f_0;else;}}");
1204 }
1205
1206 public void testComplexInlineNoResultNoParamCall3() {
1207 // We now allow vars in the global space.
1208 // Don't inline into vars into global scope.
1209 // testSame("function f(){a();b();var z=1+1}f()");
1210
1211 // But do inline into functions
1212 test("function f(){a();b();var z=1+1}function _foo(){f()}",
1213 "function _foo(){{a();b();var z$$inline_0=1+1}}");
1214
1215 }
1216
1217 public void testComplexInlineNoResultWithParamCall1() {
1218 test("function f(x){a(x)}f(1)",
1219 "{a(1)}");
1220 }
1221
1222 public void testComplexInlineNoResultWithParamCall2() {
1223 test("function f(x,y){a(x)}var b=1;f(1,b)",
1224 "var b=1;{a(1)}");
1225 }
1226
1227 public void testComplexInlineNoResultWithParamCall3() {
1228 test("function f(x,y){if (x) y(); return true;}var b=1;f(1,b)",
1229 "var b=1;{if(1)b();true}");
1230 }
1231
1232 public void testComplexInline1() {
1233 test("function f(){if (true){return;}else;} z=f();",
1234 "{JSCompiler_inline_label_f_0:" +
1235 "{if(true){z=void 0;" +
1236 "break JSCompiler_inline_label_f_0}else;z=void 0}}");
1237 }
1238
1239 public void testComplexInline2() {
1240 test("function f(){if (true){return;}else return;} z=f();",
1241 "{JSCompiler_inline_label_f_0:{if(true){z=void 0;" +
1242 "break JSCompiler_inline_label_f_0}else{z=void 0;" +
1243 "break JSCompiler_inline_label_f_0}z=void 0}}");
1244 }
1245
1246 public void testComplexInline3() {
1247 test("function f(){if (true){return 1;}else return 0;} z=f();",
1248 "{JSCompiler_inline_label_f_0:{if(true){z=1;" +
1249 "break JSCompiler_inline_label_f_0}else{z=0;" +
1250 "break JSCompiler_inline_label_f_0}z=void 0}}");
1251 }
1252
1253 public void testComplexInline4() {
1254 test("function f(x){a(x)} z = f(1)",
1255 "{a(1);z=void 0}");
1256 }
1257
1258 public void testComplexInline5() {
1259 test("function f(x,y){a(x)}var b=1;z=f(1,b)",
1260 "var b=1;{a(1);z=void 0}");
1261 }
1262
1263 public void testComplexInline6() {
1264 test("function f(x,y){if (x) y(); return true;}var b=1;z=f(1,b)",
1265 "var b=1;{if(1)b();z=true}");
1266 }
1267
1268 public void testComplexInline7() {
1269 test("function f(x,y){if (x) return y(); else return true;}" +
1270 "var b=1;z=f(1,b)",
1271 "var b=1;{JSCompiler_inline_label_f_2:{if(1){z=b();" +
1272 "break JSCompiler_inline_label_f_2}else{z=true;" +
1273 "break JSCompiler_inline_label_f_2}z=void 0}}");
1274 }
1275
1276 public void testComplexInline8() {
1277 test("function f(x){a(x)}var z=f(1)",
1278 "var z;{a(1);z=void 0}");
1279 }
1280
1281 public void testComplexInlineVars1() {
1282 test("function f(){if (true){return;}else;}var z=f();",
1283 "var z;{JSCompiler_inline_label_f_0:{" +
1284 "if(true){z=void 0;break JSCompiler_inline_label_f_0}else;z=void 0}}");
1285 }
1286
1287 public void testComplexInlineVars2() {
1288 test("function f(){if (true){return;}else return;}var z=f();",
1289 "var z;{JSCompiler_inline_label_f_0:{" +
1290 "if(true){z=void 0;break JSCompiler_inline_label_f_0" +
1291 "}else{" +
1292 "z=void 0;break JSCompiler_inline_label_f_0}z=void 0}}");
1293 }
1294
1295 public void testComplexInlineVars3() {
1296 test("function f(){if (true){return 1;}else return 0;}var z=f();",
1297 "var z;{JSCompiler_inline_label_f_0:{if(true){" +
1298 "z=1;break JSCompiler_inline_label_f_0" +
1299 "}else{" +
1300 "z=0;break JSCompiler_inline_label_f_0}z=void 0}}");
1301 }
1302
1303 public void testComplexInlineVars4() {
1304 test("function f(x){a(x)}var z = f(1)",
1305 "var z;{a(1);z=void 0}");
1306 }
1307
1308 public void testComplexInlineVars5() {
1309 test("function f(x,y){a(x)}var b=1;var z=f(1,b)",
1310 "var b=1;var z;{a(1);z=void 0}");
1311 }
1312
1313 public void testComplexInlineVars6() {
1314 test("function f(x,y){if (x) y(); return true;}var b=1;var z=f(1,b)",
1315 "var b=1;var z;{if(1)b();z=true}");
1316 }
1317
1318 public void testComplexInlineVars7() {
1319 test("function f(x,y){if (x) return y(); else return true;}" +
1320 "var b=1;var z=f(1,b)",
1321 "var b=1;var z;" +
1322 "{JSCompiler_inline_label_f_2:{if(1){z=b();" +
1323 "break JSCompiler_inline_label_f_2" +
1324 "}else{" +
1325 "z=true;break JSCompiler_inline_label_f_2}z=void 0}}");
1326 }
1327
1328 public void testComplexInlineVars8() {
1329 test("function f(x){a(x)}var x;var z=f(1)",
1330 "var x;var z;{a(1);z=void 0}");
1331 }
1332
1333 public void testComplexInlineVars9() {
1334 test("function f(x){a(x)}var x;var z=f(1);var y",
1335 "var x;var z;{a(1);z=void 0}var y");
1336 }
1337
1338 public void testComplexInlineVars10() {
1339 test("function f(x){a(x)}var x=blah();var z=f(1);var y=blah();",
1340 "var x=blah();var z;{a(1);z=void 0}var y=blah()");
1341 }
1342
1343 public void testComplexInlineVars11() {
1344 test("function f(x){a(x)}var x=blah();var z=f(1);var y;",
1345 "var x=blah();var z;{a(1);z=void 0}var y");
1346 }
1347
1348 public void testComplexInlineVars12() {
1349 test("function f(x){a(x)}var x;var z=f(1);var y=blah();",
1350 "var x;var z;{a(1);z=void 0}var y=blah()");
1351 }
1352
1353 public void testComplexInlineInExpresssions1() {
1354 test("function f(){a()}var z=f()",
1355 "var z;{a();z=void 0}");
1356 }
1357
1358 public void testComplexInlineInExpresssions2() {
1359 test("function f(){a()}c=z=f()",
1360 "var JSCompiler_inline_result$$0;" +
1361 "{a();JSCompiler_inline_result$$0=void 0;}" +
1362 "c=z=JSCompiler_inline_result$$0");
1363 }
1364
1365 public void testComplexInlineInExpresssions3() {
1366 test("function f(){a()}c=z=f()",
1367 "var JSCompiler_inline_result$$0;" +
1368 "{a();JSCompiler_inline_result$$0=void 0;}" +
1369 "c=z=JSCompiler_inline_result$$0");
1370 }
1371
1372 public void testComplexInlineInExpresssions4() {
1373 test("function f(){a()}if(z=f());",
1374 "var JSCompiler_inline_result$$0;" +
1375 "{a();JSCompiler_inline_result$$0=void 0;}" +
1376 "if(z=JSCompiler_inline_result$$0);");
1377 }
1378
1379 public void testComplexInlineInExpresssions5() {
1380 test("function f(){a()}if(z.y=f());",
1381 "var JSCompiler_temp_const$$0=z;" +
1382 "var JSCompiler_inline_result$$1;" +
1383 "{a();JSCompiler_inline_result$$1=void 0;}" +
1384 "if(JSCompiler_temp_const$$0.y=JSCompiler_inline_result$$1);");
1385 }
1386
1387 public void testComplexNoInline1() {
1388 testSame("function f(){a()}while(z=f())continue");
1389 }
1390
1391 public void testComplexNoInline2() {
1392 testSame("function f(){a()}do;while(z=f())");
1393 }
1394
1395 public void testComplexSample() {
1396 String result = "" +
1397 "{{" +
1398 "var styleSheet$$inline_2=null;" +
1399 "if(goog$userAgent$IE)" +
1400 "styleSheet$$inline_2=0;" +
1401 "else " +
1402 "var head$$inline_3=0;" +
1403 "{" +
1404 "var element$$inline_4=" +
1405 "styleSheet$$inline_2;" +
1406 "var stylesString$$inline_5=a;" +
1407 "if(goog$userAgent$IE)" +
1408 "element$$inline_4.cssText=" +
1409 "stylesString$$inline_5;" +
1410 "else " +
1411 "{" +
1412 "var propToSet$$inline_6=" +
1413 "\"innerText\";" +
1414 "element$$inline_4[" +
1415 "propToSet$$inline_6]=" +
1416 "stylesString$$inline_5" +
1417 "}" +
1418 "}" +
1419 "styleSheet$$inline_2" +
1420 "}}";
1421
1422 test("var foo = function(stylesString, opt_element) { " +
1423 "var styleSheet = null;" +
1424 "if (goog$userAgent$IE)" +
1425 "styleSheet = 0;" +
1426 "else " +
1427 "var head = 0;" +
1428 "" +
1429 "goo$zoo(styleSheet, stylesString);" +
1430 "return styleSheet;" +
1431 " };\n " +
1432
1433 "var goo$zoo = function(element, stylesString) {" +
1434 "if (goog$userAgent$IE)" +
1435 "element.cssText = stylesString;" +
1436 "else {" +
1437 "var propToSet = 'innerText';" +
1438 "element[propToSet] = stylesString;" +
1439 "}" +
1440 "};" +
1441 "(function(){foo(a,b);})();",
1442 result);
1443 }
1444
1445 public void testComplexSampleNoInline() {
1446 testSame(
1447 "foo=function(stylesString,opt_element){" +
1448 "var styleSheet=null;" +
1449 "if(goog$userAgent$IE)" +
1450 "styleSheet=0;" +
1451 "else " +
1452 "var head=0;" +
1453 "" +
1454 "goo$zoo(styleSheet,stylesString);" +
1455 "return styleSheet" +
1456 "};" +
1457 "goo$zoo=function(element,stylesString){" +
1458 "if(goog$userAgent$IE)" +
1459 "element.cssText=stylesString;" +
1460 "else{" +
1461 "var propToSet=goog$userAgent$WEBKIT?\"innerText\":\"innerHTML\";" +
1462 "element[propToSet]=stylesString" +
1463 "}" +
1464 "}");
1465 }
1466
1467 // Test redefinition of parameter name.
1468 public void testComplexNoVarSub() {
1469 test(
1470 "function foo(x){" +
1471 "var x;" +
1472 "y=x" +
1473 "}" +
1474 "foo(1)",
1475
1476 "{y=1}"
1477 );
1478 }
1479
1480 public void testComplexFunctionWithFunctionDefinition1() {
1481 test("function f(){call(function(){return})}f()",
1482 "{call(function(){return})}");
1483 }
1484
1485 public void testComplexFunctionWithFunctionDefinition2() {
1486 assumeMinimumCapture = false;
1487
1488 // Don't inline if local names might be captured.
1489 testSame("function f(a){call(function(){return})}f()");
1490
1491 assumeMinimumCapture = true;
1492
1493 test("(function(){" +
1494 "var f = function(a){call(function(){return a})};f()})()",
1495 "{{var a$$inline_0=void 0;call(function(){return a$$inline_0})}}");
1496 }
1497
1498 public void testComplexFunctionWithFunctionDefinition2a() {
1499 assumeMinimumCapture = false;
1500
1501 // Don't inline if local names might be captured.
1502 testSame("(function(){" +
1503 "var f = function(a){call(function(){return a})};f()})()");
1504
1505 assumeMinimumCapture = true;
1506
1507 test("(function(){" +
1508 "var f = function(a){call(function(){return a})};f()})()",
1509 "{{var a$$inline_0=void 0;call(function(){return a$$inline_0})}}");
1510 }
1511
1512 public void testComplexFunctionWithFunctionDefinition3() {
1513 assumeMinimumCapture = false;
1514
1515 // Don't inline if local names might need to be captured.
1516 testSame("function f(){var a; call(function(){return a})}f()");
1517
1518 assumeMinimumCapture = true;
1519
1520 test("function f(){var a; call(function(){return a})}f()",
1521 "{var a$$inline_0;call(function(){return a$$inline_0})}");
1522
1523 }
1524
1525 public void testDecomposePlusEquals() {
1526 test("function f(){a=1;return 1} var x = 1; x += f()",
1527 "var x = 1;" +
1528 "var JSCompiler_temp_const$$0 = x;" +
1529 "var JSCompiler_inline_result$$1;" +
1530 "{a=1;" +
1531 " JSCompiler_inline_result$$1=1}" +
1532 "x = JSCompiler_temp_const$$0 + JSCompiler_inline_result$$1;");
1533 }
1534
1535 public void testDecomposeFunctionExpressionInCall() {
1536 test(
1537 "(function(map){descriptions_=map})(\n" +
1538 "function(){\n" +
1539 "var ret={};\n" +
1540 "ret[ONE]='a';\n" +
1541 "ret[TWO]='b';\n" +
1542 "return ret\n" +
1543 "}()\n" +
1544 ");",
1545 "var JSCompiler_inline_result$$0;" +
1546 "{" +
1547 "var ret$$inline_1={};\n" +
1548 "ret$$inline_1[ONE]='a';\n" +
1549 "ret$$inline_1[TWO]='b';\n" +
1550 "JSCompiler_inline_result$$0 = ret$$inline_1;\n" +
1551 "}" +
1552 "{" +
1553 "descriptions_=JSCompiler_inline_result$$0;" +
1554 "}"
1555 );
1556 }
1557
1558 public void testInlineConstructor1() {
1559 test("function f() {} function _g() {f.call(this)}",
1560 "function _g() {void 0}");
1561 }
1562
1563 public void testInlineConstructor2() {
1564 test("function f() {} f.prototype.a = 0; function _g() {f.call(this)}",
1565 "function f() {} f.prototype.a = 0; function _g() {void 0}");
1566 }
1567
1568 public void testInlineConstructor3() {
1569 test("function f() {x.call(this)} f.prototype.a = 0;" +
1570 "function _g() {f.call(this)}",
1571 "function f() {x.call(this)} f.prototype.a = 0;" +
1572 "function _g() {{x.call(this)}}");
1573 }
1574
1575 public void testInlineConstructor4() {
1576 test("function f() {x.call(this)} f.prototype.a = 0;" +
1577 "function _g() {var t = f.call(this)}",
1578 "function f() {x.call(this)} f.prototype.a = 0;" +
1579 "function _g() {var t; {x.call(this); t = void 0}}");
1580 }
1581
1582 public void testFunctionExpressionInlining1() {
1583 test("(function(){})()",
1584 "void 0");
1585 }
1586
1587 public void testFunctionExpressionInlining2() {
1588 test("(function(){foo()})()",
1589 "{foo()}");
1590 }
1591
1592 public void testFunctionExpressionInlining3() {
1593 test("var a = (function(){return foo()})()",
1594 "var a = foo()");
1595 }
1596
1597 public void testFunctionExpressionInlining4() {
1598 test("var a; a = 1 + (function(){return foo()})()",
1599 "var a; a = 1 + foo()");
1600 }
1601
1602 public void testFunctionExpressionCallInlining1() {
1603 test("(function(){}).call(this)",
1604 "void 0");
1605 }
1606
1607 public void testFunctionExpressionCallInlining2() {
1608 test("(function(){foo(this)}).call(this)",
1609 "{foo(this)}");
1610 }
1611
1612 public void testFunctionExpressionCallInlining3() {
1613 test("var a = (function(){return foo(this)}).call(this)",
1614 "var a = foo(this)");
1615 }
1616
1617 public void testFunctionExpressionCallInlining4() {
1618 test("var a; a = 1 + (function(){return foo(this)}).call(this)",
1619 "var a; a = 1 + foo(this)");
1620 }
1621
1622 public void testFunctionExpressionCallInlining5() {
1623 test("a:(function(){return foo()})()",
1624 "a:foo()");
1625 }
1626
1627 public void testFunctionExpressionCallInlining6() {
1628 test("a:(function(){return foo()}).call(this)",
1629 "a:foo()");
1630 }
1631
1632 public void testFunctionExpressionCallInlining7() {
1633 test("a:(function(){})()",
1634 "a:void 0");
1635 }
1636
1637 public void testFunctionExpressionCallInlining8() {
1638 test("a:(function(){}).call(this)",
1639 "a:void 0");
1640 }
1641
1642 public void testFunctionExpressionCallInlining9() {
1643 // ... with unused recursive name.
1644 test("(function foo(){})()",
1645 "void 0");
1646 }
1647
1648 public void testFunctionExpressionCallInlining10() {
1649 // ... with unused recursive name.
1650 test("(function foo(){}).call(this)",
1651 "void 0");
1652 }
1653
1654 public void testFunctionExpressionCallInlining11a() {
1655 // Inline functions that return inner functions.
1656 test("((function(){return function(){foo()}})())();", "{foo()}");
1657 }
1658
1659 public void testFunctionExpressionCallInlining11b() {
1660 assumeMinimumCapture = false;
1661 // Can't inline functions that return inner functions and have local names.
1662 testSame("((function(){var a; return function(){foo()}})())();");
1663
1664 assumeMinimumCapture = true;
1665 test(
1666 "((function(){var a; return function(){foo()}})())();",
1667
1668 "var JSCompiler_inline_result$$0;" +
1669 "{var a$$inline_1;" +
1670 "JSCompiler_inline_result$$0=function(){foo()};}" +
1671 "JSCompiler_inline_result$$0()");
1672
1673 }
1674
1675 public void testFunctionExpressionCallInlining11c() {
1676 // TODO(johnlenz): Can inline, not temps needed.
1677 assumeMinimumCapture = false;
1678 testSame("function _x() {" +
1679 " ((function(){return function(){foo()}})())();" +
1680 "}");
1681
1682 assumeMinimumCapture = true;
1683 test(
1684 "function _x() {" +
1685 " ((function(){return function(){foo()}})())();" +
1686 "}",
1687 "function _x() {" +
1688 " {foo()}" +
1689 "}");
1690 }
1691
1692 public void testFunctionExpressionCallInlining11d() {
1693 // TODO(johnlenz): Can inline into a function containing eval, if
1694 // no names are introduced.
1695 assumeMinimumCapture = false;
1696 testSame("function _x() {" +
1697 " eval();" +
1698 " ((function(){return function(){foo()}})())();" +
1699 "}");
1700
1701 assumeMinimumCapture = true;
1702 test(
1703 "function _x() {" +
1704 " eval();" +
1705 " ((function(){return function(){foo()}})())();" +
1706 "}",
1707 "function _x() {" +
1708 " eval();" +
1709 " {foo()}" +
1710 "}");
1711
1712 }
1713
1714 public void testFunctionExpressionCallInlining11e() {
1715 // No, don't inline into a function containing eval,
1716 // if temps are introduced.
1717 assumeMinimumCapture = false;
1718 testSame("function _x() {" +
1719 " eval();" +
1720 " ((function(a){return function(){foo()}})())();" +
1721 "}");
1722
1723 assumeMinimumCapture = true;
1724 test("function _x() {" +
1725 " eval();" +
1726 " ((function(a){return function(){foo()}})())();" +
1727 "}",
1728 "function _x() {" +
1729 " eval();" +
1730 " {foo();}" +
1731 "}");
1732 }
1733
1734 public void testFunctionExpressionCallInlining12() {
1735 // Can't inline functions that recurse.
1736 testSame("(function foo(){foo()})()");
1737 }
1738
1739 public void testFunctionExpressionOmega() {
1740 // ... with unused recursive name.
1741 test("(function (f){f(f)})(function(f){f(f)})",
1742 "{var f$$inline_0=function(f$$1){f$$1(f$$1)};" +
1743 "{{f$$inline_0(f$$inline_0)}}}");
1744 }
1745
1746 public void testLocalFunctionInlining1() {
1747 test("function _f(){ function g() {} g() }",
1748 "function _f(){ void 0 }");
1749 }
1750
1751 public void testLocalFunctionInlining2() {
1752 test("function _f(){ function g() {foo(); bar();} g() }",
1753 "function _f(){ {foo(); bar();} }");
1754 }
1755
1756 public void testLocalFunctionInlining3() {
1757 test("function _f(){ function g() {foo(); bar();} g() }",
1758 "function _f(){ {foo(); bar();} }");
1759 }
1760
1761 public void testLocalFunctionInlining4() {
1762 test("function _f(){ function g() {return 1} return g() }",
1763 "function _f(){ return 1 }");
1764 }
1765
1766 public void testLocalFunctionInlining5() {
1767 testSame("function _f(){ function g() {this;} g() }");
1768 }
1769
1770 public void testLocalFunctionInlining6() {
1771 testSame("function _f(){ function g() {this;} return g; }");
1772 }
1773
1774 public void testLocalFunctionInliningOnly1() {
1775 this.allowGlobalFunctionInlining = true;
1776 test("function f(){} f()", "void 0;");
1777 this.allowGlobalFunctionInlining = false;
1778 testSame("function f(){} f()");
1779 }
1780
1781 public void testLocalFunctionInliningOnly2() {
1782 this.allowGlobalFunctionInlining = false;
1783 testSame("function f(){} f()");
1784
1785 test("function f(){ function g() {return 1} return g() }; f();",
1786 "function f(){ return 1 }; f();");
1787 }
1788
1789 public void testLocalFunctionInliningOnly3() {
1790 this.allowGlobalFunctionInlining = false;
1791 testSame("function f(){} f()");
1792
1793 test("(function(){ function g() {return 1} return g() })();",
1794 "(function(){ return 1 })();");
1795 }
1796
1797 public void testLocalFunctionInliningOnly4() {
1798 this.allowGlobalFunctionInlining = false;
1799 testSame("function f(){} f()");
1800
1801 test("(function(){ return (function() {return 1})() })();",
1802 "(function(){ return 1 })();");
1803 }
1804
1805 public void testInlineWithThis1() {
1806 assumeStrictThis = false;
1807 // If no "this" is provided it might need to be coerced to the global
1808 // "this".
1809 testSame("function f(){} f.call();");
1810 testSame("function f(){this} f.call();");
1811
1812 assumeStrictThis = true;
1813 // In strict mode, "this" is never coerced so we can use the provided value.
1814 test("function f(){} f.call();", "{}");
1815 test("function f(){this} f.call();",
1816 "{void 0;}");
1817 }
1818
1819 public void testInlineWithThis2() {
1820 // "this" can always be replaced with "this"
1821 assumeStrictThis = false;
1822 test("function f(){} f.call(this);", "void 0");
1823
1824 assumeStrictThis = true;
1825 test("function f(){} f.call(this);", "void 0");
1826 }
1827
1828 public void testInlineWithThis3() {
1829 assumeStrictThis = false;
1830 // If no "this" is provided it might need to be coerced to the global
1831 // "this".
1832 testSame("function f(){} f.call([]);");
1833
1834 assumeStrictThis = true;
1835 // In strict mode, "this" is never coerced so we can use the provided value.
1836 test("function f(){} f.call([]);", "{}");
1837 }
1838
1839 public void testInlineWithThis4() {
1840 assumeStrictThis = false;
1841 // If no "this" is provided it might need to be coerced to the global
1842 // "this".
1843 testSame("function f(){} f.call(new g);");
1844
1845 assumeStrictThis = true;
1846 // In strict mode, "this" is never coerced so we can use the provided value.
1847 test("function f(){} f.call(new g);",
1848 "{var JSCompiler_inline_this_0=new g}");
1849 }
1850
1851 public void testInlineWithThis5() {
1852 assumeStrictThis = false;
1853 // If no "this" is provided it might need to be coerced to the global
1854 // "this".
1855 testSame("function f(){} f.call(g());");
1856
1857 assumeStrictThis = true;
1858 // In strict mode, "this" is never coerced so we can use the provided value.
1859 test("function f(){} f.call(g());",
1860 "{var JSCompiler_inline_this_0=g()}");
1861 }
1862
1863 public void testInlineWithThis6() {
1864 assumeStrictThis = false;
1865 // If no "this" is provided it might need to be coerced to the global
1866 // "this".
1867 testSame("function f(){this} f.call(new g);");
1868
1869 assumeStrictThis = true;
1870 // In strict mode, "this" is never coerced so we can use the provided value.
1871 test("function f(){this} f.call(new g);",
1872 "{var JSCompiler_inline_this_0=new g;JSCompiler_inline_this_0}");
1873 }
1874
1875 public void testInlineWithThis7() {
1876 assumeStrictThis = true;
1877 // In strict mode, "this" is never coerced so we can use the provided value.
1878 test("function f(a){a=1;this} f.call();",
1879 "{var a$$inline_0=void 0; a$$inline_0=1; void 0;}");
1880 test("function f(a){a=1;this} f.call(x, x);",
1881 "{var a$$inline_0=x; a$$inline_0=1; x;}");
1882 }
1883
1884 // http://en.wikipedia.org/wiki/Fixed_point_combinator#Y_combinator
1885 public void testFunctionExpressionYCombinator() {
1886 assumeMinimumCapture = false;
1887 testSame(
1888 "var factorial = ((function(M) {\n" +
1889 " return ((function(f) {\n" +
1890 " return M(function(arg) {\n" +
1891 " return (f(f))(arg);\n" +
1892 " })\n" +
1893 " })\n" +
1894 " (function(f) {\n" +
1895 " return M(function(arg) {\n" +
1896 " return (f(f))(arg);\n" +
1897 " })\n" +
1898 " }));\n" +
1899 " })\n" +
1900 " (function(f) {\n" +
1901 " return function(n) {\n" +
1902 " if (n === 0)\n" +
1903 " return 1;\n" +
1904 " else\n" +
1905 " return n * f(n - 1);\n" +
1906 " };\n" +
1907 " }));\n" +
1908 "\n" +
1909 "factorial(5)\n");
1910
1911 assumeMinimumCapture = true;
1912 test(
1913 "var factorial = ((function(M) {\n" +
1914 " return ((function(f) {\n" +
1915 " return M(function(arg) {\n" +
1916 " return (f(f))(arg);\n" +
1917 " })\n" +
1918 " })\n" +
1919 " (function(f) {\n" +
1920 " return M(function(arg) {\n" +
1921 " return (f(f))(arg);\n" +
1922 " })\n" +
1923 " }));\n" +
1924 " })\n" +
1925 " (function(f) {\n" +
1926 " return function(n) {\n" +
1927 " if (n === 0)\n" +
1928 " return 1;\n" +
1929 " else\n" +
1930 " return n * f(n - 1);\n" +
1931 " };\n" +
1932 " }));\n" +
1933 "\n" +
1934 "factorial(5)\n",
1935 "var factorial;\n" +
1936 "{\n" +
1937 "var M$$inline_4 = function(f$$2) {\n" +
1938 " return function(n){if(n===0)return 1;else return n*f$$2(n-1)}\n" +
1939 "};\n" +
1940 "{\n" +
1941 "var f$$inline_0=function(f$$inline_7){\n" +
1942 " return M$$inline_4(\n" +
1943 " function(arg$$inline_8){\n" +
1944 " return f$$inline_7(f$$inline_7)(arg$$inline_8)\n" +
1945 " })\n" +
1946 "};\n" +
1947 "factorial=M$$inline_4(\n" +
1948 " function(arg$$inline_1){\n" +
1949 " return f$$inline_0(f$$inline_0)(arg$$inline_1)\n" +
1950 "});\n" +
1951 "}\n" +
1952 "}" +
1953 "factorial(5)");
1954 }
1955
1956 public void testRenamePropertyFunction() {
1957 testSame("function JSCompiler_renameProperty(x) {return x} " +
1958 "JSCompiler_renameProperty('foo')");
1959 }
1960
1961 public void testReplacePropertyFunction() {
1962 // baseline: an alias doesn't prevents declaration removal, but not
1963 // inlining.
1964 test("function f(x) {return x} " +
1965 "foo(window, f); f(1)",
1966 "function f(x) {return x} " +
1967 "foo(window, f); 1");
1968 // a reference passed to JSCompiler_ObjectPropertyString prevents inlining
1969 // as well.
1970 testSame("function f(x) {return x} " +
1971 "new JSCompiler_ObjectPropertyString(window, f); f(1)");
1972 }
1973
1974 public void testInlineWithClosureContainingThis() {
1975 test("(function (){return f(function(){return this})})();",
1976 "f(function(){return this})");
1977 }
1978
1979 public void testIssue5159924a() {
1980 test("function f() { if (x()) return y() }\n" +
1981 "while(1){ var m = f() || z() }",
1982 "for(;1;) {" +
1983 " var JSCompiler_inline_result$$0;" +
1984 " {" +
1985 " JSCompiler_inline_label_f_1: {" +
1986 " if(x()) {" +
1987 " JSCompiler_inline_result$$0 = y();" +
1988 " break JSCompiler_inline_label_f_1" +
1989 " }" +
1990 " JSCompiler_inline_result$$0 = void 0;" +
1991 " }" +
1992 " }" +
1993 " var m=JSCompiler_inline_result$$0 || z()" +
1994 "}");
1995 }
1996
1997 public void testIssue5159924b() {
1998 test("function f() { if (x()) return y() }\n" +
1999 "while(1){ var m = f() }",
2000 "for(;1;){" +
2001 " var m;" +
2002 " {" +
2003 " JSCompiler_inline_label_f_0: { " +
2004 " if(x()) {" +
2005 " m = y();" +
2006 " break JSCompiler_inline_label_f_0" +
2007 " }" +
2008 " m = void 0" +
2009 " }" +
2010 " }" +
2011 "}");
2012 }
2013
2014 public void testInlineObject() {
2015 new StringCompare().testInlineObject();
2016 }
2017
2018 private static class StringCompare extends CompilerTestCase {
2019 private boolean allowGlobalFunctionInlining = true;
2020
2021 StringCompare() {
2022 super("", false);
2023 this.enableNormalize();
2024 this.enableMarkNoSideEffects();
2025 }
2026
2027 @Override
2028 public void setUp() throws Exception {
2029 super.setUp();
2030 super.enableLineNumberCheck(true);
2031 allowGlobalFunctionInlining = true;
2032 }
2033
2034 @Override
2035 protected CompilerPass getProcessor(Compiler compiler) {
2036 compiler.resetUniqueNameId();
2037 return new InlineFunctions(
2038 compiler,
2039 compiler.getUniqueNameIdSupplier(),
2040 allowGlobalFunctionInlining,
2041 true, // allowLocalFunctionInlining
2042 true, // allowBlockInlining
2043 true, // assumeStrictThis
2044 true); // assumeMinimumCapture
2045 }
2046
2047 public void testInlineObject() {
2048 allowGlobalFunctionInlining = false;
2049 // TODO(johnlenz): normalize the AST so an AST comparison can be done.
2050 // As is, the expected AST does not match the actual correct result:
2051 // The AST matches "g.a()" with a FREE_CALL annotation, but this as
2052 // expected string would fail as it won't be mark as a free call.
2053 // "(0,g.a)()" matches the output, but not the resulting AST.
2054 test("function inner(){function f(){return g.a}(f())()}",
2055 "function inner(){(0,g.a)()}");
2056 }
2057 }
2058
2059 public void testBug4944818() {
2060 test(
2061 "var getDomServices_ = function(self) {\n" +
2062 " if (!self.domServices_) {\n" +
2063 " self.domServices_ = goog$component$DomServices.get(" +
2064 " self.appContext_);\n" +
2065 " }\n" +
2066 "\n" +
2067 " return self.domServices_;\n" +
2068 "};\n" +
2069 "\n" +
2070 "var getOwnerWin_ = function(self) {\n" +
2071 " return getDomServices_(self).getDomHelper().getWindow();\n" +
2072 "};\n" +
2073 "\n" +
2074 "HangoutStarter.prototype.launchHangout = function() {\n" +
2075 " var self = a.b;\n" +
2076 " var myUrl = new goog.Uri(getOwnerWin_(self).location.href);\n" +
2077 "};",
2078 "HangoutStarter.prototype.launchHangout = function() { " +
2079 " var self$$2 = a.b;" +
2080 " var JSCompiler_temp_const$$0 = goog.Uri;" +
2081 " var JSCompiler_inline_result$$1;" +
2082 " {" +
2083 " var self$$inline_2 = self$$2;" +
2084 " if (!self$$inline_2.domServices_) {" +
2085 " self$$inline_2.domServices_ = goog$component$DomServices.get(" +
2086 " self$$inline_2.appContext_);" +
2087 " }" +
2088 " JSCompiler_inline_result$$1=self$$inline_2.domServices_;" +
2089 " }" +
2090 " var myUrl = new JSCompiler_temp_const$$0(" +
2091 " JSCompiler_inline_result$$1.getDomHelper()." +
2092 " getWindow().location.href)" +
2093 "}");
2094 }
2095
2096 public void testIssue423() {
2097 assumeMinimumCapture = false;
2098 test(
2099 "(function($) {\n" +
2100 " $.fn.multicheck = function(options) {\n" +
2101 " initialize.call(this, options);\n" +
2102 " };\n" +
2103 "\n" +
2104 " function initialize(options) {\n" +
2105 " options.checkboxes = $(this).siblings(':checkbox');\n" +
2106 " preload_check_all.call(this);\n" +
2107 " }\n" +
2108 "\n" +
2109 " function preload_check_all() {\n" +
2110 " $(this).data('checkboxes');\n" +
2111 " }\n" +
2112 "})(jQuery)",
2113 "(function($){" +
2114 " $.fn.multicheck=function(options$$1){" +
2115 " {" +
2116 " options$$1.checkboxes=$(this).siblings(\":checkbox\");" +
2117 " {" +
2118 " $(this).data(\"checkboxes\")" +
2119 " }" +
2120 " }" +
2121 " }" +
2122 "})(jQuery)");
2123
2124 assumeMinimumCapture = true;
2125 test(
2126 "(function($) {\n" +
2127 " $.fn.multicheck = function(options) {\n" +
2128 " initialize.call(this, options);\n" +
2129 " };\n" +
2130 "\n" +
2131 " function initialize(options) {\n" +
2132 " options.checkboxes = $(this).siblings(':checkbox');\n" +
2133 " preload_check_all.call(this);\n" +
2134 " }\n" +
2135 "\n" +
2136 " function preload_check_all() {\n" +
2137 " $(this).data('checkboxes');\n" +
2138 " }\n" +
2139 "})(jQuery)",
2140 "{var $$$inline_0=jQuery;\n" +
2141 "$$$inline_0.fn.multicheck=function(options$$inline_4){\n" +
2142 " {options$$inline_4.checkboxes=" +
2143 "$$$inline_0(this).siblings(\":checkbox\");\n" +
2144 " {$$$inline_0(this).data(\"checkboxes\")}" +
2145 " }\n" +
2146 "}\n" +
2147 "}");
2148 }
2149
2150 public void testIssue728() {
2151 String f = "var f = function() { return false; };";
2152 StringBuilder calls = new StringBuilder();
2153 StringBuilder folded = new StringBuilder();
2154 for (int i = 0; i < 30; i++) {
2155 calls.append("if (!f()) alert('x');");
2156 folded.append("if (!false) alert('x');");
2157 }
2158
2159 test(f + calls, folded.toString());
2160 }
2161
2162 public void testAnonymous1() {
2163 assumeMinimumCapture = false;
2164 test("(function(){var a=10;(function(){var b=a;a++;alert(b)})()})();",
2165 "{var a$$inline_0=10;" +
2166 "{var b$$inline_1=a$$inline_0;" +
2167 "a$$inline_0++;alert(b$$inline_1)}}");
2168
2169 assumeMinimumCapture = true;
2170 test("(function(){var a=10;(function(){var b=a;a++;alert(b)})()})();",
2171 "{var a$$inline_2=10;" +
2172 "{var b$$inline_0=a$$inline_2;" +
2173 "a$$inline_2++;alert(b$$inline_0)}}");
2174 }
2175
2176 public void testAnonymous2() {
2177 testSame("(function(){eval();(function(){var b=a;a++;alert(b)})()})();");
2178 }
2179
2180 public void testAnonymous3() {
2181 // Introducing a new value into is tricky
2182 assumeMinimumCapture = false;
2183 testSame("(function(){var a=10;(function(){arguments;})()})();");
2184
2185 assumeMinimumCapture = true;
2186 test("(function(){var a=10;(function(){arguments;})()})();",
2187 "{var a$$inline_0=10;(function(){arguments;})();}");
2188
2189 test("(function(){(function(){arguments;})()})();",
2190 "{(function(){arguments;})()}");
2191 }
2192
2193
2194 public void testLoopWithFunctionWithFunction() {
2195 assumeMinimumCapture = true;
2196 test("function _testLocalVariableInLoop_() {\n" +
2197 " var result = 0;\n" +
2198 " function foo() {\n" +
2199 " var arr = [1, 2, 3, 4, 5];\n" +
2200 " for (var i = 0, l = arr.length; i < l; i++) {\n" +
2201 " var j = arr[i];\n" +
2202 // don't inline this function, because the correct behavior depends
2203 // captured values.
2204 " (function() {\n" +
2205 " var k = j;\n" +
2206 " setTimeout(function() { result += k; }, 5 * i);\n" +
2207 " })();\n" +
2208 " }\n" +
2209 " }\n" +
2210 " foo();\n" +
2211 "}",
2212 "function _testLocalVariableInLoop_(){\n" +
2213 " var result=0;\n" +
2214 " {" +
2215 " var arr$$inline_0=[1,2,3,4,5];\n" +
2216 " var i$$inline_1=0;\n" +
2217 " var l$$inline_2=arr$$inline_0.length;\n" +
2218 " for(;i$$inline_1<l$$inline_2;i$$inline_1++){\n" +
2219 " var j$$inline_3=arr$$inline_0[i$$inline_1];\n" +
2220 " (function(){\n" +
2221 " var k$$inline_4=j$$inline_3;\n" +
2222 " setTimeout(function(){result+=k$$inline_4},5*i$$inline_1)\n" +
2223 " })()\n" +
2224 " }\n" +
2225 " }\n" +
2226 "}");
2227 }
2228
2229 public void testMethodWithFunctionWithFunction() {
2230 assumeMinimumCapture = true;
2231 test("function _testLocalVariable_() {\n" +
2232 " var result = 0;\n" +
2233 " function foo() {\n" +
2234 " var j = [i];\n" +
2235 " (function(j) {\n" +
2236 " setTimeout(function() { result += j; }, 5 * i);\n" +
2237 " })(j);\n" +
2238 " j = null;" +
2239 " }\n" +
2240 " foo();\n" +
2241 "}",
2242 "function _testLocalVariable_(){\n" +
2243 " var result=0;\n" +
2244 " {\n" +
2245 " var j$$inline_2=[i];\n" +
2246 " {\n" +
2247 " var j$$inline_0=j$$inline_2;\n" + // this temp is needed.
2248 " setTimeout(function(){result+=j$$inline_0},5*i);\n" +
2249 " }\n" +
2250 " j$$inline_2=null\n" + // because this value can be modified later.
2251 " }\n" +
2252 "}");
2253 }
2254
2255 // Inline a single reference function into deeper modules
2256 public void testCrossModuleInlining1() {
2257 test(createModuleChain(
2258 // m1
2259 "function foo(){return f(1)+g(2)+h(3);}",
2260 // m2
2261 "foo()"
2262 ),
2263 new String[] {
2264 // m1
2265 "",
2266 // m2
2267 "f(1)+g(2)+h(3);"
2268 }
2269 );
2270 }
2271
2272 // Inline a single reference function into shallow modules, only if it
2273 // is cheaper than the call itself.
2274 public void testCrossModuleInlining2() {
2275 testSame(createModuleChain(
2276 // m1
2277 "foo()",
2278 // m2
2279 "function foo(){return f(1)+g(2)+h(3);}"
2280 )
2281 );
2282
2283 test(createModuleChain(
2284 // m1
2285 "foo()",
2286 // m2
2287 "function foo(){return f();}"
2288 ),
2289 new String[] {
2290 // m1
2291 "f();",
2292 // m2
2293 ""
2294 }
2295 );
2296 }
2297
2298 // Inline a multi-reference functions into shallow modules, only if it
2299 // is cheaper than the call itself.
2300 public void testCrossModuleInlining3() {
2301 testSame(createModuleChain(
2302 // m1
2303 "foo()",
2304 // m2
2305 "function foo(){return f(1)+g(2)+h(3);}",
2306 // m3
2307 "foo()"
2308 )
2309 );
2310
2311 test(createModuleChain(
2312 // m1
2313 "foo()",
2314 // m2
2315 "function foo(){return f();}",
2316 // m3
2317 "foo()"
2318 ),
2319 new String[] {
2320 // m1
2321 "f();",
2322 // m2
2323 "",
2324 // m3
2325 "f();"
2326 }
2327 );
2328 }
2329
2330 public void test6671158() {
2331 test(
2332 "function f() {return g()}" +
2333 "function Y(a){a.loader_()}" +
2334 "function _Z(){}" +
2335 "function _X() { new _Z(a,b, Y(singleton), f()) }",
2336
2337 "function _Z(){}" +
2338 "function _X(){" +
2339 " var JSCompiler_temp_const$$2=_Z;" +
2340 " var JSCompiler_temp_const$$1=a;" +
2341 " var JSCompiler_temp_const$$0=b;" +
2342 " var JSCompiler_inline_result$$3;" +
2343 " {" +
2344 " singleton.loader_();" +
2345 " JSCompiler_inline_result$$3=void 0;" +
2346 " }" +
2347 " new JSCompiler_temp_const$$2(" +
2348 " JSCompiler_temp_const$$1," +
2349 " JSCompiler_temp_const$$0," +
2350 " JSCompiler_inline_result$$3," +
2351 " g())}");
2352 }
2353
2354 }