enhance evaluate & inline (#3580)

This commit is contained in:
Alex Lam S.L
2019-11-13 04:17:09 +08:00
committed by GitHub
parent 0d17c5b0fa
commit d6fd18d0b0
7 changed files with 45 additions and 37 deletions

View File

@@ -3059,15 +3059,15 @@ merge(Compressor.prototype, {
}); });
def(AST_Call, function(compressor, cached, depth) { def(AST_Call, function(compressor, cached, depth) {
var exp = this.expression; var exp = this.expression;
if (exp instanceof AST_SymbolRef) { var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
var fn = exp.fixed_value(); if (fn instanceof AST_Lambda) {
if (!(fn instanceof AST_Lambda)) return this; if (fn.evaluating) return this;
if (fn.name && fn.name.definition().recursive_refs > 0) return this; if (fn.name && fn.name.definition().recursive_refs > 0) return this;
if (fn.body.length != 1 || !fn.is_constant_expression()) return this;
var stat = fn.body[0]; var stat = fn.body[0];
if (!(stat instanceof AST_Return)) return this; if (!(stat instanceof AST_Return)) return this;
var args = eval_args(this.args); var args = eval_args(this.args);
if (!args) return this; if (!args) return this;
if (!stat.value) return undefined;
fn.argnames.forEach(function(sym, i) { fn.argnames.forEach(function(sym, i) {
var value = args[i]; var value = args[i];
sym.definition().references.forEach(function(node) { sym.definition().references.forEach(function(node) {
@@ -3077,8 +3077,9 @@ merge(Compressor.prototype, {
cached.push(node); cached.push(node);
}); });
}); });
if (!stat.value) return undefined; fn.evaluating = true;
var val = stat.value._eval(compressor, cached, depth); var val = stat.value._eval(compressor, cached, depth);
delete fn.evaluating;
if (val === stat.value) return this; if (val === stat.value) return this;
return val; return val;
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) { } else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
@@ -5280,15 +5281,10 @@ merge(Compressor.prototype, {
} }
var stat = is_func && fn.body[0]; var stat = is_func && fn.body[0];
var can_inline = compressor.option("inline") && !self.is_expr_pure(compressor); var can_inline = compressor.option("inline") && !self.is_expr_pure(compressor);
if (can_inline && stat instanceof AST_Return) { if (exp === fn && can_inline && stat instanceof AST_Return) {
var value = stat.value; var value = stat.value;
if (!value || value.is_constant_expression()) { if (!value || value.is_constant_expression()) {
if (value) { var args = self.args.concat(value || make_node(AST_Undefined, self));
value = value.clone(true);
} else {
value = make_node(AST_Undefined, self);
}
var args = self.args.concat(value);
return make_sequence(self, args).optimize(compressor); return make_sequence(self, args).optimize(compressor);
} }
} }

View File

@@ -1458,7 +1458,7 @@ collapse_vars_constants: {
function f3(x) { function f3(x) {
var b = x.prop; var b = x.prop;
sideeffect1(); sideeffect1();
return b + (function() { return -9; })(); return b + -9;
} }
} }
} }
@@ -5748,7 +5748,7 @@ issue_3215_1: {
}()); }());
} }
expect: { expect: {
console.log("number"); console.log(typeof 42);
} }
expect_stdout: "number" expect_stdout: "number"
} }

View File

@@ -892,9 +892,7 @@ issue_2860_1: {
}()); }());
} }
expect: { expect: {
console.log(function(a) { console.log(1);
return 1 ^ a;
}());
} }
expect_stdout: "1" expect_stdout: "1"
} }

View File

@@ -1900,3 +1900,24 @@ issue_3568: {
} }
expect_stdout: "NaN" expect_stdout: "NaN"
} }
conditional_function: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
}
input: {
function f(a) {
return a && "undefined" != typeof A ? A : 42;
}
console.log(f(0), f(1));
}
expect: {
function f(a) {
return a && "undefined" != typeof A ? A : 42;
}
console.log(42, f(1));
}
expect_stdout: "42 42"
}

View File

@@ -2265,7 +2265,8 @@ issue_3054: {
return { a: !0 }; return { a: !0 };
} }
console.log(function(b) { console.log(function(b) {
return { a: !(b = !1) }; b = !1;
return f();
}().a, f.call().a); }().a, f.call().a);
} }
expect_stdout: "true true" expect_stdout: "true true"

View File

@@ -1427,13 +1427,13 @@ defun_inline_3: {
defun_call: { defun_call: {
options = { options = {
inline: true, evaluate: true,
reduce_funcs: true, reduce_funcs: true,
reduce_vars: true, reduce_vars: true,
unused: true, unused: true,
} }
input: { input: {
function f() { console.log(function f() {
return g() + h(1) - h(g(), 2, 3); return g() + h(1) - h(g(), 2, 3);
function g() { function g() {
return 4; return 4;
@@ -1441,21 +1441,17 @@ defun_call: {
function h(a) { function h(a) {
return a; return a;
} }
} }());
} }
expect: { expect: {
function f() { console.log(1);
return 4 + h(1) - h(4);
function h(a) {
return a;
}
}
} }
expect_stdout: "1"
} }
defun_redefine: { defun_redefine: {
options = { options = {
inline: true, evaluate: true,
reduce_funcs: true, reduce_funcs: true,
reduce_vars: true, reduce_vars: true,
unused: true, unused: true,
@@ -1480,7 +1476,7 @@ defun_redefine: {
(function() { (function() {
return 3; return 3;
}); });
return 3 + 2; return 5;
} }
console.log(f()); console.log(f());
} }
@@ -1517,7 +1513,7 @@ func_inline: {
func_modified: { func_modified: {
options = { options = {
inline: true, evaluate: true,
reduce_funcs: true, reduce_funcs: true,
reduce_vars: true, reduce_vars: true,
unused: true, unused: true,
@@ -1550,7 +1546,7 @@ func_modified: {
(function() { (function() {
return 4; return 4;
}); });
return 1 + 2 + 4; return 7;
} }
console.log(f()); console.log(f());
} }
@@ -5516,9 +5512,7 @@ issue_2860_1: {
}()); }());
} }
expect: { expect: {
console.log(function(a) { console.log(1);
return 1 ^ a;
}());
} }
expect_stdout: "1" expect_stdout: "1"
} }

View File

@@ -166,9 +166,7 @@ duplicate_lambda_arg_name: {
}()); }());
} }
expect: { expect: {
console.log(function long_name(long_name) { console.log("undefined");
return typeof long_name;
}());
} }
expect_stdout: "undefined" expect_stdout: "undefined"
} }