utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 array_to_hash(a) {
  35. var ret = Object.create(null);
  36. for (var i = 0; i < a.length; ++i)
  37. ret[a[i]] = true;
  38. return ret;
  39. };
  40. function slice(a, start) {
  41. return Array.prototype.slice.call(a, start || 0);
  42. };
  43. function characters(str) {
  44. return str.split("");
  45. };
  46. function member(name, array) {
  47. return array.indexOf(name) >= 0;
  48. };
  49. function find_if(func, array) {
  50. for (var i = 0, n = array.length; i < n; ++i) {
  51. if (func(array[i]))
  52. return array[i];
  53. }
  54. };
  55. function repeat_string(str, i) {
  56. if (i <= 0) return "";
  57. if (i == 1) return str;
  58. var d = repeat_string(str, i >> 1);
  59. d += d;
  60. if (i & 1) d += str;
  61. return d;
  62. };
  63. function configure_error_stack(fn) {
  64. Object.defineProperty(fn.prototype, "stack", {
  65. get: function() {
  66. var err = new Error(this.message);
  67. err.name = this.name;
  68. try {
  69. throw err;
  70. } catch(e) {
  71. return e.stack;
  72. }
  73. }
  74. });
  75. }
  76. function DefaultsError(msg, defs) {
  77. this.message = msg;
  78. this.defs = defs;
  79. };
  80. DefaultsError.prototype = Object.create(Error.prototype);
  81. DefaultsError.prototype.constructor = DefaultsError;
  82. DefaultsError.prototype.name = "DefaultsError";
  83. configure_error_stack(DefaultsError);
  84. DefaultsError.croak = function(msg, defs) {
  85. throw new DefaultsError(msg, defs);
  86. };
  87. function defaults(args, defs, croak) {
  88. if (args === true)
  89. args = {};
  90. var ret = args || {};
  91. if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i))
  92. DefaultsError.croak("`" + i + "` is not a supported option", defs);
  93. for (var i in defs) if (HOP(defs, i)) {
  94. ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
  95. }
  96. return ret;
  97. };
  98. function merge(obj, ext) {
  99. var count = 0;
  100. for (var i in ext) if (HOP(ext, i)) {
  101. obj[i] = ext[i];
  102. count++;
  103. }
  104. return count;
  105. };
  106. function noop() {}
  107. function return_false() { return false; }
  108. function return_true() { return true; }
  109. function return_this() { return this; }
  110. function return_null() { return null; }
  111. var MAP = (function(){
  112. function MAP(a, f, backwards) {
  113. var ret = [], top = [], i;
  114. function doit() {
  115. var val = f(a[i], i);
  116. var is_last = val instanceof Last;
  117. if (is_last) val = val.v;
  118. if (val instanceof AtTop) {
  119. val = val.v;
  120. if (val instanceof Splice) {
  121. top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
  122. } else {
  123. top.push(val);
  124. }
  125. }
  126. else if (val !== skip) {
  127. if (val instanceof Splice) {
  128. ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
  129. } else {
  130. ret.push(val);
  131. }
  132. }
  133. return is_last;
  134. };
  135. if (a instanceof Array) {
  136. if (backwards) {
  137. for (i = a.length; --i >= 0;) if (doit()) break;
  138. ret.reverse();
  139. top.reverse();
  140. } else {
  141. for (i = 0; i < a.length; ++i) if (doit()) break;
  142. }
  143. }
  144. else {
  145. for (i in a) if (HOP(a, i)) if (doit()) break;
  146. }
  147. return top.concat(ret);
  148. };
  149. MAP.at_top = function(val) { return new AtTop(val) };
  150. MAP.splice = function(val) { return new Splice(val) };
  151. MAP.last = function(val) { return new Last(val) };
  152. var skip = MAP.skip = {};
  153. function AtTop(val) { this.v = val };
  154. function Splice(val) { this.v = val };
  155. function Last(val) { this.v = val };
  156. return MAP;
  157. })();
  158. function push_uniq(array, el) {
  159. if (array.indexOf(el) < 0)
  160. array.push(el);
  161. };
  162. function string_template(text, props) {
  163. return text.replace(/\{(.+?)\}/g, function(str, p){
  164. return props && props[p];
  165. });
  166. };
  167. function remove(array, el) {
  168. for (var i = array.length; --i >= 0;) {
  169. if (array[i] === el) array.splice(i, 1);
  170. }
  171. };
  172. function mergeSort(array, cmp) {
  173. if (array.length < 2) return array.slice();
  174. function merge(a, b) {
  175. var r = [], ai = 0, bi = 0, i = 0;
  176. while (ai < a.length && bi < b.length) {
  177. cmp(a[ai], b[bi]) <= 0
  178. ? r[i++] = a[ai++]
  179. : r[i++] = b[bi++];
  180. }
  181. if (ai < a.length) r.push.apply(r, a.slice(ai));
  182. if (bi < b.length) r.push.apply(r, b.slice(bi));
  183. return r;
  184. };
  185. function _ms(a) {
  186. if (a.length <= 1)
  187. return a;
  188. var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m);
  189. left = _ms(left);
  190. right = _ms(right);
  191. return merge(left, right);
  192. };
  193. return _ms(array);
  194. };
  195. function set_difference(a, b) {
  196. return a.filter(function(el){
  197. return b.indexOf(el) < 0;
  198. });
  199. };
  200. function set_intersection(a, b) {
  201. return a.filter(function(el){
  202. return b.indexOf(el) >= 0;
  203. });
  204. };
  205. // this function is taken from Acorn [1], written by Marijn Haverbeke
  206. // [1] https://github.com/marijnh/acorn
  207. function makePredicate(words) {
  208. if (!(words instanceof Array)) words = words.split(" ");
  209. var f = "", cats = [];
  210. out: for (var i = 0; i < words.length; ++i) {
  211. for (var j = 0; j < cats.length; ++j)
  212. if (cats[j][0].length == words[i].length) {
  213. cats[j].push(words[i]);
  214. continue out;
  215. }
  216. cats.push([words[i]]);
  217. }
  218. function quote(word) {
  219. return JSON.stringify(word).replace(/[\u2028\u2029]/g, function(s) {
  220. switch (s) {
  221. case "\u2028": return "\\u2028";
  222. case "\u2029": return "\\u2029";
  223. }
  224. return s;
  225. });
  226. }
  227. function compareTo(arr) {
  228. if (arr.length == 1) return f += "return str === " + quote(arr[0]) + ";";
  229. f += "switch(str){";
  230. for (var i = 0; i < arr.length; ++i) f += "case " + quote(arr[i]) + ":";
  231. f += "return true}return false;";
  232. }
  233. // When there are more than three length categories, an outer
  234. // switch first dispatches on the lengths, to save on comparisons.
  235. if (cats.length > 3) {
  236. cats.sort(function(a, b) {return b.length - a.length;});
  237. f += "switch(str.length){";
  238. for (var i = 0; i < cats.length; ++i) {
  239. var cat = cats[i];
  240. f += "case " + cat[0].length + ":";
  241. compareTo(cat);
  242. }
  243. f += "}";
  244. // Otherwise, simply generate a flat `switch` statement.
  245. } else {
  246. compareTo(words);
  247. }
  248. return new Function("str", f);
  249. };
  250. function all(array, predicate) {
  251. for (var i = array.length; --i >= 0;)
  252. if (!predicate(array[i]))
  253. return false;
  254. return true;
  255. };
  256. function Dictionary() {
  257. this._values = Object.create(null);
  258. this._size = 0;
  259. };
  260. Dictionary.prototype = {
  261. set: function(key, val) {
  262. if (!this.has(key)) ++this._size;
  263. this._values["$" + key] = val;
  264. return this;
  265. },
  266. add: function(key, val) {
  267. if (this.has(key)) {
  268. this.get(key).push(val);
  269. } else {
  270. this.set(key, [ val ]);
  271. }
  272. return this;
  273. },
  274. get: function(key) { return this._values["$" + key] },
  275. del: function(key) {
  276. if (this.has(key)) {
  277. --this._size;
  278. delete this._values["$" + key];
  279. }
  280. return this;
  281. },
  282. has: function(key) { return ("$" + key) in this._values },
  283. each: function(f) {
  284. for (var i in this._values)
  285. f(this._values[i], i.substr(1));
  286. },
  287. size: function() {
  288. return this._size;
  289. },
  290. map: function(f) {
  291. var ret = [];
  292. for (var i in this._values)
  293. ret.push(f(this._values[i], i.substr(1)));
  294. return ret;
  295. },
  296. toObject: function() { return this._values }
  297. };
  298. Dictionary.fromObject = function(obj) {
  299. var dict = new Dictionary();
  300. dict._size = merge(dict._values, obj);
  301. return dict;
  302. };
  303. function HOP(obj, prop) {
  304. return Object.prototype.hasOwnProperty.call(obj, prop);
  305. }
  306. // return true if the node at the top of the stack (that means the
  307. // innermost node in the current output) is lexically the first in
  308. // a statement.
  309. function first_in_statement(stack) {
  310. var node = stack.parent(-1);
  311. for (var i = 0, p; p = stack.parent(i); i++) {
  312. if (p instanceof AST_Statement && p.body === node)
  313. return true;
  314. if ((p instanceof AST_Seq && p.car === node ) ||
  315. (p instanceof AST_Call && p.expression === node && !(p instanceof AST_New) ) ||
  316. (p instanceof AST_Dot && p.expression === node ) ||
  317. (p instanceof AST_Sub && p.expression === node ) ||
  318. (p instanceof AST_Conditional && p.condition === node ) ||
  319. (p instanceof AST_Binary && p.left === node ) ||
  320. (p instanceof AST_UnaryPostfix && p.expression === node ))
  321. {
  322. node = p;
  323. } else {
  324. return false;
  325. }
  326. }
  327. }