}
}
- (function(def){
+ (function(def) {
def(AST_Node, noop);
- function reset_def(compressor, def) {
+ function reset_def(tw, compressor, def) {
def.assignments = 0;
def.chained = false;
def.direct_access = false;
} else {
def.fixed = false;
}
+ if (def.init instanceof AST_Defun && !all(def.references, same_defun_scope)) {
+ tw.defun_ids[def.id] = undefined;
+ }
def.recursive_refs = 0;
def.references = [];
def.should_replace = undefined;
def.single_use = undefined;
+
+ function same_defun_scope(ref) {
+ var scope = ref.scope;
+ do {
+ if (def.scope === scope) return true;
+ } while (scope instanceof AST_Function && (scope = scope.parent_scope));
+ }
}
function reset_variables(tw, compressor, scope) {
scope.variables.each(function(def) {
- reset_def(compressor, def);
+ reset_def(tw, compressor, def);
if (def.fixed === null) {
def.safe_ids = tw.safe_ids;
mark(tw, def, true);
});
}
- 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;
- }
+ function walk_defun(tw, def) {
if (def.id in tw.defun_ids) return;
- tw.defun_ids[def.id] = true;
- def.fixed.walk(tw);
+ if (!tw.in_loop) {
+ tw.defun_ids[def.id] = true;
+ def.fixed.walk(tw);
+ } else if (tw.defun_ids[def.id] !== false) {
+ tw.defun_ids[def.id] = undefined;
+ }
}
function walk_defuns(tw, scope) {
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);
+ var exp = this.expression;
+ if (exp instanceof AST_SymbolRef && exp.fixed_value() instanceof AST_Defun) {
+ walk_defun(tw, exp.definition());
}
return true;
});
}
}
mark_escaped(tw, d, this.scope, this, value, 0, 1);
- if (d.fixed instanceof AST_Defun) walk_defun(tw, this);
+ var parent;
+ if (d.fixed instanceof AST_Defun
+ && !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
+ walk_defun(tw, d);
+ }
});
def(AST_Toplevel, function(tw, descend, compressor) {
this.globals.each(function(def) {
- reset_def(compressor, def);
+ reset_def(tw, compressor, def);
});
push(tw);
reset_variables(tw, compressor, this);