@@ -374,6 +374,15 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
}
|
||||
|
||||
function walk_defuns(tw, compressor, node) {
|
||||
node.functions.each(function(def) {
|
||||
if (def.init instanceof AST_Defun && !(def.id in tw.defun_ids)) {
|
||||
tw.defun_ids[def.id] = true;
|
||||
def.init.walk(tw);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function push(tw) {
|
||||
tw.safe_ids = Object.create(tw.safe_ids);
|
||||
}
|
||||
@@ -398,7 +407,7 @@ merge(Compressor.prototype, {
|
||||
return def.fixed instanceof AST_Defun;
|
||||
}
|
||||
|
||||
function safe_to_assign(tw, def, value) {
|
||||
function safe_to_assign(tw, def, scope, value) {
|
||||
if (def.fixed === undefined) return true;
|
||||
if (def.fixed === null && def.safe_ids) {
|
||||
def.safe_ids[def.id] = false;
|
||||
@@ -409,6 +418,9 @@ merge(Compressor.prototype, {
|
||||
if (!safe_to_read(tw, def)) return false;
|
||||
if (def.fixed === false) return false;
|
||||
if (def.fixed != null && (!value || def.references.length > def.assignments)) return false;
|
||||
if (def.fixed instanceof AST_Defun) {
|
||||
return value instanceof AST_Node && def.fixed.parent_scope === scope;
|
||||
}
|
||||
return all(def.orig, function(sym) {
|
||||
return !(sym instanceof AST_SymbolDefun
|
||||
|| sym instanceof AST_SymbolLambda);
|
||||
@@ -468,6 +480,7 @@ merge(Compressor.prototype, {
|
||||
reset_variables(tw, compressor, this);
|
||||
descend();
|
||||
pop(tw);
|
||||
walk_defuns(tw, compressor, this);
|
||||
return true;
|
||||
});
|
||||
def(AST_Assign, function(tw) {
|
||||
@@ -476,7 +489,7 @@ merge(Compressor.prototype, {
|
||||
var d = node.left.definition();
|
||||
var fixed = d.fixed;
|
||||
if (!fixed && node.operator != "=") return;
|
||||
if (!safe_to_assign(tw, d, node.right)) return;
|
||||
if (!safe_to_assign(tw, d, node.left.scope, node.right)) return;
|
||||
d.references.push(node.left);
|
||||
d.assignments++;
|
||||
if (node.operator != "=") d.chained = true;
|
||||
@@ -528,12 +541,15 @@ merge(Compressor.prototype, {
|
||||
return true;
|
||||
});
|
||||
def(AST_Defun, function(tw, descend, compressor) {
|
||||
var id = this.name.definition().id;
|
||||
if (!tw.defun_ids[id]) return true;
|
||||
tw.defun_ids[id] = false;
|
||||
this.inlined = false;
|
||||
var save_ids = tw.safe_ids;
|
||||
tw.safe_ids = Object.create(null);
|
||||
push(tw);
|
||||
reset_variables(tw, compressor, this);
|
||||
descend();
|
||||
tw.safe_ids = save_ids;
|
||||
pop(tw);
|
||||
walk_defuns(tw, compressor, this);
|
||||
return true;
|
||||
});
|
||||
def(AST_Do, function(tw) {
|
||||
@@ -606,6 +622,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
descend();
|
||||
pop(tw);
|
||||
walk_defuns(tw, compressor, node);
|
||||
return true;
|
||||
});
|
||||
def(AST_If, function(tw) {
|
||||
@@ -659,12 +676,21 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
mark_escaped(tw, d, this.scope, this, value, 0, 1);
|
||||
if (d.scope === this.scope && !tw.in_loop && d.fixed instanceof AST_Defun && !(d.id in tw.defun_ids)) {
|
||||
tw.defun_ids[d.id] = true;
|
||||
d.fixed.walk(tw);
|
||||
}
|
||||
});
|
||||
def(AST_Toplevel, function(tw, descend, compressor) {
|
||||
this.globals.each(function(def) {
|
||||
reset_def(compressor, def);
|
||||
});
|
||||
push(tw);
|
||||
reset_variables(tw, compressor, this);
|
||||
descend();
|
||||
pop(tw);
|
||||
walk_defuns(tw, compressor, this);
|
||||
return true;
|
||||
});
|
||||
def(AST_Try, function(tw) {
|
||||
push(tw);
|
||||
@@ -681,12 +707,13 @@ merge(Compressor.prototype, {
|
||||
def(AST_Unary, function(tw, descend) {
|
||||
var node = this;
|
||||
if (node.operator != "++" && node.operator != "--") return;
|
||||
if (!(node.expression instanceof AST_SymbolRef)) return;
|
||||
var d = node.expression.definition();
|
||||
var exp = node.expression;
|
||||
if (!(exp instanceof AST_SymbolRef)) return;
|
||||
var d = exp.definition();
|
||||
var fixed = d.fixed;
|
||||
if (!fixed) return;
|
||||
if (!safe_to_assign(tw, d, true)) return;
|
||||
d.references.push(node.expression);
|
||||
if (!safe_to_assign(tw, d, exp.scope, true)) return;
|
||||
d.references.push(exp);
|
||||
d.assignments++;
|
||||
d.chained = true;
|
||||
d.fixed = function() {
|
||||
@@ -708,7 +735,7 @@ merge(Compressor.prototype, {
|
||||
var node = this;
|
||||
var d = node.name.definition();
|
||||
if (node.value) {
|
||||
if (safe_to_assign(tw, d, node.value)) {
|
||||
if (safe_to_assign(tw, d, node.name.scope, node.value)) {
|
||||
d.fixed = function() {
|
||||
return node.value;
|
||||
};
|
||||
@@ -736,19 +763,24 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
|
||||
var reduce_vars = compressor.option("reduce_vars");
|
||||
var tw = new TreeWalker(function(node, descend) {
|
||||
var tw = new TreeWalker(compressor.option("reduce_vars") ? function(node, descend) {
|
||||
node._squeezed = false;
|
||||
node._optimized = false;
|
||||
return node.reduce_vars(tw, descend, compressor);
|
||||
} : function(node) {
|
||||
node._squeezed = false;
|
||||
node._optimized = false;
|
||||
if (reduce_vars) return node.reduce_vars(tw, descend, compressor);
|
||||
});
|
||||
// Flow control for visiting `AST_Defun`s
|
||||
tw.defun_ids = Object.create(null);
|
||||
// Record the loop body in which `AST_SymbolDeclaration` is first encountered
|
||||
tw.in_loop = null;
|
||||
tw.loop_ids = Object.create(null);
|
||||
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
|
||||
// properly assigned before use:
|
||||
// - `push()` & `pop()` when visiting conditional branches
|
||||
// - backup & restore via `save_ids` when visiting out-of-order sections
|
||||
tw.safe_ids = Object.create(null);
|
||||
tw.in_loop = null;
|
||||
tw.loop_ids = Object.create(null);
|
||||
this.walk(tw);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user