wallet-core/node_modules/uglify-js/lib/utils.js

278 lines
8.3 KiB
JavaScript
Raw Normal View History

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";
function characters(str) {
return str.split("");
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
function member(name, array) {
return array.indexOf(name) >= 0;
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
function find_if(func, array) {
2018-09-20 02:56:13 +02:00
for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i];
}
2017-05-03 15:35:00 +02:00
function repeat_string(str, i) {
if (i <= 0) return "";
if (i == 1) return str;
var d = repeat_string(str, i >> 1);
d += d;
2018-09-20 02:56:13 +02:00
return i & 1 ? d + str : d;
}
2017-05-03 15:35:00 +02:00
function configure_error_stack(fn) {
Object.defineProperty(fn.prototype, "stack", {
get: function() {
var err = new Error(this.message);
err.name = this.name;
try {
throw err;
} catch(e) {
return e.stack;
}
}
});
}
function DefaultsError(msg, defs) {
this.message = msg;
this.defs = defs;
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
DefaultsError.prototype = Object.create(Error.prototype);
DefaultsError.prototype.constructor = DefaultsError;
DefaultsError.prototype.name = "DefaultsError";
configure_error_stack(DefaultsError);
function defaults(args, defs, croak) {
2018-09-20 02:56:13 +02:00
if (args === true) args = {};
2017-05-03 15:35:00 +02:00
var ret = args || {};
2018-09-20 02:56:13 +02:00
if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) {
throw new DefaultsError("`" + i + "` is not a supported option", defs);
}
2017-05-03 15:35:00 +02:00
for (var i in defs) if (HOP(defs, i)) {
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
}
return ret;
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
function merge(obj, ext) {
var count = 0;
for (var i in ext) if (HOP(ext, i)) {
obj[i] = ext[i];
count++;
}
return count;
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
function noop() {}
function return_false() { return false; }
function return_true() { return true; }
function return_this() { return this; }
function return_null() { return null; }
2018-09-20 02:56:13 +02:00
var MAP = (function() {
2017-05-03 15:35:00 +02:00
function MAP(a, f, backwards) {
var ret = [], top = [], i;
function doit() {
var val = f(a[i], i);
var is_last = val instanceof Last;
if (is_last) val = val.v;
if (val instanceof AtTop) {
val = val.v;
if (val instanceof Splice) {
top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
} else {
top.push(val);
}
}
else if (val !== skip) {
if (val instanceof Splice) {
ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
} else {
ret.push(val);
}
}
return is_last;
2018-09-20 02:56:13 +02:00
}
if (Array.isArray(a)) {
2017-05-03 15:35:00 +02:00
if (backwards) {
for (i = a.length; --i >= 0;) if (doit()) break;
ret.reverse();
top.reverse();
} else {
for (i = 0; i < a.length; ++i) if (doit()) break;
}
}
else {
for (i in a) if (HOP(a, i)) if (doit()) break;
}
return top.concat(ret);
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
MAP.at_top = function(val) { return new AtTop(val) };
MAP.splice = function(val) { return new Splice(val) };
MAP.last = function(val) { return new Last(val) };
var skip = MAP.skip = {};
2018-09-20 02:56:13 +02:00
function AtTop(val) { this.v = val }
function Splice(val) { this.v = val }
function Last(val) { this.v = val }
2017-05-03 15:35:00 +02:00
return MAP;
})();
function push_uniq(array, el) {
2018-09-20 02:56:13 +02:00
if (array.indexOf(el) < 0) return array.push(el);
}
2017-05-03 15:35:00 +02:00
function string_template(text, props) {
2018-09-20 02:56:13 +02:00
return text.replace(/\{(.+?)\}/g, function(str, p) {
2017-05-03 15:35:00 +02:00
return props && props[p];
});
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
function remove(array, el) {
2018-09-20 02:56:13 +02:00
var index = array.indexOf(el);
if (index >= 0) array.splice(index, 1);
}
2017-05-03 15:35:00 +02:00
function makePredicate(words) {
2018-09-20 02:56:13 +02:00
if (!Array.isArray(words)) words = words.split(" ");
var map = Object.create(null);
words.forEach(function(word) {
map[word] = true;
});
return map;
}
2017-05-03 15:35:00 +02:00
function all(array, predicate) {
for (var i = array.length; --i >= 0;)
if (!predicate(array[i]))
return false;
return true;
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
function Dictionary() {
this._values = Object.create(null);
this._size = 0;
2018-09-20 02:56:13 +02:00
}
2017-05-03 15:35:00 +02:00
Dictionary.prototype = {
set: function(key, val) {
if (!this.has(key)) ++this._size;
this._values["$" + key] = val;
return this;
},
add: function(key, val) {
if (this.has(key)) {
this.get(key).push(val);
} else {
this.set(key, [ val ]);
}
return this;
},
get: function(key) { return this._values["$" + key] },
del: function(key) {
if (this.has(key)) {
--this._size;
delete this._values["$" + key];
}
return this;
},
has: function(key) { return ("$" + key) in this._values },
each: function(f) {
for (var i in this._values)
f(this._values[i], i.substr(1));
},
size: function() {
return this._size;
},
map: function(f) {
var ret = [];
for (var i in this._values)
ret.push(f(this._values[i], i.substr(1)));
return ret;
},
2018-09-20 02:56:13 +02:00
clone: function() {
var ret = new Dictionary();
for (var i in this._values)
ret._values[i] = this._values[i];
ret._size = this._size;
return ret;
},
2017-05-03 15:35:00 +02:00
toObject: function() { return this._values }
};
Dictionary.fromObject = function(obj) {
var dict = new Dictionary();
dict._size = merge(dict._values, obj);
return dict;
};
function HOP(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
// return true if the node at the top of the stack (that means the
// innermost node in the current output) is lexically the first in
// a statement.
function first_in_statement(stack) {
var node = stack.parent(-1);
2018-09-20 02:56:13 +02:00
for (var i = 0, p; p = stack.parent(i++); node = p) {
if (p.TYPE == "Call") {
if (p.expression === node) continue;
} else if (p instanceof AST_Binary) {
if (p.left === node) continue;
} else if (p instanceof AST_Conditional) {
if (p.condition === node) continue;
} else if (p instanceof AST_PropAccess) {
if (p.expression === node) continue;
} else if (p instanceof AST_Sequence) {
if (p.expressions[0] === node) continue;
} else if (p instanceof AST_Statement) {
return p.body === node;
} else if (p instanceof AST_UnaryPostfix) {
if (p.expression === node) continue;
2017-05-03 15:35:00 +02:00
}
2018-09-20 02:56:13 +02:00
return false;
2017-05-03 15:35:00 +02:00
}
}