Skip to content

Commit

Permalink
Merge pull request #1567 from alexlamsl/harmony-v2.8.8
Browse files Browse the repository at this point in the history
Merging from master for 2.8.8
  • Loading branch information
alexlamsl authored Mar 7, 2017
2 parents 240383a + 250b782 commit 952e265
Show file tree
Hide file tree
Showing 12 changed files with 900 additions and 145 deletions.
27 changes: 26 additions & 1 deletion lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,20 @@ var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos
}, null);

var AST_Node = DEFNODE("Node", "start end", {
clone: function() {
_clone: function(deep) {
if (deep) {
var self = this.clone();
return self.transform(new TreeTransformer(function(node) {
if (node !== self) {
return node.clone(true);
}
}));
}
return new this.CTOR(this);
},
clone: function(deep) {
return this._clone(deep);
},
$documentation: "Base class of all AST nodes",
$propdoc: {
start: "[AST_Token] The first token of this node",
Expand Down Expand Up @@ -199,6 +210,20 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
this.label._walk(visitor);
this.body._walk(visitor);
});
},
clone: function(deep) {
var node = this._clone(deep);
if (deep) {
var refs = node.label.references;
var label = this.label;
node.walk(new TreeWalker(function(node) {
if (node instanceof AST_LoopControl
&& node.label && node.label.thedef === label) {
refs.push(node);
}
}));
}
return node;
}
}, AST_StatementWithBody);

