better fix for #3113 (#3115)

This commit is contained in:
Alex Lam S.L
2018-05-03 15:51:51 +08:00
committed by GitHub
parent a0ca595c2c
commit fc0f168a0c
2 changed files with 113 additions and 14 deletions

View File

@@ -361,8 +361,8 @@ merge(Compressor.prototype, {
def.single_use = undefined; def.single_use = undefined;
} }
function reset_variables(tw, compressor, node) { function reset_variables(tw, compressor, scope) {
node.variables.each(function(def) { scope.variables.each(function(def) {
reset_def(compressor, def); reset_def(compressor, def);
if (def.fixed === null) { if (def.fixed === null) {
def.safe_ids = tw.safe_ids; def.safe_ids = tw.safe_ids;
@@ -374,9 +374,27 @@ merge(Compressor.prototype, {
}); });
} }
function walk_defuns(tw, compressor, node) { function same_defun_scope(def, ref) {
node.functions.each(function(def) { var scope = ref.scope;
if (def.init instanceof AST_Defun && !(def.id in tw.defun_ids)) { 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; tw.defun_ids[def.id] = true;
def.init.walk(tw); def.init.walk(tw);
} }
@@ -480,7 +498,7 @@ merge(Compressor.prototype, {
reset_variables(tw, compressor, this); reset_variables(tw, compressor, this);
descend(); descend();
pop(tw); pop(tw);
walk_defuns(tw, compressor, this); walk_defuns(tw, this);
return true; return true;
}); });
def(AST_Assign, function(tw) { def(AST_Assign, function(tw) {
@@ -515,6 +533,20 @@ merge(Compressor.prototype, {
pop(tw); pop(tw);
return true; 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) { def(AST_Case, function(tw) {
push(tw); push(tw);
this.expression.walk(tw); this.expression.walk(tw);
@@ -549,7 +581,7 @@ merge(Compressor.prototype, {
reset_variables(tw, compressor, this); reset_variables(tw, compressor, this);
descend(); descend();
pop(tw); pop(tw);
walk_defuns(tw, compressor, this); walk_defuns(tw, this);
return true; return true;
}); });
def(AST_Do, function(tw) { def(AST_Do, function(tw) {
@@ -622,7 +654,7 @@ merge(Compressor.prototype, {
} }
descend(); descend();
pop(tw); pop(tw);
walk_defuns(tw, compressor, node); walk_defuns(tw, node);
return true; return true;
}); });
def(AST_If, function(tw) { def(AST_If, function(tw) {
@@ -676,10 +708,7 @@ 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.scope === this.scope && !tw.in_loop && d.fixed instanceof AST_Defun && !(d.id in tw.defun_ids)) { if (d.fixed instanceof AST_Defun) walk_defun(tw, this);
tw.defun_ids[d.id] = true;
d.fixed.walk(tw);
}
}); });
def(AST_Toplevel, function(tw, descend, compressor) { def(AST_Toplevel, function(tw, descend, compressor) {
this.globals.each(function(def) { this.globals.each(function(def) {
@@ -689,7 +718,7 @@ merge(Compressor.prototype, {
reset_variables(tw, compressor, this); reset_variables(tw, compressor, this);
descend(); descend();
pop(tw); pop(tw);
walk_defuns(tw, compressor, this); walk_defuns(tw, this);
return true; return true;
}); });
def(AST_Try, function(tw) { def(AST_Try, function(tw) {

View File

@@ -5834,7 +5834,7 @@ issue_3110_3: {
] ]
} }
issue_3113: { issue_3113_1: {
options = { options = {
evaluate: true, evaluate: true,
reduce_vars: true, reduce_vars: true,
@@ -5869,3 +5869,73 @@ issue_3113: {
} }
expect_stdout: "1" expect_stdout: "1"
} }
issue_3113_2: {
options = {
evaluate: true,
reduce_vars: true,
}
input: {
var c = 0;
(function() {
function f() {
while (g());
}
var a = f();
function g() {
a && a[c++];
}
a = 1;
g();
})();
console.log(c);
}
expect: {
var c = 0;
(function() {
function f() {
while (g());
}
var a = f();
function g() {
a && a[c++];
}
a = 1;
g();
})();
console.log(c);
}
expect_stdout: "1"
}
issue_3113_3: {
options = {
evaluate: true,
inline: true,
passes: 2,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
var c = 0;
(function() {
function f() {
while (g());
}
var a;
function g() {
a && a[c++];
}
g(a = 1);
})();
console.log(c);
}
expect: {
var c = 0;
c++;
console.log(c);
}
expect_stdout: "1"
}