class DevirtualizePrototypeMethods extends java.lang.Object implements OptimizeCalls.CallGraphCompilerPass, CompilerPass
This pass should only be used in production code if property and variable renaming are turned on. Resulting code may also benefit from --collapse_anonymous_functions and --collapse_variable_declarations
This pass only rewrites functions that are part of an objects prototype. Functions that access the "arguments" variable arguments object are not eligible for this optimization.
For example:
A.prototype.accumulate = function(value) { this.total += value; return this.total } var total = a.accumulate(2)
will be rewritten as:
var accumulate = function(self, value) { self.total += value; return self.total } var total = accumulate(a, 2)
Modifier and Type | Field and Description |
---|---|
private AbstractCompiler |
compiler |
Constructor and Description |
---|
DevirtualizePrototypeMethods(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private void |
fixFunctionType(Node functionNode)
Creates a new type based on the original function type by
adding the original this pointer type to the beginning of the
argument type list and replacing the this pointer type with bottom.
|
private static java.lang.String |
getMethodName(Node node) |
private static java.lang.String |
getRewrittenMethodName(java.lang.String originalMethodName) |
private static boolean |
isCall(UseSite site)
Determines if the name node acts as the function name in a call expression.
|
private boolean |
isEligibleDefinition(SimpleDefinitionFinder defFinder,
DefinitionSite definitionSite)
Determines if a method definition is eligible for rewrite as a
global function.
|
private static boolean |
isPrototypeMethodDefinition(Node node)
Determines if the current node is a function prototype definition.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
void |
process(Node externs,
Node root,
SimpleDefinitionFinder definitions) |
private static void |
replaceReferencesToThis(Node node,
java.lang.String name)
Replaces references to "this" with references to name.
|
private void |
rewriteCallSites(SimpleDefinitionFinder defFinder,
DefinitionsRemover.Definition definition,
java.lang.String newMethodName)
Rewrites object method call sites as calls to global functions
that take "this" as their first argument.
|
private void |
rewriteDefinition(Node node,
java.lang.String newMethodName)
Rewrites method definitions as global functions that take "this"
as their first argument.
|
private void |
rewriteDefinitionIfEligible(DefinitionSite defSite,
SimpleDefinitionFinder defFinder)
Rewrites method definition and call sites if the method is
defined in the global scope exactly once.
|
private final AbstractCompiler compiler
DevirtualizePrototypeMethods(AbstractCompiler compiler)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void process(Node externs, Node root, SimpleDefinitionFinder definitions)
process
in interface OptimizeCalls.CallGraphCompilerPass
private static boolean isCall(UseSite site)
private static boolean isPrototypeMethodDefinition(Node node)
private static java.lang.String getMethodName(Node node)
private static java.lang.String getRewrittenMethodName(java.lang.String originalMethodName)
private void rewriteDefinitionIfEligible(DefinitionSite defSite, SimpleDefinitionFinder defFinder)
SimpleDefinitionFinder
passed in as an argument.defSite
- definition site to process.defFinder
- structure that hold Node -> Definition and
Definition -> [UseSite] maps.private boolean isEligibleDefinition(SimpleDefinitionFinder defFinder, DefinitionSite definitionSite)
private void rewriteCallSites(SimpleDefinitionFinder defFinder, DefinitionsRemover.Definition definition, java.lang.String newMethodName)
private void rewriteDefinition(Node node, java.lang.String newMethodName)
private void fixFunctionType(Node functionNode)
private static void replaceReferencesToThis(Node node, java.lang.String name)