2017-05-03 15:35:00 +02:00
|
|
|
/***********************************************************************
|
|
|
|
|
|
|
|
A JavaScript tokenizer / parser / beautifier / compressor.
|
|
|
|
https://github.com/mishoo/UglifyJS2
|
|
|
|
|
|
|
|
-------------------------------- (C) ---------------------------------
|
|
|
|
|
|
|
|
Author: Mihai Bazon
|
|
|
|
<mihai.bazon@gmail.com>
|
|
|
|
http://mihai.bazon.net/blog
|
|
|
|
|
|
|
|
Distributed under the BSD license:
|
|
|
|
|
|
|
|
Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the following
|
|
|
|
disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following
|
|
|
|
disclaimer in the documentation and/or other materials
|
|
|
|
provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
|
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
|
|
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
SUCH DAMAGE.
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function SymbolDef(scope, orig, init) {
|
2017-05-03 15:35:00 +02:00
|
|
|
this.name = orig.name;
|
|
|
|
this.orig = [ orig ];
|
2018-09-20 02:56:13 +02:00
|
|
|
this.init = init;
|
2017-12-10 21:51:33 +01:00
|
|
|
this.eliminated = 0;
|
2017-05-03 15:35:00 +02:00
|
|
|
this.scope = scope;
|
|
|
|
this.references = [];
|
2017-12-10 21:51:33 +01:00
|
|
|
this.replaced = 0;
|
2017-05-03 15:35:00 +02:00
|
|
|
this.global = false;
|
|
|
|
this.mangled_name = null;
|
|
|
|
this.undeclared = false;
|
|
|
|
this.id = SymbolDef.next_id++;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
SymbolDef.next_id = 1;
|
|
|
|
|
|
|
|
SymbolDef.prototype = {
|
|
|
|
unmangleable: function(options) {
|
|
|
|
if (!options) options = {};
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
return this.global && !options.toplevel
|
2017-05-03 15:35:00 +02:00
|
|
|
|| this.undeclared
|
2018-09-20 02:56:13 +02:00
|
|
|
|| !options.eval && this.scope.pinned()
|
|
|
|
|| options.keep_fnames
|
2017-05-03 15:35:00 +02:00
|
|
|
&& (this.orig[0] instanceof AST_SymbolLambda
|
2018-09-20 02:56:13 +02:00
|
|
|
|| this.orig[0] instanceof AST_SymbolDefun);
|
2017-05-03 15:35:00 +02:00
|
|
|
},
|
|
|
|
mangle: function(options) {
|
|
|
|
var cache = options.cache && options.cache.props;
|
|
|
|
if (this.global && cache && cache.has(this.name)) {
|
|
|
|
this.mangled_name = cache.get(this.name);
|
2018-09-20 02:56:13 +02:00
|
|
|
} else if (!this.mangled_name && !this.unmangleable(options)) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var def;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (def = this.redefined()) {
|
2017-05-03 15:35:00 +02:00
|
|
|
this.mangled_name = def.mangled_name || def.name;
|
2018-09-20 02:56:13 +02:00
|
|
|
} else {
|
|
|
|
this.mangled_name = next_mangled_name(this.scope, options, this);
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
if (this.global && cache) {
|
|
|
|
cache.set(this.name, this.mangled_name);
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
},
|
|
|
|
redefined: function() {
|
|
|
|
return this.defun && this.defun.variables.get(this.name);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
|
2017-05-03 15:35:00 +02:00
|
|
|
options = defaults(options, {
|
|
|
|
cache: null,
|
2017-08-14 05:01:11 +02:00
|
|
|
ie8: false,
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// pass 1: setup scope chaining and handle definitions
|
|
|
|
var self = this;
|
|
|
|
var scope = self.parent_scope = null;
|
|
|
|
var defun = null;
|
2018-09-20 02:56:13 +02:00
|
|
|
var tw = new TreeWalker(function(node, descend) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (node instanceof AST_Catch) {
|
|
|
|
var save_scope = scope;
|
|
|
|
scope = new AST_Scope(node);
|
|
|
|
scope.init_scope_vars(save_scope);
|
|
|
|
descend();
|
|
|
|
scope = save_scope;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node instanceof AST_Scope) {
|
|
|
|
node.init_scope_vars(scope);
|
|
|
|
var save_scope = scope;
|
|
|
|
var save_defun = defun;
|
|
|
|
defun = scope = node;
|
|
|
|
descend();
|
|
|
|
scope = save_scope;
|
|
|
|
defun = save_defun;
|
2018-09-20 02:56:13 +02:00
|
|
|
return true;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
if (node instanceof AST_With) {
|
2018-09-20 02:56:13 +02:00
|
|
|
for (var s = scope; s; s = s.parent_scope) s.uses_with = true;
|
2017-05-03 15:35:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node instanceof AST_Symbol) {
|
|
|
|
node.scope = scope;
|
|
|
|
}
|
|
|
|
if (node instanceof AST_Label) {
|
|
|
|
node.thedef = node;
|
|
|
|
node.references = [];
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
if (node instanceof AST_SymbolDefun) {
|
|
|
|
// This should be defined in the parent scope, as we encounter the
|
|
|
|
// AST_Defun node before getting to its AST_Symbol.
|
|
|
|
(node.scope = defun.parent_scope.resolve()).def_function(node, defun);
|
|
|
|
} else if (node instanceof AST_SymbolLambda) {
|
|
|
|
var def = defun.def_function(node, node.name == "arguments" ? undefined : defun);
|
|
|
|
if (options.ie8) def.defun = defun.parent_scope.resolve();
|
|
|
|
} else if (node instanceof AST_SymbolVar) {
|
|
|
|
defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined);
|
2017-05-03 15:35:00 +02:00
|
|
|
if (defun !== scope) {
|
|
|
|
node.mark_enclosed(options);
|
|
|
|
var def = scope.find_variable(node);
|
|
|
|
if (node.thedef !== def) {
|
|
|
|
node.thedef = def;
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
node.reference(options);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
} else if (node instanceof AST_SymbolCatch) {
|
2017-05-03 15:35:00 +02:00
|
|
|
scope.def_variable(node).defun = defun;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.walk(tw);
|
|
|
|
|
|
|
|
// pass 2: find back references and eval
|
2017-08-14 05:01:11 +02:00
|
|
|
self.globals = new Dictionary();
|
2018-09-20 02:56:13 +02:00
|
|
|
var tw = new TreeWalker(function(node) {
|
|
|
|
if (node instanceof AST_LoopControl) {
|
|
|
|
if (node.label) node.label.thedef.references.push(node);
|
2017-05-03 15:35:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node instanceof AST_SymbolRef) {
|
|
|
|
var name = node.name;
|
|
|
|
if (name == "eval" && tw.parent() instanceof AST_Call) {
|
|
|
|
for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) {
|
|
|
|
s.uses_eval = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var sym = node.scope.find_variable(name);
|
|
|
|
if (!sym) {
|
|
|
|
sym = self.def_global(node);
|
2017-08-14 05:01:11 +02:00
|
|
|
} else if (sym.scope instanceof AST_Lambda && name == "arguments") {
|
|
|
|
sym.scope.uses_arguments = true;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
node.thedef = sym;
|
|
|
|
node.reference(options);
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
// ensure mangling works if catch reuses a scope variable
|
2018-09-20 02:56:13 +02:00
|
|
|
if (node instanceof AST_SymbolCatch) {
|
|
|
|
var def = node.definition().redefined();
|
|
|
|
if (def) for (var s = node.scope; s; s = s.parent_scope) {
|
2017-08-14 05:01:11 +02:00
|
|
|
push_uniq(s.enclosed, def);
|
|
|
|
if (s === def.scope) break;
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
return true;
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
self.walk(tw);
|
|
|
|
|
|
|
|
// pass 3: fix up any scoping issue with IE8
|
2018-09-20 02:56:13 +02:00
|
|
|
if (options.ie8) self.walk(new TreeWalker(function(node) {
|
|
|
|
if (node instanceof AST_SymbolCatch) {
|
|
|
|
redefine(node, node.thedef.defun);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (node instanceof AST_SymbolLambda) {
|
|
|
|
var def = node.thedef;
|
|
|
|
if (def.orig.length == 1) {
|
|
|
|
redefine(node, node.scope.parent_scope);
|
|
|
|
node.thedef.init = def.init;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}));
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function redefine(node, scope) {
|
|
|
|
var name = node.name;
|
|
|
|
var refs = node.thedef.references;
|
|
|
|
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
|
|
|
|
refs.forEach(function(ref) {
|
|
|
|
ref.thedef = def;
|
|
|
|
ref.reference(options);
|
|
|
|
});
|
|
|
|
node.thedef = def;
|
|
|
|
node.reference(options);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Toplevel.DEFMETHOD("def_global", function(node) {
|
2017-05-03 15:35:00 +02:00
|
|
|
var globals = this.globals, name = node.name;
|
|
|
|
if (globals.has(name)) {
|
|
|
|
return globals.get(name);
|
|
|
|
} else {
|
2017-12-10 21:51:33 +01:00
|
|
|
var g = new SymbolDef(this, node);
|
2017-05-03 15:35:00 +02:00
|
|
|
g.undeclared = true;
|
|
|
|
g.global = true;
|
|
|
|
globals.set(name, g);
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope) {
|
2017-05-03 15:35:00 +02:00
|
|
|
this.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
|
|
|
|
this.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
|
|
|
|
this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
|
|
|
|
this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
|
|
|
|
this.parent_scope = parent_scope; // the parent scope
|
|
|
|
this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes
|
|
|
|
this.cname = -1; // the current index for mangling functions/variables
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Lambda.DEFMETHOD("init_scope_vars", function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
AST_Scope.prototype.init_scope_vars.apply(this, arguments);
|
|
|
|
this.uses_arguments = false;
|
2017-08-14 05:01:11 +02:00
|
|
|
this.def_variable(new AST_SymbolFunarg({
|
2017-05-03 15:35:00 +02:00
|
|
|
name: "arguments",
|
|
|
|
start: this.start,
|
|
|
|
end: this.end
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
|
|
|
|
var def = this.definition();
|
2018-09-20 02:56:13 +02:00
|
|
|
for (var s = this.scope; s; s = s.parent_scope) {
|
2017-05-03 15:35:00 +02:00
|
|
|
push_uniq(s.enclosed, def);
|
|
|
|
if (options.keep_fnames) {
|
|
|
|
s.functions.each(function(d) {
|
|
|
|
push_uniq(def.scope.enclosed, d);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (s === def.scope) break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
AST_Symbol.DEFMETHOD("reference", function(options) {
|
|
|
|
this.definition().references.push(this);
|
|
|
|
this.mark_enclosed(options);
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Scope.DEFMETHOD("find_variable", function(name) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (name instanceof AST_Symbol) name = name.name;
|
|
|
|
return this.variables.get(name)
|
|
|
|
|| (this.parent_scope && this.parent_scope.find_variable(name));
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Scope.DEFMETHOD("def_function", function(symbol, init) {
|
|
|
|
var def = this.def_variable(symbol, init);
|
|
|
|
if (!def.init || def.init instanceof AST_Defun) def.init = init;
|
|
|
|
this.functions.set(symbol.name, def);
|
|
|
|
return def;
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Scope.DEFMETHOD("def_variable", function(symbol, init) {
|
|
|
|
var def = this.variables.get(symbol.name);
|
|
|
|
if (def) {
|
|
|
|
def.orig.push(symbol);
|
|
|
|
if (def.init && (def.scope !== symbol.scope || def.init instanceof AST_Function)) {
|
|
|
|
def.init = init;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
def = new SymbolDef(this, symbol, init);
|
2017-05-03 15:35:00 +02:00
|
|
|
this.variables.set(symbol.name, def);
|
|
|
|
def.global = !this.parent_scope;
|
|
|
|
}
|
|
|
|
return symbol.thedef = def;
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Lambda.DEFMETHOD("resolve", return_this);
|
|
|
|
AST_Scope.DEFMETHOD("resolve", function() {
|
|
|
|
return this.parent_scope;
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Toplevel.DEFMETHOD("resolve", return_this);
|
|
|
|
|
|
|
|
function names_in_use(scope, options) {
|
|
|
|
var names = scope.names_in_use;
|
|
|
|
if (!names) {
|
|
|
|
scope.names_in_use = names = Object.create(scope.mangled_names || null);
|
|
|
|
scope.cname_holes = [];
|
|
|
|
scope.enclosed.forEach(function(def) {
|
|
|
|
if (def.unmangleable(options)) names[def.name] = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
function next_mangled_name(scope, options, def) {
|
|
|
|
var in_use = names_in_use(scope, options);
|
|
|
|
var holes = scope.cname_holes;
|
|
|
|
var names = Object.create(null);
|
|
|
|
var scopes = [ scope ];
|
|
|
|
def.references.forEach(function(sym) {
|
|
|
|
var scope = sym.scope;
|
|
|
|
do {
|
|
|
|
if (scopes.indexOf(scope) < 0) {
|
|
|
|
for (var name in names_in_use(scope, options)) {
|
|
|
|
names[name] = true;
|
|
|
|
}
|
|
|
|
scopes.push(scope);
|
|
|
|
} else break;
|
|
|
|
} while (scope = scope.parent_scope);
|
|
|
|
});
|
|
|
|
var name;
|
|
|
|
for (var i = 0, len = holes.length; i < len; i++) {
|
|
|
|
name = base54(holes[i]);
|
|
|
|
if (names[name]) continue;
|
|
|
|
holes.splice(i, 1);
|
|
|
|
scope.names_in_use[name] = true;
|
|
|
|
return name;
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
while (true) {
|
2018-09-20 02:56:13 +02:00
|
|
|
name = base54(++scope.cname);
|
|
|
|
if (in_use[name] || !is_identifier(name) || options.reserved.has[name]) continue;
|
|
|
|
if (!names[name]) break;
|
|
|
|
holes.push(scope.cname);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
scope.names_in_use[name] = true;
|
|
|
|
return name;
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Symbol.DEFMETHOD("unmangleable", function(options) {
|
2017-08-14 05:01:11 +02:00
|
|
|
var def = this.definition();
|
|
|
|
return !def || def.unmangleable(options);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// labels are always mangleable
|
2017-08-14 05:01:11 +02:00
|
|
|
AST_Label.DEFMETHOD("unmangleable", return_false);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Symbol.DEFMETHOD("unreferenced", function() {
|
|
|
|
return !this.definition().references.length && !this.scope.pinned();
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Symbol.DEFMETHOD("definition", function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
return this.thedef;
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Symbol.DEFMETHOD("global", function() {
|
2017-05-03 15:35:00 +02:00
|
|
|
return this.definition().global;
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function _default_mangler_options(options) {
|
2017-08-14 05:01:11 +02:00
|
|
|
options = defaults(options, {
|
2017-05-03 15:35:00 +02:00
|
|
|
eval : false,
|
2017-08-14 05:01:11 +02:00
|
|
|
ie8 : false,
|
2017-05-03 15:35:00 +02:00
|
|
|
keep_fnames : false,
|
2017-08-14 05:01:11 +02:00
|
|
|
reserved : [],
|
2017-05-03 15:35:00 +02:00
|
|
|
toplevel : false,
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
if (!Array.isArray(options.reserved)) options.reserved = [];
|
2017-12-10 21:51:33 +01:00
|
|
|
// Never mangle arguments
|
|
|
|
push_uniq(options.reserved, "arguments");
|
2018-09-20 02:56:13 +02:00
|
|
|
options.reserved.has = makePredicate(options.reserved);
|
2017-08-14 05:01:11 +02:00
|
|
|
return options;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
|
|
|
|
options = _default_mangler_options(options);
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// the AST_SymbolDeclaration that it points to).
|
|
|
|
var lname = -1;
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
if (options.cache && options.cache.props) {
|
|
|
|
var mangled_names = this.mangled_names = Object.create(null);
|
|
|
|
options.cache.props.each(function(mangled_name) {
|
|
|
|
mangled_names[mangled_name] = true;
|
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
var redefined = [];
|
|
|
|
var tw = new TreeWalker(function(node, descend) {
|
2017-05-03 15:35:00 +02:00
|
|
|
if (node instanceof AST_LabeledStatement) {
|
|
|
|
// lname is incremented when we get to the AST_Label
|
|
|
|
var save_nesting = lname;
|
|
|
|
descend();
|
|
|
|
lname = save_nesting;
|
2018-09-20 02:56:13 +02:00
|
|
|
return true;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
if (node instanceof AST_Scope) {
|
2018-09-20 02:56:13 +02:00
|
|
|
descend();
|
|
|
|
if (options.cache && node instanceof AST_Toplevel) {
|
|
|
|
node.globals.each(mangle);
|
|
|
|
}
|
|
|
|
node.variables.each(mangle);
|
|
|
|
return true;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
if (node instanceof AST_Label) {
|
|
|
|
var name;
|
2018-09-20 02:56:13 +02:00
|
|
|
do {
|
|
|
|
name = base54(++lname);
|
|
|
|
} while (!is_identifier(name));
|
2017-05-03 15:35:00 +02:00
|
|
|
node.mangled_name = name;
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
if (!options.ie8 && node instanceof AST_Catch) {
|
|
|
|
var def = node.argname.definition();
|
|
|
|
var redef = def.redefined();
|
|
|
|
if (redef) {
|
|
|
|
redefined.push(def);
|
|
|
|
reference(node.argname);
|
|
|
|
def.references.forEach(reference);
|
|
|
|
}
|
|
|
|
descend();
|
|
|
|
if (!redef) mangle(def);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reference(sym) {
|
|
|
|
sym.thedef = redef;
|
|
|
|
sym.reference(options);
|
|
|
|
sym.thedef = def;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.walk(tw);
|
2018-09-20 02:56:13 +02:00
|
|
|
redefined.forEach(mangle);
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function mangle(def) {
|
|
|
|
if (options.reserved.has[def.name]) return;
|
|
|
|
def.mangle(options);
|
2017-12-10 21:51:33 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) {
|
2017-12-10 21:51:33 +01:00
|
|
|
var cache = options.cache && options.cache.props;
|
2018-09-20 02:56:13 +02:00
|
|
|
var avoid = Object.create(null);
|
|
|
|
options.reserved.forEach(to_avoid);
|
2017-12-10 21:51:33 +01:00
|
|
|
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());
|
|
|
|
}));
|
2018-09-20 02:56:13 +02:00
|
|
|
return avoid;
|
|
|
|
|
|
|
|
function to_avoid(name) {
|
|
|
|
avoid[name] = true;
|
2017-12-10 21:51:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-09-20 02:56:13 +02:00
|
|
|
to_avoid(name);
|
2017-12-10 21:51:33 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
AST_Toplevel.DEFMETHOD("expand_names", function(options) {
|
2018-09-20 02:56:13 +02:00
|
|
|
base54.reset();
|
|
|
|
base54.sort();
|
|
|
|
options = _default_mangler_options(options);
|
|
|
|
var avoid = this.find_colliding_names(options);
|
|
|
|
var cname = 0;
|
2017-12-10 21:51:33 +01:00
|
|
|
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());
|
|
|
|
}));
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function next_name() {
|
|
|
|
var name;
|
|
|
|
do {
|
|
|
|
name = base54(cname++);
|
|
|
|
} while (avoid[name] || !is_identifier(name));
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function rename(def) {
|
2018-09-20 02:56:13 +02:00
|
|
|
if (def.global && options.cache) return;
|
|
|
|
if (def.unmangleable(options)) return;
|
|
|
|
if (options.reserved.has[def.name]) return;
|
2017-12-10 21:51:33 +01:00
|
|
|
var d = def.redefined();
|
2018-09-20 02:56:13 +02:00
|
|
|
def.name = d ? d.name : next_name();
|
2017-12-10 21:51:33 +01:00
|
|
|
def.orig.forEach(function(sym) {
|
|
|
|
sym.name = def.name;
|
|
|
|
});
|
|
|
|
def.references.forEach(function(sym) {
|
|
|
|
sym.name = def.name;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
AST_Node.DEFMETHOD("tail_node", return_this);
|
|
|
|
AST_Sequence.DEFMETHOD("tail_node", function() {
|
|
|
|
return this.expressions[this.expressions.length - 1];
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) {
|
|
|
|
options = _default_mangler_options(options);
|
|
|
|
base54.reset();
|
2017-08-14 05:01:11 +02:00
|
|
|
try {
|
|
|
|
AST_Node.prototype.print = function(stream, force_parens) {
|
|
|
|
this._print(stream, force_parens);
|
|
|
|
if (this instanceof AST_Symbol && !this.unmangleable(options)) {
|
|
|
|
base54.consider(this.name, -1);
|
|
|
|
} else if (options.properties) {
|
|
|
|
if (this instanceof AST_Dot) {
|
|
|
|
base54.consider(this.property, -1);
|
|
|
|
} else if (this instanceof AST_Sub) {
|
|
|
|
skip_string(this.property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
base54.consider(this.print_to_string(), 1);
|
|
|
|
} finally {
|
|
|
|
AST_Node.prototype.print = AST_Node.prototype._print;
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
base54.sort();
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
function skip_string(node) {
|
|
|
|
if (node instanceof AST_String) {
|
|
|
|
base54.consider(node.value, -1);
|
|
|
|
} else if (node instanceof AST_Conditional) {
|
|
|
|
skip_string(node.consequent);
|
|
|
|
skip_string(node.alternative);
|
|
|
|
} else if (node instanceof AST_Sequence) {
|
2017-12-10 21:51:33 +01:00
|
|
|
skip_string(node.tail_node());
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
var base54 = (function() {
|
2018-09-20 02:56:13 +02:00
|
|
|
var freq = Object.create(null);
|
|
|
|
function init(chars) {
|
|
|
|
var array = [];
|
|
|
|
for (var i = 0, len = chars.length; i < len; i++) {
|
|
|
|
var ch = chars[i];
|
|
|
|
array.push(ch);
|
|
|
|
freq[ch] = -1e-2 * i;
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
var digits = init("0123456789");
|
|
|
|
var leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
|
2017-05-03 15:35:00 +02:00
|
|
|
var chars, frequency;
|
|
|
|
function reset() {
|
2018-09-20 02:56:13 +02:00
|
|
|
frequency = Object.create(freq);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
base54.consider = function(str, delta) {
|
2017-05-03 15:35:00 +02:00
|
|
|
for (var i = str.length; --i >= 0;) {
|
2017-08-14 05:01:11 +02:00
|
|
|
frequency[str[i]] += delta;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
function compare(a, b) {
|
|
|
|
return frequency[b] - frequency[a];
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
base54.sort = function() {
|
2018-09-20 02:56:13 +02:00
|
|
|
chars = leading.sort(compare).concat(digits.sort(compare));
|
2017-05-03 15:35:00 +02:00
|
|
|
};
|
|
|
|
base54.reset = reset;
|
|
|
|
reset();
|
|
|
|
function base54(num) {
|
|
|
|
var ret = "", base = 54;
|
|
|
|
num++;
|
|
|
|
do {
|
|
|
|
num--;
|
2017-08-14 05:01:11 +02:00
|
|
|
ret += chars[num % base];
|
2017-05-03 15:35:00 +02:00
|
|
|
num = Math.floor(num / base);
|
|
|
|
base = 64;
|
|
|
|
} while (num > 0);
|
|
|
|
return ret;
|
2018-09-20 02:56:13 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
return base54;
|
|
|
|
})();
|