2012-08-22 18:28:59 +00: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:
|
|
|
|
|
|
2012-08-27 08:01:27 +00:00
|
|
|
Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
2012-08-22 18:28:59 +00:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
2012-10-02 09:45:31 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
2018-01-03 09:18:38 +00:00
|
|
|
function SymbolDef(scope, orig, init) {
|
2012-09-11 12:42:28 +00:00
|
|
|
this.name = orig.name;
|
|
|
|
|
this.orig = [ orig ];
|
2018-01-03 09:18:38 +00:00
|
|
|
this.init = init;
|
2017-11-12 14:31:47 +00:00
|
|
|
this.eliminated = 0;
|
2012-09-11 12:42:28 +00:00
|
|
|
this.scope = scope;
|
|
|
|
|
this.references = [];
|
2017-11-12 14:31:47 +00:00
|
|
|
this.replaced = 0;
|
2012-09-11 12:42:28 +00:00
|
|
|
this.global = false;
|
2016-02-27 12:40:57 +00:00
|
|
|
this.export = false;
|
2012-09-11 12:42:28 +00:00
|
|
|
this.mangled_name = null;
|
|
|
|
|
this.undeclared = false;
|
2016-03-28 21:58:50 +00:00
|
|
|
this.id = SymbolDef.next_id++;
|
2012-09-11 12:42:28 +00:00
|
|
|
};
|
|
|
|
|
|
2018-01-13 17:40:51 +00:00
|
|
|
SymbolDef.next_id = 1;
|
2016-03-28 21:58:50 +00:00
|
|
|
|
2012-09-11 12:42:28 +00:00
|
|
|
SymbolDef.prototype = {
|
2012-11-06 09:39:41 +00:00
|
|
|
unmangleable: function(options) {
|
2015-01-04 19:53:19 +00:00
|
|
|
if (!options) options = {};
|
|
|
|
|
|
|
|
|
|
return (this.global && !options.toplevel)
|
2016-02-27 12:40:57 +00:00
|
|
|
|| this.export
|
2012-11-06 09:39:41 +00:00
|
|
|
|| this.undeclared
|
2015-01-04 19:53:19 +00:00
|
|
|
|| (!options.eval && (this.scope.uses_eval || this.scope.uses_with))
|
|
|
|
|
|| (options.keep_fnames
|
|
|
|
|
&& (this.orig[0] instanceof AST_SymbolLambda
|
2015-11-21 13:59:18 +00:00
|
|
|
|| this.orig[0] instanceof AST_SymbolDefun))
|
|
|
|
|
|| this.orig[0] instanceof AST_SymbolMethod
|
|
|
|
|
|| (options.keep_classnames
|
|
|
|
|
&& (this.orig[0] instanceof AST_SymbolClass
|
|
|
|
|
|| this.orig[0] instanceof AST_SymbolDefClass));
|
2012-09-11 12:42:28 +00:00
|
|
|
},
|
2012-11-06 09:39:41 +00:00
|
|
|
mangle: function(options) {
|
2015-03-16 11:16:30 +00:00
|
|
|
var cache = options.cache && options.cache.props;
|
|
|
|
|
if (this.global && cache && cache.has(this.name)) {
|
|
|
|
|
this.mangled_name = cache.get(this.name);
|
|
|
|
|
}
|
|
|
|
|
else if (!this.mangled_name && !this.unmangleable(options)) {
|
2013-03-22 16:02:08 +00:00
|
|
|
var s = this.scope;
|
2017-03-28 08:42:39 +00:00
|
|
|
var sym = this.orig[0];
|
2017-04-15 15:50:50 +00:00
|
|
|
if (options.ie8 && sym instanceof AST_SymbolLambda)
|
2013-03-22 16:02:08 +00:00
|
|
|
s = s.parent_scope;
|
2017-03-28 08:42:39 +00:00
|
|
|
var def;
|
2017-06-19 18:14:05 +00:00
|
|
|
if (def = this.redefined()) {
|
2017-03-28 08:42:39 +00:00
|
|
|
this.mangled_name = def.mangled_name || def.name;
|
|
|
|
|
} else
|
|
|
|
|
this.mangled_name = s.next_mangled(options, this);
|
2015-03-16 11:16:30 +00:00
|
|
|
if (this.global && cache) {
|
|
|
|
|
cache.set(this.name, this.mangled_name);
|
|
|
|
|
}
|
2013-03-22 16:02:08 +00:00
|
|
|
}
|
2017-06-19 18:14:05 +00:00
|
|
|
},
|
|
|
|
|
redefined: function() {
|
|
|
|
|
return this.defun && this.defun.variables.get(this.name);
|
2012-09-11 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-28 14:43:30 +00:00
|
|
|
AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
|
|
|
|
options = defaults(options, {
|
2017-03-31 08:41:04 +00:00
|
|
|
cache: null,
|
2017-04-15 15:50:50 +00:00
|
|
|
ie8: false,
|
2017-05-11 08:48:43 +00:00
|
|
|
safari10: false,
|
2013-11-28 14:43:30 +00:00
|
|
|
});
|
2012-08-19 19:46:00 +00:00
|
|
|
|
|
|
|
|
// pass 1: setup scope chaining and handle definitions
|
2012-09-11 10:15:55 +00:00
|
|
|
var self = this;
|
|
|
|
|
var scope = self.parent_scope = null;
|
2015-11-12 09:48:06 +00:00
|
|
|
var labels = new Dictionary();
|
2013-11-28 14:43:30 +00:00
|
|
|
var defun = null;
|
2015-08-14 01:00:31 +00:00
|
|
|
var in_destructuring = null;
|
2017-05-11 08:48:43 +00:00
|
|
|
var for_scopes = [];
|
2012-08-19 12:57:50 +00:00
|
|
|
var tw = new TreeWalker(function(node, descend){
|
2016-10-19 13:34:26 +00:00
|
|
|
if (node.is_block_scope()) {
|
2013-12-05 11:30:29 +00:00
|
|
|
var save_scope = scope;
|
2017-07-25 14:07:21 +00:00
|
|
|
node.block_scope = scope = new AST_Scope(node);
|
2017-03-26 17:30:21 +00:00
|
|
|
scope.init_scope_vars(save_scope);
|
2016-07-01 13:36:38 +00:00
|
|
|
if (!(node instanceof AST_Scope)) {
|
|
|
|
|
scope.uses_with = save_scope.uses_with;
|
|
|
|
|
scope.uses_eval = save_scope.uses_eval;
|
|
|
|
|
scope.directives = save_scope.directives;
|
|
|
|
|
}
|
2017-05-11 08:48:43 +00:00
|
|
|
if (options.safari10) {
|
|
|
|
|
if (node instanceof AST_For || node instanceof AST_ForIn) {
|
|
|
|
|
for_scopes.push(scope);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-05 11:30:29 +00:00
|
|
|
descend();
|
|
|
|
|
scope = save_scope;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-06-23 05:11:26 +00:00
|
|
|
if (node instanceof AST_Destructuring) {
|
2015-08-14 01:00:31 +00:00
|
|
|
in_destructuring = node; // These don't nest
|
2015-01-15 03:03:38 +00:00
|
|
|
descend();
|
2015-08-14 01:00:31 +00:00
|
|
|
in_destructuring = null;
|
2015-01-15 03:03:38 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2012-08-19 12:57:50 +00:00
|
|
|
if (node instanceof AST_Scope) {
|
2017-03-26 17:30:21 +00:00
|
|
|
node.init_scope_vars(scope);
|
|
|
|
|
var save_scope = scope;
|
2013-11-28 14:43:30 +00:00
|
|
|
var save_defun = defun;
|
2015-11-12 09:48:06 +00:00
|
|
|
var save_labels = labels;
|
2013-12-05 11:30:29 +00:00
|
|
|
defun = scope = node;
|
2015-11-12 09:48:06 +00:00
|
|
|
labels = new Dictionary();
|
2017-02-18 10:59:40 +00:00
|
|
|
descend();
|
2012-08-19 12:57:50 +00:00
|
|
|
scope = save_scope;
|
2013-11-28 14:43:30 +00:00
|
|
|
defun = save_defun;
|
2015-11-12 09:48:06 +00:00
|
|
|
labels = save_labels;
|
2012-08-19 12:57:50 +00:00
|
|
|
return true; // don't descend again in TreeWalker
|
|
|
|
|
}
|
2015-11-12 09:48:06 +00:00
|
|
|
if (node instanceof AST_LabeledStatement) {
|
|
|
|
|
var l = node.label;
|
|
|
|
|
if (labels.has(l.name)) {
|
|
|
|
|
throw new Error(string_template("Label {name} defined twice", l));
|
|
|
|
|
}
|
|
|
|
|
labels.set(l.name, l);
|
|
|
|
|
descend();
|
|
|
|
|
labels.del(l.name);
|
|
|
|
|
return true; // no descend again
|
2015-10-05 23:51:09 +00:00
|
|
|
}
|
2012-08-19 12:57:50 +00:00
|
|
|
if (node instanceof AST_With) {
|
|
|
|
|
for (var s = scope; s; s = s.parent_scope)
|
|
|
|
|
s.uses_with = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-09-11 12:42:28 +00:00
|
|
|
if (node instanceof AST_Symbol) {
|
|
|
|
|
node.scope = scope;
|
|
|
|
|
}
|
2015-11-12 09:48:06 +00:00
|
|
|
if (node instanceof AST_Label) {
|
|
|
|
|
node.thedef = node;
|
|
|
|
|
node.references = [];
|
|
|
|
|
}
|
2012-10-09 13:25:45 +00:00
|
|
|
if (node instanceof AST_SymbolLambda) {
|
2018-01-05 14:21:18 +00:00
|
|
|
defun.def_function(node, node.name == "arguments" ? undefined : defun);
|
2012-08-19 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
else if (node instanceof AST_SymbolDefun) {
|
2012-08-20 14:19:30 +00:00
|
|
|
// Careful here, the scope where this should be defined is
|
|
|
|
|
// the parent scope. The reason is that we enter a new
|
|
|
|
|
// scope when we encounter the AST_Defun node (which is
|
|
|
|
|
// instanceof AST_Scope) but we get to the symbol a bit
|
|
|
|
|
// later.
|
2018-01-07 12:35:53 +00:00
|
|
|
mark_export((node.scope = defun.parent_scope.get_defun_scope()).def_function(node, defun), 1);
|
2012-08-19 12:57:50 +00:00
|
|
|
}
|
2015-11-21 12:20:20 +00:00
|
|
|
else if (node instanceof AST_SymbolClass) {
|
2018-01-07 12:35:53 +00:00
|
|
|
mark_export(defun.def_variable(node, defun), 1);
|
2015-11-21 12:20:20 +00:00
|
|
|
}
|
2016-02-27 12:01:16 +00:00
|
|
|
else if (node instanceof AST_SymbolImport) {
|
2017-05-26 05:35:40 +00:00
|
|
|
scope.def_variable(node);
|
2016-02-27 12:01:16 +00:00
|
|
|
}
|
2015-11-21 12:20:20 +00:00
|
|
|
else if (node instanceof AST_SymbolDefClass) {
|
|
|
|
|
// This deals with the name of the class being available
|
|
|
|
|
// inside the class.
|
2018-01-07 12:35:53 +00:00
|
|
|
mark_export((node.scope = defun.parent_scope).def_function(node, defun), 1);
|
2015-11-21 12:20:20 +00:00
|
|
|
}
|
2012-10-02 09:22:39 +00:00
|
|
|
else if (node instanceof AST_SymbolVar
|
2017-03-05 08:03:56 +00:00
|
|
|
|| node instanceof AST_SymbolLet
|
|
|
|
|
|| node instanceof AST_SymbolConst) {
|
2018-01-07 12:35:53 +00:00
|
|
|
var def;
|
|
|
|
|
if (node instanceof AST_SymbolBlockDeclaration) {
|
2018-01-10 10:40:54 +00:00
|
|
|
def = scope.def_variable(node, null);
|
2018-01-07 12:35:53 +00:00
|
|
|
} else {
|
|
|
|
|
def = defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined);
|
|
|
|
|
}
|
2017-08-14 04:31:12 +00:00
|
|
|
if (!all(def.orig, function(sym) {
|
|
|
|
|
if (sym === node) return true;
|
|
|
|
|
if (node instanceof AST_SymbolBlockDeclaration) {
|
|
|
|
|
return sym instanceof AST_SymbolLambda;
|
|
|
|
|
}
|
|
|
|
|
return !(sym instanceof AST_SymbolLet || sym instanceof AST_SymbolConst);
|
|
|
|
|
})) {
|
|
|
|
|
js_error(
|
|
|
|
|
node.name + " redeclared",
|
|
|
|
|
node.start.file,
|
|
|
|
|
node.start.line,
|
|
|
|
|
node.start.col,
|
|
|
|
|
node.start.pos
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-05-26 05:35:40 +00:00
|
|
|
if (!(node instanceof AST_SymbolFunarg)) mark_export(def, 2);
|
2015-08-14 01:00:31 +00:00
|
|
|
def.destructuring = in_destructuring;
|
2017-03-28 08:42:39 +00:00
|
|
|
if (defun !== scope) {
|
|
|
|
|
node.mark_enclosed(options);
|
|
|
|
|
var def = scope.find_variable(node);
|
|
|
|
|
if (node.thedef !== def) {
|
|
|
|
|
node.thedef = def;
|
|
|
|
|
node.reference(options);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-20 14:19:30 +00:00
|
|
|
}
|
2012-08-19 12:57:50 +00:00
|
|
|
else if (node instanceof AST_SymbolCatch) {
|
2017-05-26 05:35:40 +00:00
|
|
|
scope.def_variable(node).defun = defun;
|
2012-08-19 12:57:50 +00:00
|
|
|
}
|
2015-11-12 09:48:06 +00:00
|
|
|
else if (node instanceof AST_LabelRef) {
|
|
|
|
|
var sym = labels.get(node.name);
|
|
|
|
|
if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
|
|
|
|
|
name: node.name,
|
|
|
|
|
line: node.start.line,
|
|
|
|
|
col: node.start.col
|
|
|
|
|
}));
|
|
|
|
|
node.thedef = sym;
|
|
|
|
|
}
|
2017-06-20 19:18:48 +00:00
|
|
|
if (!(scope instanceof AST_Toplevel) && (node instanceof AST_Export || node instanceof AST_Import)) {
|
|
|
|
|
js_error(
|
|
|
|
|
node.TYPE + " statement may only appear at top level",
|
|
|
|
|
node.start.file,
|
|
|
|
|
node.start.line,
|
|
|
|
|
node.start.col,
|
|
|
|
|
node.start.pos
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-05-26 05:35:40 +00:00
|
|
|
|
|
|
|
|
function mark_export(def, level) {
|
2017-06-21 06:22:09 +00:00
|
|
|
if (in_destructuring) {
|
|
|
|
|
var i = 0;
|
|
|
|
|
do {
|
|
|
|
|
level++;
|
|
|
|
|
} while (tw.parent(i++) !== in_destructuring);
|
|
|
|
|
}
|
2017-05-26 05:35:40 +00:00
|
|
|
var node = tw.parent(level);
|
2017-06-21 06:22:09 +00:00
|
|
|
def.export = node instanceof AST_Export;
|
2017-05-26 05:35:40 +00:00
|
|
|
}
|
2012-08-19 12:57:50 +00:00
|
|
|
});
|
2012-09-11 10:15:55 +00:00
|
|
|
self.walk(tw);
|
2012-08-19 19:46:00 +00:00
|
|
|
|
2012-08-21 09:55:56 +00:00
|
|
|
// pass 2: find back references and eval
|
2017-05-25 19:58:35 +00:00
|
|
|
self.globals = new Dictionary();
|
2012-08-21 09:55:56 +00:00
|
|
|
var tw = new TreeWalker(function(node, descend){
|
2015-11-12 09:48:06 +00:00
|
|
|
if (node instanceof AST_LoopControl && node.label) {
|
|
|
|
|
node.label.thedef.references.push(node);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-08-21 08:38:49 +00:00
|
|
|
if (node instanceof AST_SymbolRef) {
|
2012-09-11 12:42:28 +00:00
|
|
|
var name = node.name;
|
2017-01-26 11:14:18 +00:00
|
|
|
if (name == "eval" && tw.parent() instanceof AST_Call) {
|
2016-02-16 18:52:28 +00:00
|
|
|
for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) {
|
|
|
|
|
s.uses_eval = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-22 19:49:30 +00:00
|
|
|
var sym;
|
|
|
|
|
if (tw.parent() instanceof AST_NameMapping && tw.parent(1).module_name
|
|
|
|
|
|| !(sym = node.scope.find_variable(name))) {
|
2017-02-18 11:27:31 +00:00
|
|
|
sym = self.def_global(node);
|
2017-06-14 19:28:26 +00:00
|
|
|
} else if (sym.scope instanceof AST_Lambda && name == "arguments") {
|
|
|
|
|
sym.scope.uses_arguments = true;
|
2016-09-20 14:23:27 +00:00
|
|
|
}
|
|
|
|
|
node.thedef = sym;
|
2017-01-26 11:18:28 +00:00
|
|
|
node.reference(options);
|
2018-01-01 17:37:59 +00:00
|
|
|
if (node.scope.is_block_scope()
|
|
|
|
|
&& !(sym.orig[0] instanceof AST_SymbolBlockDeclaration)) {
|
|
|
|
|
node.scope = node.scope.get_defun_scope();
|
|
|
|
|
}
|
2012-09-28 08:12:47 +00:00
|
|
|
return true;
|
2012-08-19 12:57:50 +00:00
|
|
|
}
|
2017-06-19 18:14:05 +00:00
|
|
|
// ensure mangling works if catch reuses a scope variable
|
|
|
|
|
var def;
|
|
|
|
|
if (node instanceof AST_SymbolCatch && (def = node.definition().redefined())) {
|
|
|
|
|
var s = node.scope;
|
|
|
|
|
while (s) {
|
|
|
|
|
push_uniq(s.enclosed, def);
|
|
|
|
|
if (s === def.scope) break;
|
|
|
|
|
s = s.parent_scope;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-19 12:57:50 +00:00
|
|
|
});
|
2012-09-11 10:15:55 +00:00
|
|
|
self.walk(tw);
|
2015-03-16 11:16:30 +00:00
|
|
|
|
2017-03-05 04:51:11 +00:00
|
|
|
// pass 3: fix up any scoping issue with IE8
|
2017-04-15 15:50:50 +00:00
|
|
|
if (options.ie8) {
|
2017-03-05 04:51:11 +00:00
|
|
|
self.walk(new TreeWalker(function(node, descend) {
|
|
|
|
|
if (node instanceof AST_SymbolCatch) {
|
|
|
|
|
var name = node.name;
|
2017-03-09 19:15:21 +00:00
|
|
|
var refs = node.thedef.references;
|
2017-03-30 18:57:47 +00:00
|
|
|
var scope = node.thedef.defun;
|
2017-03-05 04:51:11 +00:00
|
|
|
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
|
2017-03-09 19:15:21 +00:00
|
|
|
refs.forEach(function(ref) {
|
2017-03-05 04:51:11 +00:00
|
|
|
ref.thedef = def;
|
|
|
|
|
ref.reference(options);
|
|
|
|
|
});
|
|
|
|
|
node.thedef = def;
|
2017-07-31 18:38:32 +00:00
|
|
|
node.reference(options);
|
2017-03-05 04:51:11 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 08:48:43 +00:00
|
|
|
// pass 4: add symbol definitions to loop scopes
|
|
|
|
|
// Safari/Webkit bug workaround - loop init let variable shadowing argument.
|
|
|
|
|
// https://github.com/mishoo/UglifyJS2/issues/1753
|
|
|
|
|
// https://bugs.webkit.org/show_bug.cgi?id=171041
|
|
|
|
|
if (options.safari10) {
|
|
|
|
|
for (var i = 0; i < for_scopes.length; i++) {
|
|
|
|
|
var scope = for_scopes[i];
|
|
|
|
|
scope.parent_scope.variables.each(function(def) {
|
|
|
|
|
push_uniq(scope.enclosed, def);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-19 12:57:50 +00:00
|
|
|
});
|
2012-08-19 19:46:00 +00:00
|
|
|
|
2017-02-18 11:27:31 +00:00
|
|
|
AST_Toplevel.DEFMETHOD("def_global", function(node){
|
|
|
|
|
var globals = this.globals, name = node.name;
|
|
|
|
|
if (globals.has(name)) {
|
|
|
|
|
return globals.get(name);
|
|
|
|
|
} else {
|
2017-11-19 11:29:51 +00:00
|
|
|
var g = new SymbolDef(this, node);
|
2017-02-18 11:27:31 +00:00
|
|
|
g.undeclared = true;
|
|
|
|
|
g.global = true;
|
|
|
|
|
globals.set(name, g);
|
|
|
|
|
return g;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-03-26 17:30:21 +00:00
|
|
|
AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope){
|
|
|
|
|
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
|
2012-08-21 09:55:56 +00:00
|
|
|
});
|
|
|
|
|
|
2017-05-26 05:35:40 +00:00
|
|
|
AST_Node.DEFMETHOD("is_block_scope", return_false);
|
|
|
|
|
AST_Class.DEFMETHOD("is_block_scope", return_false);
|
|
|
|
|
AST_Lambda.DEFMETHOD("is_block_scope", return_false);
|
|
|
|
|
AST_Toplevel.DEFMETHOD("is_block_scope", return_false);
|
|
|
|
|
AST_SwitchBranch.DEFMETHOD("is_block_scope", return_false);
|
|
|
|
|
AST_Block.DEFMETHOD("is_block_scope", return_true);
|
|
|
|
|
AST_IterationStatement.DEFMETHOD("is_block_scope", return_true);
|
2017-03-07 15:50:58 +00:00
|
|
|
|
2012-08-21 09:55:56 +00:00
|
|
|
AST_Lambda.DEFMETHOD("init_scope_vars", function(){
|
2012-11-06 09:39:41 +00:00
|
|
|
AST_Scope.prototype.init_scope_vars.apply(this, arguments);
|
2012-08-21 09:55:56 +00:00
|
|
|
this.uses_arguments = false;
|
2017-08-14 04:31:12 +00:00
|
|
|
this.def_variable(new AST_SymbolFunarg({
|
2017-03-26 17:30:21 +00:00
|
|
|
name: "arguments",
|
|
|
|
|
start: this.start,
|
|
|
|
|
end: this.end
|
|
|
|
|
}));
|
2012-08-21 09:55:56 +00:00
|
|
|
});
|
|
|
|
|
|
2017-03-27 19:26:35 +00:00
|
|
|
AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
|
2012-09-11 12:42:28 +00:00
|
|
|
var def = this.definition();
|
2012-10-04 05:49:18 +00:00
|
|
|
var s = this.scope;
|
2012-09-11 12:42:28 +00:00
|
|
|
while (s) {
|
|
|
|
|
push_uniq(s.enclosed, def);
|
2017-01-26 11:18:28 +00:00
|
|
|
if (options.keep_fnames) {
|
2017-02-18 11:00:54 +00:00
|
|
|
s.functions.each(function(d) {
|
2017-01-26 11:18:28 +00:00
|
|
|
push_uniq(def.scope.enclosed, d);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-02-18 11:00:54 +00:00
|
|
|
if (s === def.scope) break;
|
2012-09-11 12:42:28 +00:00
|
|
|
s = s.parent_scope;
|
|
|
|
|
}
|
2012-08-21 09:55:56 +00:00
|
|
|
});
|
|
|
|
|
|
2017-03-28 08:42:39 +00:00
|
|
|
AST_Symbol.DEFMETHOD("reference", function(options) {
|
2017-03-27 19:26:35 +00:00
|
|
|
this.definition().references.push(this);
|
|
|
|
|
this.mark_enclosed(options);
|
|
|
|
|
});
|
|
|
|
|
|
2012-08-19 12:57:50 +00:00
|
|
|
AST_Scope.DEFMETHOD("find_variable", function(name){
|
|
|
|
|
if (name instanceof AST_Symbol) name = name.name;
|
2012-11-02 08:58:45 +00:00
|
|
|
return this.variables.get(name)
|
2012-10-11 08:07:42 +00:00
|
|
|
|| (this.parent_scope && this.parent_scope.find_variable(name));
|
2012-08-19 12:57:50 +00:00
|
|
|
});
|
2012-08-19 19:46:00 +00:00
|
|
|
|
2018-01-03 09:18:38 +00:00
|
|
|
AST_Scope.DEFMETHOD("def_function", function(symbol, init){
|
|
|
|
|
var def = this.def_variable(symbol, init);
|
2018-01-22 17:28:09 +00:00
|
|
|
if (!def.init || def.init instanceof AST_Defun) def.init = init;
|
2017-05-26 05:35:40 +00:00
|
|
|
this.functions.set(symbol.name, def);
|
|
|
|
|
return def;
|
2012-08-19 12:57:50 +00:00
|
|
|
});
|
2012-08-19 19:46:00 +00:00
|
|
|
|
2018-01-03 09:18:38 +00:00
|
|
|
AST_Scope.DEFMETHOD("def_variable", function(symbol, init){
|
|
|
|
|
var def = this.variables.get(symbol.name);
|
|
|
|
|
if (def) {
|
|
|
|
|
def.orig.push(symbol);
|
2018-01-03 17:03:33 +00:00
|
|
|
if (def.init && (def.scope !== symbol.scope || def.init instanceof AST_Function)) {
|
|
|
|
|
def.init = init;
|
|
|
|
|
}
|
2018-01-03 09:18:38 +00:00
|
|
|
} else {
|
|
|
|
|
def = new SymbolDef(this, symbol, init);
|
2012-11-02 08:58:45 +00:00
|
|
|
this.variables.set(symbol.name, def);
|
2017-05-26 05:35:40 +00:00
|
|
|
def.global = !this.parent_scope;
|
2012-08-21 08:38:49 +00:00
|
|
|
}
|
2012-09-11 12:42:28 +00:00
|
|
|
return symbol.thedef = def;
|
2012-08-19 12:57:50 +00:00
|
|
|
});
|
2012-08-19 19:46:00 +00:00
|
|
|
|
2018-01-04 21:08:09 +00:00
|
|
|
function next_mangled(scope, options) {
|
|
|
|
|
var ext = scope.enclosed;
|
2012-08-21 08:38:49 +00:00
|
|
|
out: while (true) {
|
2018-01-04 21:08:09 +00:00
|
|
|
var m = base54(++scope.cname);
|
2012-08-20 14:19:30 +00:00
|
|
|
if (!is_identifier(m)) continue; // skip over "do"
|
2013-12-16 18:37:09 +00:00
|
|
|
|
|
|
|
|
// https://github.com/mishoo/UglifyJS2/issues/242 -- do not
|
2017-04-15 15:50:50 +00:00
|
|
|
// shadow a name reserved from mangling.
|
2017-11-19 11:29:51 +00:00
|
|
|
if (member(m, options.reserved)) continue;
|
2013-12-16 18:37:09 +00:00
|
|
|
|
2012-08-21 17:06:57 +00:00
|
|
|
// we must ensure that the mangled name does not shadow a name
|
|
|
|
|
// from some parent scope that is referenced in this or in
|
|
|
|
|
// inner scopes.
|
2013-03-22 16:02:08 +00:00
|
|
|
for (var i = ext.length; --i >= 0;) {
|
2012-08-20 14:19:30 +00:00
|
|
|
var sym = ext[i];
|
2012-11-06 09:39:41 +00:00
|
|
|
var name = sym.mangled_name || (sym.unmangleable(options) && sym.name);
|
2012-08-20 14:19:30 +00:00
|
|
|
if (m == name) continue out;
|
|
|
|
|
}
|
|
|
|
|
return m;
|
|
|
|
|
}
|
2018-01-04 21:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AST_Scope.DEFMETHOD("next_mangled", function(options){
|
|
|
|
|
return next_mangled(this, options);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AST_Toplevel.DEFMETHOD("next_mangled", function(options){
|
|
|
|
|
var name;
|
|
|
|
|
do {
|
|
|
|
|
name = next_mangled(this, options);
|
|
|
|
|
} while (member(name, this.mangled_names));
|
|
|
|
|
return name;
|
2012-08-20 14:19:30 +00:00
|
|
|
});
|
|
|
|
|
|
2013-10-29 11:18:09 +00:00
|
|
|
AST_Function.DEFMETHOD("next_mangled", function(options, def){
|
|
|
|
|
// #179, #326
|
2013-10-29 13:53:54 +00:00
|
|
|
// in Safari strict mode, something like (function x(x){...}) is a syntax error;
|
2013-10-29 11:18:09 +00:00
|
|
|
// a function expression's argument cannot shadow the function expression's name
|
|
|
|
|
|
|
|
|
|
var tricky_def = def.orig[0] instanceof AST_SymbolFunarg && this.name && this.name.definition();
|
2016-07-13 15:44:28 +00:00
|
|
|
|
|
|
|
|
// the function's mangled_name is null when keep_fnames is true
|
|
|
|
|
var tricky_name = tricky_def ? tricky_def.mangled_name || tricky_def.name : null;
|
|
|
|
|
|
2013-10-29 11:18:09 +00:00
|
|
|
while (true) {
|
2018-01-04 21:08:09 +00:00
|
|
|
var name = next_mangled(this, options);
|
2016-07-13 15:44:28 +00:00
|
|
|
if (!tricky_name || tricky_name != name)
|
2013-10-29 11:18:09 +00:00
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2012-11-06 09:39:41 +00:00
|
|
|
AST_Symbol.DEFMETHOD("unmangleable", function(options){
|
2016-02-04 21:54:21 +00:00
|
|
|
var def = this.definition();
|
2017-06-23 12:05:31 +00:00
|
|
|
return !def || def.unmangleable(options);
|
2012-09-10 15:25:52 +00:00
|
|
|
});
|
|
|
|
|
|
2012-09-11 12:42:28 +00:00
|
|
|
// labels are always mangleable
|
2017-05-25 19:58:35 +00:00
|
|
|
AST_Label.DEFMETHOD("unmangleable", return_false);
|
2012-09-10 15:25:52 +00:00
|
|
|
|
2012-09-11 12:42:28 +00:00
|
|
|
AST_Symbol.DEFMETHOD("unreferenced", function(){
|
2012-09-19 09:27:38 +00:00
|
|
|
return this.definition().references.length == 0
|
|
|
|
|
&& !(this.scope.uses_eval || this.scope.uses_with);
|
2012-08-20 14:19:30 +00:00
|
|
|
});
|
|
|
|
|
|
2012-09-11 12:42:28 +00:00
|
|
|
AST_Symbol.DEFMETHOD("definition", function(){
|
|
|
|
|
return this.thedef;
|
2012-08-21 08:53:19 +00:00
|
|
|
});
|
|
|
|
|
|
2012-09-11 12:42:28 +00:00
|
|
|
AST_Symbol.DEFMETHOD("global", function(){
|
|
|
|
|
return this.definition().global;
|
2012-08-21 08:53:19 +00:00
|
|
|
});
|
|
|
|
|
|
2017-11-19 11:29:51 +00:00
|
|
|
AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options) {
|
2017-06-08 20:29:12 +00:00
|
|
|
options = defaults(options, {
|
2015-01-04 19:53:19 +00:00
|
|
|
eval : false,
|
2017-04-15 15:50:50 +00:00
|
|
|
ie8 : false,
|
2017-04-02 09:24:45 +00:00
|
|
|
keep_classnames: false,
|
2017-03-31 08:41:04 +00:00
|
|
|
keep_fnames : false,
|
2017-04-15 15:50:50 +00:00
|
|
|
reserved : [],
|
2015-01-04 19:53:19 +00:00
|
|
|
toplevel : false,
|
2012-10-02 08:00:47 +00:00
|
|
|
});
|
2017-06-08 20:29:12 +00:00
|
|
|
if (!Array.isArray(options.reserved)) options.reserved = [];
|
2017-11-19 11:29:51 +00:00
|
|
|
// Never mangle arguments
|
|
|
|
|
push_uniq(options.reserved, "arguments");
|
2017-06-08 20:29:12 +00:00
|
|
|
return options;
|
2012-11-08 10:31:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
|
|
|
|
options = this._default_mangler_options(options);
|
2016-01-10 22:33:54 +00:00
|
|
|
|
2012-08-21 09:37:05 +00: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;
|
2012-09-11 12:42:28 +00:00
|
|
|
var to_mangle = [];
|
2015-03-16 11:16:30 +00:00
|
|
|
|
2018-01-04 21:08:09 +00:00
|
|
|
var mangled_names = this.mangled_names = [];
|
2015-03-16 11:16:30 +00:00
|
|
|
if (options.cache) {
|
2017-11-19 11:29:51 +00:00
|
|
|
this.globals.each(collect);
|
2018-01-04 21:08:09 +00:00
|
|
|
if (options.cache.props) {
|
|
|
|
|
options.cache.props.each(function(mangled_name) {
|
|
|
|
|
push_uniq(mangled_names, mangled_name);
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-03-16 11:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-21 09:37:05 +00:00
|
|
|
var tw = new TreeWalker(function(node, descend){
|
|
|
|
|
if (node instanceof AST_LabeledStatement) {
|
|
|
|
|
// lname is incremented when we get to the AST_Label
|
|
|
|
|
var save_nesting = lname;
|
|
|
|
|
descend();
|
|
|
|
|
lname = save_nesting;
|
|
|
|
|
return true; // don't descend again in TreeWalker
|
|
|
|
|
}
|
2012-08-21 08:38:49 +00:00
|
|
|
if (node instanceof AST_Scope) {
|
2017-11-19 11:29:51 +00:00
|
|
|
node.variables.each(collect);
|
2012-08-21 09:37:05 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2018-01-14 04:12:29 +00:00
|
|
|
if (node.is_block_scope()) {
|
|
|
|
|
node.block_scope.variables.each(collect);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-08-21 09:37:05 +00:00
|
|
|
if (node instanceof AST_Label) {
|
|
|
|
|
var name;
|
|
|
|
|
do name = base54(++lname); while (!is_identifier(name));
|
|
|
|
|
node.mangled_name = name;
|
2012-10-02 11:02:33 +00:00
|
|
|
return true;
|
2012-08-20 14:19:30 +00:00
|
|
|
}
|
2018-01-14 04:12:29 +00:00
|
|
|
if (!options.ie8 && node instanceof AST_SymbolCatch) {
|
2014-02-14 11:58:14 +00:00
|
|
|
to_mangle.push(node.definition());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-08-20 14:19:30 +00:00
|
|
|
});
|
|
|
|
|
this.walk(tw);
|
2018-01-14 04:12:29 +00:00
|
|
|
to_mangle.forEach(function(def){ def.mangle(options) });
|
2015-03-16 11:16:30 +00:00
|
|
|
|
2017-11-19 11:29:51 +00:00
|
|
|
function collect(symbol) {
|
|
|
|
|
if (!member(symbol.name, options.reserved)) {
|
|
|
|
|
to_mangle.push(symbol);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-09 09:55:41 +00:00
|
|
|
AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) {
|
2017-11-19 11:29:51 +00:00
|
|
|
var cache = options.cache && options.cache.props;
|
2018-01-09 09:55:41 +00:00
|
|
|
var avoid = Object.create(null);
|
|
|
|
|
options.reserved.forEach(to_avoid);
|
2017-11-19 11:29:51 +00:00
|
|
|
this.globals.each(add_def);
|
|
|
|
|
this.walk(new TreeWalker(function(node) {
|
|
|
|
|
if (node instanceof AST_Scope) node.variables.each(add_def);
|
2017-11-23 19:05:43 +00:00
|
|
|
if (node instanceof AST_SymbolCatch) add_def(node.definition());
|
2017-11-19 11:29:51 +00:00
|
|
|
}));
|
2018-01-09 09:55:41 +00:00
|
|
|
return avoid;
|
2017-11-19 11:29:51 +00:00
|
|
|
|
2018-01-09 09:55:41 +00:00
|
|
|
function to_avoid(name) {
|
|
|
|
|
avoid[name] = true;
|
2017-11-19 11:29:51 +00: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-01-09 09:55:41 +00:00
|
|
|
to_avoid(name);
|
2017-11-19 11:29:51 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AST_Toplevel.DEFMETHOD("expand_names", function(options) {
|
2018-01-09 09:55:41 +00:00
|
|
|
base54.reset();
|
|
|
|
|
base54.sort();
|
2017-11-19 11:29:51 +00:00
|
|
|
options = this._default_mangler_options(options);
|
2018-01-09 09:55:41 +00:00
|
|
|
var avoid = this.find_colliding_names(options);
|
|
|
|
|
var cname = 0;
|
2017-11-19 11:29:51 +00:00
|
|
|
this.globals.each(rename);
|
|
|
|
|
this.walk(new TreeWalker(function(node) {
|
|
|
|
|
if (node instanceof AST_Scope) node.variables.each(rename);
|
2017-11-23 19:05:43 +00:00
|
|
|
if (node instanceof AST_SymbolCatch) rename(node.definition());
|
2017-11-19 11:29:51 +00:00
|
|
|
}));
|
|
|
|
|
|
2018-01-09 09:55:41 +00:00
|
|
|
function next_name() {
|
|
|
|
|
var name;
|
|
|
|
|
do {
|
|
|
|
|
name = base54(cname++);
|
|
|
|
|
} while (avoid[name] || !is_identifier(name));
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 11:29:51 +00:00
|
|
|
function rename(def) {
|
2018-01-09 09:55:41 +00:00
|
|
|
if (def.global && options.cache) return;
|
|
|
|
|
if (def.unmangleable(options)) return;
|
2017-11-19 11:29:51 +00:00
|
|
|
if (member(def.name, options.reserved)) return;
|
2017-11-23 19:05:43 +00:00
|
|
|
var d = def.redefined();
|
2018-01-09 09:55:41 +00:00
|
|
|
def.name = d ? d.name : next_name();
|
2017-11-19 11:29:51 +00:00
|
|
|
def.orig.forEach(function(sym) {
|
2017-11-23 19:05:43 +00:00
|
|
|
sym.name = def.name;
|
2017-11-19 11:29:51 +00:00
|
|
|
});
|
|
|
|
|
def.references.forEach(function(sym) {
|
2017-11-23 19:05:43 +00:00
|
|
|
sym.name = def.name;
|
2017-11-19 11:29:51 +00:00
|
|
|
});
|
|
|
|
|
}
|
2012-08-20 14:19:30 +00:00
|
|
|
});
|
2012-09-10 15:25:52 +00:00
|
|
|
|
2017-11-30 19:40:46 +00:00
|
|
|
AST_Node.DEFMETHOD("tail_node", return_this);
|
|
|
|
|
AST_Sequence.DEFMETHOD("tail_node", function() {
|
|
|
|
|
return this.expressions[this.expressions.length - 1];
|
|
|
|
|
});
|
|
|
|
|
|
2012-11-06 09:39:41 +00:00
|
|
|
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
|
2012-11-08 10:31:28 +00:00
|
|
|
options = this._default_mangler_options(options);
|
2017-06-23 12:05:31 +00: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;
|
|
|
|
|
}
|
2012-10-08 10:37:27 +00:00
|
|
|
base54.sort();
|
2017-06-23 12:05:31 +00: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-11-30 19:40:46 +00:00
|
|
|
skip_string(node.tail_node());
|
2017-06-23 12:05:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-09-10 15:25:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var base54 = (function() {
|
2017-06-23 12:05:31 +00:00
|
|
|
var leading = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".split("");
|
|
|
|
|
var digits = "0123456789".split("");
|
2012-09-10 15:25:52 +00:00
|
|
|
var chars, frequency;
|
|
|
|
|
function reset() {
|
2012-10-11 08:07:42 +00:00
|
|
|
frequency = Object.create(null);
|
2017-06-23 12:05:31 +00:00
|
|
|
leading.forEach(function(ch) {
|
|
|
|
|
frequency[ch] = 0;
|
|
|
|
|
});
|
|
|
|
|
digits.forEach(function(ch) {
|
|
|
|
|
frequency[ch] = 0;
|
|
|
|
|
});
|
2012-09-10 15:25:52 +00:00
|
|
|
}
|
2017-06-23 12:05:31 +00:00
|
|
|
base54.consider = function(str, delta) {
|
2012-09-10 15:25:52 +00:00
|
|
|
for (var i = str.length; --i >= 0;) {
|
2017-06-23 12:05:31 +00:00
|
|
|
frequency[str[i]] += delta;
|
2012-09-10 15:25:52 +00:00
|
|
|
}
|
|
|
|
|
};
|
2017-06-23 12:05:31 +00:00
|
|
|
function compare(a, b) {
|
|
|
|
|
return frequency[b] - frequency[a];
|
|
|
|
|
}
|
2012-09-10 15:25:52 +00:00
|
|
|
base54.sort = function() {
|
2017-06-23 12:05:31 +00:00
|
|
|
chars = mergeSort(leading, compare).concat(mergeSort(digits, compare));
|
2012-09-10 15:25:52 +00:00
|
|
|
};
|
|
|
|
|
base54.reset = reset;
|
|
|
|
|
reset();
|
|
|
|
|
function base54(num) {
|
|
|
|
|
var ret = "", base = 54;
|
2014-12-01 05:16:44 +00:00
|
|
|
num++;
|
2012-09-10 15:25:52 +00:00
|
|
|
do {
|
2014-12-01 05:16:44 +00:00
|
|
|
num--;
|
2017-06-23 12:05:31 +00:00
|
|
|
ret += chars[num % base];
|
2012-09-10 15:25:52 +00:00
|
|
|
num = Math.floor(num / base);
|
|
|
|
|
base = 64;
|
|
|
|
|
} while (num > 0);
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
|
|
|
|
return base54;
|
|
|
|
|
})();
|