utils.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. function slice(a, start) {
  35. return Array.prototype.slice.call(a, start || 0);
  36. };
  37. function characters(str) {
  38. return str.split("");
  39. };
  40. function member(name, array) {
  41. return array.indexOf(name) >= 0;
  42. };
  43. function find_if(func, array) {
  44. for (var i = 0, n = array.length; i < n; ++i) {
  45. if (func(array[i]))
  46. return array[i];
  47. }
  48. };
  49. function repeat_string(str, i) {
  50. if (i <= 0) return "";
  51. if (i == 1) return str;
  52. var d = repeat_string(str, i >> 1);
  53. d += d;
  54. if (i & 1) d += str;
  55. return d;
  56. };
  57. function configure_error_stack(fn) {
  58. Object.defineProperty(fn.prototype, "stack", {
  59. get: function() {
  60. var err = new Error(this.message);
  61. err.name = this.name;
  62. try {
  63. throw err;
  64. } catch(e) {
  65. return e.stack;
  66. }
  67. }
  68. });
  69. }
  70. function DefaultsError(msg, defs) {
  71. this.message = msg;
  72. this.defs = defs;
  73. };
  74. DefaultsError.prototype = Object.create(Error.prototype);
  75. DefaultsError.prototype.constructor = DefaultsError;
  76. DefaultsError.prototype.name = "DefaultsError";
  77. configure_error_stack(DefaultsError);
  78. DefaultsError.croak = function(msg, defs) {
  79. throw new DefaultsError(msg, defs);
  80. };
  81. function defaults(args, defs, croak) {
  82. if (args === true)
  83. args = {};
  84. var ret = args || {};
  85. if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i))
  86. DefaultsError.croak("`" + i + "` is not a supported option", defs);
  87. for (var i in defs) if (HOP(defs, i)) {
  88. ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
  89. }
  90. return ret;
  91. };
  92. function merge(obj, ext) {
  93. var count = 0;
  94. for (var i in ext) if (HOP(ext, i)) {
  95. obj[i] = ext[i];
  96. count++;
  97. }
  98. return count;
  99. };
  100. function noop() {}
  101. function return_false() { return false; }
  102. function return_true() { return true; }
  103. function return_this() { return this; }
  104. function return_null() { return null; }
  105. var MAP = (function(){
  106. function MAP(a, f, backwards) {
  107. var ret = [], top = [], i;
  108. function doit() {
  109. var val = f(a[i], i);
  110. var is_last = val instanceof Last;
  111. if (is_last) val = val.v;
  112. if (val instanceof AtTop) {
  113. val = val.v;
  114. if (val instanceof Splice) {
  115. top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
  116. } else {
  117. top.push(val);
  118. }
  119. }
  120. else if (val !== skip) {
  121. if (val instanceof Splice) {
  122. ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
  123. } else {
  124. ret.push(val);
  125. }
  126. }
  127. return is_last;
  128. };
  129. if (a instanceof Array) {
  130. if (backwards) {
  131. for (i = a.length; --i >= 0;) if (doit()) break;
  132. ret.reverse();
  133. top.reverse();
  134. } else {
  135. for (i = 0; i < a.length; ++i) if (doit()) break;
  136. }
  137. }
  138. else {
  139. for (i in a) if (HOP(a, i)) if (doit()) break;
  140. }
  141. return top.concat(ret);
  142. };
  143. MAP.at_top = function(val) { return new AtTop(val) };
  144. MAP.splice = function(val) { return new Splice(val) };
  145. MAP.last = function(val) { return new Last(val) };
  146. var skip = MAP.skip = {};
  147. function AtTop(val) { this.v = val };
  148. function Splice(val) { this.v = val };
  149. function Last(val) { this.v = val };
  150. return MAP;
  151. })();
  152. function push_uniq(array, el) {
  153. if (array.indexOf(el) < 0)
  154. array.push(el);
  155. };
  156. function string_template(text, props) {
  157. return text.replace(/\{(.+?)\}/g, function(str, p){
  158. return props && props[p];
  159. });
  160. };
  161. function remove(array, el) {
  162. for (var i = array.length; --i >= 0;) {
  163. if (array[i] === el) array.splice(i, 1);
  164. }
  165. };
  166. function mergeSort(array, cmp) {
  167. if (array.length < 2) return array.slice();
  168. function merge(a, b) {
  169. var r = [], ai = 0, bi = 0, i = 0;
  170. while (ai < a.length && bi < b.length) {
  171. cmp(a[ai], b[bi]) <= 0
  172. ? r[i++] = a[ai++]
  173. : r[i++] = b[bi++];
  174. }
  175. if (ai < a.length) r.push.apply(r, a.slice(ai));
  176. if (bi < b.length) r.push.apply(r, b.slice(bi));
  177. return r;
  178. };
  179. function _ms(a) {
  180. if (a.length <= 1)
  181. return a;
  182. var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m);
  183. left = _ms(left);
  184. right = _ms(right);
  185. return merge(left, right);
  186. };
  187. return _ms(array);
  188. };
  189. function set_difference(a, b) {
  190. return a.filter(function(el){
  191. return b.indexOf(el) < 0;
  192. });
  193. };
  194. function set_intersection(a, b) {
  195. return a.filter(function(el){
  196. return b.indexOf(el) >= 0;
  197. });
  198. };
  199. // this function is taken from Acorn [1], written by Marijn Haverbeke
  200. // [1] https://github.com/marijnh/acorn
  201. function makePredicate(words) {
  202. if (!(words instanceof Array)) words = words.split(" ");
  203. var f = "", cats = [];
  204. out: for (var i = 0; i < words.length; ++i) {
  205. for (var j = 0; j < cats.length; ++j)
  206. if (cats[j][0].length == words[i].length) {
  207. cats[j].push(words[i]);
  208. continue out;
  209. }
  210. cats.push([words[i]]);
  211. }
  212. function quote(word) {
  213. return JSON.stringify(word).replace(/[\u2028\u2029]/g, function(s) {
  214. switch (s) {
  215. case "\u2028": return "\\u2028";
  216. case "\u2029": return "\\u2029";
  217. }
  218. return s;
  219. });
  220. }
  221. function compareTo(arr) {
  222. if (arr.length == 1) return f += "return str === " + quote(arr[0]) + ";";
  223. f += "switch(str){";
  224. for (var i = 0; i < arr.length; ++i) f += "case " + quote(arr[i]) + ":";
  225. f += "return true}return false;";
  226. }
  227. // When there are more than three length categories, an outer
  228. // switch first dispatches on the lengths, to save on comparisons.
  229. if (cats.length > 3) {
  230. cats.sort(function(a, b) {return b.length - a.length;});
  231. f += "switch(str.length){";
  232. for (var i = 0; i < cats.length; ++i) {
  233. var cat = cats[i];
  234. f += "case " + cat[0].length + ":";
  235. compareTo(cat);
  236. }
  237. f += "}";
  238. // Otherwise, simply generate a flat `switch` statement.
  239. } else {
  240. compareTo(words);
  241. }
  242. return new Function("str", f);
  243. };
  244. function all(array, predicate) {
  245. for (var i = array.length; --i >= 0;)
  246. if (!predicate(array[i]))
  247. return false;
  248. return true;
  249. };
  250. function Dictionary() {
  251. this._values = Object.create(null);
  252. this._size = 0;
  253. };
  254. Dictionary.prototype = {
  255. set: function(key, val) {
  256. if (!this.has(key)) ++this._size;
  257. this._values["$" + key] = val;
  258. return this;
  259. },
  260. add: function(key, val) {
  261. if (this.has(key)) {
  262. this.get(key).push(val);
  263. } else {
  264. this.set(key, [ val ]);
  265. }
  266. return this;
  267. },
  268. get: function(key) { return this._values["$" + key] },
  269. del: function(key) {
  270. if (this.has(key)) {
  271. --this._size;
  272. delete this._values["$" + key];
  273. }
  274. return this;
  275. },
  276. has: function(key) { return ("$" + key) in this._values },
  277. each: function(f) {
  278. for (var i in this._values)
  279. f(this._values[i], i.substr(1));
  280. },
  281. size: function() {
  282. return this._size;
  283. },
  284. map: function(f) {
  285. var ret = [];
  286. for (var i in this._values)
  287. ret.push(f(this._values[i], i.substr(1)));
  288. return ret;
  289. },
  290. toObject: function() { return this._values }
  291. };
  292. Dictionary.fromObject = function(obj) {
  293. var dict = new Dictionary();
  294. dict._size = merge(dict._values, obj);
  295. return dict;
  296. };
  297. function HOP(obj, prop) {
  298. return Object.prototype.hasOwnProperty.call(obj, prop);
  299. }
  300. // return true if the node at the top of the stack (that means the
  301. // innermost node in the current output) is lexically the first in
  302. // a statement.
  303. function first_in_statement(stack) {
  304. var node = stack.parent(-1);
  305. for (var i = 0, p; p = stack.parent(i); i++) {
  306. if (p instanceof AST_Statement && p.body === node)
  307. return true;
  308. if ((p instanceof AST_Sequence && p.expressions[0] === node) ||
  309. (p instanceof AST_Call && p.expression === node && !(p instanceof AST_New) ) ||
  310. (p instanceof AST_Dot && p.expression === node ) ||
  311. (p instanceof AST_Sub && p.expression === node ) ||
  312. (p instanceof AST_Conditional && p.condition === node ) ||
  313. (p instanceof AST_Binary && p.left === node ) ||
  314. (p instanceof AST_UnaryPostfix && p.expression === node ))
  315. {
  316. node = p;
  317. } else {
  318. return false;
  319. }
  320. }
  321. }