support safe reassignments in reduce_vars

`var a=1;a=2;x(a)` => `x(2)`
This commit is contained in:
alexlamsl 2017-04-18 03:08:25 +08:00
parent d1aa09c5c7
commit 9dc5247bc7
2 changed files with 36 additions and 22 deletions

View File

@ -267,7 +267,7 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef) {
var d = node.definition();
d.references.push(node);
if (d.fixed === undefined || !is_safe(d)
if (d.fixed === undefined || !safe_to_read(d)
|| is_modified(node, 0, is_immutable(node.fixed_value()))) {
d.fixed = false;
}
@ -277,7 +277,7 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_VarDef) {
var d = node.name.definition();
if (d.fixed == null) {
if (safe_to_assign(d, node.value)) {
if (node.value) {
d.fixed = function() {
return node.value;
@ -297,7 +297,7 @@ merge(Compressor.prototype, {
&& node.operator == "="
&& node.left instanceof AST_SymbolRef) {
var d = node.left.definition();
if (HOP(safe_ids, d.id) && d.fixed == null) {
if (HOP(safe_ids, d.id) && safe_to_assign(d, node.right)) {
d.fixed = function() {
return node.right;
};
@ -309,7 +309,7 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Defun) {
var d = node.name.definition();
if (!toplevel && d.global || is_safe(d)) {
if (!toplevel && d.global || safe_to_read(d)) {
d.fixed = false;
} else {
d.fixed = node;
@ -385,11 +385,19 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_For) {
if (node.init) node.init.walk(tw);
if (node.condition) {
push();
node.condition.walk(tw);
pop();
}
push();
if (node.condition) node.condition.walk(tw);
node.body.walk(tw);
if (node.step) node.step.walk(tw);
pop();
if (node.step) {
push();
node.step.walk(tw);
pop();
}
return true;
}
if (node instanceof AST_ForIn) {
@ -426,7 +434,7 @@ merge(Compressor.prototype, {
safe_ids[def.id] = safe;
}
function is_safe(def) {
function safe_to_read(def) {
if (safe_ids[def.id]) {
if (def.fixed == null) {
var orig = def.orig[0];
@ -437,6 +445,17 @@ merge(Compressor.prototype, {
}
}
function safe_to_assign(def, value) {
if (def.fixed === undefined) return true;
if (def.fixed === false) return false;
if (def.fixed != null && (!value || def.references.length > 0)) return false;
return !def.orig.some(function(sym) {
return sym instanceof AST_SymbolConst
|| sym instanceof AST_SymbolDefun
|| sym instanceof AST_SymbolLambda;
});
}
function push() {
safe_ids = Object.create(safe_ids);
}

View File

@ -66,7 +66,7 @@ modified: {
conditionals : true,
evaluate : true,
reduce_vars : true,
unused : true
unused : true,
}
input: {
function f0() {
@ -136,12 +136,11 @@ modified: {
}
function f2() {
var b = 2;
b = 3;
console.log(1 + b);
console.log(b + 3);
3;
console.log(4);
console.log(1 + b + 3);
console.log(6);
console.log(4);
console.log(7);
}
function f3() {
@ -375,12 +374,11 @@ passes: {
}
expect: {
function f() {
var b = 2;
b = 3;
console.log(1 + b);
console.log(b + 3);
3;
console.log(4);
console.log(1 + b + 3);
console.log(6);
console.log(4);
console.log(7);
}
}
}
@ -1828,10 +1826,7 @@ redefine_farg_3: {
console.log(function(a) {
var a;
return typeof a;
}([]), "number", function(a) {
var a = void 0;
return typeof a;
}([]));
}([]), "number", "undefined");
}
expect_stdout: "object number undefined"
}