| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.Lists; |
| 20 |
import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback; |
| 21 |
import com.google.javascript.rhino.IR; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
import java.util.LinkedList; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
class ExtractPrototypeMemberDeclarations implements CompilerPass { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private String prototypeAlias = "JSCompiler_prototypeAlias"; |
| 90 |
|
| 91 |
private final AbstractCompiler compiler; |
| 92 |
|
| 93 |
private final Pattern pattern; |
| 94 |
|
| 95 |
enum Pattern { |
| 96 |
USE_GLOBAL_TEMP( |
| 97 |
|
| 98 |
|
| 99 |
"var t;".length(), |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
"t=y.prototype;".length(), |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
"t.y=".length() - "x[p].y=".length()), |
| 108 |
|
| 109 |
USE_ANON_FUNCTION( |
| 110 |
|
| 111 |
0, |
| 112 |
|
| 113 |
|
| 114 |
"(function(t){})(y.prototype);".length(), |
| 115 |
|
| 116 |
|
| 117 |
"t.y=".length() - "x.prototype.y=".length()); |
| 118 |
|
| 119 |
|
| 120 |
private final int globalOverhead; |
| 121 |
private final int perExtractionOverhead; |
| 122 |
private final int perMemberOverhead; |
| 123 |
|
| 124 |
Pattern(int globalOverHead, int perExtractionOverhead, int perMemberOverhead) { |
| 125 |
this.globalOverhead = globalOverHead; |
| 126 |
this.perExtractionOverhead = perExtractionOverhead; |
| 127 |
this.perMemberOverhead = perMemberOverhead; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
ExtractPrototypeMemberDeclarations(AbstractCompiler compiler, Pattern pattern) { |
| 132 |
this.compiler = compiler; |
| 133 |
this.pattern = pattern; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void process(Node externs, Node root) { |
| 138 |
GatherExtractionInfo extractionInfo = new GatherExtractionInfo(); |
| 139 |
NodeTraversal.traverse(compiler, root, extractionInfo); |
| 140 |
if (extractionInfo.shouldExtract()) { |
| 141 |
doExtraction(extractionInfo); |
| 142 |
compiler.reportCodeChange(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private void doExtraction(GatherExtractionInfo info) { |
| 151 |
|
| 152 |
|
| 153 |
if (pattern == Pattern.USE_GLOBAL_TEMP) { |
| 154 |
Node injectionPoint = compiler.getNodeForCodeInsertion(null); |
| 155 |
|
| 156 |
Node var = NodeUtil.newVarNode(prototypeAlias, null) |
| 157 |
.copyInformationFromForTree(injectionPoint); |
| 158 |
|
| 159 |
injectionPoint.addChildrenToFront(var); |
| 160 |
} |
| 161 |
|
| 162 |
for (ExtractionInstance instance : info.instances) { |
| 163 |
extractInstance(instance); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private void extractInstance(ExtractionInstance instance) { |
| 173 |
PrototypeMemberDeclaration first = instance.declarations.getFirst(); |
| 174 |
String className = first.qualifiedClassName; |
| 175 |
if (pattern == Pattern.USE_GLOBAL_TEMP) { |
| 176 |
|
| 177 |
Node stmt = new Node(first.node.getType(), |
| 178 |
IR.assign( |
| 179 |
IR.name(prototypeAlias), |
| 180 |
NodeUtil.newQualifiedNameNode( |
| 181 |
compiler.getCodingConvention(), className + ".prototype", |
| 182 |
instance.parent, className + ".prototype"))) |
| 183 |
.copyInformationFromForTree(first.node); |
| 184 |
|
| 185 |
instance.parent.addChildBefore(stmt, first.node); |
| 186 |
} else if (pattern == Pattern.USE_ANON_FUNCTION){ |
| 187 |
Node block = IR.block(); |
| 188 |
Node func = IR.function( |
| 189 |
IR.name(""), |
| 190 |
IR.paramList(IR.name(prototypeAlias)), |
| 191 |
block); |
| 192 |
|
| 193 |
Node call = IR.call(func, |
| 194 |
NodeUtil.newQualifiedNameNode( |
| 195 |
compiler.getCodingConvention(), className + ".prototype", |
| 196 |
instance.parent, className + ".prototype")); |
| 197 |
call.putIntProp(Node.FREE_CALL, 1); |
| 198 |
|
| 199 |
Node stmt = new Node(first.node.getType(), call); |
| 200 |
stmt.copyInformationFromForTree(first.node); |
| 201 |
instance.parent.addChildBefore(stmt, first.node); |
| 202 |
for (PrototypeMemberDeclaration declar : instance.declarations) { |
| 203 |
block.addChildToBack(declar.node.detachFromParent()); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
for (PrototypeMemberDeclaration declar : instance.declarations) { |
| 209 |
replacePrototypeMemberDeclaration(declar); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private void replacePrototypeMemberDeclaration( |
| 218 |
PrototypeMemberDeclaration declar) { |
| 219 |
|
| 220 |
Node assignment = declar.node.getFirstChild(); |
| 221 |
Node lhs = assignment.getFirstChild(); |
| 222 |
Node name = NodeUtil.newQualifiedNameNode( |
| 223 |
compiler.getCodingConvention(), |
| 224 |
prototypeAlias + "." + declar.memberName, declar.node, |
| 225 |
declar.memberName); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
Node accessNode = declar.lhs.getFirstChild().getFirstChild(); |
| 232 |
Object originalName = accessNode.getProp(Node.ORIGINALNAME_PROP); |
| 233 |
|
| 234 |
String className = "?"; |
| 235 |
|
| 236 |
if (originalName != null) { |
| 237 |
className = originalName.toString(); |
| 238 |
} |
| 239 |
|
| 240 |
NodeUtil.setDebugInformation(name.getFirstChild(), lhs, |
| 241 |
className + ".prototype"); |
| 242 |
|
| 243 |
assignment.replaceChild(lhs, name); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
private class GatherExtractionInfo extends AbstractShallowCallback { |
| 250 |
|
| 251 |
private List<ExtractionInstance> instances = Lists.newLinkedList(); |
| 252 |
private int totalDelta = pattern.globalOverhead; |
| 253 |
|
| 254 |
@Override |
| 255 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 256 |
|
| 257 |
if (!n.isScript() && !n.isBlock()) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
for (Node cur = n.getFirstChild(); cur != null; cur = cur.getNext()) { |
| 262 |
PrototypeMemberDeclaration prototypeMember = |
| 263 |
PrototypeMemberDeclaration.extractDeclaration(cur); |
| 264 |
if (prototypeMember == null) { |
| 265 |
continue; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
ExtractionInstance instance = |
| 271 |
new ExtractionInstance(prototypeMember, n); |
| 272 |
cur = instance.declarations.getLast().node; |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if (instance.isFavorable()) { |
| 277 |
instances.add(instance); |
| 278 |
totalDelta += instance.delta; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
private boolean shouldExtract() { |
| 288 |
return totalDelta < 0; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
private class ExtractionInstance { |
| 293 |
LinkedList<PrototypeMemberDeclaration> declarations = Lists.newLinkedList(); |
| 294 |
private int delta = 0; |
| 295 |
private final Node parent; |
| 296 |
|
| 297 |
private ExtractionInstance(PrototypeMemberDeclaration head, Node parent) { |
| 298 |
this.parent = parent; |
| 299 |
declarations.add(head); |
| 300 |
delta = pattern.perExtractionOverhead + pattern.perMemberOverhead; |
| 301 |
|
| 302 |
for (Node cur = head.node.getNext(); cur != null; cur = cur.getNext()) { |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if (cur.isFunction()) { |
| 309 |
continue; |
| 310 |
} |
| 311 |
|
| 312 |
PrototypeMemberDeclaration prototypeMember = |
| 313 |
PrototypeMemberDeclaration.extractDeclaration(cur); |
| 314 |
if (prototypeMember == null || !head.isSameClass(prototypeMember)) { |
| 315 |
break; |
| 316 |
} |
| 317 |
declarations.add(prototypeMember); |
| 318 |
delta += pattern.perMemberOverhead; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
boolean isFavorable() { |
| 327 |
return delta <= 0; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
private static class PrototypeMemberDeclaration { |
| 337 |
final String memberName; |
| 338 |
final Node node; |
| 339 |
final String qualifiedClassName; |
| 340 |
final Node lhs; |
| 341 |
|
| 342 |
private PrototypeMemberDeclaration(Node lhs, Node node) { |
| 343 |
this.lhs = lhs; |
| 344 |
this.memberName = NodeUtil.getPrototypePropertyName(lhs); |
| 345 |
this.node = node; |
| 346 |
this.qualifiedClassName = |
| 347 |
NodeUtil.getPrototypeClassName(lhs).getQualifiedName(); |
| 348 |
} |
| 349 |
|
| 350 |
private boolean isSameClass(PrototypeMemberDeclaration other) { |
| 351 |
return qualifiedClassName.equals(other.qualifiedClassName); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
private static PrototypeMemberDeclaration extractDeclaration(Node n) { |
| 359 |
if (!NodeUtil.isPrototypePropertyDeclaration(n)) { |
| 360 |
return null; |
| 361 |
} |
| 362 |
Node lhs = n.getFirstChild().getFirstChild(); |
| 363 |
return new PrototypeMemberDeclaration(lhs, n); |
| 364 |
} |
| 365 |
} |
| 366 |
} |