exec
public static boolean exec(JsProgram program,
FreshNameGenerator nameGenerator)
Entry point for the removeDuplicateFunctions optimization.
This optimization will collapse functions whose JavaScript (output) code is identical. After
collapsing duplicate functions it will remove functions that become unreferenced as a result.
This pass is safe only for JavaScript functions generated from Java where references to
local function variables can not be extruded by returning a function. E,g. in the next example
function f1() {return a;}
funcion f2() { var a; return function() {return a;}}
f1() and the return of f2() are not duplicates even though the have a syntacticaly identical
parameters and body. The reason is that a in f1() refers to some globally scoped variable a,
whereas a in the return of f2() refers to the local variable a. It would be not correct to
move the return of f2() to the global scope.
This situation does NOT arise from functions that where generated from Java sources (non
native)
IMPORTANT NOTE: It is NOT safe to rename JsNames after this pass is performed. E.g.
Consider an output JavaScript for two unrelated classes:
defineSeed(...) //class A
_.a
_.m1 = function() { return this.a; }
defineSeed(...) // class B
_.a
_.m2 = function() { return this.a; }
Here m1() in class A and m2 in class B have identical parameters and bodies; hence the result
will be
defineSeed(...) //class A
_.a
_.m1 = g1
defineSeed(...) // class B
_.a
_.m2 = g1
function g1() { return this.a; }
The reference to this.a in g1 will be to either A.a or B.a and as long as those names remain
the same the removal was correct. However if A.a gets renamed then A.m1() and B.m2() would
no longer have been identical hence the dedup that is already done is incorrect.
- Parameters:
program
- the program to optimize
nameGenerator
- a freshNameGenerator to assign fresh names to deduped functions that are
lifted to the global scope
- Returns:
true
if it made any changes; false
otherwise.