class OptimizeArgumentsArray extends java.lang.Object implements CompilerPass, NodeTraversal.ScopedCallback
var_args
or access the
arguments array.
Example:
function() { alert(arguments[0] + argument[1]) }to:
function(a, b) { alert(a, b) }Each newly inserted variable name will be unique very much like the output of the AST found after the
Normalize
pass.Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
ARGUMENTS |
private java.util.Deque<java.util.List<Node>> |
argumentsAccessStack |
private AbstractCompiler |
compiler |
private java.util.List<Node> |
currentArgumentsAccess |
private static java.lang.String |
PARAMETER_PREFIX |
private java.lang.String |
paramPredix |
private int |
uniqueId |
Constructor and Description |
---|
OptimizeArgumentsArray(AbstractCompiler compiler)
Construct this pass and use
PARAMETER_PREFIX as the prefix for
all parameter names that it introduces. |
OptimizeArgumentsArray(AbstractCompiler compiler,
java.lang.String paramPrefix) |
Modifier and Type | Method and Description |
---|---|
void |
enterScope(NodeTraversal traversal)
Called immediately after entering a new scope.
|
void |
exitScope(NodeTraversal traversal)
Called immediately before exiting a scope.
|
private java.lang.String |
getNewName()
Generate a unique name for the next parameter.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
boolean |
shouldTraverse(NodeTraversal nodeTraversal,
Node node,
Node parent)
Visits a node in pre order (before visiting its children) and decides
whether this node's children should be traversed.
|
private boolean |
tryReplaceArguments(Scope scope)
Tries to optimize all the arguments array access in this scope by assigning
a name to each element.
|
void |
visit(NodeTraversal traversal,
Node node,
Node parent)
Visits a node in postorder (after its children have been visited).
|
private static final java.lang.String ARGUMENTS
private static final java.lang.String PARAMETER_PREFIX
private final java.lang.String paramPredix
private int uniqueId
private final AbstractCompiler compiler
private final java.util.Deque<java.util.List<Node>> argumentsAccessStack
private java.util.List<Node> currentArgumentsAccess
OptimizeArgumentsArray(AbstractCompiler compiler)
PARAMETER_PREFIX
as the prefix for
all parameter names that it introduces.OptimizeArgumentsArray(AbstractCompiler compiler, java.lang.String paramPrefix)
paramPrefix
- the prefix to use for all parameter names that this
pass introducespublic void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void enterScope(NodeTraversal traversal)
NodeTraversal.ScopedCallback
enterScope
in interface NodeTraversal.ScopedCallback
public void exitScope(NodeTraversal traversal)
NodeTraversal.ScopedCallback
exitScope
in interface NodeTraversal.ScopedCallback
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node, Node parent)
NodeTraversal.Callback
Visits a node in pre order (before visiting its children) and decides
whether this node's children should be traversed. If children are
traversed, they will be visited by
NodeTraversal.Callback.visit(NodeTraversal, Node, Node)
in postorder.
Implementations can have side effects (e.g. modifying the parse tree).
shouldTraverse
in interface NodeTraversal.Callback
public void visit(NodeTraversal traversal, Node node, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private boolean tryReplaceArguments(Scope scope)
scope
- scope of the functionprivate java.lang.String getNewName()