fix incorrect context in variable substitution (#1791)

`AST_Node.optimize()` is context-aware, so don't cache its results to be used elsewhere.

Also fixed a few cases of AST corruption and beef up safety of `pure_getters`.
This commit is contained in:
Alex Lam S.L
2017-04-07 03:42:17 +08:00
committed by GitHub
parent e869779a98
commit cc6aa3e5ac
2 changed files with 133 additions and 19 deletions

View File

@@ -283,10 +283,16 @@ merge(Compressor.prototype, {
if (node instanceof AST_VarDef) { if (node instanceof AST_VarDef) {
var d = node.name.definition(); var d = node.name.definition();
if (d.fixed == null) { if (d.fixed == null) {
d.fixed = node.value && function() { if (node.value) {
return node.value; d.fixed = function() {
}; return node.value;
};
descend();
} else {
d.fixed = null;
}
mark_as_safe(d); mark_as_safe(d);
return true;
} else if (node.value) { } else if (node.value) {
d.fixed = false; d.fixed = false;
} }
@@ -1160,13 +1166,42 @@ merge(Compressor.prototype, {
&& !node.expression.has_side_effects(compressor); && !node.expression.has_side_effects(compressor);
} }
// may_eq_null()
// returns true if this node may evaluate to null or undefined
(function(def) { (function(def) {
def(AST_Node, return_false); def(AST_Node, return_true);
def(AST_Null, return_true); def(AST_Null, return_true);
def(AST_Undefined, return_true); def(AST_Undefined, return_true);
def(AST_Constant, return_false);
def(AST_Array, return_false);
def(AST_Object, return_false);
def(AST_Function, return_false);
def(AST_UnaryPostfix, return_false);
def(AST_UnaryPrefix, function() { def(AST_UnaryPrefix, function() {
return this.operator == "void"; return this.operator == "void";
}); });
def(AST_Binary, function(compressor) {
switch (this.operator) {
case "&&":
return this.left.may_eq_null(compressor);
case "||":
return this.left.may_eq_null(compressor)
&& this.right.may_eq_null(compressor);
default:
return false;
}
})
def(AST_Assign, function(compressor) {
return this.operator == "="
&& this.right.may_eq_null(compressor);
})
def(AST_Conditional, function(compressor) {
return this.consequent.may_eq_null(compressor)
|| this.alternative.may_eq_null(compressor);
})
def(AST_Seq, function(compressor) {
return this.cdr.may_eq_null(compressor);
});
def(AST_PropAccess, function(compressor) { def(AST_PropAccess, function(compressor) {
return !compressor.option("unsafe"); return !compressor.option("unsafe");
}); });
@@ -3055,8 +3090,9 @@ merge(Compressor.prototype, {
if (this.expression instanceof AST_Seq) { if (this.expression instanceof AST_Seq) {
var seq = this.expression; var seq = this.expression;
var x = seq.to_array(); var x = seq.to_array();
this.expression = x.pop(); var e = this.clone();
x.push(this); e.expression = x.pop();
x.push(e);
seq = AST_Seq.from_array(x).transform(compressor); seq = AST_Seq.from_array(x).transform(compressor);
return seq; return seq;
} }
@@ -3110,9 +3146,14 @@ merge(Compressor.prototype, {
if (e instanceof AST_Binary if (e instanceof AST_Binary
&& (self.operator == "+" || self.operator == "-") && (self.operator == "+" || self.operator == "-")
&& (e.operator == "*" || e.operator == "/" || e.operator == "%")) { && (e.operator == "*" || e.operator == "/" || e.operator == "%")) {
self.expression = e.left; return make_node(AST_Binary, self, {
e.left = self; operator: e.operator,
return e; left: make_node(AST_UnaryPrefix, e.left, {
operator: self.operator,
expression: e.left
}),
right: e.right
});
} }
// avoids infinite recursion of numerals // avoids infinite recursion of numerals
if (self.operator != "-" if (self.operator != "-"
@@ -3131,23 +3172,25 @@ merge(Compressor.prototype, {
if (this.left instanceof AST_Seq) { if (this.left instanceof AST_Seq) {
var seq = this.left; var seq = this.left;
var x = seq.to_array(); var x = seq.to_array();
this.left = x.pop(); var e = this.clone();
x.push(this); e.left = x.pop();
x.push(e);
return AST_Seq.from_array(x).optimize(compressor); return AST_Seq.from_array(x).optimize(compressor);
} }
if (this.right instanceof AST_Seq && !this.left.has_side_effects(compressor)) { if (this.right instanceof AST_Seq && !this.left.has_side_effects(compressor)) {
var assign = this.operator == "=" && this.left instanceof AST_SymbolRef; var assign = this.operator == "=" && this.left instanceof AST_SymbolRef;
var root = this.right; var root = this.right.clone();
var cursor, seq = root; var cursor, seq = root;
while (assign || !seq.car.has_side_effects(compressor)) { while (assign || !seq.car.has_side_effects(compressor)) {
cursor = seq; cursor = seq;
if (seq.cdr instanceof AST_Seq) { if (seq.cdr instanceof AST_Seq) {
seq = seq.cdr; seq = seq.cdr = seq.cdr.clone();
} else break; } else break;
} }
if (cursor) { if (cursor) {
this.right = cursor.cdr; var e = this.clone();
cursor.cdr = this; e.right = cursor.cdr;
cursor.cdr = e;
return root.optimize(compressor); return root.optimize(compressor);
} }
} }
@@ -3547,9 +3590,8 @@ merge(Compressor.prototype, {
if (d.should_replace === undefined) { if (d.should_replace === undefined) {
var init = fixed.evaluate(compressor); var init = fixed.evaluate(compressor);
if (init !== fixed) { if (init !== fixed) {
init = make_node_from_constant(init, fixed).optimize(compressor); init = make_node_from_constant(init, fixed);
init = best_of_expression(init, fixed); var value = best_of_expression(init.optimize(compressor), fixed).print_to_string().length;
var value = init.print_to_string().length;
var name = d.name.length; var name = d.name.length;
var freq = d.references.length; var freq = d.references.length;
var overhead = d.global || !freq ? 0 : (name + 2 + value) / freq; var overhead = d.global || !freq ? 0 : (name + 2 + value) / freq;
@@ -3559,7 +3601,7 @@ merge(Compressor.prototype, {
} }
} }
if (d.should_replace) { if (d.should_replace) {
return d.should_replace.clone(true); return best_of_expression(d.should_replace.optimize(compressor), fixed).clone(true);
} }
} }
} }

View File

@@ -1866,3 +1866,75 @@ delay_def: {
} }
expect_stdout: true expect_stdout: true
} }
booleans: {
options = {
booleans: true,
evaluate: true,
reduce_vars: true,
}
input: {
console.log(function(a) {
if (a != 0);
switch (a) {
case 0:
return "FAIL";
case false:
return "PASS";
}
}(false));
}
expect: {
console.log(function(a) {
if (!1);
switch (!1) {
case 0:
return "FAIL";
case !1:
return "PASS";
}
}(!1));
}
expect_stdout: "PASS"
}
side_effects_assign: {
options = {
evaluate: true,
reduce_vars: true,
sequences: true,
side_effects: true,
toplevel: true,
}
input: {
var a = typeof void (a && a.in == 1, 0);
console.log(a);
}
expect: {
var a = typeof void (a && a.in);
console.log(a);
}
expect_stdout: "undefined"
}
pure_getters: {
options = {
pure_getters: true,
reduce_vars: true,
side_effects: true,
toplevel: true,
}
input: {
try {
var a = (a.b, 2);
} catch (e) {}
console.log(a);
}
expect: {
try {
var a = (a.b, 2);
} catch (e) {}
console.log(a);
}
expect_stdout: "undefined"
}