diff options
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 55 |
1 files changed, 42 insertions, 13 deletions
diff --git a/lib/compress.js b/lib/compress.js index 99b77829..60293187 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -361,8 +361,8 @@ merge(Compressor.prototype, { def.single_use = undefined; } - function reset_variables(tw, compressor, node) { - node.variables.each(function(def) { + function reset_variables(tw, compressor, scope) { + scope.variables.each(function(def) { reset_def(compressor, def); if (def.fixed === null) { def.safe_ids = tw.safe_ids; @@ -374,9 +374,27 @@ 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)) { + function same_defun_scope(def, ref) { + var scope = ref.scope; + do { + if (def.scope === scope) return true; + } while (scope instanceof AST_Function && (scope = scope.parent_scope)); + } + + function walk_defun(tw, ref) { + var def = ref.definition(); + if (tw.in_loop || !same_defun_scope(def, ref)) { + if (tw.defun_ids[def.id] !== false) tw.defun_ids[def.id] = undefined; + return; + } + if (def.id in tw.defun_ids) return; + tw.defun_ids[def.id] = true; + def.fixed.walk(tw); + } + + function walk_defuns(tw, scope) { + scope.functions.each(function(def) { + if (def.init instanceof AST_Defun && tw.defun_ids[def.id] === undefined) { tw.defun_ids[def.id] = true; def.init.walk(tw); } @@ -480,7 +498,7 @@ merge(Compressor.prototype, { reset_variables(tw, compressor, this); descend(); pop(tw); - walk_defuns(tw, compressor, this); + walk_defuns(tw, this); return true; }); def(AST_Assign, function(tw) { @@ -515,6 +533,20 @@ merge(Compressor.prototype, { pop(tw); return true; }); + def(AST_Call, function(tw, descend) { + var exp = this.expression; + if (!(exp instanceof AST_SymbolRef)) return; + var def = exp.definition(); + if (!(def.fixed instanceof AST_Defun)) return; + if (def.id in tw.defun_ids) return; + tw.defun_ids[def.id] = 0; + descend(); + if (tw.defun_ids[def.id] === 0) { + delete tw.defun_ids[def.id]; + walk_defun(tw, exp); + } + return true; + }); def(AST_Case, function(tw) { push(tw); this.expression.walk(tw); @@ -549,7 +581,7 @@ merge(Compressor.prototype, { reset_variables(tw, compressor, this); descend(); pop(tw); - walk_defuns(tw, compressor, this); + walk_defuns(tw, this); return true; }); def(AST_Do, function(tw) { @@ -622,7 +654,7 @@ merge(Compressor.prototype, { } descend(); pop(tw); - walk_defuns(tw, compressor, node); + walk_defuns(tw, node); return true; }); def(AST_If, function(tw) { @@ -676,10 +708,7 @@ 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); - } + if (d.fixed instanceof AST_Defun) walk_defun(tw, this); }); def(AST_Toplevel, function(tw, descend, compressor) { this.globals.each(function(def) { @@ -689,7 +718,7 @@ merge(Compressor.prototype, { reset_variables(tw, compressor, this); descend(); pop(tw); - walk_defuns(tw, compressor, this); + walk_defuns(tw, this); return true; }); def(AST_Try, function(tw) { |