fix unsafe escape analysis in reduce_vars (#2387)

This commit is contained in:
Alex Lam S.L
2017-10-22 03:23:31 +08:00
committed by GitHub
parent 96439ca246
commit 011123223b
4 changed files with 85 additions and 45 deletions

View File

@@ -327,13 +327,7 @@ merge(Compressor.prototype, {
d.fixed = false; d.fixed = false;
} }
} else { } else {
var parent = tw.parent(); mark_escaped(d, node, 0);
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;
}
} }
} }
} }
@@ -579,6 +573,18 @@ merge(Compressor.prototype, {
return !immutable && is_modified(parent, level + 1); return !immutable && is_modified(parent, level + 1);
} }
} }
function mark_escaped(d, node, level) {
var parent = tw.parent(level);
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;
} else if (parent instanceof AST_PropAccess && node === parent.expression) {
mark_escaped(d, parent, level + 1);
}
}
}); });
AST_SymbolRef.DEFMETHOD("fixed_value", function() { AST_SymbolRef.DEFMETHOD("fixed_value", function() {

View File

@@ -128,50 +128,55 @@ constant_join_3: {
for_loop: { for_loop: {
options = { options = {
unsafe : true, evaluate: true,
unused : true, reduce_vars: true,
evaluate : true, unsafe: true,
reduce_vars : true
}; };
input: { input: {
function f0() { function f0() {
var a = [1, 2, 3]; var a = [1, 2, 3];
for (var i = 0; i < a.length; i++) { var b = 0;
console.log(a[i]); for (var i = 0; i < a.length; i++)
} b += a[i];
return b;
} }
function f1() { function f1() {
var a = [1, 2, 3]; var a = [1, 2, 3];
for (var i = 0, len = a.length; i < len; i++) { var b = 0;
console.log(a[i]); for (var i = 0, len = a.length; i < len; i++)
} b += a[i];
return b;
} }
function f2() {
var a = [1, 2, 3];
for (var i = 0; i < a.length; i++) {
a[i]++;
}
}
}
expect: {
function f0() {
var a = [1, 2, 3];
for (var i = 0; i < 3; i++)
console.log(a[i]);
}
function f1() {
var a = [1, 2, 3];
for (var i = 0; i < 3; i++)
console.log(a[i]);
}
function f2() { function f2() {
var a = [1, 2, 3]; var a = [1, 2, 3];
for (var i = 0; i < a.length; i++) for (var i = 0; i < a.length; i++)
a[i]++; a[i]++;
return a[2];
} }
console.log(f0(), f1(), f2());
} }
expect: {
function f0() {
var a = [1, 2, 3];
var b = 0;
for (var i = 0; i < 3; i++)
b += a[i];
return b;
}
function f1() {
var a = [1, 2, 3];
var b = 0;
for (var i = 0, len = a.length; i < len; i++)
b += a[i];
return b;
}
function f2() {
var a = [1, 2, 3];
for (var i = 0; i < a.length; i++)
a[i]++;
return a[2];
}
console.log(f0(), f1(), f2());
}
expect_stdout: "6 6 4"
} }

View File

@@ -151,13 +151,13 @@ issue_1841_2: {
function_returning_constant_literal: { function_returning_constant_literal: {
options = { options = {
reduce_vars: true,
unsafe: true,
toplevel: true,
evaluate: true,
cascade: true,
unused: true,
inline: true, inline: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
unsafe: true,
unused: true,
} }
input: { input: {
function greeter() { function greeter() {

View File

@@ -2954,3 +2954,32 @@ const_expr_2: {
} }
expect_stdout: "2 2" expect_stdout: "2 2"
} }
escaped_prop: {
options = {
collapse_vars: true,
evaluate: true,
inline: true,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
toplevel: true,
unsafe: true,
unused: true,
}
input: {
var obj = { o: { a: 1 } };
(function(o) {
o.a++;
})(obj.o);
(function(o) {
console.log(o.a);
})(obj.o);
}
expect: {
var obj = { o: { a: 1 } };
obj.o.a++;
console.log(obj.o.a);
}
expect_stdout: "2"
}