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

View File

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