util.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. "use strict";
  2. var es5 = require("./es5.js");
  3. var canEvaluate = typeof navigator == "undefined";
  4. var haveGetters = (function(){
  5. try {
  6. var o = {};
  7. es5.defineProperty(o, "f", {
  8. get: function () {
  9. return 3;
  10. }
  11. });
  12. return o.f === 3;
  13. }
  14. catch (e) {
  15. return false;
  16. }
  17. })();
  18. var errorObj = {e: {}};
  19. var tryCatchTarget;
  20. function tryCatcher() {
  21. try {
  22. return tryCatchTarget.apply(this, arguments);
  23. } catch (e) {
  24. errorObj.e = e;
  25. return errorObj;
  26. }
  27. }
  28. function tryCatch(fn) {
  29. tryCatchTarget = fn;
  30. return tryCatcher;
  31. }
  32. var inherits = function(Child, Parent) {
  33. var hasProp = {}.hasOwnProperty;
  34. function T() {
  35. this.constructor = Child;
  36. this.constructor$ = Parent;
  37. for (var propertyName in Parent.prototype) {
  38. if (hasProp.call(Parent.prototype, propertyName) &&
  39. propertyName.charAt(propertyName.length-1) !== "$"
  40. ) {
  41. this[propertyName + "$"] = Parent.prototype[propertyName];
  42. }
  43. }
  44. }
  45. T.prototype = Parent.prototype;
  46. Child.prototype = new T();
  47. return Child.prototype;
  48. };
  49. function isPrimitive(val) {
  50. return val == null || val === true || val === false ||
  51. typeof val === "string" || typeof val === "number";
  52. }
  53. function isObject(value) {
  54. return !isPrimitive(value);
  55. }
  56. function maybeWrapAsError(maybeError) {
  57. if (!isPrimitive(maybeError)) return maybeError;
  58. return new Error(safeToString(maybeError));
  59. }
  60. function withAppended(target, appendee) {
  61. var len = target.length;
  62. var ret = new Array(len + 1);
  63. var i;
  64. for (i = 0; i < len; ++i) {
  65. ret[i] = target[i];
  66. }
  67. ret[i] = appendee;
  68. return ret;
  69. }
  70. function getDataPropertyOrDefault(obj, key, defaultValue) {
  71. if (es5.isES5) {
  72. var desc = Object.getOwnPropertyDescriptor(obj, key);
  73. if (desc != null) {
  74. return desc.get == null && desc.set == null
  75. ? desc.value
  76. : defaultValue;
  77. }
  78. } else {
  79. return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
  80. }
  81. }
  82. function notEnumerableProp(obj, name, value) {
  83. if (isPrimitive(obj)) return obj;
  84. var descriptor = {
  85. value: value,
  86. configurable: true,
  87. enumerable: false,
  88. writable: true
  89. };
  90. es5.defineProperty(obj, name, descriptor);
  91. return obj;
  92. }
  93. var wrapsPrimitiveReceiver = (function() {
  94. return this !== "string";
  95. }).call("string");
  96. function thrower(r) {
  97. throw r;
  98. }
  99. var inheritedDataKeys = (function() {
  100. if (es5.isES5) {
  101. var oProto = Object.prototype;
  102. var getKeys = Object.getOwnPropertyNames;
  103. return function(obj) {
  104. var ret = [];
  105. var visitedKeys = Object.create(null);
  106. while (obj != null && obj !== oProto) {
  107. var keys;
  108. try {
  109. keys = getKeys(obj);
  110. } catch (e) {
  111. return ret;
  112. }
  113. for (var i = 0; i < keys.length; ++i) {
  114. var key = keys[i];
  115. if (visitedKeys[key]) continue;
  116. visitedKeys[key] = true;
  117. var desc = Object.getOwnPropertyDescriptor(obj, key);
  118. if (desc != null && desc.get == null && desc.set == null) {
  119. ret.push(key);
  120. }
  121. }
  122. obj = es5.getPrototypeOf(obj);
  123. }
  124. return ret;
  125. };
  126. } else {
  127. return function(obj) {
  128. var ret = [];
  129. /*jshint forin:false */
  130. for (var key in obj) {
  131. ret.push(key);
  132. }
  133. return ret;
  134. };
  135. }
  136. })();
  137. function isClass(fn) {
  138. try {
  139. if (typeof fn === "function") {
  140. var keys = es5.names(fn.prototype);
  141. if (es5.isES5) return keys.length > 1;
  142. return keys.length > 0 &&
  143. !(keys.length === 1 && keys[0] === "constructor");
  144. }
  145. return false;
  146. } catch (e) {
  147. return false;
  148. }
  149. }
  150. function toFastProperties(obj) {
  151. /*jshint -W027,-W055,-W031*/
  152. function f() {}
  153. f.prototype = obj;
  154. var l = 8;
  155. while (l--) new f();
  156. return obj;
  157. eval(obj);
  158. }
  159. var rident = /^[a-z$_][a-z$_0-9]*$/i;
  160. function isIdentifier(str) {
  161. return rident.test(str);
  162. }
  163. function filledRange(count, prefix, suffix) {
  164. var ret = new Array(count);
  165. for(var i = 0; i < count; ++i) {
  166. ret[i] = prefix + i + suffix;
  167. }
  168. return ret;
  169. }
  170. function safeToString(obj) {
  171. try {
  172. return obj + "";
  173. } catch (e) {
  174. return "[no string representation]";
  175. }
  176. }
  177. function markAsOriginatingFromRejection(e) {
  178. try {
  179. notEnumerableProp(e, "isOperational", true);
  180. }
  181. catch(ignore) {}
  182. }
  183. function originatesFromRejection(e) {
  184. if (e == null) return false;
  185. return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
  186. e["isOperational"] === true);
  187. }
  188. function canAttachTrace(obj) {
  189. return obj instanceof Error && es5.propertyIsWritable(obj, "stack");
  190. }
  191. var ensureErrorObject = (function() {
  192. if (!("stack" in new Error())) {
  193. return function(value) {
  194. if (canAttachTrace(value)) return value;
  195. try {throw new Error(safeToString(value));}
  196. catch(err) {return err;}
  197. };
  198. } else {
  199. return function(value) {
  200. if (canAttachTrace(value)) return value;
  201. return new Error(safeToString(value));
  202. };
  203. }
  204. })();
  205. function classString(obj) {
  206. return {}.toString.call(obj);
  207. }
  208. function copyDescriptors(from, to, filter) {
  209. var keys = es5.names(from);
  210. for (var i = 0; i < keys.length; ++i) {
  211. var key = keys[i];
  212. if (filter(key)) {
  213. es5.defineProperty(to, key, es5.getDescriptor(from, key));
  214. }
  215. }
  216. }
  217. var ret = {
  218. isClass: isClass,
  219. isIdentifier: isIdentifier,
  220. inheritedDataKeys: inheritedDataKeys,
  221. getDataPropertyOrDefault: getDataPropertyOrDefault,
  222. thrower: thrower,
  223. isArray: es5.isArray,
  224. haveGetters: haveGetters,
  225. notEnumerableProp: notEnumerableProp,
  226. isPrimitive: isPrimitive,
  227. isObject: isObject,
  228. canEvaluate: canEvaluate,
  229. errorObj: errorObj,
  230. tryCatch: tryCatch,
  231. inherits: inherits,
  232. withAppended: withAppended,
  233. maybeWrapAsError: maybeWrapAsError,
  234. wrapsPrimitiveReceiver: wrapsPrimitiveReceiver,
  235. toFastProperties: toFastProperties,
  236. filledRange: filledRange,
  237. toString: safeToString,
  238. canAttachTrace: canAttachTrace,
  239. ensureErrorObject: ensureErrorObject,
  240. originatesFromRejection: originatesFromRejection,
  241. markAsOriginatingFromRejection: markAsOriginatingFromRejection,
  242. classString: classString,
  243. copyDescriptors: copyDescriptors,
  244. hasDevTools: typeof chrome !== "undefined" && chrome &&
  245. typeof chrome.loadTimes === "function",
  246. isNode: typeof process !== "undefined" &&
  247. classString(process).toLowerCase() === "[object process]"
  248. };
  249. try {throw new Error(); } catch (e) {ret.lastLineError = e;}
  250. module.exports = ret;