utils.js 8.5 KB

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