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