Skip to content

Commit

Permalink
fix reduce_vars on nested invocations (#3118)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored May 3, 2018
1 parent d51a00a commit c4cebb4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 29 deletions.
58 changes: 29 additions & 29 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,10 @@ merge(Compressor.prototype, {
}
}

(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;
Expand All @@ -355,15 +355,25 @@ merge(Compressor.prototype, {
} 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);
Expand All @@ -374,22 +384,14 @@ merge(Compressor.prototype, {
});
}

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) {
Expand Down Expand Up @@ -534,16 +536,10 @@ merge(Compressor.prototype, {
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;
});
Expand Down Expand Up @@ -708,11 +704,15 @@ merge(Compressor.prototype, {
}
}
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);
Expand Down
59 changes: 59 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -5939,3 +5939,62 @@ issue_3113_3: {
}
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",
]
}

0 comments on commit c4cebb4

Please sign in to comment.