fix AST corruption

This commit is contained in:
alexlamsl 2017-04-06 19:47:05 +08:00
parent 1a8c9dfdc4
commit 1d1a8575dd
2 changed files with 39 additions and 12 deletions

View File

@ -3055,8 +3055,9 @@ merge(Compressor.prototype, {
if (this.expression instanceof AST_Seq) {
var seq = this.expression;
var x = seq.to_array();
this.expression = x.pop();
x.push(this);
var e = this.clone();
e.expression = x.pop();
x.push(e);
seq = AST_Seq.from_array(x).transform(compressor);
return seq;
}
@ -3110,9 +3111,14 @@ merge(Compressor.prototype, {
if (e instanceof AST_Binary
&& (self.operator == "+" || self.operator == "-")
&& (e.operator == "*" || e.operator == "/" || e.operator == "%")) {
self.expression = e.left;
e.left = self;
return e;
return make_node(AST_Binary, self, {
operator: e.operator,
left: make_node(AST_UnaryPrefix, e.left, {
operator: self.operator,
expression: e.left
}),
right: e.right
});
}
// avoids infinite recursion of numerals
if (self.operator != "-"
@ -3131,23 +3137,25 @@ merge(Compressor.prototype, {
if (this.left instanceof AST_Seq) {
var seq = this.left;
var x = seq.to_array();
this.left = x.pop();
x.push(this);
var e = this.clone();
e.left = x.pop();
x.push(e);
return AST_Seq.from_array(x).optimize(compressor);
}
if (this.right instanceof AST_Seq && !this.left.has_side_effects(compressor)) {
var assign = this.operator == "=" && this.left instanceof AST_SymbolRef;
var root = this.right;
var root = this.right.clone();
var cursor, seq = root;
while (assign || !seq.car.has_side_effects(compressor)) {
cursor = seq;
if (seq.cdr instanceof AST_Seq) {
seq = seq.cdr;
seq = seq.cdr = seq.cdr.clone();
} else break;
}
if (cursor) {
this.right = cursor.cdr;
cursor.cdr = this;
var e = this.clone();
e.right = cursor.cdr;
cursor.cdr = e;
return root.optimize(compressor);
}
}
@ -3558,7 +3566,7 @@ merge(Compressor.prototype, {
}
}
if (d.should_replace) {
return best_of_expression(d.should_replace.clone(true).optimize(compressor), fixed.clone(true));
return best_of_expression(d.should_replace.optimize(compressor), fixed).clone(true);
}
}
}

View File

@ -1897,3 +1897,22 @@ booleans: {
}
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"
}