| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 19 |
import com.google.javascript.rhino.Node; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class SimpleFunctionAliasAnalysisTest extends CompilerTestCase { |
| 27 |
|
| 28 |
private SimpleFunctionAliasAnalysis analysis; |
| 29 |
|
| 30 |
private Compiler lastCompiler; |
| 31 |
|
| 32 |
@Override |
| 33 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 34 |
return new CompilerPass() { |
| 35 |
|
| 36 |
@Override |
| 37 |
public void process(Node externs, Node root) { |
| 38 |
SimpleDefinitionFinder finder = new SimpleDefinitionFinder(compiler); |
| 39 |
finder.process(externs, root); |
| 40 |
|
| 41 |
analysis = new SimpleFunctionAliasAnalysis(); |
| 42 |
|
| 43 |
analysis.analyze(finder); |
| 44 |
|
| 45 |
lastCompiler = compiler; |
| 46 |
} |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
public void testFunctionGetIsAliased() { |
| 51 |
|
| 52 |
String source = |
| 53 |
"function A(){};\n" + |
| 54 |
"var ns = {};\n" + |
| 55 |
"ns.B = function() {};\n" + |
| 56 |
"var C = function() {}\n" + |
| 57 |
"var D = function() {}\n" + |
| 58 |
"var aliasA = A;\n" + |
| 59 |
"var aliasB = ns.B;\n" + |
| 60 |
"var aliasC = C;\n" + |
| 61 |
"D();"; |
| 62 |
|
| 63 |
compileAndRun(source); |
| 64 |
|
| 65 |
assertFunctionAliased(true, "A"); |
| 66 |
assertFunctionAliased(true, "ns.B"); |
| 67 |
assertFunctionAliased(true, "C"); |
| 68 |
assertFunctionAliased(false, "D"); |
| 69 |
|
| 70 |
|
| 71 |
source = |
| 72 |
"function A(){};\n" + |
| 73 |
"var ns = {};\n" + |
| 74 |
"ns.B = function() {};\n" + |
| 75 |
"var C = function() {}\n" + |
| 76 |
"ns.D = function() {}\n" + |
| 77 |
"var aliasA;\n" + |
| 78 |
"aliasA = A;\n" + |
| 79 |
"var aliasB = {};\n" + |
| 80 |
"aliasB.foo = ns.B;\n" + |
| 81 |
"var aliasC;\n" + |
| 82 |
"aliasC = C;\n" + |
| 83 |
"ns.D();"; |
| 84 |
|
| 85 |
compileAndRun(source); |
| 86 |
|
| 87 |
assertFunctionAliased(true, "A"); |
| 88 |
assertFunctionAliased(true, "ns.B"); |
| 89 |
assertFunctionAliased(true, "C"); |
| 90 |
assertFunctionAliased(false, "ns.D"); |
| 91 |
|
| 92 |
|
| 93 |
source = |
| 94 |
"function A(){};\n" + |
| 95 |
"var ns = {};\n" + |
| 96 |
"ns.B = function() {};\n" + |
| 97 |
"var C = function() {}\n" + |
| 98 |
"function D() {}\n" + |
| 99 |
"var foo = function(a) {}\n" + |
| 100 |
"foo(A);\n" + |
| 101 |
"foo(ns.B)\n" + |
| 102 |
"foo(C);\n" + |
| 103 |
"D();"; |
| 104 |
|
| 105 |
compileAndRun(source); |
| 106 |
|
| 107 |
assertFunctionAliased(true, "A"); |
| 108 |
assertFunctionAliased(true, "ns.B"); |
| 109 |
assertFunctionAliased(true, "C"); |
| 110 |
assertFunctionAliased(false, "D"); |
| 111 |
|
| 112 |
|
| 113 |
source = |
| 114 |
"function A(){};\n" + |
| 115 |
"var ns = {};\n" + |
| 116 |
"ns.B = function() {};\n" + |
| 117 |
"var C = function() {}\n" + |
| 118 |
"A();\n" + |
| 119 |
"ns.B();\n" + |
| 120 |
"C();\n"; |
| 121 |
|
| 122 |
compileAndRun(source); |
| 123 |
|
| 124 |
assertFunctionAliased(false, "A"); |
| 125 |
assertFunctionAliased(false, "ns.B"); |
| 126 |
assertFunctionAliased(false, "C"); |
| 127 |
|
| 128 |
|
| 129 |
source = |
| 130 |
"function A(){};\n" + |
| 131 |
"var ns = {};\n" + |
| 132 |
"ns.B = function() {};\n" + |
| 133 |
"var C = function() {}\n" + |
| 134 |
"A.foo;\n" + |
| 135 |
"ns.B.prototype;\n" + |
| 136 |
"C[0];\n"; |
| 137 |
|
| 138 |
compileAndRun(source); |
| 139 |
|
| 140 |
assertFunctionAliased(false, "A"); |
| 141 |
assertFunctionAliased(false, "ns.B"); |
| 142 |
assertFunctionAliased(false, "C"); |
| 143 |
} |
| 144 |
|
| 145 |
public void testFunctionGetIsExposedToCallOrApply() { |
| 146 |
|
| 147 |
String source = |
| 148 |
"function A(){};\n" + |
| 149 |
"function B(){};\n" + |
| 150 |
"function C(){};\n" + |
| 151 |
"var x;\n" + |
| 152 |
"A.call(x);\n" + |
| 153 |
"B.apply(x);\n" + |
| 154 |
"C();\n"; |
| 155 |
|
| 156 |
compileAndRun(source); |
| 157 |
|
| 158 |
assertFunctionExposedToCallOrApply(true, "A"); |
| 159 |
assertFunctionExposedToCallOrApply(true, "B"); |
| 160 |
assertFunctionExposedToCallOrApply(false, "C"); |
| 161 |
|
| 162 |
source = |
| 163 |
"var ns = {};" + |
| 164 |
"ns.A = function(){};\n" + |
| 165 |
"ns.B = function(){};\n" + |
| 166 |
"ns.C = function(){};\n" + |
| 167 |
"var x;\n" + |
| 168 |
"ns.A.call(x);\n" + |
| 169 |
"ns.B.apply(x);\n" + |
| 170 |
"ns.C();\n"; |
| 171 |
|
| 172 |
compileAndRun(source); |
| 173 |
|
| 174 |
assertFunctionExposedToCallOrApply(true, "ns.A"); |
| 175 |
assertFunctionExposedToCallOrApply(true, "ns.B"); |
| 176 |
assertFunctionExposedToCallOrApply(false, "ns.C"); |
| 177 |
} |
| 178 |
|
| 179 |
private void assertFunctionAliased(boolean aliasStatus, |
| 180 |
String functionName) { |
| 181 |
Node function = findFunction(functionName); |
| 182 |
|
| 183 |
assertEquals(aliasStatus, analysis.isAliased(function)); |
| 184 |
} |
| 185 |
|
| 186 |
private void assertFunctionExposedToCallOrApply(boolean exposure, |
| 187 |
String functionName) { |
| 188 |
Node function = findFunction(functionName); |
| 189 |
|
| 190 |
assertEquals(exposure, analysis.isExposedToCallOrApply(function)); |
| 191 |
} |
| 192 |
|
| 193 |
private void compileAndRun(String source) { |
| 194 |
testSame(source, source, null); |
| 195 |
} |
| 196 |
|
| 197 |
private Node findFunction(String name) { |
| 198 |
FunctionFinder f = new FunctionFinder(name); |
| 199 |
new NodeTraversal(lastCompiler, f).traverse(lastCompiler.jsRoot); |
| 200 |
assertNotNull("Couldn't find " + name, f.found); |
| 201 |
return f.found; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
private class FunctionFinder extends AbstractPostOrderCallback { |
| 208 |
Node found = null; |
| 209 |
final String target; |
| 210 |
|
| 211 |
FunctionFinder(String target) { |
| 212 |
this.target = target; |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 217 |
if (n.isFunction() |
| 218 |
&& target.equals(NodeUtil.getFunctionName(n))) { |
| 219 |
found = n; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |