Merge branch 'master' into harmony

This commit is contained in:
alexlamsl
2017-03-31 12:54:03 +08:00
21 changed files with 495 additions and 145 deletions

View File

@@ -197,11 +197,10 @@ merge(Compressor.prototype, {
});
AST_Node.DEFMETHOD("equivalent_to", function(node){
// XXX: this is a rather expensive way to test two node's equivalence:
return this.print_to_string() == node.print_to_string();
return this.TYPE == node.TYPE && this.print_to_string() == node.print_to_string();
});
AST_Node.DEFMETHOD("process_expression", function(insert) {
AST_Node.DEFMETHOD("process_expression", function(insert, compressor) {
var self = this;
var tt = new TreeTransformer(function(node) {
if (insert && node instanceof AST_SimpleStatement) {
@@ -210,6 +209,12 @@ merge(Compressor.prototype, {
});
}
if (!insert && node instanceof AST_Return) {
if (compressor) {
var value = node.value && node.value.drop_side_effect_free(compressor, true);
return value ? make_node(AST_SimpleStatement, node, {
body: value
}) : make_node(AST_EmptyStatement, node);
}
return make_node(AST_SimpleStatement, node, {
body: node.value || make_node(AST_Undefined, node)
});
@@ -418,18 +423,17 @@ merge(Compressor.prototype, {
value: val
});
case "number":
if (isNaN(val)) {
return make_node(AST_NaN, orig);
}
if ((1 / val) < 0) {
return make_node(AST_UnaryPrefix, orig, {
if (isNaN(val)) return make_node(AST_NaN, orig);
if (isFinite(val)) {
return 1 / val < 0 ? make_node(AST_UnaryPrefix, orig, {
operator: "-",
expression: make_node(AST_Number, orig, { value: -val })
});
}) : make_node(AST_Number, orig, { value: val });
}
return make_node(AST_Number, orig, { value: val });
return val < 0 ? make_node(AST_UnaryPrefix, orig, {
operator: "-",
expression: make_node(AST_Infinity, orig)
}) : make_node(AST_Infinity, orig);
case "boolean":
return make_node(val ? AST_True : AST_False, orig);
case "undefined":
@@ -2006,8 +2010,7 @@ merge(Compressor.prototype, {
var sym = def.name.definition();
if (sym.id in in_use_ids) return true;
if (!drop_vars && sym.global) return true;
if (sym.orig[0] instanceof AST_SymbolCatch
&& sym.scope.parent_scope.find_variable(def.name).orig[0] === def.name) {
if (sym.orig[0] instanceof AST_SymbolCatch) {
def.value = def.value && def.value.drop_side_effect_free(compressor);
return true;
}
@@ -2278,7 +2281,7 @@ merge(Compressor.prototype, {
if (this.expression instanceof AST_Function
&& (!this.expression.name || !this.expression.name.definition().references.length)) {
var node = this.clone();
node.expression = node.expression.process_expression(false);
node.expression = node.expression.process_expression(false, compressor);
return node;
}
return this;
@@ -2642,7 +2645,6 @@ merge(Compressor.prototype, {
self.expression = best_of_expression(expression, self.expression);
}
if (!compressor.option("dead_code")) return self;
var prev_block;
var decl = [];
var body = [];
var default_branch;
@@ -2671,14 +2673,16 @@ merge(Compressor.prototype, {
}
}
if (aborts(branch)) {
var block = make_node(AST_BlockStatement, branch, branch).print_to_string();
if (!fallthrough && prev_block === block) body[body.length - 1].body = [];
if (body.length > 0 && !fallthrough) {
var prev = body[body.length - 1];
if (prev.body.length == branch.body.length
&& make_node(AST_BlockStatement, prev, prev).equivalent_to(make_node(AST_BlockStatement, branch, branch)))
prev.body = [];
}
body.push(branch);
prev_block = block;
fallthrough = false;
} else {
body.push(branch);
prev_block = null;
fallthrough = true;
}
}
@@ -2724,6 +2728,15 @@ merge(Compressor.prototype, {
OPT(AST_Try, function(self, compressor){
self.body = tighten_body(self.body, compressor);
if (self.bcatch && self.bfinally && all(self.bfinally.body, is_empty)) self.bfinally = null;
if (all(self.body, is_empty)) {
var body = [];
if (self.bcatch) extract_declarations_from_unreachable_code(compressor, self.bcatch, body);
if (self.bfinally) body = body.concat(self.bfinally.body);
return body.length > 0 ? make_node(AST_BlockStatement, self, {
body: body
}).optimize(compressor) : make_node(AST_EmptyStatement, self);
}
return self;
});
@@ -3009,11 +3022,9 @@ merge(Compressor.prototype, {
return AST_Seq.from_array(args).transform(compressor);
}
}
if (compressor.option("side_effects")) {
if (!AST_Block.prototype.has_side_effects.call(exp, compressor)) {
var args = self.args.concat(make_node(AST_Undefined, self));
return AST_Seq.from_array(args).transform(compressor);
}
if (compressor.option("side_effects") && all(exp.body, is_empty)) {
var args = self.args.concat(make_node(AST_Undefined, self));
return AST_Seq.from_array(args).transform(compressor);
}
}
if (compressor.option("drop_console")) {
@@ -3164,8 +3175,17 @@ merge(Compressor.prototype, {
})).optimize(compressor);
}
}
if (e instanceof AST_Binary
&& (self.operator == "+" || self.operator == "-")
&& (e.operator == "*" || e.operator == "/" || e.operator == "%")) {
self.expression = e.left;
e.left = self;
return e.optimize(compressor);
}
// avoids infinite recursion of numerals
if (self.operator != "-" || !(self.expression instanceof AST_Number)) {
if (self.operator != "-"
|| !(self.expression instanceof AST_Number
|| self.expression instanceof AST_Infinity)) {
var ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);
@@ -3597,9 +3617,9 @@ merge(Compressor.prototype, {
case "undefined":
return make_node(AST_Undefined, self).optimize(compressor);
case "NaN":
return make_node(AST_NaN, self).optimize(compressor);
return make_node(AST_NaN, self);
case "Infinity":
return make_node(AST_Infinity, self).optimize(compressor);
return make_node(AST_Infinity, self);
}
}
if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
@@ -3627,14 +3647,6 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_Infinity, function (self, compressor) {
return make_node(AST_Binary, self, {
operator : '/',
left : make_node(AST_Number, self, {value: 1}),
right : make_node(AST_Number, self, {value: 0})
});
});
OPT(AST_Undefined, function(self, compressor){
if (compressor.option("unsafe")) {
var scope = compressor.find_parent(AST_Scope);

View File

@@ -652,6 +652,15 @@ function OutputStream(options) {
&& this.operator !== "--";
});
PARENS([ AST_Infinity, AST_NaN ], function(output){
var p = output.parent();
return p instanceof AST_PropAccess && p.expression === this
|| p instanceof AST_Call && p.expression === this
|| p instanceof AST_Unary && p.operator != "+" && p.operator != "-"
|| p instanceof AST_Binary && p.right === this
&& (p.operator == "/" || p.operator == "%");
});
PARENS(AST_Seq, function(output){
var p = output.parent();
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
@@ -1139,24 +1148,24 @@ function OutputStream(options) {
self.expression.print(output);
});
output.space();
if (self.body.length > 0) output.with_block(function(){
self.body.forEach(function(stmt, i){
if (i) output.newline();
var last = self.body.length - 1;
if (last < 0) output.print("{}");
else output.with_block(function(){
self.body.forEach(function(branch, i){
output.indent(true);
stmt.print(output);
branch.print(output);
if (i < last && branch.body.length > 0)
output.newline();
});
});
else output.print("{}");
});
AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output){
if (this.body.length > 0) {
output.newline();
this.body.forEach(function(stmt){
output.indent();
stmt.print(output);
output.newline();
this.body.forEach(function(stmt){
output.indent();
stmt.print(output);
output.newline();
});
}
});
});
DEFPRINT(AST_Default, function(self, output){
output.print("default:");
@@ -1617,10 +1626,18 @@ function OutputStream(options) {
});
DEFPRINT(AST_Hole, noop);
DEFPRINT(AST_Infinity, function(self, output){
output.print("Infinity");
output.print("1");
output.space();
output.print("/");
output.space();
output.print("0");
});
DEFPRINT(AST_NaN, function(self, output){
output.print("NaN");
output.print("0");
output.space();
output.print("/");
output.space();
output.print("0");
});
DEFPRINT(AST_This, function(self, output){
output.print("this");

View File

@@ -87,9 +87,7 @@ SymbolDef.prototype = {
if (!options.screw_ie8 && sym instanceof AST_SymbolLambda)
s = s.parent_scope;
var def;
if (options.screw_ie8
&& sym instanceof AST_SymbolCatch
&& (def = s.parent_scope.find_variable(sym))) {
if (this.defun && (def = this.defun.variables.get(this.name))) {
this.mangled_name = def.mangled_name || def.name;
} else
this.mangled_name = s.next_mangled(options, this);
@@ -229,7 +227,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
}
}
else if (node instanceof AST_SymbolCatch) {
scope.def_variable(node, in_export, in_block);
scope.def_variable(node, in_export, in_block).defun = defun;
}
else if (node instanceof AST_LabelRef) {
var sym = labels.get(node.name);
@@ -293,7 +291,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
if (node instanceof AST_SymbolCatch) {
var name = node.name;
var refs = node.thedef.references;
var scope = node.thedef.scope.parent_scope.parent_scope;
var scope = node.thedef.defun;
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
refs.forEach(function(ref) {
ref.thedef = def;