if (def.global) for (var i = 0; i < def.orig.length; i++)
if (!this.toplevel[def.orig[i] instanceof AST_SymbolDefun ? "funcs" : "vars"])
return true;
- return false;
+ return def.undeclared;
},
compress: function(node) {
node = node.resolve_defines(this);
var scan_lhs = lhs && !side_effects && !is_lhs_read_only(lhs, compressor);
var scan_rhs = foldable(candidate);
if (!scan_lhs && !scan_rhs) continue;
+ var read_toplevel = false;
var modify_toplevel = false;
// Locate symbols which may execute code outside of scanning range
var lvalues = get_lvalues(candidate);
return lvalues.has(node.name.name) || side_effects && may_modify(node.name);
}
var sym = is_lhs(node.left, node);
- if (sym && lvalues.has(sym.name)) return true;
- if (sym instanceof AST_PropAccess) return true;
+ if (!sym) return false;
+ return lvalues.has(sym.name)
+ || sym instanceof AST_PropAccess
+ || read_toplevel && compressor.exposed(sym.definition());
}
function extract_args() {
};
}
+ function may_be_global(node) {
+ if (node instanceof AST_SymbolRef) {
+ node = node.fixed_value();
+ if (!node) return true;
+ }
+ if (node instanceof AST_Assign) return node.operator == "=" && may_be_global(node.right);
+ return node instanceof AST_PropAccess || node instanceof AST_This;
+ }
+
function get_lvalues(expr) {
var lvalues = new Dictionary();
- if (candidate instanceof AST_VarDef) lvalues.add(candidate.name.name, lhs);
- var scan_iife = scope instanceof AST_Toplevel;
+ if (expr instanceof AST_VarDef) lvalues.add(expr.name.name, lhs);
+ var scan_toplevel = scope instanceof AST_Toplevel;
var tw = new TreeWalker(function(node) {
- if (scan_iife && node.TYPE == "Call") {
- var exp = node.expression;
- if (exp instanceof AST_PropAccess) return;
- if (exp instanceof AST_Function && !exp.contains_this()) return;
- modify_toplevel = true;
- scan_iife = false;
- return;
- }
var value;
if (node instanceof AST_SymbolRef) {
value = node.fixed_value() || node;
} else if (node instanceof AST_This) {
value = node;
}
- if (value) lvalues.add(node.name, is_modified(compressor, tw, node, value, 0));
+ if (value) {
+ lvalues.add(node.name, is_modified(compressor, tw, node, value, 0));
+ } else if (scan_toplevel) {
+ if (node.TYPE == "Call") {
+ if (modify_toplevel) return;
+ var exp = node.expression;
+ if (exp instanceof AST_PropAccess) return;
+ if (exp instanceof AST_Function && !exp.contains_this()) return;
+ modify_toplevel = true;
+ } else if (node instanceof AST_PropAccess && may_be_global(node.expression)) {
+ if (node === lhs && !(expr instanceof AST_Unary)) {
+ modify_toplevel = true;
+ } else {
+ read_toplevel = true;
+ }
+ }
+ }
});
expr.walk(tw);
return lvalues;