improve literal return optimization (#1860)

This commit is contained in:
kzc
2017-05-01 12:10:11 -04:00
committed by Alex Lam S.L
parent 2cb55b2ad0
commit ea9289771b
4 changed files with 55 additions and 12 deletions

View File

@@ -1780,6 +1780,35 @@ merge(Compressor.prototype, {
node.DEFMETHOD("has_side_effects", func); node.DEFMETHOD("has_side_effects", func);
}); });
// determine if expression is constant
(function(def){
function all(list) {
for (var i = list.length; --i >= 0;)
if (!list[i].is_constant_expression())
return false;
return true;
}
def(AST_Node, return_false);
def(AST_Constant, return_true);
def(AST_Unary, function(){
return this.expression.is_constant_expression();
});
def(AST_Binary, function(){
return this.left.is_constant_expression() && this.right.is_constant_expression();
});
def(AST_Array, function(){
return all(this.elements);
});
def(AST_Object, function(){
return all(this.properties);
});
def(AST_ObjectProperty, function(){
return this.value.is_constant_expression();
});
})(function(node, func){
node.DEFMETHOD("is_constant_expression", func);
});
// tell me if a statement aborts // tell me if a statement aborts
function aborts(thing) { function aborts(thing) {
return thing && thing.aborts(); return thing && thing.aborts();
@@ -3004,7 +3033,7 @@ merge(Compressor.prototype, {
if (exp instanceof AST_Function) { if (exp instanceof AST_Function) {
if (exp.body[0] instanceof AST_Return) { if (exp.body[0] instanceof AST_Return) {
var value = exp.body[0].value; var value = exp.body[0].value;
if (!value || value.is_constant()) { if (!value || value.is_constant_expression()) {
var args = self.args.concat(value || make_node(AST_Undefined, self)); var args = self.args.concat(value || make_node(AST_Undefined, self));
return make_sequence(self, args).transform(compressor); return make_sequence(self, args).transform(compressor);
} }

View File

@@ -145,3 +145,25 @@ issue_1841_2: {
} }
expect_exact: "42" expect_exact: "42"
} }
function_returning_constant_literal: {
options = {
reduce_vars: true,
unsafe: true,
toplevel: true,
evaluate: true,
cascade: true,
unused: true,
}
input: {
function greeter() {
return { message: 'Hello there' };
}
var greeting = greeter();
console.log(greeting.message);
}
expect: {
console.log("Hello there");
}
expect_stdout: "Hello there"
}

View File

@@ -10,10 +10,6 @@ unary_prefix: {
return x; return x;
}()); }());
} }
expect: { expect_exact: "console.log(-2/3);"
console.log(function() {
return -2 / 3;
}());
}
expect_stdout: true expect_stdout: true
} }

View File

@@ -25,11 +25,9 @@ negate_iife_2: {
negate_iife: true negate_iife: true
}; };
input: { input: {
(function(){ return {} })().x = 10; // should not transform this one
}
expect: {
(function(){ return {} })().x = 10; (function(){ return {} })().x = 10;
} }
expect_exact: "({}).x=10;"
} }
negate_iife_2_side_effects: { negate_iife_2_side_effects: {
@@ -38,11 +36,9 @@ negate_iife_2_side_effects: {
side_effects: true, side_effects: true,
} }
input: { input: {
(function(){ return {} })().x = 10; // should not transform this one
}
expect: {
(function(){ return {} })().x = 10; (function(){ return {} })().x = 10;
} }
expect_exact: "({}).x=10;"
} }
negate_iife_3: { negate_iife_3: {