Version 1
[yaffs-website] / node_modules / es5-shim / es5-sham.js
1 /*!
2  * https://github.com/es-shims/es5-shim
3  * @license es5-shim Copyright 2009-2015 by contributors, MIT License
4  * see https://github.com/es-shims/es5-shim/blob/master/LICENSE
5  */
6
7 // vim: ts=4 sts=4 sw=4 expandtab
8
9 // Add semicolon to prevent IIFE from being passed as argument to concatenated code.
10 ;
11
12 // UMD (Universal Module Definition)
13 // see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
14 (function (root, factory) {
15     'use strict';
16
17     /* global define, exports, module */
18     if (typeof define === 'function' && define.amd) {
19         // AMD. Register as an anonymous module.
20         define(factory);
21     } else if (typeof exports === 'object') {
22         // Node. Does not work with strict CommonJS, but
23         // only CommonJS-like enviroments that support module.exports,
24         // like Node.
25         module.exports = factory();
26     } else {
27         // Browser globals (root is window)
28         root.returnExports = factory();
29     }
30 }(this, function () {
31
32     var call = Function.call;
33     var prototypeOfObject = Object.prototype;
34     var owns = call.bind(prototypeOfObject.hasOwnProperty);
35     var isEnumerable = call.bind(prototypeOfObject.propertyIsEnumerable);
36     var toStr = call.bind(prototypeOfObject.toString);
37
38     // If JS engine supports accessors creating shortcuts.
39     var defineGetter;
40     var defineSetter;
41     var lookupGetter;
42     var lookupSetter;
43     var supportsAccessors = owns(prototypeOfObject, '__defineGetter__');
44     if (supportsAccessors) {
45         /* eslint-disable no-underscore-dangle */
46         defineGetter = call.bind(prototypeOfObject.__defineGetter__);
47         defineSetter = call.bind(prototypeOfObject.__defineSetter__);
48         lookupGetter = call.bind(prototypeOfObject.__lookupGetter__);
49         lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
50         /* eslint-enable no-underscore-dangle */
51     }
52
53     var isPrimitive = function isPrimitive(o) {
54         return o == null || (typeof o !== 'object' && typeof o !== 'function');
55     };
56
57     // ES5 15.2.3.2
58     // http://es5.github.com/#x15.2.3.2
59     if (!Object.getPrototypeOf) {
60         // https://github.com/es-shims/es5-shim/issues#issue/2
61         // http://ejohn.org/blog/objectgetprototypeof/
62         // recommended by fschaefer on github
63         //
64         // sure, and webreflection says ^_^
65         // ... this will nerever possibly return null
66         // ... Opera Mini breaks here with infinite loops
67         Object.getPrototypeOf = function getPrototypeOf(object) {
68             /* eslint-disable no-proto */
69             var proto = object.__proto__;
70             /* eslint-enable no-proto */
71             if (proto || proto === null) {
72                 return proto;
73             } else if (toStr(object.constructor) === '[object Function]') {
74                 return object.constructor.prototype;
75             } else if (object instanceof Object) {
76                 return prototypeOfObject;
77             } else {
78                 // Correctly return null for Objects created with `Object.create(null)`
79                 // (shammed or native) or `{ __proto__: null}`.  Also returns null for
80                 // cross-realm objects on browsers that lack `__proto__` support (like
81                 // IE <11), but that's the best we can do.
82                 return null;
83             }
84         };
85     }
86
87     // ES5 15.2.3.3
88     // http://es5.github.com/#x15.2.3.3
89
90     var doesGetOwnPropertyDescriptorWork = function doesGetOwnPropertyDescriptorWork(object) {
91         try {
92             object.sentinel = 0;
93             return Object.getOwnPropertyDescriptor(object, 'sentinel').value === 0;
94         } catch (exception) {
95             return false;
96         }
97     };
98
99     // check whether getOwnPropertyDescriptor works if it's given. Otherwise, shim partially.
100     if (Object.defineProperty) {
101         var getOwnPropertyDescriptorWorksOnObject = doesGetOwnPropertyDescriptorWork({});
102         var getOwnPropertyDescriptorWorksOnDom = typeof document === 'undefined' ||
103         doesGetOwnPropertyDescriptorWork(document.createElement('div'));
104         if (!getOwnPropertyDescriptorWorksOnDom || !getOwnPropertyDescriptorWorksOnObject) {
105             var getOwnPropertyDescriptorFallback = Object.getOwnPropertyDescriptor;
106         }
107     }
108
109     if (!Object.getOwnPropertyDescriptor || getOwnPropertyDescriptorFallback) {
110         var ERR_NON_OBJECT = 'Object.getOwnPropertyDescriptor called on a non-object: ';
111
112         /* eslint-disable no-proto */
113         Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
114             if (isPrimitive(object)) {
115                 throw new TypeError(ERR_NON_OBJECT + object);
116             }
117
118             // make a valiant attempt to use the real getOwnPropertyDescriptor
119             // for I8's DOM elements.
120             if (getOwnPropertyDescriptorFallback) {
121                 try {
122                     return getOwnPropertyDescriptorFallback.call(Object, object, property);
123                 } catch (exception) {
124                     // try the shim if the real one doesn't work
125                 }
126             }
127
128             var descriptor;
129
130             // If object does not owns property return undefined immediately.
131             if (!owns(object, property)) {
132                 return descriptor;
133             }
134
135             // If object has a property then it's for sure `configurable`, and
136             // probably `enumerable`. Detect enumerability though.
137             descriptor = {
138                 enumerable: isEnumerable(object, property),
139                 configurable: true
140             };
141
142             // If JS engine supports accessor properties then property may be a
143             // getter or setter.
144             if (supportsAccessors) {
145                 // Unfortunately `__lookupGetter__` will return a getter even
146                 // if object has own non getter property along with a same named
147                 // inherited getter. To avoid misbehavior we temporary remove
148                 // `__proto__` so that `__lookupGetter__` will return getter only
149                 // if it's owned by an object.
150                 var prototype = object.__proto__;
151                 var notPrototypeOfObject = object !== prototypeOfObject;
152                 // avoid recursion problem, breaking in Opera Mini when
153                 // Object.getOwnPropertyDescriptor(Object.prototype, 'toString')
154                 // or any other Object.prototype accessor
155                 if (notPrototypeOfObject) {
156                     object.__proto__ = prototypeOfObject;
157                 }
158
159                 var getter = lookupGetter(object, property);
160                 var setter = lookupSetter(object, property);
161
162                 if (notPrototypeOfObject) {
163                     // Once we have getter and setter we can put values back.
164                     object.__proto__ = prototype;
165                 }
166
167                 if (getter || setter) {
168                     if (getter) {
169                         descriptor.get = getter;
170                     }
171                     if (setter) {
172                         descriptor.set = setter;
173                     }
174                     // If it was accessor property we're done and return here
175                     // in order to avoid adding `value` to the descriptor.
176                     return descriptor;
177                 }
178             }
179
180             // If we got this far we know that object has an own property that is
181             // not an accessor so we set it as a value and return descriptor.
182             descriptor.value = object[property];
183             descriptor.writable = true;
184             return descriptor;
185         };
186         /* eslint-enable no-proto */
187     }
188
189     // ES5 15.2.3.4
190     // http://es5.github.com/#x15.2.3.4
191     if (!Object.getOwnPropertyNames) {
192         Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
193             return Object.keys(object);
194         };
195     }
196
197     // ES5 15.2.3.5
198     // http://es5.github.com/#x15.2.3.5
199     if (!Object.create) {
200
201         // Contributed by Brandon Benvie, October, 2012
202         var createEmpty;
203         var supportsProto = !({ __proto__: null } instanceof Object);
204                             // the following produces false positives
205                             // in Opera Mini => not a reliable check
206                             // Object.prototype.__proto__ === null
207
208         // Check for document.domain and active x support
209         // No need to use active x approach when document.domain is not set
210         // see https://github.com/es-shims/es5-shim/issues/150
211         // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
212         /* global ActiveXObject */
213         var shouldUseActiveX = function shouldUseActiveX() {
214             // return early if document.domain not set
215             if (!document.domain) {
216                 return false;
217             }
218
219             try {
220                 return !!new ActiveXObject('htmlfile');
221             } catch (exception) {
222                 return false;
223             }
224         };
225
226         // This supports IE8 when document.domain is used
227         // see https://github.com/es-shims/es5-shim/issues/150
228         // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
229         var getEmptyViaActiveX = function getEmptyViaActiveX() {
230             var empty;
231             var xDoc;
232
233             xDoc = new ActiveXObject('htmlfile');
234
235             var script = 'script';
236             xDoc.write('<' + script + '></' + script + '>');
237             xDoc.close();
238
239             empty = xDoc.parentWindow.Object.prototype;
240             xDoc = null;
241
242             return empty;
243         };
244
245         // The original implementation using an iframe
246         // before the activex approach was added
247         // see https://github.com/es-shims/es5-shim/issues/150
248         var getEmptyViaIFrame = function getEmptyViaIFrame() {
249             var iframe = document.createElement('iframe');
250             var parent = document.body || document.documentElement;
251             var empty;
252
253             iframe.style.display = 'none';
254             parent.appendChild(iframe);
255             /* eslint-disable no-script-url */
256             iframe.src = 'javascript:';
257             /* eslint-enable no-script-url */
258
259             empty = iframe.contentWindow.Object.prototype;
260             parent.removeChild(iframe);
261             iframe = null;
262
263             return empty;
264         };
265
266         /* global document */
267         if (supportsProto || typeof document === 'undefined') {
268             createEmpty = function () {
269                 return { __proto__: null };
270             };
271         } else {
272             // In old IE __proto__ can't be used to manually set `null`, nor does
273             // any other method exist to make an object that inherits from nothing,
274             // aside from Object.prototype itself. Instead, create a new global
275             // object and *steal* its Object.prototype and strip it bare. This is
276             // used as the prototype to create nullary objects.
277             createEmpty = function () {
278                 // Determine which approach to use
279                 // see https://github.com/es-shims/es5-shim/issues/150
280                 var empty = shouldUseActiveX() ? getEmptyViaActiveX() : getEmptyViaIFrame();
281
282                 delete empty.constructor;
283                 delete empty.hasOwnProperty;
284                 delete empty.propertyIsEnumerable;
285                 delete empty.isPrototypeOf;
286                 delete empty.toLocaleString;
287                 delete empty.toString;
288                 delete empty.valueOf;
289
290                 var Empty = function Empty() {};
291                 Empty.prototype = empty;
292                 // short-circuit future calls
293                 createEmpty = function () {
294                     return new Empty();
295                 };
296                 return new Empty();
297             };
298         }
299
300         Object.create = function create(prototype, properties) {
301
302             var object;
303             var Type = function Type() {}; // An empty constructor.
304
305             if (prototype === null) {
306                 object = createEmpty();
307             } else {
308                 if (prototype !== null && isPrimitive(prototype)) {
309                     // In the native implementation `parent` can be `null`
310                     // OR *any* `instanceof Object`  (Object|Function|Array|RegExp|etc)
311                     // Use `typeof` tho, b/c in old IE, DOM elements are not `instanceof Object`
312                     // like they are in modern browsers. Using `Object.create` on DOM elements
313                     // is...err...probably inappropriate, but the native version allows for it.
314                     throw new TypeError('Object prototype may only be an Object or null'); // same msg as Chrome
315                 }
316                 Type.prototype = prototype;
317                 object = new Type();
318                 // IE has no built-in implementation of `Object.getPrototypeOf`
319                 // neither `__proto__`, but this manually setting `__proto__` will
320                 // guarantee that `Object.getPrototypeOf` will work as expected with
321                 // objects created using `Object.create`
322                 /* eslint-disable no-proto */
323                 object.__proto__ = prototype;
324                 /* eslint-enable no-proto */
325             }
326
327             if (properties !== void 0) {
328                 Object.defineProperties(object, properties);
329             }
330
331             return object;
332         };
333     }
334
335     // ES5 15.2.3.6
336     // http://es5.github.com/#x15.2.3.6
337
338     // Patch for WebKit and IE8 standard mode
339     // Designed by hax <hax.github.com>
340     // related issue: https://github.com/es-shims/es5-shim/issues#issue/5
341     // IE8 Reference:
342     //     http://msdn.microsoft.com/en-us/library/dd282900.aspx
343     //     http://msdn.microsoft.com/en-us/library/dd229916.aspx
344     // WebKit Bugs:
345     //     https://bugs.webkit.org/show_bug.cgi?id=36423
346
347     var doesDefinePropertyWork = function doesDefinePropertyWork(object) {
348         try {
349             Object.defineProperty(object, 'sentinel', {});
350             return 'sentinel' in object;
351         } catch (exception) {
352             return false;
353         }
354     };
355
356     // check whether defineProperty works if it's given. Otherwise,
357     // shim partially.
358     if (Object.defineProperty) {
359         var definePropertyWorksOnObject = doesDefinePropertyWork({});
360         var definePropertyWorksOnDom = typeof document === 'undefined' ||
361             doesDefinePropertyWork(document.createElement('div'));
362         if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
363             var definePropertyFallback = Object.defineProperty,
364                 definePropertiesFallback = Object.defineProperties;
365         }
366     }
367
368     if (!Object.defineProperty || definePropertyFallback) {
369         var ERR_NON_OBJECT_DESCRIPTOR = 'Property description must be an object: ';
370         var ERR_NON_OBJECT_TARGET = 'Object.defineProperty called on non-object: ';
371         var ERR_ACCESSORS_NOT_SUPPORTED = 'getters & setters can not be defined on this javascript engine';
372
373         Object.defineProperty = function defineProperty(object, property, descriptor) {
374             if (isPrimitive(object)) {
375                 throw new TypeError(ERR_NON_OBJECT_TARGET + object);
376             }
377             if (isPrimitive(descriptor)) {
378                 throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
379             }
380             // make a valiant attempt to use the real defineProperty
381             // for I8's DOM elements.
382             if (definePropertyFallback) {
383                 try {
384                     return definePropertyFallback.call(Object, object, property, descriptor);
385                 } catch (exception) {
386                     // try the shim if the real one doesn't work
387                 }
388             }
389
390             // If it's a data property.
391             if ('value' in descriptor) {
392                 // fail silently if 'writable', 'enumerable', or 'configurable'
393                 // are requested but not supported
394                 /*
395                 // alternate approach:
396                 if ( // can't implement these features; allow false but not true
397                     ('writable' in descriptor && !descriptor.writable) ||
398                     ('enumerable' in descriptor && !descriptor.enumerable) ||
399                     ('configurable' in descriptor && !descriptor.configurable)
400                 ))
401                     throw new RangeError(
402                         'This implementation of Object.defineProperty does not support configurable, enumerable, or writable.'
403                     );
404                 */
405
406                 if (supportsAccessors && (lookupGetter(object, property) || lookupSetter(object, property))) {
407                     // As accessors are supported only on engines implementing
408                     // `__proto__` we can safely override `__proto__` while defining
409                     // a property to make sure that we don't hit an inherited
410                     // accessor.
411                     /* eslint-disable no-proto */
412                     var prototype = object.__proto__;
413                     object.__proto__ = prototypeOfObject;
414                     // Deleting a property anyway since getter / setter may be
415                     // defined on object itself.
416                     delete object[property];
417                     object[property] = descriptor.value;
418                     // Setting original `__proto__` back now.
419                     object.__proto__ = prototype;
420                     /* eslint-enable no-proto */
421                 } else {
422                     object[property] = descriptor.value;
423                 }
424             } else {
425                 var hasGetter = 'get' in descriptor;
426                 var hasSetter = 'set' in descriptor;
427                 if (!supportsAccessors && (hasGetter || hasSetter)) {
428                     throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
429                 }
430                 // If we got that far then getters and setters can be defined !!
431                 if (hasGetter) {
432                     defineGetter(object, property, descriptor.get);
433                 }
434                 if (hasSetter) {
435                     defineSetter(object, property, descriptor.set);
436                 }
437             }
438             return object;
439         };
440     }
441
442     // ES5 15.2.3.7
443     // http://es5.github.com/#x15.2.3.7
444     if (!Object.defineProperties || definePropertiesFallback) {
445         Object.defineProperties = function defineProperties(object, properties) {
446             // make a valiant attempt to use the real defineProperties
447             if (definePropertiesFallback) {
448                 try {
449                     return definePropertiesFallback.call(Object, object, properties);
450                 } catch (exception) {
451                     // try the shim if the real one doesn't work
452                 }
453             }
454
455             Object.keys(properties).forEach(function (property) {
456                 if (property !== '__proto__') {
457                     Object.defineProperty(object, property, properties[property]);
458                 }
459             });
460             return object;
461         };
462     }
463
464     // ES5 15.2.3.8
465     // http://es5.github.com/#x15.2.3.8
466     if (!Object.seal) {
467         Object.seal = function seal(object) {
468             if (Object(object) !== object) {
469                 throw new TypeError('Object.seal can only be called on Objects.');
470             }
471             // this is misleading and breaks feature-detection, but
472             // allows "securable" code to "gracefully" degrade to working
473             // but insecure code.
474             return object;
475         };
476     }
477
478     // ES5 15.2.3.9
479     // http://es5.github.com/#x15.2.3.9
480     if (!Object.freeze) {
481         Object.freeze = function freeze(object) {
482             if (Object(object) !== object) {
483                 throw new TypeError('Object.freeze can only be called on Objects.');
484             }
485             // this is misleading and breaks feature-detection, but
486             // allows "securable" code to "gracefully" degrade to working
487             // but insecure code.
488             return object;
489         };
490     }
491
492     // detect a Rhino bug and patch it
493     try {
494         Object.freeze(function () {});
495     } catch (exception) {
496         Object.freeze = (function (freezeObject) {
497             return function freeze(object) {
498                 if (typeof object === 'function') {
499                     return object;
500                 } else {
501                     return freezeObject(object);
502                 }
503             };
504         }(Object.freeze));
505     }
506
507     // ES5 15.2.3.10
508     // http://es5.github.com/#x15.2.3.10
509     if (!Object.preventExtensions) {
510         Object.preventExtensions = function preventExtensions(object) {
511             if (Object(object) !== object) {
512                 throw new TypeError('Object.preventExtensions can only be called on Objects.');
513             }
514             // this is misleading and breaks feature-detection, but
515             // allows "securable" code to "gracefully" degrade to working
516             // but insecure code.
517             return object;
518         };
519     }
520
521     // ES5 15.2.3.11
522     // http://es5.github.com/#x15.2.3.11
523     if (!Object.isSealed) {
524         Object.isSealed = function isSealed(object) {
525             if (Object(object) !== object) {
526                 throw new TypeError('Object.isSealed can only be called on Objects.');
527             }
528             return false;
529         };
530     }
531
532     // ES5 15.2.3.12
533     // http://es5.github.com/#x15.2.3.12
534     if (!Object.isFrozen) {
535         Object.isFrozen = function isFrozen(object) {
536             if (Object(object) !== object) {
537                 throw new TypeError('Object.isFrozen can only be called on Objects.');
538             }
539             return false;
540         };
541     }
542
543     // ES5 15.2.3.13
544     // http://es5.github.com/#x15.2.3.13
545     if (!Object.isExtensible) {
546         Object.isExtensible = function isExtensible(object) {
547             // 1. If Type(O) is not Object throw a TypeError exception.
548             if (Object(object) !== object) {
549                 throw new TypeError('Object.isExtensible can only be called on Objects.');
550             }
551             // 2. Return the Boolean value of the [[Extensible]] internal property of O.
552             var name = '';
553             while (owns(object, name)) {
554                 name += '?';
555             }
556             object[name] = true;
557             var returnValue = owns(object, name);
558             delete object[name];
559             return returnValue;
560         };
561     }
562
563 }));