Debian Codegraph @

test/com/google/javascript/jscomp/FunctionInjectorTest.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 import com.google.common.collect.Lists;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage;
22 import com.google.javascript.jscomp.FunctionInjector.CanInlineResult;
23 import com.google.javascript.jscomp.FunctionInjector.InliningMode;
24 import com.google.javascript.jscomp.NodeTraversal.Callback;
25 import com.google.javascript.rhino.Node;
26 import junit.framework.TestCase;
27
28 import java.util.List;
29 import java.util.Set;
30
31 /**
32 * Inline function tests.
33 * @author [email protected] (John Lenz)
34 */
35
36 public class FunctionInjectorTest extends TestCase {
37 static final InliningMode INLINE_DIRECT = InliningMode.DIRECT;
38 static final InliningMode INLINE_BLOCK = InliningMode.BLOCK;
39 private boolean assumeStrictThis = false;
40 private boolean assumeMinimumCapture = false;
41
42 @Override
43 protected void setUp() throws Exception {
44 super.setUp();
45 assumeStrictThis = false;
46 }
47
48 private FunctionInjector getInjector() {
49 Compiler compiler = new Compiler();
50 return new FunctionInjector(
51 compiler, compiler.getUniqueNameIdSupplier(), true,
52 assumeStrictThis, assumeMinimumCapture);
53 }
54
55 public void testIsSimpleFunction1() {
56 assertTrue(getInjector().isDirectCallNodeReplacementPossible(
57 prep("function f(){}")));
58 }
59
60 public void testIsSimpleFunction2() {
61 assertTrue(getInjector().isDirectCallNodeReplacementPossible(
62 prep("function f(){return 0;}")));
63 }
64
65 public void testIsSimpleFunction3() {
66 assertTrue(getInjector().isDirectCallNodeReplacementPossible(
67 prep("function f(){return x ? 0 : 1}")));
68 }
69
70 public void testIsSimpleFunction4() {
71 assertFalse(getInjector().isDirectCallNodeReplacementPossible(
72 prep("function f(){return;}")));
73 }
74
75 public void testIsSimpleFunction5() {
76 assertFalse(getInjector().isDirectCallNodeReplacementPossible(
77 prep("function f(){return 0; return 0;}")));
78 }
79
80 public void testIsSimpleFunction6() {
81 assertFalse(getInjector().isDirectCallNodeReplacementPossible(
82 prep("function f(){var x=true;return x ? 0 : 1}")));
83 }
84
85 public void testIsSimpleFunction7() {
86 assertFalse(getInjector().isDirectCallNodeReplacementPossible(
87 prep("function f(){if (x) return 0; else return 1}")));
88 }
89
90 public void testCanInlineReferenceToFunction1() {
91 helperCanInlineReferenceToFunction(CanInlineResult.YES,
92 "function foo(){}; foo();", "foo", INLINE_DIRECT);
93 }
94
95 public void testCanInlineReferenceToFunction2() {
96 helperCanInlineReferenceToFunction(CanInlineResult.YES,
97 "function foo(){}; foo();", "foo", INLINE_BLOCK);
98 }
99
100 public void testCanInlineReferenceToFunction3() {
101 // NOTE: FoldConstants will convert this to a empty function,
102 // so there is no need to explicitly support it.
103 helperCanInlineReferenceToFunction(CanInlineResult.NO,
104 "function foo(){return;}; foo();", "foo", INLINE_DIRECT);
105 }
106
107 public void testCanInlineReferenceToFunction4() {
108 helperCanInlineReferenceToFunction(CanInlineResult.YES,
109 "function foo(){return;}; foo();", "foo", INLINE_BLOCK);
110 }
111
112 public void testCanInlineReferenceToFunction5() {
113 helperCanInlineReferenceToFunction(CanInlineResult.YES,
114 "function foo(){return true;}; foo();", "foo", INLINE_DIRECT);
115 }
116
117 public void testCanInlineReferenceToFunction6() {
118 helperCanInlineReferenceToFunction(CanInlineResult.YES,
119 "function foo(){return true;}; foo();", "foo", INLINE_BLOCK);
120 }
121
122 public void testCanInlineReferenceToFunction7() {
123 // In var initialization.
124 helperCanInlineReferenceToFunction(CanInlineResult.YES,
125 "function foo(){return true;}; var x=foo();", "foo", INLINE_DIRECT);
126 }
127
128 public void testCanInlineReferenceToFunction8() {
129 helperCanInlineReferenceToFunction(CanInlineResult.YES,
130 "function foo(){return true;}; var x=foo();", "foo", INLINE_BLOCK);
131 }
132
133 public void testCanInlineReferenceToFunction9() {
134 // In assignment.
135 helperCanInlineReferenceToFunction(CanInlineResult.YES,
136 "function foo(){return true;}; var x; x=foo();", "foo", INLINE_DIRECT);
137 }
138
139 public void testCanInlineReferenceToFunction10() {
140 helperCanInlineReferenceToFunction(CanInlineResult.YES,
141 "function foo(){return true;}; var x; x=foo();", "foo", INLINE_BLOCK);
142 }
143
144 public void testCanInlineReferenceToFunction11() {
145 // In expression.
146 helperCanInlineReferenceToFunction(CanInlineResult.YES,
147 "function foo(){return true;}; var x; x=x+foo();", "foo",
148 INLINE_DIRECT);
149 }
150
151 public void testCanInlineReferenceToFunction12() {
152 // "foo" is not known to be side-effect free, it might change the value
153 // of "x", so it can't be inlined.
154 helperCanInlineReferenceToFunction(CanInlineResult.NO,
155 "function foo(){return true;}; var x; x=x+foo();", "foo", INLINE_BLOCK);
156 }
157
158 public void testCanInlineReferenceToFunction12b() {
159 // "foo" is not known to be side-effect free, it might change the value
160 // of "x", so it can't be inlined.
161 helperCanInlineReferenceToFunction(
162 CanInlineResult.AFTER_PREPARATION,
163 "function foo(){return true;}; var x; x=x+foo();",
164 "foo", INLINE_BLOCK, true);
165 }
166
167 // TODO(nicksantos): Re-enable with side-effect detection.
168 // public void testCanInlineReferenceToFunction13() {
169 // // ... if foo is side-effect free we can inline here.
170 // helperCanInlineReferenceToFunction(true,
171 // "/** @nosideeffects */ function foo(){return true;};" +
172 // "var x; x=x+foo();", "foo", INLINE_BLOCK);
173 // }
174
175 public void testCanInlineReferenceToFunction14() {
176 // Simple call with parameters
177 helperCanInlineReferenceToFunction(CanInlineResult.YES,
178 "function foo(a){return true;}; foo(x);", "foo", INLINE_DIRECT);
179 }
180
181 public void testCanInlineReferenceToFunction15() {
182 helperCanInlineReferenceToFunction(CanInlineResult.YES,
183 "function foo(a){return true;}; foo(x);", "foo", INLINE_BLOCK);
184 }
185
186 // TODO(johnlenz): remove this constant once this has been proven in
187 // production code.
188 static final CanInlineResult NEW_VARS_IN_GLOBAL_SCOPE =
189 CanInlineResult.YES;
190
191 public void testCanInlineReferenceToFunction16() {
192 // Function "foo" as it contains "var b" which
193 // must be brought into the global scope.
194 helperCanInlineReferenceToFunction(NEW_VARS_IN_GLOBAL_SCOPE,
195 "function foo(a){var b;return a;}; foo(goo());", "foo", INLINE_BLOCK);
196 }
197
198 public void testCanInlineReferenceToFunction17() {
199 // This doesn't bring names into the global name space.
200 helperCanInlineReferenceToFunction(CanInlineResult.YES,
201 "function foo(a){return a;}; " +
202 "function x() { foo(goo()); }",
203 "foo", INLINE_BLOCK);
204 }
205
206 public void testCanInlineReferenceToFunction18() {
207 // Parameter has side-effects.
208 helperCanInlineReferenceToFunction(CanInlineResult.NO,
209 "function foo(a){return a;} foo(x++);", "foo", INLINE_DIRECT);
210 }
211
212 public void testCanInlineReferenceToFunction19() {
213 // Parameter has mutable parameter referenced more than once.
214 helperCanInlineReferenceToFunction(CanInlineResult.NO,
215 "function foo(a){return a+a} foo([]);", "foo", INLINE_DIRECT);
216 }
217
218 public void testCanInlineReferenceToFunction20() {
219 helperCanInlineReferenceToFunction(CanInlineResult.NO,
220 "function foo(a){return a+a} foo({});", "foo", INLINE_DIRECT);
221 }
222
223 public void testCanInlineReferenceToFunction21() {
224 helperCanInlineReferenceToFunction(CanInlineResult.NO,
225 "function foo(a){return a+a} foo(new Date);", "foo", INLINE_DIRECT);
226 }
227
228 public void testCanInlineReferenceToFunction22() {
229 helperCanInlineReferenceToFunction(CanInlineResult.NO,
230 "function foo(a){return a+a} foo(true && new Date);", "foo",
231 INLINE_DIRECT);
232 }
233
234 public void testCanInlineReferenceToFunction23() {
235 // variables to global scope.
236 helperCanInlineReferenceToFunction(NEW_VARS_IN_GLOBAL_SCOPE,
237 "function foo(a){return a;}; foo(x++);", "foo", INLINE_BLOCK);
238 }
239
240 public void testCanInlineReferenceToFunction24() {
241 // ... this is OK, because it doesn't introduce a new global name.
242 helperCanInlineReferenceToFunction(CanInlineResult.YES,
243 "function foo(a){return a;}; " +
244 "function x() { foo(x++); }",
245 "foo", INLINE_BLOCK);
246 }
247
248 public void testCanInlineReferenceToFunction25() {
249 // Parameter has side-effects.
250 helperCanInlineReferenceToFunction(CanInlineResult.NO,
251 "function foo(a){return a+a;}; foo(x++);", "foo", INLINE_DIRECT);
252 }
253
254 public void testCanInlineReferenceToFunction26() {
255 helperCanInlineReferenceToFunction(NEW_VARS_IN_GLOBAL_SCOPE,
256 "function foo(a){return a+a;}; foo(x++);", "foo", INLINE_BLOCK);
257 }
258
259 public void testCanInlineReferenceToFunction27() {
260 helperCanInlineReferenceToFunction(CanInlineResult.YES,
261 "function foo(a){return a+a;}; " +
262 "function x() { foo(x++); }",
263 "foo", INLINE_BLOCK);
264 }
265
266 public void testCanInlineReferenceToFunction28() {
267 // Parameter has side-effects.
268 helperCanInlineReferenceToFunction(CanInlineResult.NO,
269 "function foo(a){return true;}; foo(goo());", "foo", INLINE_DIRECT);
270 }
271
272 public void testCanInlineReferenceToFunction29() {
273 helperCanInlineReferenceToFunction(NEW_VARS_IN_GLOBAL_SCOPE,
274 "function foo(a){return true;}; foo(goo());", "foo", INLINE_BLOCK);
275 }
276
277 public void testCanInlineReferenceToFunction30() {
278 helperCanInlineReferenceToFunction(CanInlineResult.YES,
279 "function foo(a){return true;}; " +
280 "function x() { foo(goo()); }",
281 "foo", INLINE_BLOCK);
282 }
283
284 public void testCanInlineReferenceToFunction31() {
285 helperCanInlineReferenceToFunction(CanInlineResult.YES,
286 "function foo(a) {return true;}; " +
287 "function x() {foo.call(this, 1);}",
288 "foo", INLINE_DIRECT);
289 }
290
291 public void testCanInlineReferenceToFunction32() {
292 helperCanInlineReferenceToFunction(CanInlineResult.NO,
293 "function foo(a){return true;}; " +
294 "function x() { foo.apply(this, [1]); }",
295 "foo", INLINE_DIRECT);
296 }
297
298 public void testCanInlineReferenceToFunction33() {
299 // No special handling is required for method calls passing this.
300 helperCanInlineReferenceToFunction(CanInlineResult.YES,
301 "function foo(a){return true;}; " +
302 "function x() { foo.bar(this, 1); }",
303 "foo", INLINE_DIRECT);
304 }
305
306 public void testCanInlineReferenceToFunction34() {
307 helperCanInlineReferenceToFunction(CanInlineResult.YES,
308 "function foo(a){return true;}; " +
309 "function x() { foo.call(this, goo()); }",
310 "foo", INLINE_BLOCK);
311 }
312
313 public void testCanInlineReferenceToFunction35() {
314 helperCanInlineReferenceToFunction(CanInlineResult.NO,
315 "function foo(a){return true;}; " +
316 "function x() { foo.apply(this, goo()); }",
317 "foo", INLINE_BLOCK);
318 }
319
320 public void testCanInlineReferenceToFunction36() {
321 helperCanInlineReferenceToFunction(CanInlineResult.YES,
322 "function foo(a){return true;}; " +
323 "function x() { foo.bar(this, goo()); }",
324 "foo", INLINE_BLOCK);
325 }
326
327 public void testCanInlineReferenceToFunction37() {
328 helperCanInlineReferenceToFunction(CanInlineResult.NO,
329 "function foo(a){return true;}; " +
330 "function x() { foo.call(null, 1); }",
331 "foo", INLINE_DIRECT);
332 }
333
334 public void testCanInlineReferenceToFunction38() {
335 assumeStrictThis = false;
336
337 helperCanInlineReferenceToFunction(CanInlineResult.NO,
338 "function foo(a){return true;}; " +
339 "function x() { foo.call(null, goo()); }",
340 "foo", INLINE_BLOCK);
341
342 assumeStrictThis = true;
343
344 helperCanInlineReferenceToFunction(CanInlineResult.YES,
345 "function foo(a){return true;}; " +
346 "function x() { foo.call(null, goo()); }",
347 "foo", INLINE_BLOCK);
348 }
349
350 public void testCanInlineReferenceToFunction39() {
351 helperCanInlineReferenceToFunction(CanInlineResult.NO,
352 "function foo(a){return true;}; " +
353 "function x() { foo.call(bar, 1); }",
354 "foo", INLINE_DIRECT);
355 }
356
357 public void testCanInlineReferenceToFunction40() {
358 assumeStrictThis = false;
359 helperCanInlineReferenceToFunction(CanInlineResult.NO,
360 "function foo(a){return true;}; " +
361 "function x() { foo.call(bar, goo()); }",
362 "foo", INLINE_BLOCK);
363
364 assumeStrictThis = true;
365 helperCanInlineReferenceToFunction(CanInlineResult.YES,
366 "function foo(a){return true;}; " +
367 "function x() { foo.call(bar, goo()); }",
368 "foo", INLINE_BLOCK);
369 }
370
371 public void testCanInlineReferenceToFunction41() {
372 helperCanInlineReferenceToFunction(CanInlineResult.NO,
373 "function foo(a){return true;}; " +
374 "function x() { foo.call(new bar(), 1); }",
375 "foo", INLINE_DIRECT);
376 }
377
378 public void testCanInlineReferenceToFunction42() {
379 assumeStrictThis = false;
380 helperCanInlineReferenceToFunction(CanInlineResult.NO,
381 "function foo(a){return true;}; " +
382 "function x() { foo.call(new bar(), goo()); }",
383 "foo", INLINE_BLOCK);
384
385 assumeStrictThis = true;
386 helperCanInlineReferenceToFunction(CanInlineResult.YES,
387 "function foo(a){return true;}; " +
388 "function x() { foo.call(new bar(), goo()); }",
389 "foo", INLINE_BLOCK);
390 }
391
392 public void testCanInlineReferenceToFunction43() {
393 // Handle the case of a missing 'this' value in a call.
394 helperCanInlineReferenceToFunction(CanInlineResult.NO,
395 "function foo(){return true;}; " +
396 "function x() { foo.call(); }",
397 "foo", INLINE_DIRECT);
398 }
399
400 public void testCanInlineReferenceToFunction44() {
401 assumeStrictThis = false;
402 // Handle the case of a missing 'this' value in a call.
403 helperCanInlineReferenceToFunction(CanInlineResult.NO,
404 "function foo(){return true;}; " +
405 "function x() { foo.call(); }",
406 "foo", INLINE_BLOCK);
407
408 assumeStrictThis = true;
409 // Handle the case of a missing 'this' value in a call.
410 helperCanInlineReferenceToFunction(CanInlineResult.YES,
411 "function foo(){return true;}; " +
412 "function x() { foo.call(); }",
413 "foo", INLINE_BLOCK);
414 }
415
416 public void testCanInlineReferenceToFunction45() {
417 // Call with inner function expression.
418 helperCanInlineReferenceToFunction(CanInlineResult.YES,
419 "function foo(){return function() {return true;}}; foo();",
420 "foo", INLINE_DIRECT);
421 }
422
423 public void testCanInlineReferenceToFunction46() {
424 // Call with inner function expression.
425 helperCanInlineReferenceToFunction(CanInlineResult.YES,
426 "function foo(){return function() {return true;}}; foo();",
427 "foo", INLINE_BLOCK);
428 }
429
430 public void testCanInlineReferenceToFunction47() {
431 // Call with inner function expression and variable decl.
432 helperCanInlineReferenceToFunction(CanInlineResult.NO,
433 "function foo(){var a; return function() {return true;}}; foo();",
434 "foo", INLINE_DIRECT);
435 }
436
437 public void testCanInlineReferenceToFunction48() {
438 // Call with inner function expression and variable decl.
439 // TODO(johnlenz): should we validate no values in scope?
440 helperCanInlineReferenceToFunction(CanInlineResult.YES,
441 "function foo(){var a; return function() {return true;}}; foo();",
442 "foo", INLINE_BLOCK);
443 }
444
445 public void testCanInlineReferenceToFunction49() {
446 // Call with inner function expression.
447 helperCanInlineReferenceToFunction(CanInlineResult.YES,
448 "function foo(){return function() {var a; return true;}}; foo();",
449 "foo", INLINE_DIRECT);
450 }
451
452 public void testCanInlineReferenceToFunction50() {
453 // Call with inner function expression.
454 helperCanInlineReferenceToFunction(CanInlineResult.YES,
455 "function foo(){return function() {var a; return true;}}; foo();",
456 "foo", INLINE_BLOCK);
457 }
458
459 public void testCanInlineReferenceToFunction51() {
460 // Call with inner function statement.
461 helperCanInlineReferenceToFunction(CanInlineResult.YES,
462 "function foo(){function x() {var a; return true;} return x}; foo();",
463 "foo", INLINE_BLOCK);
464 }
465
466 public void testCanInlineReferenceToFunctionInExpression1() {
467 // Call in if condition
468 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
469 "function foo(a){return true;}; " +
470 "function x() { if (foo(1)) throw 'test'; }",
471 "foo", INLINE_BLOCK, true);
472 }
473
474 public void testCanInlineReferenceToFunctionInExpression2() {
475 // Call in return expression
476 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
477 "function foo(a){return true;}; " +
478 "function x() { return foo(1); }",
479 "foo", INLINE_BLOCK, true);
480 }
481
482 public void testCanInlineReferenceToFunctionInExpression3() {
483 // Call in switch expression
484 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
485 "function foo(a){return true;}; " +
486 "function x() { switch(foo(1)) { default:break; } }",
487 "foo", INLINE_BLOCK, true);
488 }
489
490 public void testCanInlineReferenceToFunctionInExpression4() {
491 // Call in hook condition
492 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
493 "function foo(a){return true;}; " +
494 "function x() {foo(1)?0:1 }",
495 "foo", INLINE_BLOCK, true);
496 }
497
498 public void testCanInlineReferenceToFunctionInExpression5() {
499 // Call in hook side-effect free condition
500 helperCanInlineReferenceToFunction(CanInlineResult.NO,
501 "function foo(a){return true;}; " +
502 "function x() {true?foo(1):1 }",
503 "foo", INLINE_BLOCK);
504 }
505
506 public void testCanInlineReferenceToFunctionInExpression5a() {
507 // Call in hook side-effect free condition
508 helperCanInlineReferenceToFunction(
509 CanInlineResult.AFTER_PREPARATION,
510 "function foo(a){return true;}; " +
511 "function x() {true?foo(1):1 }",
512 "foo", INLINE_BLOCK, true);
513 }
514
515 public void testCanInlineReferenceToFunctionInExpression6() {
516 // Call in expression statement "condition"
517 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
518 "function foo(a){return true;}; " +
519 "function x() {foo(1) && 1 }",
520 "foo", INLINE_BLOCK, true);
521 }
522
523 public void testCanInlineReferenceToFunctionInExpression7() {
524 // Call in expression statement after side-effect free "condition"
525 helperCanInlineReferenceToFunction(CanInlineResult.NO,
526 "function foo(a){return true;}; " +
527 "function x() {1 && foo(1) }",
528 "foo", INLINE_BLOCK);
529 }
530
531 public void testCanInlineReferenceToFunctionInExpression7a() {
532 // Call in expression statement after side-effect free "condition"
533 helperCanInlineReferenceToFunction(
534 CanInlineResult.AFTER_PREPARATION,
535 "function foo(a){return true;}; " +
536 "function x() {1 && foo(1) }",
537 "foo", INLINE_BLOCK, true);
538 }
539
540 public void testCanInlineReferenceToFunctionInExpression8() {
541 // Call in expression statement after side-effect free operator
542 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
543 "function foo(a){return true;}; " +
544 "function x() {1 + foo(1) }",
545 "foo", INLINE_BLOCK, true);
546 }
547
548 public void testCanInlineReferenceToFunctionInExpression9() {
549 // Call in VAR expression.
550 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
551 "function foo(a){return true;}; " +
552 "function x() {var b = 1 + foo(1)}",
553 "foo", INLINE_BLOCK, true);
554 }
555
556 public void testCanInlineReferenceToFunctionInExpression10() {
557 // Call in assignment expression.
558 helperCanInlineReferenceToFunction(CanInlineResult.NO,
559 "function foo(a){return true;}; " +
560 "function x() {var b; b += 1 + foo(1) }",
561 "foo", INLINE_BLOCK);
562 }
563
564 public void testCanInlineReferenceToFunctionInExpression10a() {
565 // Call in assignment expression.
566 helperCanInlineReferenceToFunction(
567 CanInlineResult.AFTER_PREPARATION,
568 "function foo(a){return true;}; " +
569 "function x() {var b; b += 1 + foo(1) }",
570 "foo", INLINE_BLOCK, true);
571 }
572
573 // TODO(nicksantos): Re-enable with side-effect detection.
574 // public void testCanInlineReferenceToFunctionInExpression11() {
575 // helperCanInlineReferenceToFunction(true,
576 // "/** @nosideeffects */ function foo(a){return true;}; " +
577 // "function x() {var b; b += 1 + foo(1) }",
578 // "foo", INLINE_BLOCK);
579 // }
580
581 public void testCanInlineReferenceToFunctionInExpression12() {
582 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
583 "function foo(a){return true;}; " +
584 "function x() {var a,b,c; a = b = c = foo(1) }",
585 "foo", INLINE_BLOCK, true);
586 }
587
588 public void testCanInlineReferenceToFunctionInExpression13() {
589 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
590 "function foo(a){return true;}; " +
591 "function x() {var a,b,c; a = b = c = 1 + foo(1) }",
592 "foo", INLINE_BLOCK, true);
593 }
594
595 public void testCanInlineReferenceToFunctionInExpression14() {
596 // ... foo can not be inlined because of possible changes to "c".
597 helperCanInlineReferenceToFunction(CanInlineResult.NO,
598 "var a = {}, b = {}, c;" +
599 "a.test = 'a';" +
600 "b.test = 'b';" +
601 "c = a;" +
602 "function foo(){c = b; return 'foo'};" +
603 "c.test=foo();",
604 "foo", INLINE_BLOCK);
605 }
606
607 public void testCanInlineReferenceToFunctionInExpression14a() {
608 // ... foo can be inlined despite possible changes to "c".
609 helperCanInlineReferenceToFunction(
610 CanInlineResult.AFTER_PREPARATION,
611 "var a = {}, b = {}, c;" +
612 "a.test = 'a';" +
613 "b.test = 'b';" +
614 "c = a;" +
615 "function foo(){c = b; return 'foo'};" +
616 "c.test=foo();",
617 "foo", INLINE_BLOCK, true);
618 }
619
620 // TODO(nicksantos): Re-enable with side-effect detection.
621 // public void testCanInlineReferenceToFunctionInExpression15() {
622 // // ... foo can be inlined as it is side-effect free.
623 // helperCanInlineReferenceToFunction(true,
624 // "var a = {}, b = {}, c;" +
625 // "a.test = 'a';" +
626 // "b.test = 'b';" +
627 // "c = a;" +
628 // "/** @nosideeffects */ function foo(){return 'foo'};" +
629 // "c.test=foo();",
630 // "foo", INLINE_BLOCK);
631 // }
632
633 // public void testCanInlineReferenceToFunctionInExpression16() {
634 // // ... foo can not be inlined because of possible side-effects of x()
635 // helperCanInlineReferenceToFunction(false,
636 // "var a = {}, b = {}, c;" +
637 // "a.test = 'a';" +
638 // "b.test = 'b';" +
639 // "c = a;" +
640 // "function x(){return c};" +
641 // "/** @nosideeffects */ function foo(){return 'foo'};" +
642 // "x().test=foo();",
643 // "foo", INLINE_BLOCK);
644 // }
645
646 // public void testCanInlineReferenceToFunctionInExpression17() {
647 // // ... foo can be inlined because of x() is side-effect free.
648 // helperCanInlineReferenceToFunction(true,
649 // "var a = {}, b = {}, c;" +
650 // "a.test = 'a';" +
651 // "b.test = 'b';" +
652 // "c = a;" +
653 // "/** @nosideeffects */ function x(){return c};" +
654 // "/** @nosideeffects */ function foo(){return 'foo'};" +
655 // "x().test=foo();",
656 // "foo", INLINE_BLOCK);
657 // }
658
659 public void testCanInlineReferenceToFunctionInExpression18() {
660 // Call in within a call
661 helperCanInlineReferenceToFunction(CanInlineResult.AFTER_PREPARATION,
662 "function foo(){return _g();}; " +
663 "function x() {1 + foo()() }",
664 "foo", INLINE_BLOCK, true);
665 }
666
667 public void testCanInlineReferenceToFunctionInExpression19() {
668 // ... unless foo is known to be side-effect free, it might actually
669 // change the value of "_g" which would unfortunately change the behavior,
670 // so we can't inline here.
671 helperCanInlineReferenceToFunction(CanInlineResult.NO,
672 "function foo(){return a;}; " +
673 "function x() {1 + _g(foo()) }",
674 "foo", INLINE_BLOCK);
675 }
676
677 public void testCanInlineReferenceToFunctionInExpression19a() {
678 // ... unless foo is known to be side-effect free, it might actually
679 // change the value of "_g" which would unfortunately change the behavior,
680 // so we can't inline here.
681 helperCanInlineReferenceToFunction(
682 CanInlineResult.AFTER_PREPARATION,
683 "function foo(){return a;}; " +
684 "function x() {1 + _g(foo()) }",
685 "foo", INLINE_BLOCK, true);
686 }
687
688 // TODO(nicksantos): Re-enable with side-effect detection.
689 // public void testCanInlineReferenceToFunctionInExpression20() {
690 // helperCanInlineReferenceToFunction(true,
691 // "/** @nosideeffects */ function foo(){return a;}; " +
692 // "function x() {1 + _g(foo()) }",
693 // "foo", INLINE_BLOCK);
694 // }
695
696 public void testCanInlineReferenceToFunctionInExpression21() {
697 // Assignments to object are problematic if the call has side-effects,
698 // as the object that is being referred to can change.
699 // Note: This could be changed be inlined if we in some way make "z"
700 // as not escaping from the local scope.
701 helperCanInlineReferenceToFunction(CanInlineResult.NO,
702 "var z = {};" +
703 "function foo(a){z = {};return true;}; " +
704 "function x() { z.gack = foo(1) }",
705 "foo", INLINE_BLOCK);
706 }
707
708 public void testCanInlineReferenceToFunctionInExpression21a() {
709 // Assignments to object are problematic if the call has side-effects,
710 // as the object that is being referred to can change.
711 // Note: This could be changed be inlined if we in some way make "z"
712 // as not escaping from the local scope.
713 helperCanInlineReferenceToFunction(
714 CanInlineResult.AFTER_PREPARATION,
715 "var z = {};" +
716 "function foo(a){z = {};return true;}; " +
717 "function x() { z.gack = foo(1) }",
718 "foo", INLINE_BLOCK, true);
719 }
720
721 public void testCanInlineReferenceToFunctionInExpression22() {
722 // ... foo() is after a side-effect
723 helperCanInlineReferenceToFunction(CanInlineResult.NO,
724 "function foo(){return a;}; " +
725 "function x() {1 + _g(_a(), foo()) }",
726 "foo", INLINE_BLOCK);
727 }
728
729 public void testCanInlineReferenceToFunctionInExpression22a() {
730 // ... foo() is after a side-effect
731 helperCanInlineReferenceToFunction(
732 CanInlineResult.AFTER_PREPARATION,
733 "function foo(){return a;}; " +
734 "function x() {1 + _g(_a(), foo()) }",
735 "foo", INLINE_BLOCK, true);
736 }
737
738 public void testCanInlineReferenceToFunctionInExpression23() {
739 // ... foo() is after a side-effect
740 helperCanInlineReferenceToFunction(CanInlineResult.NO,
741 "function foo(){return a;}; " +
742 "function x() {1 + _g(_a(), foo.call(this)) }",
743 "foo", INLINE_BLOCK);
744 }
745
746 public void testCanInlineReferenceToFunctionInExpression23a() {
747 // ... foo() is after a side-effect
748 helperCanInlineReferenceToFunction(
749 CanInlineResult.AFTER_PREPARATION,
750 "function foo(){return a;}; " +
751 "function x() {1 + _g(_a(), foo.call(this)) }",
752 "foo", INLINE_BLOCK, true);
753 }
754
755 public void testCanInlineReferenceToFunctionInLoop1() {
756 helperCanInlineReferenceToFunction(
757 CanInlineResult.YES,
758 "function foo(){return a;}; " +
759 "while(1) { foo(); }",
760 "foo", INLINE_BLOCK, true);
761 }
762
763 public void testCanInlineReferenceToFunctionInLoop2() {
764 // If function contains function, don't inline it into a loop.
765 // TODO(johnlenz): this can be improved by looking to see
766 // if the inner function contains any references to values defined
767 // in the outer function.
768 helperCanInlineReferenceToFunction(
769 CanInlineResult.NO,
770 "function foo(){return function() {};}; " +
771 "while(1) { foo(); }",
772 "foo", INLINE_BLOCK, true);
773 }
774
775 public void testInline1() {
776 helperInlineReferenceToFunction(
777 "function foo(){}; foo();",
778 "function foo(){}; void 0",
779 "foo", INLINE_DIRECT);
780 }
781
782 public void testInline2() {
783 helperInlineReferenceToFunction(
784 "function foo(){}; foo();",
785 "function foo(){}; {}",
786 "foo", INLINE_BLOCK);
787 }
788
789 public void testInline3() {
790 helperInlineReferenceToFunction(
791 "function foo(){return;}; foo();",
792 "function foo(){return;}; {}",
793 "foo", INLINE_BLOCK);
794 }
795
796 public void testInline4() {
797 helperInlineReferenceToFunction(
798 "function foo(){return true;}; foo();",
799 "function foo(){return true;}; true;",
800 "foo", INLINE_DIRECT);
801 }
802
803 public void testInline5() {
804 helperInlineReferenceToFunction(
805 "function foo(){return true;}; foo();",
806 "function foo(){return true;}; {true;}",
807 "foo", INLINE_BLOCK);
808 }
809
810 public void testInline6() {
811 // In var initialization.
812 helperInlineReferenceToFunction(
813 "function foo(){return true;}; var x=foo();",
814 "function foo(){return true;}; var x=true;",
815 "foo", INLINE_DIRECT);
816 }
817
818 public void testInline7() {
819 helperInlineReferenceToFunction(
820 "function foo(){return true;}; var x=foo();",
821 "function foo(){return true;}; var x;" +
822 "{x=true}",
823 "foo", INLINE_BLOCK);
824 }
825
826 public void testInline8() {
827 // In assignment.
828 helperInlineReferenceToFunction(
829 "function foo(){return true;}; var x; x=foo();",
830 "function foo(){return true;}; var x; x=true;",
831 "foo", INLINE_DIRECT);
832 }
833
834 public void testInline9() {
835 helperInlineReferenceToFunction(
836 "function foo(){return true;}; var x; x=foo();",
837 "function foo(){return true;}; var x;{x=true}",
838 "foo", INLINE_BLOCK);
839 }
840
841 public void testInline10() {
842 // In expression.
843 helperInlineReferenceToFunction(
844 "function foo(){return true;}; var x; x=x+foo();",
845 "function foo(){return true;}; var x; x=x+true;",
846 "foo", INLINE_DIRECT);
847 }
848
849 public void testInline11() {
850 // Simple call with parameters
851 helperInlineReferenceToFunction(
852 "function foo(a){return true;}; foo(x);",
853 "function foo(a){return true;}; true;",
854 "foo", INLINE_DIRECT);
855 }
856
857 public void testInline12() {
858 helperInlineReferenceToFunction(
859 "function foo(a){return true;}; foo(x);",
860 "function foo(a){return true;}; {true}",
861 "foo", INLINE_BLOCK);
862 }
863
864 public void testInline13() {
865 // Parameter has side-effects.
866 helperInlineReferenceToFunction(
867 "function foo(a){return a;}; " +
868 "function x() { foo(x++); }",
869 "function foo(a){return a;}; " +
870 "function x() {{var a$$inline_0=x++;" +
871 "a$$inline_0}}",
872 "foo", INLINE_BLOCK);
873 }
874
875 public void testInline14() {
876 // Parameter has side-effects.
877 helperInlineReferenceToFunction(
878 "function foo(a){return a+a;}; foo(x++);",
879 "function foo(a){return a+a;}; " +
880 "{var a$$inline_0=x++;" +
881 " a$$inline_0+" +
882 "a$$inline_0;}",
883 "foo", INLINE_BLOCK);
884 }
885
886 public void testInline15() {
887 // Parameter has mutable, references more than once.
888 helperInlineReferenceToFunction(
889 "function foo(a){return a+a;}; foo(new Date());",
890 "function foo(a){return a+a;}; " +
891 "{var a$$inline_0=new Date();" +
892 " a$$inline_0+" +
893 "a$$inline_0;}",
894 "foo", INLINE_BLOCK);
895 }
896
897 public void testInline16() {
898 // Parameter is large, references more than once.
899 helperInlineReferenceToFunction(
900 "function foo(a){return a+a;}; foo(function(){});",
901 "function foo(a){return a+a;}; " +
902 "{var a$$inline_0=function(){};" +
903 " a$$inline_0+" +
904 "a$$inline_0;}",
905 "foo", INLINE_BLOCK);
906 }
907
908 public void testInline17() {
909 // Parameter has side-effects.
910 helperInlineReferenceToFunction(
911 "function foo(a){return true;}; foo(goo());",
912 "function foo(a){return true;};" +
913 "{var a$$inline_0=goo();true}",
914 "foo", INLINE_BLOCK);
915 }
916
917 public void testInline18() {
918 // This doesn't bring names into the global name space.
919 helperInlineReferenceToFunction(
920 "function foo(a){var b;return a;}; " +
921 "function x() { foo(goo()); }",
922 "function foo(a){var b;return a;}; " +
923 "function x() {{var a$$inline_0=goo();" +
924 "var b$$inline_1;a$$inline_0}}",
925 "foo", INLINE_BLOCK);
926 }
927
928 public void testInline19() {
929 // Properly alias.
930 helperInlineReferenceToFunction(
931 "var x = 1; var y = 2;" +
932 "function foo(a,b){x = b; y = a;}; " +
933 "function bar() { foo(x,y); }",
934 "var x = 1; var y = 2;" +
935 "function foo(a,b){x = b; y = a;}; " +
936 "function bar() {" +
937 "{var a$$inline_0=x;" +
938 "x = y;" +
939 "y = a$$inline_0;}" +
940 "}",
941 "foo", INLINE_BLOCK);
942 }
943
944 public void testInline19b() {
945 helperInlineReferenceToFunction(
946 "var x = 1; var y = 2;" +
947 "function foo(a,b){y = a; x = b;}; " +
948 "function bar() { foo(x,y); }",
949 "var x = 1; var y = 2;" +
950 "function foo(a,b){y = a; x = b;}; " +
951 "function bar() {" +
952 "{var b$$inline_1=y;" +
953 "y = x;" +
954 "x = b$$inline_1;}" +
955 "}",
956 "foo", INLINE_BLOCK);
957 }
958
959 public void testInlineIntoLoop() {
960 helperInlineReferenceToFunction(
961 "function foo(a){var b;return a;}; " +
962 "for(;1;){ foo(1); }",
963 "function foo(a){var b;return a;}; " +
964 "for(;1;){ {" +
965 "var b$$inline_1=void 0;1}}",
966 "foo", INLINE_BLOCK);
967
968 helperInlineReferenceToFunction(
969 "function foo(a){var b;return a;}; " +
970 "do{ foo(1); } while(1)",
971 "function foo(a){var b;return a;}; " +
972 "do{ {" +
973 "var b$$inline_1=void 0;1}}while(1)",
974 "foo", INLINE_BLOCK);
975
976 helperInlineReferenceToFunction(
977 "function foo(a){for(var b in c)return a;}; " +
978 "for(;1;){ foo(1); }",
979 "function foo(a){var b;for(b in c)return a;}; " +
980 "for(;1;){ {JSCompiler_inline_label_foo_2:{" +
981 "var b$$inline_1=void 0;for(b$$inline_1 in c){" +
982 "1;break JSCompiler_inline_label_foo_2" +
983 "}}}}",
984 "foo", INLINE_BLOCK);
985 }
986
987 public void testInlineFunctionWithInnerFunction1() {
988 // Call with inner function expression.
989 helperInlineReferenceToFunction(
990 "function foo(){return function() {return true;}}; foo();",
991 "function foo(){return function() {return true;}};" +
992 "(function() {return true;})",
993 "foo", INLINE_DIRECT);
994 }
995
996 public void testInlineFunctionWithInnerFunction2() {
997 // Call with inner function expression.
998 helperInlineReferenceToFunction(
999 "function foo(){return function() {return true;}}; foo();",
1000 "function foo(){return function() {return true;}};" +
1001 "{(function() {return true;})}",
1002 "foo", INLINE_BLOCK);
1003 }
1004
1005 public void testInlineFunctionWithInnerFunction3() {
1006 // Call with inner function expression.
1007 helperInlineReferenceToFunction(
1008 "function foo(){return function() {var a; return true;}}; foo();",
1009 "function foo(){return function() {var a; return true;}};" +
1010 "(function() {var a; return true;});",
1011 "foo", INLINE_DIRECT);
1012 }
1013
1014 public void testInlineFunctionWithInnerFunction4() {
1015 // Call with inner function expression.
1016 helperInlineReferenceToFunction(
1017 "function foo(){return function() {var a; return true;}}; foo();",
1018 "function foo(){return function() {var a; return true;}};" +
1019 "{(function() {var a$$inline_0; return true;});}",
1020 "foo", INLINE_BLOCK);
1021 }
1022
1023 public void testInlineFunctionWithInnerFunction5() {
1024 // Call with inner function statement.
1025 helperInlineReferenceToFunction(
1026 "function foo(){function x() {var a; return true;} return x}; foo();",
1027 "function foo(){function x(){var a;return true}return x};" +
1028 "{var x$$inline_0 = function(){" +
1029 "var a$$inline_1;return true};x$$inline_0}",
1030 "foo", INLINE_BLOCK);
1031 }
1032
1033 public void testInlineReferenceInExpression1() {
1034 // Call in if condition
1035 helperInlineReferenceToFunction(
1036 "function foo(a){return true;}; " +
1037 "function x() { if (foo(1)) throw 'test'; }",
1038 "function foo(a){return true;}; " +
1039 "function x() { var JSCompiler_inline_result$$0; " +
1040 "{JSCompiler_inline_result$$0=true;}" +
1041 "if (JSCompiler_inline_result$$0) throw 'test'; }",
1042 "foo", INLINE_BLOCK, true);
1043 }
1044
1045 public void testInlineReferenceInExpression2() {
1046 // Call in return expression
1047 helperInlineReferenceToFunction(
1048 "function foo(a){return true;}; " +
1049 "function x() { return foo(1); }",
1050 "function foo(a){return true;}; " +
1051 "function x() { var JSCompiler_inline_result$$0; " +
1052 "{JSCompiler_inline_result$$0=true;}" +
1053 "return JSCompiler_inline_result$$0; }",
1054 "foo", INLINE_BLOCK, true);
1055 }
1056
1057 public void testInlineReferenceInExpression3() {
1058 // Call in switch expression
1059 helperInlineReferenceToFunction(
1060 "function foo(a){return true;}; " +
1061 "function x() { switch(foo(1)) { default:break; } }",
1062 "function foo(a){return true;}; " +
1063 "function x() { var JSCompiler_inline_result$$0; " +
1064 "{JSCompiler_inline_result$$0=true;}" +
1065 "switch(JSCompiler_inline_result$$0) { default:break; } }",
1066 "foo", INLINE_BLOCK, true);
1067 }
1068
1069 public void testInlineReferenceInExpression4() {
1070 // Call in hook condition
1071 helperInlineReferenceToFunction(
1072 "function foo(a){return true;}; " +
1073 "function x() {foo(1)?0:1 }",
1074 "function foo(a){return true;}; " +
1075 "function x() { var JSCompiler_inline_result$$0; " +
1076 "{JSCompiler_inline_result$$0=true;}" +
1077 "JSCompiler_inline_result$$0?0:1 }",
1078 "foo", INLINE_BLOCK, true);
1079 }
1080
1081 public void testInlineReferenceInExpression5() {
1082 // Call in expression statement "condition"
1083 helperInlineReferenceToFunction(
1084 "function foo(a){return true;}; " +
1085 "function x() {foo(1)&&1 }",
1086 "function foo(a){return true;}; " +
1087 "function x() { var JSCompiler_inline_result$$0; " +
1088 "{JSCompiler_inline_result$$0=true;}" +
1089 "JSCompiler_inline_result$$0&&1 }",
1090 "foo", INLINE_BLOCK, true);
1091 }
1092
1093 public void testInlineReferenceInExpression6() {
1094 // Call in expression statement after side-effect free "condition"
1095 helperInlineReferenceToFunction(
1096 "function foo(a){return true;}; " +
1097 "function x() {1 + foo(1) }",
1098 "function foo(a){return true;}; " +
1099 "function x() { var JSCompiler_inline_result$$0; " +
1100 "{JSCompiler_inline_result$$0=true;}" +
1101 "1 + JSCompiler_inline_result$$0 }",
1102 "foo", INLINE_BLOCK, true);
1103 }
1104
1105 public void testInlineReferenceInExpression7() {
1106 // Call in expression statement "condition"
1107 helperInlineReferenceToFunction(
1108 "function foo(a){return true;}; " +
1109 "function x() {foo(1) && 1 }",
1110 "function foo(a){return true;}; " +
1111 "function x() { var JSCompiler_inline_result$$0; " +
1112 "{JSCompiler_inline_result$$0=true;}" +
1113 "JSCompiler_inline_result$$0&&1 }",
1114 "foo", INLINE_BLOCK, true);
1115 }
1116
1117 public void testInlineReferenceInExpression8() {
1118 // Call in expression statement after side-effect free operator
1119 helperInlineReferenceToFunction(
1120 "function foo(a){return true;}; " +
1121 "function x() {1 + foo(1) }",
1122 "function foo(a){return true;}; " +
1123 "function x() { var JSCompiler_inline_result$$0;" +
1124 "{JSCompiler_inline_result$$0=true;}" +
1125 "1 + JSCompiler_inline_result$$0 }",
1126 "foo", INLINE_BLOCK, true);
1127 }
1128
1129 public void testInlineReferenceInExpression9() {
1130 // Call in VAR expression.
1131 helperInlineReferenceToFunction(
1132 "function foo(a){return true;}; " +
1133 "function x() {var b = 1 + foo(1)}",
1134 "function foo(a){return true;}; " +
1135 "function x() { " +
1136 "var JSCompiler_inline_result$$0;" +
1137 "{JSCompiler_inline_result$$0=true;}" +
1138 "var b = 1 + JSCompiler_inline_result$$0 " +
1139 "}",
1140 "foo", INLINE_BLOCK, true);
1141 }
1142
1143 // TODO(nicksantos): Re-enable with side-effect detection.
1144 // public void testInlineReferenceInExpression10() {
1145 // // Call in assignment expression.
1146 // helperInlineReferenceToFunction(
1147 // "/** @nosideeffects */ function foo(a){return true;}; " +
1148 // "function x() {var b; b += 1 + foo(1) }",
1149 // "function foo(a){return true;}; " +
1150 // "function x() {var b;" +
1151 // "{var JSCompiler_inline_result$$0; " +
1152 // "JSCompiler_inline_result$$0=true;}" +
1153 // "b += 1 + JSCompiler_inline_result$$0 }",
1154 // "foo", INLINE_BLOCK);
1155 // }
1156
1157 public void testInlineReferenceInExpression11() {
1158 // Call under label
1159 helperInlineReferenceToFunction(
1160 "function foo(a){return true;}; " +
1161 "function x() {a:foo(1)?0:1 }",
1162 "function foo(a){return true;}; " +
1163 "function x() {" +
1164 " a:{" +
1165 " var JSCompiler_inline_result$$0; " +
1166 " {JSCompiler_inline_result$$0=true;}" +
1167 " JSCompiler_inline_result$$0?0:1 " +
1168 " }" +
1169 "}",
1170 "foo", INLINE_BLOCK, true);
1171 }
1172
1173 public void testInlineReferenceInExpression12() {
1174 helperInlineReferenceToFunction(
1175 "function foo(a){return true;}" +
1176 "function x() { 1?foo(1):1; }",
1177 "function foo(a){return true}" +
1178 "function x() {" +
1179 " if(1) {" +
1180 " {true;}" +
1181 " } else {" +
1182 " 1;" +
1183 " }" +
1184 "}",
1185 "foo", INLINE_BLOCK, true);
1186 }
1187
1188 public void testInlineReferenceInExpression13() {
1189 helperInlineReferenceToFunction(
1190 "function foo(a){return true;}; " +
1191 "function x() { goo() + (1?foo(1):1) }",
1192 "function foo(a){return true;}; " +
1193 "function x() { var JSCompiler_temp_const$$0=goo();" +
1194 "var JSCompiler_temp$$1;" +
1195 "if(1) {" +
1196 " {JSCompiler_temp$$1=true;} " +
1197 "} else {" +
1198 " JSCompiler_temp$$1=1;" +
1199 "}" +
1200 "JSCompiler_temp_const$$0 + JSCompiler_temp$$1" +
1201 "}",
1202 "foo", INLINE_BLOCK, true);
1203 }
1204
1205 public void testInlineReferenceInExpression14() {
1206 helperInlineReferenceToFunction(
1207 "var z = {};" +
1208 "function foo(a){z = {};return true;}; " +
1209 "function x() { z.gack = foo(1) }",
1210
1211 "var z = {};" +
1212 "function foo(a){z = {};return true;}; " +
1213 "function x() {" +
1214 "var JSCompiler_temp_const$$0=z;" +
1215 "var JSCompiler_inline_result$$1;" +
1216 "{" +
1217 "z= {};" +
1218 "JSCompiler_inline_result$$1 = true;" +
1219 "}" +
1220 "JSCompiler_temp_const$$0.gack = JSCompiler_inline_result$$1;" +
1221 "}",
1222 "foo", INLINE_BLOCK, true);
1223 }
1224
1225 public void testInlineReferenceInExpression15() {
1226 helperInlineReferenceToFunction(
1227 "var z = {};" +
1228 "function foo(a){z = {};return true;}; " +
1229 "function x() { z.gack = foo.call(this,1) }",
1230
1231 "var z = {};" +
1232 "function foo(a){z = {};return true;}; " +
1233 "function x() {" +
1234 "var JSCompiler_temp_const$$0=z;" +
1235 "var JSCompiler_inline_result$$1;" +
1236 "{" +
1237 "z= {};" +
1238 "JSCompiler_inline_result$$1 = true;" +
1239 "}" +
1240 "JSCompiler_temp_const$$0.gack = JSCompiler_inline_result$$1;" +
1241 "}",
1242 "foo", INLINE_BLOCK, true);
1243 }
1244
1245 public void testInlineReferenceInExpression16() {
1246 helperInlineReferenceToFunction(
1247 "var z = {};" +
1248 "function foo(a){z = {};return true;}; " +
1249 "function x() { z[bar()] = foo(1) }",
1250
1251 "var z = {};" +
1252 "function foo(a){z = {};return true;}; " +
1253 "function x() {" +
1254 "var JSCompiler_temp_const$$1=z;" +
1255 "var JSCompiler_temp_const$$0=bar();" +
1256 "var JSCompiler_inline_result$$2;" +
1257 "{" +
1258 "z= {};" +
1259 "JSCompiler_inline_result$$2 = true;" +
1260 "}" +
1261 "JSCompiler_temp_const$$1[JSCompiler_temp_const$$0] = " +
1262 "JSCompiler_inline_result$$2;" +
1263 "}",
1264 "foo", INLINE_BLOCK, true);
1265 }
1266
1267 public void testInlineReferenceInExpression17() {
1268 helperInlineReferenceToFunction(
1269 "var z = {};" +
1270 "function foo(a){z = {};return true;}; " +
1271 "function x() { z.y.x.gack = foo(1) }",
1272
1273 "var z = {};" +
1274 "function foo(a){z = {};return true;}; " +
1275 "function x() {" +
1276 "var JSCompiler_temp_const$$0=z.y.x;" +
1277 "var JSCompiler_inline_result$$1;" +
1278 "{" +
1279 "z= {};" +
1280 "JSCompiler_inline_result$$1 = true;" +
1281 "}" +
1282 "JSCompiler_temp_const$$0.gack = JSCompiler_inline_result$$1;" +
1283 "}",
1284 "foo", INLINE_BLOCK, true);
1285 }
1286
1287
1288 public void testInlineWithinCalls1() {
1289 // Call in within a call
1290 helperInlineReferenceToFunction(
1291 "function foo(){return _g;}; " +
1292 "function x() {1 + foo()() }",
1293 "function foo(){return _g;}; " +
1294 "function x() { var JSCompiler_inline_result$$0;" +
1295 "{JSCompiler_inline_result$$0=_g;}" +
1296 "1 + JSCompiler_inline_result$$0() }",
1297 "foo", INLINE_BLOCK, true);
1298 }
1299
1300 // TODO(nicksantos): Re-enable with side-effect detection.
1301 // public void testInlineWithinCalls2() {
1302 // helperInlineReferenceToFunction(
1303 // "/** @nosideeffects */ function foo(){return true;}; " +
1304 // "function x() {1 + _g(foo()) }",
1305 // "function foo(){return true;}; " +
1306 // "function x() { {var JSCompiler_inline_result$$0; " +
1307 // "JSCompiler_inline_result$$0=true;}" +
1308 // "1 + _g(JSCompiler_inline_result$$0) }",
1309 // "foo", INLINE_BLOCK, true);
1310 // }
1311
1312 public void testInlineAssignmentToConstant() {
1313 // Call in within a call
1314 helperInlineReferenceToFunction(
1315 "function foo(){return _g;}; " +
1316 "function x(){var CONSTANT_RESULT = foo(); }",
1317
1318 "function foo(){return _g;}; " +
1319 "function x() {" +
1320 " var JSCompiler_inline_result$$0;" +
1321 " {JSCompiler_inline_result$$0=_g;}" +
1322 " var CONSTANT_RESULT = JSCompiler_inline_result$$0;" +
1323 "}",
1324 "foo", INLINE_BLOCK, true);
1325 }
1326
1327 public void testBug1897706() {
1328 helperInlineReferenceToFunction(
1329 "function foo(a){}; foo(x())",
1330 "function foo(a){}; {var a$$inline_0=x()}",
1331 "foo", INLINE_BLOCK);
1332
1333 helperInlineReferenceToFunction(
1334 "function foo(a){bar()}; foo(x())",
1335 "function foo(a){bar()}; {var a$$inline_0=x();bar()}",
1336 "foo", INLINE_BLOCK);
1337
1338 helperInlineReferenceToFunction(
1339 "function foo(a,b){bar()}; foo(x(),y())",
1340 "function foo(a,b){bar()};" +
1341 "{var a$$inline_0=x();var b$$inline_1=y();bar()}",
1342 "foo", INLINE_BLOCK);
1343 }
1344
1345 /**
1346 * Test case
1347 *
1348 * var a = {}, b = {}
1349 * a.test = "a", b.test = "b"
1350 * c = a;
1351 * foo() { c=b; return "a" }
1352 * c.teste
1353 *
1354 */
1355
1356 public void helperCanInlineReferenceToFunction(
1357 final CanInlineResult expectedResult,
1358 final String code,
1359 final String fnName,
1360 final InliningMode mode) {
1361 helperCanInlineReferenceToFunction(
1362 expectedResult, code, fnName, mode, false);
1363 }
1364
1365 public void helperCanInlineReferenceToFunction(
1366 final CanInlineResult expectedResult,
1367 final String code,
1368 final String fnName,
1369 final InliningMode mode,
1370 boolean allowDecomposition) {
1371 final Compiler compiler = new Compiler();
1372 final FunctionInjector injector = new FunctionInjector(
1373 compiler, compiler.getUniqueNameIdSupplier(), allowDecomposition,
1374 assumeStrictThis,
1375 assumeMinimumCapture);
1376 final Node tree = parse(compiler, code);
1377
1378 final Node fnNode = findFunction(tree, fnName);
1379 final Set<String> unsafe =
1380 FunctionArgumentInjector.findModifiedParameters(fnNode);
1381
1382 // can-inline tester
1383 Method tester = new Method() {
1384 @Override
1385 public boolean call(NodeTraversal t, Node n, Node parent) {
1386 CanInlineResult result = injector.canInlineReferenceToFunction(
1387 t, n, fnNode, unsafe, mode,
1388 NodeUtil.referencesThis(fnNode),
1389 NodeUtil.containsFunction(NodeUtil.getFunctionBody(fnNode)));
1390 assertEquals(expectedResult, result);
1391 return true;
1392 }
1393 };
1394
1395 compiler.resetUniqueNameId();
1396 TestCallback test = new TestCallback(fnName, tester);
1397 NodeTraversal.traverse(compiler, tree, test);
1398 }
1399
1400 public void helperInlineReferenceToFunction(
1401 String code, final String expectedResult,
1402 final String fnName, final InliningMode mode) {
1403 helperInlineReferenceToFunction(
1404 code, expectedResult, fnName, mode, false);
1405 }
1406
1407 private void validateSourceInfo(Compiler compiler, Node subtree) {
1408 (new LineNumberCheck(compiler)).setCheckSubTree(subtree);
1409 // Source information problems are reported as compiler errors.
1410 if (compiler.getErrorCount() != 0) {
1411 String msg = "Error encountered: ";
1412 for (JSError err : compiler.getErrors()) {
1413 msg += err.toString() + "\n";
1414 }
1415 assertTrue(msg, compiler.getErrorCount() == 0);
1416 }
1417 }
1418
1419 public void helperInlineReferenceToFunction(
1420 String code, final String expectedResult,
1421 final String fnName, final InliningMode mode,
1422 final boolean decompose) {
1423 final Compiler compiler = new Compiler();
1424 final FunctionInjector injector = new FunctionInjector(
1425 compiler, compiler.getUniqueNameIdSupplier(), decompose,
1426 assumeStrictThis,
1427 assumeMinimumCapture);
1428
1429 List<SourceFile> externsInputs = Lists.newArrayList(
1430 SourceFile.fromCode("externs", ""));
1431
1432 CompilerOptions options = new CompilerOptions();
1433 options.setCodingConvention(new GoogleCodingConvention());
1434 compiler.init(externsInputs, Lists.newArrayList(
1435 SourceFile.fromCode("code", code)), options);
1436 Node parseRoot = compiler.parseInputs();
1437 Node externsRoot = parseRoot.getFirstChild();
1438 final Node tree = parseRoot.getLastChild();
1439 assertNotNull(tree);
1440 assertTrue(tree != externsRoot);
1441
1442 final Node expectedRoot = parseExpected(new Compiler(), expectedResult);
1443
1444 Node mainRoot = tree;
1445 MarkNoSideEffectCalls mark = new MarkNoSideEffectCalls(compiler);
1446 mark.process(externsRoot, mainRoot);
1447
1448 Normalize normalize = new Normalize(compiler, false);
1449 normalize.process(externsRoot, mainRoot);
1450 compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED);
1451
1452 final Node fnNode = findFunction(tree, fnName);
1453 assertNotNull(fnNode);
1454 final Set<String> unsafe =
1455 FunctionArgumentInjector.findModifiedParameters(fnNode);
1456 assertNotNull(fnNode);
1457
1458 // inline tester
1459 Method tester = new Method() {
1460 @Override
1461 public boolean call(NodeTraversal t, Node n, Node parent) {
1462
1463 CanInlineResult canInline = injector.canInlineReferenceToFunction(
1464 t, n, fnNode, unsafe, mode,
1465 NodeUtil.referencesThis(fnNode),
1466 NodeUtil.containsFunction(NodeUtil.getFunctionBody(fnNode)));
1467 assertTrue("canInlineReferenceToFunction should not be CAN_NOT_INLINE",
1468 CanInlineResult.NO != canInline);
1469 if (decompose) {
1470 assertTrue("canInlineReferenceToFunction " +
1471 "should be CAN_INLINE_AFTER_DECOMPOSITION",
1472 CanInlineResult.AFTER_PREPARATION == canInline);
1473
1474 Set<String> knownConstants = Sets.newHashSet();
1475 injector.setKnownConstants(knownConstants);
1476 injector.maybePrepareCall(n);
1477
1478 assertTrue("canInlineReferenceToFunction " +
1479 "should be CAN_INLINE",
1480 CanInlineResult.YES != canInline);
1481 }
1482
1483 Node result = injector.inline(n, fnName, fnNode, mode);
1484 validateSourceInfo(compiler, result);
1485 String explanation = expectedRoot.checkTreeEquals(tree.getFirstChild());
1486 assertNull("\nExpected: " + toSource(expectedRoot) +
1487 "\nResult: " + toSource(tree.getFirstChild()) +
1488 "\n" + explanation, explanation);
1489 return true;
1490 }
1491 };
1492
1493 compiler.resetUniqueNameId();
1494 TestCallback test = new TestCallback(fnName, tester);
1495 NodeTraversal.traverse(compiler, tree, test);
1496 }
1497
1498 interface Method {
1499 boolean call(NodeTraversal t, Node n, Node parent);
1500 }
1501
1502 class TestCallback implements Callback {
1503
1504 private final String callname;
1505 private final Method method;
1506 private boolean complete = false;
1507
1508 TestCallback(String callname, Method method) {
1509 this.callname = callname;
1510 this.method = method;
1511 }
1512
1513 @Override
1514 public boolean shouldTraverse(
1515 NodeTraversal nodeTraversal, Node n, Node parent) {
1516 return !complete;
1517 }
1518
1519 @Override
1520 public void visit(NodeTraversal t, Node n, Node parent) {
1521 if (n.isCall()) {
1522 Node callee;
1523 if (NodeUtil.isGet(n.getFirstChild())) {
1524 callee = n.getFirstChild().getFirstChild();
1525 } else {
1526 callee = n.getFirstChild();
1527 }
1528
1529 if (callee.isName() &&
1530 callee.getString().equals(callname)) {
1531 complete = method.call(t, n, parent);
1532 }
1533 }
1534
1535 if (parent == null) {
1536 assertTrue(complete);
1537 }
1538 }
1539 }
1540
1541 private static Node findFunction(Node n, String name) {
1542 if (n.isFunction()) {
1543 if (n.getFirstChild().getString().equals(name)) {
1544 return n;
1545 }
1546 }
1547
1548 for (Node c : n.children()) {
1549 Node result = findFunction(c, name);
1550 if (result != null) {
1551 return result;
1552 }
1553 }
1554
1555 return null;
1556 }
1557
1558 private static Node prep(String js) {
1559 Compiler compiler = new Compiler();
1560 Node n = compiler.parseTestCode(js);
1561 assertEquals(0, compiler.getErrorCount());
1562 return n.getFirstChild();
1563 }
1564
1565 private static Node parse(Compiler compiler, String js) {
1566 Node n = compiler.parseTestCode(js);
1567 assertEquals(0, compiler.getErrorCount());
1568 return n;
1569 }
1570
1571 private static Node parseExpected(Compiler compiler, String js) {
1572 Node n = compiler.parseTestCode(js);
1573 String message = "Unexpected errors: ";
1574 JSError[] errs = compiler.getErrors();
1575 for (int i = 0; i < errs.length; i++){
1576 message += "\n" + errs[i].toString();
1577 }
1578 assertEquals(message, 0, compiler.getErrorCount());
1579 return n;
1580 }
1581
1582 private static String toSource(Node n) {
1583 return new CodePrinter.Builder(n)
1584 .setPrettyPrint(false)
1585 .setLineBreak(false)
1586 .setSourceMap(null)
1587 .build();
1588 }
1589
1590 }