| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class OptimizeArgumentsArrayTest extends CompilerTestCase { |
| 24 |
|
| 25 |
public OptimizeArgumentsArrayTest() { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
super("var arguments, alert" ); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void setUp() { |
| 38 |
super.enableLineNumberCheck(false); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 43 |
return new OptimizeArgumentsArray(compiler, "p"); |
| 44 |
} |
| 45 |
|
| 46 |
public void testSimple() { |
| 47 |
test("function foo() { alert(arguments[0]); }", |
| 48 |
"function foo(p0) { alert(p0); }"); |
| 49 |
} |
| 50 |
|
| 51 |
public void testNoVarArgs() { |
| 52 |
testSame("function f(a,b,c) { alert(a + b + c) }"); |
| 53 |
|
| 54 |
test("function f(a,b,c) { alert(arguments[0]) }", |
| 55 |
"function f(a,b,c) { alert(a) }"); |
| 56 |
} |
| 57 |
|
| 58 |
public void testMissingVarArgs() { |
| 59 |
testSame("function f() { alert(arguments[x]) }"); |
| 60 |
} |
| 61 |
|
| 62 |
public void testArgumentRefOnNamedParameter() { |
| 63 |
test("function f(a,b) { alert(arguments[0]) }", |
| 64 |
"function f(a,b) { alert(a) }"); |
| 65 |
} |
| 66 |
|
| 67 |
public void testTwoVarArgs() { |
| 68 |
test("function foo(a) { alert(arguments[1] + arguments[2]); }", |
| 69 |
"function foo(a, p0, p1) { alert(p0 + p1); }"); |
| 70 |
} |
| 71 |
|
| 72 |
public void testTwoFourArgsTwoUsed() { |
| 73 |
test("function foo() { alert(arguments[0] + arguments[3]); }", |
| 74 |
"function foo(p0, p1, p2, p3) { alert(p0 + p3); }"); |
| 75 |
} |
| 76 |
|
| 77 |
public void testOneRequired() { |
| 78 |
test("function foo(req0, var_args) { alert(req0 + arguments[1]); }", |
| 79 |
"function foo(req0, var_args) { alert(req0 + var_args); }"); |
| 80 |
} |
| 81 |
|
| 82 |
public void testTwoRequiredSixthVarArgReferenced() { |
| 83 |
test("function foo(r0, r1, var_args) {alert(r0 + r1 + arguments[5]);}", |
| 84 |
"function foo(r0, r1, var_args, p0, p1, p2) { alert(r0 + r1 + p2); }"); |
| 85 |
} |
| 86 |
|
| 87 |
public void testTwoRequiredOneOptionalFifthVarArgReferenced() { |
| 88 |
test("function foo(r0, r1, opt_1)" |
| 89 |
+ " {alert(r0 + r1 + opt_1 + arguments[4]);}", |
| 90 |
"function foo(r0, r1, opt_1, p0, p1)" |
| 91 |
+ " {alert(r0 + r1 + opt_1 + p1); }"); |
| 92 |
} |
| 93 |
|
| 94 |
public void testTwoRequiredTwoOptionalSixthVarArgReferenced() { |
| 95 |
test("function foo(r0, r1, opt_1, opt_2)" |
| 96 |
+ " {alert(r0 + r1 + opt_1 + opt_2 + arguments[5]);}", |
| 97 |
"function foo(r0, r1, opt_1, opt_2, p0, p1)" |
| 98 |
+ " {alert(r0 + r1 + opt_1 + opt_2 + p1); }"); |
| 99 |
} |
| 100 |
|
| 101 |
public void testInnerFunctions() { |
| 102 |
test("function f() { function b( ) { arguments[0] }}", |
| 103 |
"function f() { function b(p0) { p0 }}"); |
| 104 |
|
| 105 |
test("function f( ) { function b() { } arguments[0] }", |
| 106 |
"function f(p0) { function b() { } p0 }"); |
| 107 |
|
| 108 |
test("function f( ) { arguments[0]; function b( ) { arguments[0] }}", |
| 109 |
"function f(p1) { p1; function b(p0) { p0 }}"); |
| 110 |
} |
| 111 |
|
| 112 |
public void testInnerFunctionsWithNamedArgumentInInnerFunction() { |
| 113 |
test("function f() { function b(x ) { arguments[1] }}", |
| 114 |
"function f() { function b(x,p0) { p0 }}"); |
| 115 |
|
| 116 |
test("function f( ) { function b(x) { } arguments[0] }", |
| 117 |
"function f(p0) { function b(x) { } p0 }"); |
| 118 |
|
| 119 |
test("function f( ) { arguments[0]; function b(x ) { arguments[1] }}", |
| 120 |
"function f(p1) { p1; function b(x,p0) { p0 }}"); |
| 121 |
} |
| 122 |
|
| 123 |
public void testInnerFunctionsWithNamedArgumentInOutterFunction() { |
| 124 |
test("function f(x) { function b( ) { arguments[0] }}", |
| 125 |
"function f(x) { function b(p0) { p0 }}"); |
| 126 |
|
| 127 |
test("function f(x ) { function b() { } arguments[1] }", |
| 128 |
"function f(x,p0) { function b() { } p0 }"); |
| 129 |
|
| 130 |
test("function f(x ) { arguments[1]; function b( ) { arguments[0] }}", |
| 131 |
"function f(x,p1) { p1; function b(p0) { p0 }}"); |
| 132 |
} |
| 133 |
|
| 134 |
public void testInnerFunctionsWithNamedArgumentInInnerAndOutterFunction() { |
| 135 |
test("function f(x) { function b(x ) { arguments[1] }}", |
| 136 |
"function f(x) { function b(x,p0) { p0 }}"); |
| 137 |
|
| 138 |
test("function f(x ) { function b(x) { } arguments[1] }", |
| 139 |
"function f(x,p0) { function b(x) { } p0 }"); |
| 140 |
|
| 141 |
test("function f(x ) { arguments[1]; function b(x ) { arguments[1] }}", |
| 142 |
"function f(x,p1) { p1; function b(x,p0) { p0 }}"); |
| 143 |
} |
| 144 |
|
| 145 |
public void testInnerFunctionsAfterArguments() { |
| 146 |
|
| 147 |
|
| 148 |
test("function f( ) { arguments[0]; function b() { function c() { }} }", |
| 149 |
"function f(p0) { p0; function b() { function c() { }} }"); |
| 150 |
} |
| 151 |
|
| 152 |
public void testNoOptimizationWhenGetProp() { |
| 153 |
testSame("function f() { arguments[0]; arguments.size }"); |
| 154 |
} |
| 155 |
|
| 156 |
public void testNoOptimizationWhenIndexIsNotNumberConstant() { |
| 157 |
testSame("function f() { arguments[0]; arguments['callee'].length}"); |
| 158 |
testSame("function f() { arguments[0]; arguments.callee.length}"); |
| 159 |
testSame( |
| 160 |
"function f() { arguments[0]; var x = 'callee'; arguments[x].length}"); |
| 161 |
} |
| 162 |
|
| 163 |
public void testNoOptimizationWhenArgumentIsUsedAsFunctionCall() { |
| 164 |
testSame("function f() {arguments[0]()}"); |
| 165 |
} |
| 166 |
} |