unsafe fix-ups for #2351 (#2379)

This commit is contained in:
Alex Lam S.L
2017-10-21 04:08:26 +08:00
committed by GitHub
parent 9f4b98f8e4
commit c927cea632
2 changed files with 92 additions and 27 deletions

View File

@@ -151,7 +151,7 @@ merge(Compressor.prototype, {
var last_count = 1 / 0;
for (var pass = 0; pass < passes; pass++) {
if (pass > 0 || this.option("reduce_vars"))
node.reset_opt_flags(this, true);
node.reset_opt_flags(this);
node = node.transform(this);
if (passes > 1) {
var count = 0;
@@ -283,8 +283,9 @@ merge(Compressor.prototype, {
self.transform(tt);
});
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) {
var reduce_vars = rescan && compressor.option("reduce_vars");
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor) {
var reduce_vars = compressor.option("reduce_vars");
var unused = compressor.option("unused");
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
// properly assigned before use:
// - `push()` & `pop()` when visiting conditional branches
@@ -308,16 +309,31 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef) {
var d = node.definition();
d.references.push(node);
if (d.fixed === undefined || !safe_to_read(d)
|| is_modified(node, 0, is_immutable(node))) {
if (d.fixed === undefined || !safe_to_read(d) || d.single_use == "m") {
d.fixed = false;
} else {
var parent = tw.parent();
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|| parent instanceof AST_Call && node !== parent.expression
|| parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
|| parent instanceof AST_VarDef && node === parent.value) {
d.escaped = true;
var value = node.fixed_value();
if (unused) {
d.single_use = value
&& d.references.length == 1
&& loop_ids[d.id] === in_loop
&& d.scope === node.scope
&& value.is_constant_expression();
}
if (is_modified(node, 0, is_immutable(value))) {
if (d.single_use) {
d.single_use = "m";
} else {
d.fixed = false;
}
} else {
var parent = tw.parent();
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|| parent instanceof AST_Call && node !== parent.expression
|| parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
|| parent instanceof AST_VarDef && node === parent.value) {
d.escaped = true;
}
}
}
}
@@ -550,20 +566,8 @@ merge(Compressor.prototype, {
def.single_use = undefined;
}
function is_immutable(node) {
var value = node.fixed_value();
if (!value) return false;
if (value.is_constant()) return true;
if (compressor.option("unused")) {
var d = node.definition();
if (d.single_use === undefined) {
d.single_use = loop_ids[d.id] === in_loop
&& d.scope === node.scope
&& value.is_constant_expression();
}
if (d.references.length == 1 && d.single_use) return true;
}
return value instanceof AST_Lambda;
function is_immutable(value) {
return value && (value.is_constant() || value instanceof AST_Lambda);
}
function is_modified(node, level, immutable) {
@@ -4107,6 +4111,7 @@ merge(Compressor.prototype, {
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
}
if (compressor.option("unused")
&& fixed
&& d.references.length == 1
&& (d.single_use || fixed instanceof AST_Function
&& !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg)

View File

@@ -1190,10 +1190,10 @@ defun_label: {
!function() {
console.log(function(a) {
L: {
if (a) break L;
if (2) break L;
return 1;
}
}(2));
}());
}();
}
expect_stdout: true
@@ -2894,3 +2894,63 @@ array_forin_2: {
}
expect_stdout: "3"
}
const_expr_1: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unsafe: true,
unused: true,
}
input: {
var o = {
a: 1,
b: 2
};
o.a++;
console.log(o.a, o.b);
}
expect: {
var o = {
a: 1,
b: 2
};
o.a++;
console.log(o.a, o.b);
}
expect_stdout: "2 2"
}
const_expr_2: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unsafe: true,
unused: true,
}
input: {
Object.prototype.c = function() {
this.a++;
};
var o = {
a: 1,
b: 2
};
o.c();
console.log(o.a, o.b);
}
expect: {
Object.prototype.c = function() {
this.a++;
};
var o = {
a: 1,
b: 2
};
o.c();
console.log(o.a, o.b);
}
expect_stdout: "2 2"
}