properly drop mutually-referring declarations that are not otherwise

referenced and have no side effects
This commit is contained in:
Mihai Bazon
2012-09-23 12:47:34 +03:00
parent 76d88b59dc
commit a83b28503f
5 changed files with 137 additions and 67 deletions

View File

@@ -101,6 +101,9 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
delete labels[l.name];
return true; // no descend again
}
if (node instanceof AST_SymbolDeclaration) {
node.init_scope_vars();
}
if (node instanceof AST_Symbol) {
node.scope = scope;
}
@@ -130,6 +133,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
}
else if (node instanceof AST_SymbolLambda) {
scope.def_function(node);
node.init.push(tw.parent());
}
else if (node instanceof AST_SymbolDefun) {
// Careful here, the scope where this should be defined is
@@ -138,9 +142,12 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
// instanceof AST_Scope) but we get to the symbol a bit
// later.
(node.scope = scope.parent_scope).def_function(node);
node.init.push(tw.parent());
}
else if (node instanceof AST_SymbolVar) {
scope.def_variable(node);
var def = tw.parent();
if (def.value) node.init.push(def);
}
else if (node instanceof AST_SymbolCatch) {
// XXX: this is wrong according to ECMA-262 (12.4). the
@@ -233,6 +240,10 @@ AST_SymbolRef.DEFMETHOD("reference", function() {
}
});
AST_SymbolDeclaration.DEFMETHOD("init_scope_vars", function(){
this.init = [];
});
AST_Label.DEFMETHOD("init_scope_vars", function(){
this.references = [];
});
@@ -318,7 +329,7 @@ AST_LoopControl.DEFMETHOD("target", function(){
return this.loopcontrol_target;
});
AST_Toplevel.DEFMETHOD("mangle_names", function(){
AST_Toplevel.DEFMETHOD("mangle_names", function(sort){
// We only need to mangle declaration nodes. Special logic wired
// into the code generator will display the mangled name if it's
// present (and for AST_SymbolRef-s it'll use the mangled name of
@@ -353,13 +364,9 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(){
});
this.walk(tw);
// strangely, if we try to give more frequently used variables
// shorter name, the size after gzip seems to be higher! so leave
// this commented out I guess...
//
// to_mangle = mergeSort(to_mangle, function(a, b){
// return b.references.length - a.references.length;
// });
if (sort) to_mangle = mergeSort(to_mangle, function(a, b){
return b.references.length - a.references.length;
});
to_mangle.forEach(function(def){ def.mangle() });
});