Merge branch 'master' into harmony-v3.2.0
This commit is contained in:
@@ -63,7 +63,7 @@ function Compressor(options, false_by_default) {
|
||||
expression : false,
|
||||
global_defs : {},
|
||||
hoist_funs : !false_by_default,
|
||||
hoist_props : false,
|
||||
hoist_props : !false_by_default,
|
||||
hoist_vars : false,
|
||||
ie8 : false,
|
||||
if_return : !false_by_default,
|
||||
@@ -341,7 +341,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
}
|
||||
mark_escaped(d, node, value, 0);
|
||||
mark_escaped(d, node.scope, node, value, 0);
|
||||
}
|
||||
if (node instanceof AST_SymbolCatch) {
|
||||
node.definition().fixed = false;
|
||||
@@ -627,7 +627,8 @@ merge(Compressor.prototype, {
|
||||
|| !immutable
|
||||
&& parent instanceof AST_Call
|
||||
&& parent.expression === node
|
||||
&& (!(value instanceof AST_Function) || value.contains_this(parent))) {
|
||||
&& (!(value instanceof AST_Function)
|
||||
|| !(parent instanceof AST_New) && value.contains_this())) {
|
||||
return true;
|
||||
} else if (parent instanceof AST_Array) {
|
||||
return is_modified(parent, parent, level + 1);
|
||||
@@ -639,10 +640,13 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function mark_escaped(d, node, value, level) {
|
||||
function mark_escaped(d, scope, node, value, level) {
|
||||
var parent = tw.parent(level);
|
||||
if (value instanceof AST_Constant || value instanceof AST_ClassExpression) return;
|
||||
if (level > 0 && value instanceof AST_Function) return;
|
||||
if (value) {
|
||||
if (value.is_constant()) return;
|
||||
if (value instanceof AST_ClassExpression) return;
|
||||
if (level > 0 && value.is_constant_expression(scope)) return;
|
||||
}
|
||||
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|
||||
|| parent instanceof AST_Call && node !== parent.expression
|
||||
|| parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
|
||||
@@ -650,13 +654,13 @@ merge(Compressor.prototype, {
|
||||
d.escaped = true;
|
||||
return;
|
||||
} else if (parent instanceof AST_Array) {
|
||||
mark_escaped(d, parent, parent, level + 1);
|
||||
mark_escaped(d, scope, parent, parent, level + 1);
|
||||
} else if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
|
||||
var obj = tw.parent(level + 1);
|
||||
mark_escaped(d, obj, obj, level + 2);
|
||||
mark_escaped(d, scope, obj, obj, level + 2);
|
||||
} else if (parent instanceof AST_PropAccess && node === parent.expression) {
|
||||
value = read_property(value, parent.property);
|
||||
mark_escaped(d, parent, value, level + 1);
|
||||
mark_escaped(d, scope, parent, value, level + 1);
|
||||
if (value) return;
|
||||
}
|
||||
if (level == 0) d.direct_access = true;
|
||||
@@ -845,6 +849,12 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function is_identifier_atom(node) {
|
||||
return node instanceof AST_Infinity
|
||||
|| node instanceof AST_NaN
|
||||
|| node instanceof AST_Undefined;
|
||||
}
|
||||
|
||||
function tighten_body(statements, compressor) {
|
||||
var CHANGED, max_iter = 10;
|
||||
do {
|
||||
@@ -911,7 +921,7 @@ merge(Compressor.prototype, {
|
||||
&& !(node instanceof AST_SymbolDeclaration)
|
||||
&& lhs.equivalent_to(node)) {
|
||||
if (is_lhs(node, parent)) {
|
||||
if (candidate.multiple) replaced++;
|
||||
if (value_def) replaced++;
|
||||
return node;
|
||||
}
|
||||
CHANGED = abort = true;
|
||||
@@ -926,19 +936,24 @@ merge(Compressor.prototype, {
|
||||
return make_node(AST_UnaryPrefix, candidate, candidate);
|
||||
}
|
||||
if (candidate instanceof AST_VarDef) {
|
||||
if (candidate.multiple) {
|
||||
if (value_def) {
|
||||
abort = false;
|
||||
return node;
|
||||
}
|
||||
var def = candidate.name.definition();
|
||||
var value = candidate.value;
|
||||
if (def.references.length - def.replaced == 1 && !compressor.exposed(def)) {
|
||||
def.replaced++;
|
||||
return maintain_this_binding(parent, node, candidate.value);
|
||||
if (funarg && is_identifier_atom(value)) {
|
||||
return value.transform(compressor);
|
||||
} else {
|
||||
return maintain_this_binding(parent, node, value);
|
||||
}
|
||||
}
|
||||
return make_node(AST_Assign, candidate, {
|
||||
operator: "=",
|
||||
left: make_node(AST_SymbolRef, candidate.name, candidate.name),
|
||||
right: candidate.value
|
||||
right: value
|
||||
});
|
||||
}
|
||||
candidate.write_only = false;
|
||||
@@ -1000,18 +1015,20 @@ merge(Compressor.prototype, {
|
||||
extract_candidates(statements[stat_index]);
|
||||
while (candidates.length > 0) {
|
||||
var candidate = candidates.pop();
|
||||
var value_def = null;
|
||||
var lhs = get_lhs(candidate);
|
||||
if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor)) continue;
|
||||
// Locate symbols which may execute code outside of scanning range
|
||||
var lvalues = get_lvalues(candidate);
|
||||
if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false;
|
||||
var replace_all = candidate.multiple;
|
||||
var replace_all = value_def;
|
||||
if (!replace_all && lhs instanceof AST_SymbolRef) {
|
||||
var def = lhs.definition();
|
||||
replace_all = def.references.length - def.replaced == 1;
|
||||
}
|
||||
var side_effects = value_has_side_effects(candidate);
|
||||
var hit = candidate.name instanceof AST_SymbolFunarg;
|
||||
var funarg = candidate.name instanceof AST_SymbolFunarg;
|
||||
var hit = funarg;
|
||||
var abort = false, replaced = 0, can_replace = !args || !hit;
|
||||
if (!can_replace) {
|
||||
for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) {
|
||||
@@ -1022,13 +1039,12 @@ merge(Compressor.prototype, {
|
||||
for (var i = stat_index; !abort && i < statements.length; i++) {
|
||||
statements[i].transform(scanner);
|
||||
}
|
||||
if (candidate.multiple) {
|
||||
if (value_def) {
|
||||
var def = candidate.name.definition();
|
||||
if (abort && def.references.length - def.replaced > replaced) replaced = false;
|
||||
else {
|
||||
abort = false;
|
||||
hit = candidate.name instanceof AST_SymbolFunarg;
|
||||
var value_def = candidate.value.definition();
|
||||
hit = funarg;
|
||||
for (var i = stat_index; !abort && i < statements.length; i++) {
|
||||
statements[i].transform(multi_replacer);
|
||||
}
|
||||
@@ -1131,13 +1147,13 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function mangleable_var(expr) {
|
||||
var value = expr.value;
|
||||
if (!(value instanceof AST_SymbolRef)) return false;
|
||||
if (value.name == "arguments") return false;
|
||||
if (value.definition().undeclared) return false;
|
||||
expr.multiple = true;
|
||||
return true;
|
||||
function mangleable_var(var_def) {
|
||||
var value = var_def.value;
|
||||
if (!(value instanceof AST_SymbolRef)) return;
|
||||
if (value.name == "arguments") return;
|
||||
var def = value.definition();
|
||||
if (def.undeclared) return;
|
||||
return value_def = def;
|
||||
}
|
||||
|
||||
function get_lhs(expr) {
|
||||
@@ -4062,9 +4078,7 @@ merge(Compressor.prototype, {
|
||||
if (self.operator == "delete"
|
||||
&& !(e instanceof AST_SymbolRef
|
||||
|| e instanceof AST_PropAccess
|
||||
|| e instanceof AST_NaN
|
||||
|| e instanceof AST_Infinity
|
||||
|| e instanceof AST_Undefined)) {
|
||||
|| is_identifier_atom(e))) {
|
||||
if (e instanceof AST_Sequence) {
|
||||
e = e.expressions.slice();
|
||||
e.push(make_node(AST_True, self));
|
||||
@@ -4972,6 +4986,17 @@ merge(Compressor.prototype, {
|
||||
if (compressor.option("properties")) {
|
||||
var key = prop.evaluate(compressor);
|
||||
if (key !== prop) {
|
||||
if (typeof key == "string") {
|
||||
if (key == "undefined") {
|
||||
key = undefined;
|
||||
} else {
|
||||
var value = parseFloat(key);
|
||||
if (value.toString() == key) {
|
||||
key = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
prop = self.property = best_of_expression(prop, make_node_from_constant(key, prop).transform(compressor));
|
||||
var property = "" + key;
|
||||
if (is_identifier_string(property)
|
||||
&& property.length <= prop.print_to_string().length + 1) {
|
||||
@@ -4980,14 +5005,6 @@ merge(Compressor.prototype, {
|
||||
property: property
|
||||
}).optimize(compressor);
|
||||
}
|
||||
if (!(prop instanceof AST_Number)) {
|
||||
var value = parseFloat(property);
|
||||
if (value.toString() == property) {
|
||||
prop = self.property = make_node(AST_Number, prop, {
|
||||
value: value
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_lhs(self, compressor.parent())) return self;
|
||||
@@ -5044,8 +5061,7 @@ merge(Compressor.prototype, {
|
||||
return self;
|
||||
});
|
||||
|
||||
AST_Lambda.DEFMETHOD("contains_this", function(grandparent) {
|
||||
if (grandparent instanceof AST_New) return false;
|
||||
AST_Lambda.DEFMETHOD("contains_this", function() {
|
||||
var result;
|
||||
var self = this;
|
||||
self.walk(new TreeWalker(function(node) {
|
||||
@@ -5071,7 +5087,8 @@ merge(Compressor.prototype, {
|
||||
})) break;
|
||||
var value = prop.value;
|
||||
if ((value instanceof AST_Accessor || value instanceof AST_Function)
|
||||
&& value.contains_this(compressor.parent())) break;
|
||||
&& !(compressor.parent() instanceof AST_New)
|
||||
&& value.contains_this()) break;
|
||||
return make_node(AST_Sub, this, {
|
||||
expression: make_node(AST_Array, expr, {
|
||||
elements: props.map(function(prop) {
|
||||
|
||||
@@ -57,6 +57,7 @@ function minify(files, options) {
|
||||
nameCache: null,
|
||||
output: {},
|
||||
parse: {},
|
||||
rename: undefined,
|
||||
sourceMap: false,
|
||||
timings: false,
|
||||
toplevel: false,
|
||||
@@ -69,6 +70,9 @@ function minify(files, options) {
|
||||
if (options.keep_classnames === undefined) {
|
||||
options.keep_classnames = options.keep_fnames;
|
||||
}
|
||||
if (options.rename === undefined) {
|
||||
options.rename = options.compress && options.mangle;
|
||||
}
|
||||
set_shorthand("ecma", options, [ "parse", "compress", "output" ]);
|
||||
set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
|
||||
set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
|
||||
@@ -146,6 +150,11 @@ function minify(files, options) {
|
||||
if (options.wrap) {
|
||||
toplevel = toplevel.wrap_commonjs(options.wrap);
|
||||
}
|
||||
if (timings) timings.rename = Date.now();
|
||||
if (options.rename) {
|
||||
toplevel.figure_out_scope(options.mangle);
|
||||
toplevel.expand_names(options.mangle);
|
||||
}
|
||||
if (timings) timings.compress = Date.now();
|
||||
if (options.compress) toplevel = new Compressor(options.compress).compress(toplevel);
|
||||
if (timings) timings.scope = Date.now();
|
||||
@@ -206,7 +215,8 @@ function minify(files, options) {
|
||||
if (timings) {
|
||||
timings.end = Date.now();
|
||||
result.timings = {
|
||||
parse: 1e-3 * (timings.compress - timings.parse),
|
||||
parse: 1e-3 * (timings.rename - timings.parse),
|
||||
rename: 1e-3 * (timings.compress - timings.rename),
|
||||
compress: 1e-3 * (timings.scope - timings.compress),
|
||||
scope: 1e-3 * (timings.mangle - timings.scope),
|
||||
mangle: 1e-3 * (timings.properties - timings.mangle),
|
||||
|
||||
98
lib/scope.js
98
lib/scope.js
@@ -43,7 +43,7 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
function SymbolDef(scope, index, orig) {
|
||||
function SymbolDef(scope, orig) {
|
||||
this.name = orig.name;
|
||||
this.orig = [ orig ];
|
||||
this.eliminated = 0;
|
||||
@@ -54,7 +54,6 @@ function SymbolDef(scope, index, orig) {
|
||||
this.export = false;
|
||||
this.mangled_name = null;
|
||||
this.undeclared = false;
|
||||
this.index = index;
|
||||
this.id = SymbolDef.next_id++;
|
||||
};
|
||||
|
||||
@@ -346,7 +345,7 @@ AST_Toplevel.DEFMETHOD("def_global", function(node){
|
||||
if (globals.has(name)) {
|
||||
return globals.get(name);
|
||||
} else {
|
||||
var g = new SymbolDef(this, globals.size(), node);
|
||||
var g = new SymbolDef(this, node);
|
||||
g.undeclared = true;
|
||||
g.global = true;
|
||||
globals.set(name, g);
|
||||
@@ -417,7 +416,7 @@ AST_Scope.DEFMETHOD("def_function", function(symbol){
|
||||
AST_Scope.DEFMETHOD("def_variable", function(symbol){
|
||||
var def;
|
||||
if (!this.variables.has(symbol.name)) {
|
||||
def = new SymbolDef(this, this.variables.size(), symbol);
|
||||
def = new SymbolDef(this, symbol);
|
||||
this.variables.set(symbol.name, def);
|
||||
def.global = !this.parent_scope;
|
||||
} else {
|
||||
@@ -435,7 +434,7 @@ AST_Scope.DEFMETHOD("next_mangled", function(options){
|
||||
|
||||
// https://github.com/mishoo/UglifyJS2/issues/242 -- do not
|
||||
// shadow a name reserved from mangling.
|
||||
if (options.reserved.indexOf(m) >= 0) continue;
|
||||
if (member(m, options.reserved)) continue;
|
||||
|
||||
// we must ensure that the mangled name does not shadow a name
|
||||
// from some parent scope that is referenced in this or in
|
||||
@@ -487,7 +486,7 @@ AST_Symbol.DEFMETHOD("global", function(){
|
||||
return this.definition().global;
|
||||
});
|
||||
|
||||
AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
|
||||
AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options) {
|
||||
options = defaults(options, {
|
||||
eval : false,
|
||||
ie8 : false,
|
||||
@@ -497,15 +496,14 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
|
||||
toplevel : false,
|
||||
});
|
||||
if (!Array.isArray(options.reserved)) options.reserved = [];
|
||||
// Never mangle arguments
|
||||
push_uniq(options.reserved, "arguments");
|
||||
return options;
|
||||
});
|
||||
|
||||
AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||
options = this._default_mangler_options(options);
|
||||
|
||||
// Never mangle arguments
|
||||
options.reserved.push('arguments');
|
||||
|
||||
// We only need to mangle declaration nodes. Special logic wired
|
||||
// into the code generator will display the mangled name if it's
|
||||
// present (and for AST_SymbolRef-s it'll use the mangled name of
|
||||
@@ -514,11 +512,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||
var to_mangle = [];
|
||||
|
||||
if (options.cache) {
|
||||
this.globals.each(function(symbol){
|
||||
if (options.reserved.indexOf(symbol.name) < 0) {
|
||||
to_mangle.push(symbol);
|
||||
}
|
||||
});
|
||||
this.globals.each(collect);
|
||||
}
|
||||
|
||||
var tw = new TreeWalker(function(node, descend){
|
||||
@@ -530,13 +524,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||
return true; // don't descend again in TreeWalker
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
var p = tw.parent(), a = [];
|
||||
node.variables.each(function(symbol){
|
||||
if (options.reserved.indexOf(symbol.name) < 0) {
|
||||
a.push(symbol);
|
||||
}
|
||||
});
|
||||
to_mangle.push.apply(to_mangle, a);
|
||||
node.variables.each(collect);
|
||||
return;
|
||||
}
|
||||
if (node instanceof AST_Label) {
|
||||
@@ -561,6 +549,74 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||
if (options.cache) {
|
||||
options.cache.cname = this.cname;
|
||||
}
|
||||
|
||||
function collect(symbol) {
|
||||
if (!member(symbol.name, options.reserved)) {
|
||||
to_mangle.push(symbol);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
AST_Toplevel.DEFMETHOD("find_unique_prefix", function(options) {
|
||||
var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
|
||||
var cache = options.cache && options.cache.props;
|
||||
var prefixes = Object.create(null);
|
||||
options.reserved.forEach(add_prefix);
|
||||
this.globals.each(add_def);
|
||||
this.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_Scope) node.variables.each(add_def);
|
||||
if (node instanceof AST_SymbolCatch) add_def(node.definition());
|
||||
}));
|
||||
var prefix, i = 0;
|
||||
do {
|
||||
prefix = create_name(i++);
|
||||
} while (prefixes[prefix]);
|
||||
return prefix;
|
||||
|
||||
function add_prefix(name) {
|
||||
if (/[0-9]$/.test(name)) {
|
||||
prefixes[name.replace(/[0-9]+$/, "")] = true;
|
||||
}
|
||||
}
|
||||
|
||||
function add_def(def) {
|
||||
var name = def.name;
|
||||
if (def.global && cache && cache.has(name)) name = cache.get(name);
|
||||
else if (!def.unmangleable(options)) return;
|
||||
add_prefix(name);
|
||||
}
|
||||
|
||||
function create_name(num) {
|
||||
var name = "";
|
||||
do {
|
||||
name += letters[num % letters.length];
|
||||
num = Math.floor(num / letters.length);
|
||||
} while (num);
|
||||
return name;
|
||||
}
|
||||
});
|
||||
|
||||
AST_Toplevel.DEFMETHOD("expand_names", function(options) {
|
||||
options = this._default_mangler_options(options);
|
||||
var prefix = this.find_unique_prefix(options);
|
||||
this.globals.each(rename);
|
||||
this.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_Scope) node.variables.each(rename);
|
||||
if (node instanceof AST_SymbolCatch) rename(node.definition());
|
||||
}));
|
||||
|
||||
function rename(def) {
|
||||
if (def.global || def.unmangleable(options)) return;
|
||||
if (member(def.name, options.reserved)) return;
|
||||
var d = def.redefined();
|
||||
def.name = d ? d.name : prefix + def.id;
|
||||
def.orig.forEach(function(sym) {
|
||||
sym.name = def.name;
|
||||
});
|
||||
def.references.forEach(function(sym) {
|
||||
sym.name = def.name;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
|
||||
|
||||
Reference in New Issue
Block a user