util.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. "use strict";
  2. var es5 = require("./es5");
  3. var canEvaluate = typeof navigator == "undefined";
  4. var errorObj = {e: {}};
  5. var tryCatchTarget;
  6. var globalObject = typeof self !== "undefined" ? self :
  7. typeof window !== "undefined" ? window :
  8. typeof global !== "undefined" ? global :
  9. this !== undefined ? this : null;
  10. function tryCatcher() {
  11. try {
  12. var target = tryCatchTarget;
  13. tryCatchTarget = null;
  14. return target.apply(this, arguments);
  15. } catch (e) {
  16. errorObj.e = e;
  17. return errorObj;
  18. }
  19. }
  20. function tryCatch(fn) {
  21. tryCatchTarget = fn;
  22. return tryCatcher;
  23. }
  24. var inherits = function(Child, Parent) {
  25. var hasProp = {}.hasOwnProperty;
  26. function T() {
  27. this.constructor = Child;
  28. this.constructor$ = Parent;
  29. for (var propertyName in Parent.prototype) {
  30. if (hasProp.call(Parent.prototype, propertyName) &&
  31. propertyName.charAt(propertyName.length-1) !== "$"
  32. ) {
  33. this[propertyName + "$"] = Parent.prototype[propertyName];
  34. }
  35. }
  36. }
  37. T.prototype = Parent.prototype;
  38. Child.prototype = new T();
  39. return Child.prototype;
  40. };
  41. function isPrimitive(val) {
  42. return val == null || val === true || val === false ||
  43. typeof val === "string" || typeof val === "number";
  44. }
  45. function isObject(value) {
  46. return typeof value === "function" ||
  47. typeof value === "object" && value !== null;
  48. }
  49. function maybeWrapAsError(maybeError) {
  50. if (!isPrimitive(maybeError)) return maybeError;
  51. return new Error(safeToString(maybeError));
  52. }
  53. function withAppended(target, appendee) {
  54. var len = target.length;
  55. var ret = new Array(len + 1);
  56. var i;
  57. for (i = 0; i < len; ++i) {
  58. ret[i] = target[i];
  59. }
  60. ret[i] = appendee;
  61. return ret;
  62. }
  63. function getDataPropertyOrDefault(obj, key, defaultValue) {
  64. if (es5.isES5) {
  65. var desc = Object.getOwnPropertyDescriptor(obj, key);
  66. if (desc != null) {
  67. return desc.get == null && desc.set == null
  68. ? desc.value
  69. : defaultValue;
  70. }
  71. } else {
  72. return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
  73. }
  74. }
  75. function notEnumerableProp(obj, name, value) {
  76. if (isPrimitive(obj)) return obj;
  77. var descriptor = {
  78. value: value,
  79. configurable: true,
  80. enumerable: false,
  81. writable: true
  82. };
  83. es5.defineProperty(obj, name, descriptor);
  84. return obj;
  85. }
  86. function thrower(r) {
  87. throw r;
  88. }
  89. var inheritedDataKeys = (function() {
  90. var excludedPrototypes = [
  91. Array.prototype,
  92. Object.prototype,
  93. Function.prototype
  94. ];
  95. var isExcludedProto = function(val) {
  96. for (var i = 0; i < excludedPrototypes.length; ++i) {
  97. if (excludedPrototypes[i] === val) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. };
  103. if (es5.isES5) {
  104. var getKeys = Object.getOwnPropertyNames;
  105. return function(obj) {
  106. var ret = [];
  107. var visitedKeys = Object.create(null);
  108. while (obj != null && !isExcludedProto(obj)) {
  109. var keys;
  110. try {
  111. keys = getKeys(obj);
  112. } catch (e) {
  113. return ret;
  114. }
  115. for (var i = 0; i < keys.length; ++i) {
  116. var key = keys[i];
  117. if (visitedKeys[key]) continue;
  118. visitedKeys[key] = true;
  119. var desc = Object.getOwnPropertyDescriptor(obj, key);
  120. if (desc != null && desc.get == null && desc.set == null) {
  121. ret.push(key);
  122. }
  123. }
  124. obj = es5.getPrototypeOf(obj);
  125. }
  126. return ret;
  127. };
  128. } else {
  129. var hasProp = {}.hasOwnProperty;
  130. return function(obj) {
  131. if (isExcludedProto(obj)) return [];
  132. var ret = [];
  133. /*jshint forin:false */
  134. enumeration: for (var key in obj) {
  135. if (hasProp.call(obj, key)) {
  136. ret.push(key);
  137. } else {
  138. for (var i = 0; i < excludedPrototypes.length; ++i) {
  139. if (hasProp.call(excludedPrototypes[i], key)) {
  140. continue enumeration;
  141. }
  142. }
  143. ret.push(key);
  144. }
  145. }
  146. return ret;
  147. };
  148. }
  149. })();
  150. var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
  151. function isClass(fn) {
  152. try {
  153. if (typeof fn === "function") {
  154. var keys = es5.names(fn.prototype);
  155. var hasMethods = es5.isES5 && keys.length > 1;
  156. var hasMethodsOtherThanConstructor = keys.length > 0 &&
  157. !(keys.length === 1 && keys[0] === "constructor");
  158. var hasThisAssignmentAndStaticMethods =
  159. thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
  160. if (hasMethods || hasMethodsOtherThanConstructor ||
  161. hasThisAssignmentAndStaticMethods) {
  162. return true;
  163. }
  164. }
  165. return false;
  166. } catch (e) {
  167. return false;
  168. }
  169. }
  170. function toFastProperties(obj) {
  171. /*jshint -W027,-W055,-W031*/
  172. function FakeConstructor() {}
  173. FakeConstructor.prototype = obj;
  174. var l = 8;
  175. while (l--) new FakeConstructor();
  176. return obj;
  177. eval(obj);
  178. }
  179. var rident = /^[a-z$_][a-z$_0-9]*$/i;
  180. function isIdentifier(str) {
  181. return rident.test(str);
  182. }
  183. function filledRange(count, prefix, suffix) {
  184. var ret = new Array(count);
  185. for(var i = 0; i < count; ++i) {
  186. ret[i] = prefix + i + suffix;
  187. }
  188. return ret;
  189. }
  190. function safeToString(obj) {
  191. try {
  192. return obj + "";
  193. } catch (e) {
  194. return "[no string representation]";
  195. }
  196. }
  197. function isError(obj) {
  198. return obj instanceof Error ||
  199. (obj !== null &&
  200. typeof obj === "object" &&
  201. typeof obj.message === "string" &&
  202. typeof obj.name === "string");
  203. }
  204. function markAsOriginatingFromRejection(e) {
  205. try {
  206. notEnumerableProp(e, "isOperational", true);
  207. }
  208. catch(ignore) {}
  209. }
  210. function originatesFromRejection(e) {
  211. if (e == null) return false;
  212. return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
  213. e["isOperational"] === true);
  214. }
  215. function canAttachTrace(obj) {
  216. return isError(obj) && es5.propertyIsWritable(obj, "stack");
  217. }
  218. var ensureErrorObject = (function() {
  219. if (!("stack" in new Error())) {
  220. return function(value) {
  221. if (canAttachTrace(value)) return value;
  222. try {throw new Error(safeToString(value));}
  223. catch(err) {return err;}
  224. };
  225. } else {
  226. return function(value) {
  227. if (canAttachTrace(value)) return value;
  228. return new Error(safeToString(value));
  229. };
  230. }
  231. })();
  232. function classString(obj) {
  233. return {}.toString.call(obj);
  234. }
  235. function copyDescriptors(from, to, filter) {
  236. var keys = es5.names(from);
  237. for (var i = 0; i < keys.length; ++i) {
  238. var key = keys[i];
  239. if (filter(key)) {
  240. try {
  241. es5.defineProperty(to, key, es5.getDescriptor(from, key));
  242. } catch (ignore) {}
  243. }
  244. }
  245. }
  246. var asArray = function(v) {
  247. if (es5.isArray(v)) {
  248. return v;
  249. }
  250. return null;
  251. };
  252. if (typeof Symbol !== "undefined" && Symbol.iterator) {
  253. var ArrayFrom = typeof Array.from === "function" ? function(v) {
  254. return Array.from(v);
  255. } : function(v) {
  256. var ret = [];
  257. var it = v[Symbol.iterator]();
  258. var itResult;
  259. while (!((itResult = it.next()).done)) {
  260. ret.push(itResult.value);
  261. }
  262. return ret;
  263. };
  264. asArray = function(v) {
  265. if (es5.isArray(v)) {
  266. return v;
  267. } else if (v != null && typeof v[Symbol.iterator] === "function") {
  268. return ArrayFrom(v);
  269. }
  270. return null;
  271. };
  272. }
  273. var isNode = typeof process !== "undefined" &&
  274. classString(process).toLowerCase() === "[object process]";
  275. var hasEnvVariables = typeof process !== "undefined" &&
  276. typeof process.env !== "undefined";
  277. function env(key) {
  278. return hasEnvVariables ? process.env[key] : undefined;
  279. }
  280. function getNativePromise() {
  281. if (typeof Promise === "function") {
  282. try {
  283. var promise = new Promise(function(){});
  284. if ({}.toString.call(promise) === "[object Promise]") {
  285. return Promise;
  286. }
  287. } catch (e) {}
  288. }
  289. }
  290. function domainBind(self, cb) {
  291. return self.bind(cb);
  292. }
  293. var ret = {
  294. isClass: isClass,
  295. isIdentifier: isIdentifier,
  296. inheritedDataKeys: inheritedDataKeys,
  297. getDataPropertyOrDefault: getDataPropertyOrDefault,
  298. thrower: thrower,
  299. isArray: es5.isArray,
  300. asArray: asArray,
  301. notEnumerableProp: notEnumerableProp,
  302. isPrimitive: isPrimitive,
  303. isObject: isObject,
  304. isError: isError,
  305. canEvaluate: canEvaluate,
  306. errorObj: errorObj,
  307. tryCatch: tryCatch,
  308. inherits: inherits,
  309. withAppended: withAppended,
  310. maybeWrapAsError: maybeWrapAsError,
  311. toFastProperties: toFastProperties,
  312. filledRange: filledRange,
  313. toString: safeToString,
  314. canAttachTrace: canAttachTrace,
  315. ensureErrorObject: ensureErrorObject,
  316. originatesFromRejection: originatesFromRejection,
  317. markAsOriginatingFromRejection: markAsOriginatingFromRejection,
  318. classString: classString,
  319. copyDescriptors: copyDescriptors,
  320. hasDevTools: typeof chrome !== "undefined" && chrome &&
  321. typeof chrome.loadTimes === "function",
  322. isNode: isNode,
  323. hasEnvVariables: hasEnvVariables,
  324. env: env,
  325. global: globalObject,
  326. getNativePromise: getNativePromise,
  327. domainBind: domainBind
  328. };
  329. ret.isRecentNode = ret.isNode && (function() {
  330. var version = process.versions.node.split(".").map(Number);
  331. return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
  332. })();
  333. if (ret.isNode) ret.toFastProperties(process);
  334. try {throw new Error(); } catch (e) {ret.lastLineError = e;}
  335. module.exports = ret;