Skip to content

Commit

Permalink
enhance evaluate & inline (#3580)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Nov 12, 2019
1 parent 0d17c5b commit d6fd18d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 37 deletions.
20 changes: 8 additions & 12 deletions lib/compress.js
Expand Up @@ -3059,15 +3059,15 @@ merge(Compressor.prototype, {
});
def(AST_Call, function(compressor, cached, depth) {
var exp = this.expression;
if (exp instanceof AST_SymbolRef) {
var fn = exp.fixed_value();
if (!(fn instanceof AST_Lambda)) return this;
var fn = exp instanceof AST_SymbolRef ? exp.fixed_value() : exp;
if (fn instanceof AST_Lambda) {
if (fn.evaluating) 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];
if (!(stat instanceof AST_Return)) return this;
var args = eval_args(this.args);
if (!args) return this;
if (!stat.value) return undefined;
fn.argnames.forEach(function(sym, i) {
var value = args[i];
sym.definition().references.forEach(function(node) {
Expand All @@ -3077,8 +3077,9 @@ merge(Compressor.prototype, {
cached.push(node);
});
});
if (!stat.value) return undefined;
fn.evaluating = true;
var val = stat.value._eval(compressor, cached, depth);
delete fn.evaluating;
if (val === stat.value) return this;
return val;
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
Expand Down Expand Up @@ -5280,15 +5281,10 @@ merge(Compressor.prototype, {
}
var stat = is_func && fn.body[0];
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;
if (!value || value.is_constant_expression()) {
if (value) {
value = value.clone(true);
} else {
value = make_node(AST_Undefined, self);
}
var args = self.args.concat(value);
var args = self.args.concat(value || make_node(AST_Undefined, self));
return make_sequence(self, args).optimize(compressor);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/compress/collapse_vars.js
Expand Up @@ -1458,7 +1458,7 @@ collapse_vars_constants: {
function f3(x) {
var b = x.prop;
sideeffect1();
return b + (function() { return -9; })();
return b + -9;
}
}
}
Expand Down Expand Up @@ -5748,7 +5748,7 @@ issue_3215_1: {
}());
}
expect: {
console.log("number");
console.log(typeof 42);
}
expect_stdout: "number"
}
Expand Down
4 changes: 1 addition & 3 deletions test/compress/dead-code.js
Expand Up @@ -892,9 +892,7 @@ issue_2860_1: {
}());
}
expect: {
console.log(function(a) {
return 1 ^ a;
}());
console.log(1);
}
expect_stdout: "1"
}
Expand Down
21 changes: 21 additions & 0 deletions test/compress/evaluate.js
Expand Up @@ -1900,3 +1900,24 @@ issue_3568: {
}
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"
}
3 changes: 2 additions & 1 deletion test/compress/functions.js
Expand Up @@ -2265,7 +2265,8 @@ issue_3054: {
return { a: !0 };
}
console.log(function(b) {
return { a: !(b = !1) };
b = !1;
return f();
}().a, f.call().a);
}
expect_stdout: "true true"
Expand Down
26 changes: 10 additions & 16 deletions test/compress/reduce_vars.js
Expand Up @@ -1427,35 +1427,31 @@ defun_inline_3: {

defun_call: {
options = {
inline: true,
evaluate: true,
reduce_funcs: true,
reduce_vars: true,
unused: true,
}
input: {
function f() {
console.log(function f() {
return g() + h(1) - h(g(), 2, 3);
function g() {
return 4;
}
function h(a) {
return a;
}
}
}());
}
expect: {
function f() {
return 4 + h(1) - h(4);
function h(a) {
return a;
}
}
console.log(1);
}
expect_stdout: "1"
}

defun_redefine: {
options = {
inline: true,
evaluate: true,
reduce_funcs: true,
reduce_vars: true,
unused: true,
Expand All @@ -1480,7 +1476,7 @@ defun_redefine: {
(function() {
return 3;
});
return 3 + 2;
return 5;
}
console.log(f());
}
Expand Down Expand Up @@ -1517,7 +1513,7 @@ func_inline: {

func_modified: {
options = {
inline: true,
evaluate: true,
reduce_funcs: true,
reduce_vars: true,
unused: true,
Expand Down Expand Up @@ -1550,7 +1546,7 @@ func_modified: {
(function() {
return 4;
});
return 1 + 2 + 4;
return 7;
}
console.log(f());
}
Expand Down Expand Up @@ -5516,9 +5512,7 @@ issue_2860_1: {
}());
}
expect: {
console.log(function(a) {
return 1 ^ a;
}());
console.log(1);
}
expect_stdout: "1"
}
Expand Down
4 changes: 1 addition & 3 deletions test/compress/typeof.js
Expand Up @@ -166,9 +166,7 @@ duplicate_lambda_arg_name: {
}());
}
expect: {
console.log(function long_name(long_name) {
return typeof long_name;
}());
console.log("undefined");
}
expect_stdout: "undefined"
}
Expand Down

0 comments on commit d6fd18d

Please sign in to comment.