fix reduce_vars on nested invocations (#3118)
This commit is contained in:
@@ -340,10 +340,10 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(function(def){
|
(function(def) {
|
||||||
def(AST_Node, noop);
|
def(AST_Node, noop);
|
||||||
|
|
||||||
function reset_def(compressor, def) {
|
function reset_def(tw, compressor, def) {
|
||||||
def.assignments = 0;
|
def.assignments = 0;
|
||||||
def.chained = false;
|
def.chained = false;
|
||||||
def.direct_access = false;
|
def.direct_access = false;
|
||||||
@@ -355,15 +355,25 @@ merge(Compressor.prototype, {
|
|||||||
} else {
|
} else {
|
||||||
def.fixed = false;
|
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.recursive_refs = 0;
|
||||||
def.references = [];
|
def.references = [];
|
||||||
def.should_replace = undefined;
|
def.should_replace = undefined;
|
||||||
def.single_use = 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) {
|
function reset_variables(tw, compressor, scope) {
|
||||||
scope.variables.each(function(def) {
|
scope.variables.each(function(def) {
|
||||||
reset_def(compressor, def);
|
reset_def(tw, compressor, def);
|
||||||
if (def.fixed === null) {
|
if (def.fixed === null) {
|
||||||
def.safe_ids = tw.safe_ids;
|
def.safe_ids = tw.safe_ids;
|
||||||
mark(tw, def, true);
|
mark(tw, def, true);
|
||||||
@@ -374,22 +384,14 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function same_defun_scope(def, ref) {
|
function walk_defun(tw, def) {
|
||||||
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;
|
if (def.id in tw.defun_ids) return;
|
||||||
tw.defun_ids[def.id] = true;
|
if (!tw.in_loop) {
|
||||||
def.fixed.walk(tw);
|
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) {
|
function walk_defuns(tw, scope) {
|
||||||
@@ -534,16 +536,10 @@ merge(Compressor.prototype, {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
def(AST_Call, function(tw, descend) {
|
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();
|
descend();
|
||||||
if (tw.defun_ids[def.id] === 0) {
|
var exp = this.expression;
|
||||||
delete tw.defun_ids[def.id];
|
if (exp instanceof AST_SymbolRef && exp.fixed_value() instanceof AST_Defun) {
|
||||||
walk_defun(tw, exp);
|
walk_defun(tw, exp.definition());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -708,11 +704,15 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
mark_escaped(tw, d, this.scope, this, value, 0, 1);
|
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) {
|
def(AST_Toplevel, function(tw, descend, compressor) {
|
||||||
this.globals.each(function(def) {
|
this.globals.each(function(def) {
|
||||||
reset_def(compressor, def);
|
reset_def(tw, compressor, def);
|
||||||
});
|
});
|
||||||
push(tw);
|
push(tw);
|
||||||
reset_variables(tw, compressor, this);
|
reset_variables(tw, compressor, this);
|
||||||
|
|||||||
@@ -5939,3 +5939,62 @@ issue_3113_3: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "1"
|
expect_stdout: "1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_3113_4: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
toplevel: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 0, b = 0;
|
||||||
|
function f() {
|
||||||
|
b += a;
|
||||||
|
}
|
||||||
|
f(f(), ++a);
|
||||||
|
console.log(a, b);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 0, b = 0;
|
||||||
|
function f() {
|
||||||
|
b += a;
|
||||||
|
}
|
||||||
|
f(f(), ++a);
|
||||||
|
console.log(a, b);
|
||||||
|
}
|
||||||
|
expect_stdout: "1 1"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_3113_5: {
|
||||||
|
options = {
|
||||||
|
evaluate: true,
|
||||||
|
reduce_vars: true,
|
||||||
|
toplevel: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
function g() {
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
while (g());
|
||||||
|
var a = 1;
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
function g() {
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
while (g());
|
||||||
|
var a = 1;
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"undefined",
|
||||||
|
"1",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user