Compare commits

..

8 Commits

Author SHA1 Message Date
Alex Lam S.L
f46281e2b7 v3.1.7 2017-11-05 15:03:19 +08:00
Alex Lam S.L
25a18883f5 tweak #2424 (#2432) 2017-11-05 12:49:14 +08:00
Alex Lam S.L
5b4b07e9a7 extend function inlining safety checks (#2430) 2017-11-05 06:18:45 +08:00
Alex Lam S.L
a8aa28a7a6 consolidate single-use function reduction (#2427)
fixes #2423
2017-11-05 04:27:01 +08:00
Alex Lam S.L
fe5a68f9d5 maintain call argument order in collapse_vars (#2426)
fixes #2425
2017-11-05 00:00:18 +08:00
Alex Lam S.L
71e61153b1 improve variations on call arguments for ufuzz (#2424) 2017-11-04 16:29:42 +08:00
Alex Lam S.L
c8b6f4733d reduce this within functions (#2421)
- only replace same-scope usages
- augment `test/ufuzz.js` to test for `this`


fixes #2420
2017-11-04 00:31:37 +08:00
Alex Lam S.L
a48f87abf2 compress new function containing this (#2417) 2017-10-30 23:19:27 +08:00
8 changed files with 537 additions and 87 deletions

View File

@@ -285,7 +285,7 @@ merge(Compressor.prototype, {
self.transform(tt);
});
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor) {
AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
var reduce_vars = compressor.option("reduce_vars");
var unused = compressor.option("unused");
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
@@ -315,12 +315,18 @@ merge(Compressor.prototype, {
d.fixed = false;
} else if (d.fixed) {
var value = node.fixed_value();
if (unused) {
d.single_use = value
&& d.references.length == 1
&& loop_ids[d.id] === in_loop
&& d.scope === node.scope
&& value.is_constant_expression();
if (unused && value && d.references.length == 1) {
if (value instanceof AST_Lambda) {
d.single_use = d.scope === node.scope
&& !(d.orig[0] instanceof AST_SymbolFunarg)
|| value.is_constant_expression(node.scope);
} else {
d.single_use = d.scope === node.scope
&& loop_ids[d.id] === in_loop
&& value.is_constant_expression();
}
} else {
d.single_use = false;
}
if (is_modified(node, value, 0, is_immutable(value))) {
if (d.single_use) {
@@ -377,6 +383,10 @@ merge(Compressor.prototype, {
} else {
d.fixed = node;
mark(d, true);
if (unused && d.references.length == 1) {
d.single_use = d.scope === d.references[0].scope
|| node.is_constant_expression(d.references[0].scope);
}
}
var save_ids = safe_ids;
safe_ids = Object.create(null);
@@ -527,6 +537,7 @@ merge(Compressor.prototype, {
}
return true;
}
return def.fixed instanceof AST_Defun;
}
function safe_to_assign(def, value) {
@@ -564,7 +575,10 @@ merge(Compressor.prototype, {
}
function is_immutable(value) {
return value && (value.is_constant() || value instanceof AST_Lambda);
if (!value) return false;
return value.is_constant()
|| value instanceof AST_Lambda
|| value instanceof AST_This;
}
function read_property(obj, key) {
@@ -592,7 +606,7 @@ merge(Compressor.prototype, {
|| !immutable
&& parent instanceof AST_Call
&& parent.expression === node
&& (!(value instanceof AST_Function) || value.contains_this())) {
&& (!(value instanceof AST_Function) || value.contains_this(parent))) {
return true;
} else if (parent instanceof AST_Array || parent instanceof AST_Object) {
return is_modified(parent, parent, level + 1);
@@ -805,6 +819,7 @@ merge(Compressor.prototype, {
function collapse(statements, compressor) {
var scope = compressor.find_parent(AST_Scope);
if (scope.uses_eval || scope.uses_with) return statements;
var args;
var candidates = [];
var stat_index = statements.length;
while (--stat_index >= 0) {
@@ -825,7 +840,7 @@ merge(Compressor.prototype, {
var one_off = lhs instanceof AST_Symbol && lhs.definition().references.length == 1;
var side_effects = value_has_side_effects(candidate);
var hit = candidate.name instanceof AST_SymbolFunarg;
var abort = false, replaced = false;
var abort = false, replaced = false, can_replace = !args || !hit;
var tt = new TreeTransformer(function(node, descend) {
if (abort) return node;
// Skip nodes before `candidate` as quickly as possible
@@ -850,7 +865,8 @@ merge(Compressor.prototype, {
return node;
}
// Replace variable with assignment when found
if (!(node instanceof AST_SymbolDeclaration)
if (can_replace
&& !(node instanceof AST_SymbolDeclaration)
&& !is_lhs(node, parent)
&& lhs.equivalent_to(node)) {
CHANGED = replaced = abort = true;
@@ -901,6 +917,12 @@ merge(Compressor.prototype, {
// Skip (non-executed) functions and (leading) default case in switch statements
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
});
if (!can_replace) {
for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; j < args.length; j++) {
args[j].transform(tt);
}
can_replace = true;
}
for (var i = stat_index; !abort && i < statements.length; i++) {
statements[i].transform(tt);
}
@@ -918,12 +940,18 @@ merge(Compressor.prototype, {
&& iife.expression === fn) {
var fn_strict = compressor.has_directive("use strict");
if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false;
var len = fn.argnames.length;
args = iife.args.slice(len);
var names = Object.create(null);
for (var i = fn.argnames.length; --i >= 0;) {
for (var i = len; --i >= 0;) {
var sym = fn.argnames[i];
var arg = iife.args[i];
args.unshift(make_node(AST_VarDef, sym, {
name: sym,
value: arg
}));
if (sym.name in names) continue;
names[sym.name] = true;
var arg = iife.args[i];
if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
else {
var tw = new TreeWalker(function(node) {
@@ -2148,18 +2176,22 @@ merge(Compressor.prototype, {
}
def(AST_Node, return_false);
def(AST_Constant, return_true);
def(AST_Function, function(){
def(AST_Lambda, function(scope){
var self = this;
var result = true;
self.walk(new TreeWalker(function(node) {
if (!result) return true;
if (node instanceof AST_SymbolRef) {
var def = node.definition();
if (self.enclosed.indexOf(def) >= 0
&& self.variables.get(def.name) !== def) {
if (member(def, self.enclosed)
&& !self.variables.has(def.name)) {
if (scope) {
var scope_def = scope.find_variable(node);
if (def.undeclared ? !scope_def : scope_def === def) return true;
}
result = false;
return true;
}
return true;
}
}));
return result;
@@ -2302,7 +2334,10 @@ merge(Compressor.prototype, {
});
return true;
}
if (assign_as_unused(node) instanceof AST_SymbolRef && scope === self) {
var sym;
if (scope === self
&& (sym = assign_as_unused(node)) instanceof AST_SymbolRef
&& self.variables.get(sym.name) === sym.definition()) {
if (node instanceof AST_Assign) node.right.walk(tw);
return true;
}
@@ -2375,8 +2410,10 @@ merge(Compressor.prototype, {
}
}
if (drop_funcs && node instanceof AST_Defun && node !== self) {
if (!(node.name.definition().id in in_use_ids)) {
var def = node.name.definition();
if (!(def.id in in_use_ids)) {
compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
drop_decl(def, node.name);
return make_node(AST_EmptyStatement, node);
}
return node;
@@ -2398,7 +2435,7 @@ merge(Compressor.prototype, {
if (var_defs.length > 1 && !def.value) {
compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
remove(var_defs, def);
remove(sym.orig, def.name);
drop_decl(sym, def.name);
return;
}
}
@@ -2431,7 +2468,7 @@ merge(Compressor.prototype, {
} else {
compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
}
remove(sym.orig, def.name);
drop_decl(sym, def.name);
}
});
if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) {
@@ -2440,7 +2477,7 @@ merge(Compressor.prototype, {
var def = tail.pop();
compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name));
remove(var_defs, def);
remove(def.name.definition().orig, def.name);
drop_decl(def.name.definition(), def.name);
side_effects.unshift(make_node(AST_Assign, def, {
operator: "=",
left: make_node(AST_SymbolRef, def.name, def.name),
@@ -2469,10 +2506,9 @@ merge(Compressor.prototype, {
}
}
if (drop_vars) {
var def = assign_as_unused(node);
if (def instanceof AST_SymbolRef
&& !((def = def.definition()).id in in_use_ids)
&& self.variables.get(def.name) === def) {
var sym = assign_as_unused(node);
if (sym instanceof AST_SymbolRef
&& !(sym.definition().id in in_use_ids)) {
if (node instanceof AST_Assign) {
return maintain_this_binding(parent, node, node.right.transform(tt));
}
@@ -2523,6 +2559,14 @@ merge(Compressor.prototype, {
col : sym.start.col
};
}
function drop_decl(def, decl) {
remove(def.orig, decl);
if (!def.orig.length) {
def.scope.functions.del(def.name);
def.scope.variables.del(def.name);
}
}
}
);
self.transform(tt);
@@ -4204,46 +4248,53 @@ merge(Compressor.prototype, {
if (compressor.option("unused")
&& fixed
&& d.references.length == 1
&& (d.single_use || fixed instanceof AST_Function
&& !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg)
&& !d.scope.uses_eval
&& compressor.find_parent(AST_Scope) === fixed.parent_scope)) {
&& d.single_use) {
var value = fixed.optimize(compressor);
return value === fixed ? fixed.clone(true) : value;
}
if (compressor.option("evaluate") && fixed) {
if (d.should_replace === undefined) {
var init = fixed.evaluate(compressor);
if (init !== fixed && (compressor.option("unsafe_regexp") || !(init instanceof RegExp))) {
init = make_node_from_constant(init, fixed);
var value_length = init.optimize(compressor).print_to_string().length;
var fn;
if (has_symbol_ref(fixed)) {
fn = function() {
var result = init.optimize(compressor);
return result === init ? result.clone(true) : result;
};
} else {
value_length = Math.min(value_length, fixed.print_to_string().length);
fn = function() {
var result = best_of_expression(init.optimize(compressor), fixed);
return result === init || result === fixed ? result.clone(true) : result;
};
}
var name_length = d.name.length;
var overhead = 0;
if (compressor.option("unused") && !compressor.exposed(d)) {
overhead = (name_length + 2 + value_length) / d.references.length;
}
d.should_replace = value_length <= name_length + overhead ? fn : false;
} else {
d.should_replace = false;
if (fixed && d.should_replace === undefined) {
var init;
if (fixed instanceof AST_This) {
if (!(d.orig[0] instanceof AST_SymbolFunarg)
&& all(d.references, function(ref) {
return d.scope === ref.scope;
})) {
init = fixed;
}
} else {
var ev = fixed.evaluate(compressor);
if (ev !== fixed && (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))) {
init = make_node_from_constant(ev, fixed);
}
}
if (d.should_replace) {
return d.should_replace();
if (init) {
var value_length = init.optimize(compressor).print_to_string().length;
var fn;
if (has_symbol_ref(fixed)) {
fn = function() {
var result = init.optimize(compressor);
return result === init ? result.clone(true) : result;
};
} else {
value_length = Math.min(value_length, fixed.print_to_string().length);
fn = function() {
var result = best_of_expression(init.optimize(compressor), fixed);
return result === init || result === fixed ? result.clone(true) : result;
};
}
var name_length = d.name.length;
var overhead = 0;
if (compressor.option("unused") && !compressor.exposed(d)) {
overhead = (name_length + 2 + value_length) / d.references.length;
}
d.should_replace = value_length <= name_length + overhead ? fn : false;
} else {
d.should_replace = false;
}
}
if (d.should_replace) {
return d.should_replace();
}
}
return self;
@@ -4561,11 +4612,11 @@ merge(Compressor.prototype, {
}
}
if (is_lhs(self, compressor.parent())) return self;
if (compressor.option("properties") && key !== prop) {
var node = self.flatten_object(property);
if (node) {
expr = self.expression = node.expression;
prop = self.property = node.property;
if (key !== prop) {
var sub = self.flatten_object(property, compressor);
if (sub) {
expr = self.expression = sub.expression;
prop = self.property = sub.property;
}
}
if (compressor.option("properties") && compressor.option("side_effects")
@@ -4611,7 +4662,8 @@ merge(Compressor.prototype, {
return self;
});
AST_Lambda.DEFMETHOD("contains_this", function() {
AST_Lambda.DEFMETHOD("contains_this", function(grandparent) {
if (grandparent instanceof AST_New) return false;
var result;
var self = this;
self.walk(new TreeWalker(function(node) {
@@ -4622,7 +4674,8 @@ merge(Compressor.prototype, {
return result;
});
AST_PropAccess.DEFMETHOD("flatten_object", function(key) {
AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) {
if (!compressor.option("properties")) return;
var expr = this.expression;
if (expr instanceof AST_Object) {
var props = expr.properties;
@@ -4633,7 +4686,8 @@ merge(Compressor.prototype, {
return prop instanceof AST_ObjectKeyVal;
})) break;
var value = prop.value;
if (value instanceof AST_Function && value.contains_this()) break;
if (value instanceof AST_Function
&& value.contains_this(compressor.parent())) break;
return make_node(AST_Sub, this, {
expression: make_node(AST_Array, expr, {
elements: props.map(function(prop) {
@@ -4677,10 +4731,8 @@ merge(Compressor.prototype, {
}
}
if (is_lhs(self, compressor.parent())) return self;
if (compressor.option("properties")) {
var node = self.flatten_object(self.property);
if (node) return node.optimize(compressor);
}
var sub = self.flatten_object(self.property, compressor);
if (sub) return sub.optimize(compressor);
var ev = self.evaluate(compressor);
if (ev !== self) {
ev = make_node_from_constant(ev, self).optimize(compressor);

View File

@@ -4,7 +4,7 @@
"homepage": "http://lisperator.net/uglifyjs",
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "3.1.6",
"version": "3.1.7",
"engines": {
"node": ">=0.8.0"
},

View File

@@ -2949,3 +2949,69 @@ conditional_2: {
}
expect_stdout: "5 5"
}
issue_2425_1: {
options = {
collapse_vars: true,
unused: true,
}
input: {
var a = 8;
(function(b) {
b.toString();
})(--a, a |= 10);
console.log(a);
}
expect: {
var a = 8;
(function(b) {
b.toString();
})(--a, a |= 10);
console.log(a);
}
expect_stdout: "15"
}
issue_2425_2: {
options = {
collapse_vars: true,
unused: true,
}
input: {
var a = 8;
(function(b, c) {
b.toString();
})(--a, a |= 10);
console.log(a);
}
expect: {
var a = 8;
(function(b, c) {
b.toString();
})(--a, a |= 10);
console.log(a);
}
expect_stdout: "15"
}
issue_2425_3: {
options = {
collapse_vars: true,
unused: true,
}
input: {
var a = 8;
(function(b, b) {
b.toString();
})(--a, a |= 10);
console.log(a);
}
expect: {
var a = 8;
(function(b, b) {
(a |= 10).toString();
})(--a);
console.log(a);
}
expect_stdout: "15"
}

View File

@@ -1109,11 +1109,11 @@ var_catch_toplevel: {
}
}
issue_2105: {
issue_2105_1: {
options = {
collapse_vars: true,
inline: true,
passes: 3,
passes: 2,
reduce_vars: true,
side_effects: true,
unused: true,
@@ -1139,17 +1139,50 @@ issue_2105: {
});
}
expect: {
(function() {
var quux = function() {
({
prop: function() {
console.log;
console.log("PASS");
};
return {
prop: function() {
console.log;
quux();
}
}).prop();
}
expect_stdout: "PASS"
}
issue_2105_2: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
properties: true,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
unsafe: true,
unused: true,
}
input: {
!function(factory) {
factory();
}( function() {
return function(fn) {
fn()().prop();
}( function() {
function bar() {
var quux = function() {
console.log("PASS");
}, foo = function() {
console.log;
quux();
};
return { prop: foo };
}
};
})().prop();
return bar;
} );
});
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
}

View File

@@ -369,3 +369,31 @@ contains_this_3: {
}
expect_stdout: "1 1 true"
}
new_this: {
options = {
evaluate: true,
hoist_props: true,
inline: true,
passes: 2,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var o = {
a: 1,
b: 2,
f: function(a) {
this.b = a;
}
};
console.log(new o.f(o.a).b, o.b);
}
expect: {
console.log(new function(a) {
this.b = a;
}(1).b, 2);
}
expect_stdout: "1 2"
}

View File

@@ -1006,3 +1006,22 @@ array_hole: {
}
expect_stdout: "2 undefined 3"
}
new_this: {
options = {
properties: true,
side_effects: true,
}
input: {
new {
f: function(a) {
this.a = a;
}
}.f(42);
}
expect: {
new function(a) {
this.a = a;
}(42);
}
}

View File

@@ -3295,3 +3295,250 @@ escaped_prop: {
}
expect_stdout: "2"
}
issue_2420_1: {
options = {
reduce_vars: true,
unused: true,
}
input: {
function run() {
var self = this;
if (self.count++)
self.foo();
else
self.bar();
}
var o = {
count: 0,
foo: function() { console.log("foo"); },
bar: function() { console.log("bar"); },
};
run.call(o);
run.call(o);
}
expect: {
function run() {
if (this.count++)
this.foo();
else
this.bar();
}
var o = {
count: 0,
foo: function() { console.log("foo"); },
bar: function() { console.log("bar"); },
};
run.call(o);
run.call(o);
}
expect_stdout: [
"bar",
"foo",
]
}
issue_2420_2: {
options = {
reduce_vars: true,
unused: true,
}
input: {
function f() {
var that = this;
if (that.bar)
that.foo();
else
!function(that, self) {
console.log(this === that, self === this, that === self);
}(that, this);
}
f.call({
bar: 1,
foo: function() { console.log("foo", this.bar); },
});
f.call({});
}
expect: {
function f() {
if (this.bar)
this.foo();
else
!function(that, self) {
console.log(this === that, self === this, that === self);
}(this, this);
}
f.call({
bar: 1,
foo: function() { console.log("foo", this.bar); },
});
f.call({});
}
expect_stdout: [
"foo 1",
"false false true",
]
}
issue_2423_1: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function c() { return 1; }
function p() { console.log(c()); }
p();
p();
}
expect: {
function p() { console.log(function() { return 1; }()); }
p();
p();
}
expect_stdout: [
"1",
"1",
]
}
issue_2423_2: {
options = {
inline: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function c() { return 1; }
function p() { console.log(c()); }
p();
p();
}
expect: {
function p() { console.log(1); }
p();
p();
}
expect_stdout: [
"1",
"1",
]
}
issue_2423_3: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
function c() { return 1; }
function p() { console.log(c()); }
p();
}
expect: {
(function() { console.log(function() { return 1; }()); })();
}
expect_stdout: "1"
}
issue_2423_4: {
options = {
inline: true,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function c() { return 1; }
function p() { console.log(c()); }
p();
}
expect: {
console.log(1);
}
expect_stdout: "1"
}
issue_2423_5: {
options = {
inline: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function x() {
y();
}
function y() {
console.log(1);
}
function z() {
function y() {
console.log(2);
}
x();
}
z();
z();
}
expect: {
function z() {
console.log(1);
}
z();
z();
}
expect_stdout: [
"1",
"1",
]
}
issue_2423_6: {
options = {
inline: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function x() {
y();
}
function y() {
console.log(1);
}
function z() {
function y() {
console.log(2);
}
x();
y();
}
z();
z();
}
expect: {
function z(){
console.log(1);
console.log(2);
}
z();
z();
}
expect_stdout: [
"1",
"2",
"1",
"2",
]
}

View File

@@ -162,6 +162,7 @@ var VALUES = [
'"object"',
'"number"',
'"function"',
'this',
];
var BINARY_OPS_NO_COMMA = [
@@ -349,10 +350,10 @@ function createParams() {
return params.join(', ');
}
function createArgs() {
function createArgs(recurmax, stmtDepth, canThrow) {
var args = [];
for (var n = rng(4); --n >= 0;) {
args.push(createValue());
args.push(rng(2) ? createValue() : createExpression(recurmax - 1, COMMA_OK, stmtDepth, canThrow));
}
return args.join(', ');
}
@@ -390,9 +391,10 @@ function createFunction(recurmax, inGlobal, noDecl, canThrow, stmtDepth) {
VAR_NAMES.length = namesLenBefore;
if (noDecl) s = 'var ' + createVarName(MANDATORY) + ' = ' + s + '(' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ');';
if (noDecl) s = 'var ' + createVarName(MANDATORY) + ' = ' + s;
// avoid "function statements" (decl inside statements)
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name + '(' + createArgs() + ');';
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name;
s += '(' + createArgs(recurmax, stmtDepth, canThrow) + ');';
return s;
}
@@ -626,6 +628,9 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
case p++:
case p++:
return createValue();
case p++:
case p++:
return getVarName();
case p++:
return createExpression(recurmax, COMMA_OK, stmtDepth, canThrow);
case p++: