2 var es5 = require("./es5");
3 var canEvaluate = typeof navigator == "undefined";
5 var errorObj = {e: {}};
7 function tryCatcher() {
9 var target = tryCatchTarget;
10 tryCatchTarget = null;
11 return target.apply(this, arguments);
17 function tryCatch(fn) {
22 var inherits = function(Child, Parent) {
23 var hasProp = {}.hasOwnProperty;
26 this.constructor = Child;
27 this.constructor$ = Parent;
28 for (var propertyName in Parent.prototype) {
29 if (hasProp.call(Parent.prototype, propertyName) &&
30 propertyName.charAt(propertyName.length-1) !== "$"
32 this[propertyName + "$"] = Parent.prototype[propertyName];
36 T.prototype = Parent.prototype;
37 Child.prototype = new T();
38 return Child.prototype;
42 function isPrimitive(val) {
43 return val == null || val === true || val === false ||
44 typeof val === "string" || typeof val === "number";
48 function isObject(value) {
49 return typeof value === "function" ||
50 typeof value === "object" && value !== null;
53 function maybeWrapAsError(maybeError) {
54 if (!isPrimitive(maybeError)) return maybeError;
56 return new Error(safeToString(maybeError));
59 function withAppended(target, appendee) {
60 var len = target.length;
61 var ret = new Array(len + 1);
63 for (i = 0; i < len; ++i) {
70 function getDataPropertyOrDefault(obj, key, defaultValue) {
72 var desc = Object.getOwnPropertyDescriptor(obj, key);
75 return desc.get == null && desc.set == null
80 return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
84 function notEnumerableProp(obj, name, value) {
85 if (isPrimitive(obj)) return obj;
92 es5.defineProperty(obj, name, descriptor);
100 var inheritedDataKeys = (function() {
101 var excludedPrototypes = [
107 var isExcludedProto = function(val) {
108 for (var i = 0; i < excludedPrototypes.length; ++i) {
109 if (excludedPrototypes[i] === val) {
117 var getKeys = Object.getOwnPropertyNames;
118 return function(obj) {
120 var visitedKeys = Object.create(null);
121 while (obj != null && !isExcludedProto(obj)) {
128 for (var i = 0; i < keys.length; ++i) {
130 if (visitedKeys[key]) continue;
131 visitedKeys[key] = true;
132 var desc = Object.getOwnPropertyDescriptor(obj, key);
133 if (desc != null && desc.get == null && desc.set == null) {
137 obj = es5.getPrototypeOf(obj);
142 var hasProp = {}.hasOwnProperty;
143 return function(obj) {
144 if (isExcludedProto(obj)) return [];
147 /*jshint forin:false */
148 enumeration: for (var key in obj) {
149 if (hasProp.call(obj, key)) {
152 for (var i = 0; i < excludedPrototypes.length; ++i) {
153 if (hasProp.call(excludedPrototypes[i], key)) {
154 continue enumeration;
166 var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
167 function isClass(fn) {
169 if (typeof fn === "function") {
170 var keys = es5.names(fn.prototype);
172 var hasMethods = es5.isES5 && keys.length > 1;
173 var hasMethodsOtherThanConstructor = keys.length > 0 &&
174 !(keys.length === 1 && keys[0] === "constructor");
175 var hasThisAssignmentAndStaticMethods =
176 thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
178 if (hasMethods || hasMethodsOtherThanConstructor ||
179 hasThisAssignmentAndStaticMethods) {
189 function toFastProperties(obj) {
190 /*jshint -W027,-W055,-W031*/
191 function FakeConstructor() {}
192 FakeConstructor.prototype = obj;
194 while (l--) new FakeConstructor();
199 var rident = /^[a-z$_][a-z$_0-9]*$/i;
200 function isIdentifier(str) {
201 return rident.test(str);
204 function filledRange(count, prefix, suffix) {
205 var ret = new Array(count);
206 for(var i = 0; i < count; ++i) {
207 ret[i] = prefix + i + suffix;
212 function safeToString(obj) {
216 return "[no string representation]";
220 function markAsOriginatingFromRejection(e) {
222 notEnumerableProp(e, "isOperational", true);
227 function originatesFromRejection(e) {
228 if (e == null) return false;
229 return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
230 e["isOperational"] === true);
233 function canAttachTrace(obj) {
234 return obj instanceof Error && es5.propertyIsWritable(obj, "stack");
237 var ensureErrorObject = (function() {
238 if (!("stack" in new Error())) {
239 return function(value) {
240 if (canAttachTrace(value)) return value;
241 try {throw new Error(safeToString(value));}
242 catch(err) {return err;}
245 return function(value) {
246 if (canAttachTrace(value)) return value;
247 return new Error(safeToString(value));
252 function classString(obj) {
253 return {}.toString.call(obj);
256 function copyDescriptors(from, to, filter) {
257 var keys = es5.names(from);
258 for (var i = 0; i < keys.length; ++i) {
262 es5.defineProperty(to, key, es5.getDescriptor(from, key));
268 var asArray = function(v) {
269 if (es5.isArray(v)) {
275 if (typeof Symbol !== "undefined" && Symbol.iterator) {
276 var ArrayFrom = typeof Array.from === "function" ? function(v) {
277 return Array.from(v);
280 var it = v[Symbol.iterator]();
282 while (!((itResult = it.next()).done)) {
283 ret.push(itResult.value);
288 asArray = function(v) {
289 if (es5.isArray(v)) {
291 } else if (v != null && typeof v[Symbol.iterator] === "function") {
298 var isNode = typeof process !== "undefined" &&
299 classString(process).toLowerCase() === "[object process]";
301 function env(key, def) {
302 return isNode ? process.env[key] : def;
307 isIdentifier: isIdentifier,
308 inheritedDataKeys: inheritedDataKeys,
309 getDataPropertyOrDefault: getDataPropertyOrDefault,
311 isArray: es5.isArray,
313 notEnumerableProp: notEnumerableProp,
314 isPrimitive: isPrimitive,
316 canEvaluate: canEvaluate,
320 withAppended: withAppended,
321 maybeWrapAsError: maybeWrapAsError,
322 toFastProperties: toFastProperties,
323 filledRange: filledRange,
324 toString: safeToString,
325 canAttachTrace: canAttachTrace,
326 ensureErrorObject: ensureErrorObject,
327 originatesFromRejection: originatesFromRejection,
328 markAsOriginatingFromRejection: markAsOriginatingFromRejection,
329 classString: classString,
330 copyDescriptors: copyDescriptors,
331 hasDevTools: typeof chrome !== "undefined" && chrome &&
332 typeof chrome.loadTimes === "function",
336 ret.isRecentNode = ret.isNode && (function() {
337 var version = process.versions.node.split(".").map(Number);
338 return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
341 if (ret.isNode) ret.toFastProperties(process);
343 try {throw new Error(); } catch (e) {ret.lastLineError = e;}
344 module.exports = ret;