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