utils.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 characters(str) {
  35. return str.split("");
  36. }
  37. function member(name, array) {
  38. return array.indexOf(name) >= 0;
  39. }
  40. function find_if(func, array) {
  41. for (var i = array.length; --i >= 0;) if (func(array[i])) return array[i];
  42. }
  43. function repeat_string(str, i) {
  44. if (i <= 0) return "";
  45. if (i == 1) return str;
  46. var d = repeat_string(str, i >> 1);
  47. d += d;
  48. return i & 1 ? d + str : d;
  49. }
  50. function configure_error_stack(fn) {
  51. Object.defineProperty(fn.prototype, "stack", {
  52. get: function() {
  53. var err = new Error(this.message);
  54. err.name = this.name;
  55. try {
  56. throw err;
  57. } catch(e) {
  58. return e.stack;
  59. }
  60. }
  61. });
  62. }
  63. function DefaultsError(msg, defs) {
  64. this.message = msg;
  65. this.defs = defs;
  66. }
  67. DefaultsError.prototype = Object.create(Error.prototype);
  68. DefaultsError.prototype.constructor = DefaultsError;
  69. DefaultsError.prototype.name = "DefaultsError";
  70. configure_error_stack(DefaultsError);
  71. function defaults(args, defs, croak) {
  72. if (args === true) args = {};
  73. var ret = args || {};
  74. if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) {
  75. throw new DefaultsError("`" + i + "` is not a supported option", defs);
  76. }
  77. for (var i in defs) if (HOP(defs, i)) {
  78. ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
  79. }
  80. return ret;
  81. }
  82. function merge(obj, ext) {
  83. var count = 0;
  84. for (var i in ext) if (HOP(ext, i)) {
  85. obj[i] = ext[i];
  86. count++;
  87. }
  88. return count;
  89. }
  90. function noop() {}
  91. function return_false() { return false; }
  92. function return_true() { return true; }
  93. function return_this() { return this; }
  94. function return_null() { return null; }
  95. var MAP = (function() {
  96. function MAP(a, f, backwards) {
  97. var ret = [], top = [], i;
  98. function doit() {
  99. var val = f(a[i], i);
  100. var is_last = val instanceof Last;
  101. if (is_last) val = val.v;
  102. if (val instanceof AtTop) {
  103. val = val.v;
  104. if (val instanceof Splice) {
  105. top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
  106. } else {
  107. top.push(val);
  108. }
  109. }
  110. else if (val !== skip) {
  111. if (val instanceof Splice) {
  112. ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
  113. } else {
  114. ret.push(val);
  115. }
  116. }
  117. return is_last;
  118. }
  119. if (Array.isArray(a)) {
  120. if (backwards) {
  121. for (i = a.length; --i >= 0;) if (doit()) break;
  122. ret.reverse();
  123. top.reverse();
  124. } else {
  125. for (i = 0; i < a.length; ++i) if (doit()) break;
  126. }
  127. }
  128. else {
  129. for (i in a) if (HOP(a, i)) if (doit()) break;
  130. }
  131. return top.concat(ret);
  132. }
  133. MAP.at_top = function(val) { return new AtTop(val) };
  134. MAP.splice = function(val) { return new Splice(val) };
  135. MAP.last = function(val) { return new Last(val) };
  136. var skip = MAP.skip = {};
  137. function AtTop(val) { this.v = val }
  138. function Splice(val) { this.v = val }
  139. function Last(val) { this.v = val }
  140. return MAP;
  141. })();
  142. function push_uniq(array, el) {
  143. if (array.indexOf(el) < 0) return array.push(el);
  144. }
  145. function string_template(text, props) {
  146. return text.replace(/\{(.+?)\}/g, function(str, p) {
  147. return props && props[p];
  148. });
  149. }
  150. function remove(array, el) {
  151. var index = array.indexOf(el);
  152. if (index >= 0) array.splice(index, 1);
  153. }
  154. function makePredicate(words) {
  155. if (!Array.isArray(words)) words = words.split(" ");
  156. var map = Object.create(null);
  157. words.forEach(function(word) {
  158. map[word] = true;
  159. });
  160. return map;
  161. }
  162. function all(array, predicate) {
  163. for (var i = array.length; --i >= 0;)
  164. if (!predicate(array[i]))
  165. return false;
  166. return true;
  167. }
  168. function Dictionary() {
  169. this._values = Object.create(null);
  170. this._size = 0;
  171. }
  172. Dictionary.prototype = {
  173. set: function(key, val) {
  174. if (!this.has(key)) ++this._size;
  175. this._values["$" + key] = val;
  176. return this;
  177. },
  178. add: function(key, val) {
  179. if (this.has(key)) {
  180. this.get(key).push(val);
  181. } else {
  182. this.set(key, [ val ]);
  183. }
  184. return this;
  185. },
  186. get: function(key) { return this._values["$" + key] },
  187. del: function(key) {
  188. if (this.has(key)) {
  189. --this._size;
  190. delete this._values["$" + key];
  191. }
  192. return this;
  193. },
  194. has: function(key) { return ("$" + key) in this._values },
  195. each: function(f) {
  196. for (var i in this._values)
  197. f(this._values[i], i.substr(1));
  198. },
  199. size: function() {
  200. return this._size;
  201. },
  202. map: function(f) {
  203. var ret = [];
  204. for (var i in this._values)
  205. ret.push(f(this._values[i], i.substr(1)));
  206. return ret;
  207. },
  208. clone: function() {
  209. var ret = new Dictionary();
  210. for (var i in this._values)
  211. ret._values[i] = this._values[i];
  212. ret._size = this._size;
  213. return ret;
  214. },
  215. toObject: function() { return this._values }
  216. };
  217. Dictionary.fromObject = function(obj) {
  218. var dict = new Dictionary();
  219. dict._size = merge(dict._values, obj);
  220. return dict;
  221. };
  222. function HOP(obj, prop) {
  223. return Object.prototype.hasOwnProperty.call(obj, prop);
  224. }
  225. // return true if the node at the top of the stack (that means the
  226. // innermost node in the current output) is lexically the first in
  227. // a statement.
  228. function first_in_statement(stack) {
  229. var node = stack.parent(-1);
  230. for (var i = 0, p; p = stack.parent(i++); node = p) {
  231. if (p.TYPE == "Call") {
  232. if (p.expression === node) continue;
  233. } else if (p instanceof AST_Binary) {
  234. if (p.left === node) continue;
  235. } else if (p instanceof AST_Conditional) {
  236. if (p.condition === node) continue;
  237. } else if (p instanceof AST_PropAccess) {
  238. if (p.expression === node) continue;
  239. } else if (p instanceof AST_Sequence) {
  240. if (p.expressions[0] === node) continue;
  241. } else if (p instanceof AST_Statement) {
  242. return p.body === node;
  243. } else if (p instanceof AST_UnaryPostfix) {
  244. if (p.expression === node) continue;
  245. }
  246. return false;
  247. }
  248. }