| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.mozilla.javascript.optimizer; |
| 7 |
|
| 8 |
import org.mozilla.javascript.*; |
| 9 |
import org.mozilla.javascript.ast.ScriptNode; |
| 10 |
import java.util.Map; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class OptTransformer extends NodeTransformer { |
| 20 |
|
| 21 |
OptTransformer(Map<String,OptFunctionNode> possibleDirectCalls, ObjArray directCallTargets) |
| 22 |
{ |
| 23 |
this.possibleDirectCalls = possibleDirectCalls; |
| 24 |
this.directCallTargets = directCallTargets; |
| 25 |
} |
| 26 |
|
| 27 |
@Override |
| 28 |
protected void visitNew(Node node, ScriptNode tree) { |
| 29 |
detectDirectCall(node, tree); |
| 30 |
super.visitNew(node, tree); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
protected void visitCall(Node node, ScriptNode tree) { |
| 35 |
detectDirectCall(node, tree); |
| 36 |
super.visitCall(node, tree); |
| 37 |
} |
| 38 |
|
| 39 |
private void detectDirectCall(Node node, ScriptNode tree) |
| 40 |
{ |
| 41 |
if (tree.getType() == Token.FUNCTION) { |
| 42 |
Node left = node.getFirstChild(); |
| 43 |
|
| 44 |
|
| 45 |
int argCount = 0; |
| 46 |
Node arg = left.getNext(); |
| 47 |
while (arg != null) { |
| 48 |
arg = arg.getNext(); |
| 49 |
argCount++; |
| 50 |
} |
| 51 |
|
| 52 |
if (argCount == 0) { |
| 53 |
OptFunctionNode.get(tree).itsContainsCalls0 = true; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (possibleDirectCalls != null) { |
| 69 |
String targetName = null; |
| 70 |
if (left.getType() == Token.NAME) { |
| 71 |
targetName = left.getString(); |
| 72 |
} else if (left.getType() == Token.GETPROP) { |
| 73 |
targetName = left.getFirstChild().getNext().getString(); |
| 74 |
} else if (left.getType() == Token.GETPROPNOWARN) { |
| 75 |
throw Kit.codeBug(); |
| 76 |
} |
| 77 |
if (targetName != null) { |
| 78 |
OptFunctionNode ofn; |
| 79 |
ofn = possibleDirectCalls.get(targetName); |
| 80 |
if (ofn != null |
| 81 |
&& argCount == ofn.fnode.getParamCount() |
| 82 |
&& !ofn.fnode.requiresActivation()) |
| 83 |
{ |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if (argCount <= 32) { |
| 88 |
node.putProp(Node.DIRECTCALL_PROP, ofn); |
| 89 |
if (!ofn.isTargetOfDirectCall()) { |
| 90 |
int index = directCallTargets.size(); |
| 91 |
directCallTargets.add(ofn); |
| 92 |
ofn.setDirectTargetIndex(index); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
private Map<String,OptFunctionNode> possibleDirectCalls; |
| 102 |
private ObjArray directCallTargets; |
| 103 |
} |