Expand Down
69 changes: 52 additions & 17 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ merge(Compressor.prototype, {

AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan){
var reduce_vars = rescan && compressor.option("reduce_vars");
var toplevel = compressor.option("toplevel");
var ie8 = !compressor.option("screw_ie8");
var safe_ids = [];
push();
Expand All @@ -245,7 +246,8 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef) {
var d = node.definition();
d.references.push(node);
if (!d.fixed || isModified(node, 0) || !is_safe(d)) {
if (!d.fixed || !is_safe(d)
|| is_modified(node, 0, d.fixed instanceof AST_Lambda)) {
d.fixed = false;
}
}
Expand All @@ -265,6 +267,21 @@ merge(Compressor.prototype, {
}
}
}
if (node instanceof AST_Defun) {
var d = node.name.definition();
if (!toplevel && d.global || is_safe(d)) {
d.fixed = false;
} else {
d.fixed = node;
mark_as_safe(d);
}
var save_ids = safe_ids;
safe_ids = [];
push();
descend();
safe_ids = save_ids;
return true;
}
var iife;
if (node instanceof AST_Function
&& (iife = tw.parent()) instanceof AST_Call
Expand Down Expand Up @@ -339,18 +356,22 @@ merge(Compressor.prototype, {
}

function reset_def(def) {
def.fixed = undefined;
if (toplevel || !def.global || def.orig[0] instanceof AST_SymbolConst) {
def.fixed = undefined;
} else {
def.fixed = false;
}
def.references = [];
def.should_replace = undefined;
}

function isModified(node, level) {
function is_modified(node, level, func) {
var parent = tw.parent(level);
if (isLHS(node, parent)
|| parent instanceof AST_Call && parent.expression === node) {
|| !func && parent instanceof AST_Call && parent.expression === node) {
return true;
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
return isModified(parent, level + 1);
return !func && is_modified(parent, level + 1);
}
}
});
Expand Down Expand Up @@ -548,11 +569,14 @@ merge(Compressor.prototype, {
// Constant single use vars can be replaced in any scope.
if (var_decl.value.is_constant()) {
var ctt = new TreeTransformer(function(node) {
if (node === ref
&& !ctt.find_parent(AST_Destructuring)
&& !ctt.find_parent(AST_ForIn)) {
return replace_var(node, ctt.parent(), true);
if (node instanceof AST_Destructuring) return node;
var parent = ctt.parent();
if (parent instanceof AST_IterationStatement
&& (parent.condition === node || parent.init === node)) {
return node;
}
if (node === ref)
return replace_var(node, parent, true);
});
stat.transform(ctt);
continue;
Expand Down Expand Up @@ -1325,14 +1349,7 @@ merge(Compressor.prototype, {
def(AST_Statement, function(){
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
});
// XXX: AST_Accessor and AST_Function both inherit from AST_Scope,
// which itself inherits from AST_Statement; however, they aren't
// really statements. This could bite in other places too. :-(
// Wish JS had multiple inheritance.
def(AST_Accessor, function(){
throw def;
});
def(AST_Function, function(){
def(AST_Lambda, function(){
throw def;
});
def(AST_Arrow, function() {
Expand Down Expand Up @@ -2714,6 +2731,24 @@ merge(Compressor.prototype, {

OPT(AST_Call, function(self, compressor){
var exp = self.expression;
if (compressor.option("reduce_vars")
&& exp instanceof AST_SymbolRef) {
var def = exp.definition();
if (def.fixed instanceof AST_Defun) {
def.fixed = make_node(AST_Function, def.fixed, def.fixed).clone(true);
}
if (def.fixed instanceof AST_Function) {
exp = def.fixed;
if (compressor.option("unused")
&& def.references.length == 1
&& compressor.find_parent(AST_Scope) === def.scope) {
if (!compressor.option("keep_fnames")) {
exp.name = null;
}
self.expression = exp;
}
}
}
if (compressor.option("unused")
&& exp instanceof AST_Function
&& !exp.uses_arguments
Expand Down
13 changes: 3 additions & 10 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,9 @@ function OutputStream(options) {
output.with_parens(function(){
self.condition.print(output);
});
output.semicolon();
if (output.option("beautify") && output.option("screw_ie8")) {
output.semicolon();
}
});
DEFPRINT(AST_While, function(self, output){
output.print("while");
Expand Down Expand Up @@ -1096,15 +1098,6 @@ function OutputStream(options) {
// adds the block brackets if needed.
if (!self.body)
return output.force_semicolon();
if (self.body instanceof AST_Do) {
// Unconditionally use the if/do-while workaround for all browsers.
// https://github.com/mishoo/UglifyJS/issues/#issue/57 IE
// croaks with "syntax error" on code like this: if (foo)
// do ... while(cond); else ... we need block brackets
// around do/while
make_block(self.body, output);
return;
}
var b = self.body;
while (true) {
if (b instanceof AST_If) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"homepage": "http://lisperator.net/uglifyjs",
"author": "Mihai Bazon <[email protected]> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "2.8.7",
"version": "2.8.8",
"engines": {
"node": ">=0.8.0"
},
Expand Down
45 changes: 38 additions & 7 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,57 @@ var results = {};
var remaining = 2 * urls.length;
function done() {
if (!--remaining) {
var failures = [];
urls.forEach(function(url) {
var info = results[url];
console.log();
console.log(url);
console.log(results[url].time);
console.log("SHA1:", results[url].sha1);
console.log(info.log);
var elapsed = 0;
info.log.replace(/: ([0-9]+\.[0-9]{3})s/g, function(match, time) {
elapsed += parseFloat(time);
});
console.log("Run-time:", elapsed.toFixed(3), "s");
console.log("Original:", info.input, "bytes");
console.log("Uglified:", info.output, "bytes");
console.log("SHA1 sum:", info.sha1);
if (info.code) {
failures.push(url);
}
});
if (failures.length) {
console.error("Benchmark failed:");
failures.forEach(function(url) {
console.error(url);
});
process.exit(1);
}
}
}
urls.forEach(function(url) {
results[url] = { time: "" };
results[url] = {
input: 0,
output: 0,
log: ""
};
require(url.slice(0, url.indexOf(":"))).get(url, function(res) {
var uglifyjs = fork("bin/uglifyjs", args, { silent: true });
res.pipe(uglifyjs.stdin);
uglifyjs.stdout.pipe(createHash("sha1")).on("data", function(data) {
res.on("data", function(data) {
results[url].input += data.length;
}).pipe(uglifyjs.stdin);
uglifyjs.stdout.on("data", function(data) {
results[url].output += data.length;
}).pipe(createHash("sha1")).on("data", function(data) {
results[url].sha1 = data.toString("hex");
done();
});
uglifyjs.stderr.setEncoding("utf8");
uglifyjs.stderr.on("data", function(data) {
results[url].time += data;
}).on("end", done)
results[url].log += data;
});
uglifyjs.on("exit", function(code) {
results[url].code = code;
done();
});
});
});
50 changes: 42 additions & 8 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ collapse_vars_do_while: {
}
input: {
function f1(y) {
// The constant do-while condition `c` will be replaced.
// The constant do-while condition `c` will not be replaced.
var c = 9;
do { } while (c === 77);
do {} while (c === 77);
}
function f2(y) {
// The non-constant do-while condition `c` will not be replaced.
Expand Down Expand Up @@ -381,7 +381,8 @@ collapse_vars_do_while: {
}
expect: {
function f1(y) {
do ; while (false);
var c = 9;
do ; while (77 === c);
}
function f2(y) {
var c = 5 - y;
Expand Down Expand Up @@ -418,9 +419,9 @@ collapse_vars_do_while_drop_assign: {
}
input: {
function f1(y) {
// The constant do-while condition `c` will be replaced.
// The constant do-while condition `c` will be not replaced.
var c = 9;
do { } while (c === 77);
do {} while (c === 77);
}
function f2(y) {
// The non-constant do-while condition `c` will not be replaced.
Expand Down Expand Up @@ -455,7 +456,8 @@ collapse_vars_do_while_drop_assign: {
}
expect: {
function f1(y) {
do ; while (false);
var c = 9;
do ; while (77 === c);
}
function f2(y) {
var c = 5 - y;
Expand Down Expand Up @@ -1309,8 +1311,8 @@ collapse_vars_regexp: {
};
}
(function(){
var result, rx = /ab*/g;
while (result = rx.exec('acdabcdeabbb'))
var result, s = "acdabcdeabbb", rx = /ab*/g;
while (result = rx.exec(s))
console.log(result[0]);
})();
}
Expand Down Expand Up @@ -1421,3 +1423,35 @@ issue_1537_destructuring_for_of: {
})();
}
}

issue_1562: {
options = {
collapse_vars: true,
}
input: {
var v = 1, B = 2;
for (v in objs) f(B);

var x = 3, C = 10;
while(x + 2) bar(C);

var y = 4, D = 20;
do bar(D); while(y + 2);

var z = 5, E = 30;
for (; f(z + 2) ;) bar(E);
}
expect: {
var v = 1;
for (v in objs) f(2);

var x = 3;
while(x + 2) bar(10);

var y = 4;
do bar(20); while(y + 2);

var z = 5;
for (; f(z + 2) ;) bar(30);
}
}
2 changes: 2 additions & 0 deletions test/compress/dead-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ dead_code_const_annotation: {
conditionals : true,
evaluate : true,
reduce_vars : true,
toplevel : true,
};
input: {
var unused;
Expand Down Expand Up @@ -194,6 +195,7 @@ dead_code_const_annotation_complex_scope: {
conditionals : true,
evaluate : true,
reduce_vars : true,
toplevel : true,
};
input: {
var unused_var;
Expand Down
Loading

0 comments on commit 952e265

Please sign in to comment.