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
7 // vim: ts=4 sts=4 sw=4 expandtab
9 // Add semicolon to prevent IIFE from being passed as argument to concatenated code.
12 // UMD (Universal Module Definition)
13 // see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
14 (function (root, factory) {
17 /* global define, exports, module */
18 if (typeof define === 'function' && define.amd) {
19 // AMD. Register as an anonymous module.
21 } else if (typeof exports === 'object') {
22 // Node. Does not work with strict CommonJS, but
23 // only CommonJS-like enviroments that support module.exports,
25 module.exports = factory();
27 // Browser globals (root is window)
28 root.returnExports = factory();
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);
38 // If JS engine supports accessors creating shortcuts.
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 */
53 var isPrimitive = function isPrimitive(o) {
54 return o == null || (typeof o !== 'object' && typeof o !== 'function');
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
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) {
73 } else if (toStr(object.constructor) === '[object Function]') {
74 return object.constructor.prototype;
75 } else if (object instanceof Object) {
76 return prototypeOfObject;
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.
88 // http://es5.github.com/#x15.2.3.3
90 var doesGetOwnPropertyDescriptorWork = function doesGetOwnPropertyDescriptorWork(object) {
93 return Object.getOwnPropertyDescriptor(object, 'sentinel').value === 0;
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;
109 if (!Object.getOwnPropertyDescriptor || getOwnPropertyDescriptorFallback) {
110 var ERR_NON_OBJECT = 'Object.getOwnPropertyDescriptor called on a non-object: ';
112 /* eslint-disable no-proto */
113 Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
114 if (isPrimitive(object)) {
115 throw new TypeError(ERR_NON_OBJECT + object);
118 // make a valiant attempt to use the real getOwnPropertyDescriptor
119 // for I8's DOM elements.
120 if (getOwnPropertyDescriptorFallback) {
122 return getOwnPropertyDescriptorFallback.call(Object, object, property);
123 } catch (exception) {
124 // try the shim if the real one doesn't work
130 // If object does not owns property return undefined immediately.
131 if (!owns(object, property)) {
135 // If object has a property then it's for sure `configurable`, and
136 // probably `enumerable`. Detect enumerability though.
138 enumerable: isEnumerable(object, property),
142 // If JS engine supports accessor properties then property may be a
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;
159 var getter = lookupGetter(object, property);
160 var setter = lookupSetter(object, property);
162 if (notPrototypeOfObject) {
163 // Once we have getter and setter we can put values back.
164 object.__proto__ = prototype;
167 if (getter || setter) {
169 descriptor.get = getter;
172 descriptor.set = setter;
174 // If it was accessor property we're done and return here
175 // in order to avoid adding `value` to the descriptor.
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;
186 /* eslint-enable no-proto */
190 // http://es5.github.com/#x15.2.3.4
191 if (!Object.getOwnPropertyNames) {
192 Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
193 return Object.keys(object);
198 // http://es5.github.com/#x15.2.3.5
199 if (!Object.create) {
201 // Contributed by Brandon Benvie, October, 2012
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
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) {
220 return !!new ActiveXObject('htmlfile');
221 } catch (exception) {
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() {
233 xDoc = new ActiveXObject('htmlfile');
235 var script = 'script';
236 xDoc.write('<' + script + '></' + script + '>');
239 empty = xDoc.parentWindow.Object.prototype;
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;
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 */
259 empty = iframe.contentWindow.Object.prototype;
260 parent.removeChild(iframe);
266 /* global document */
267 if (supportsProto || typeof document === 'undefined') {
268 createEmpty = function () {
269 return { __proto__: null };
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();
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;
290 var Empty = function Empty() {};
291 Empty.prototype = empty;
292 // short-circuit future calls
293 createEmpty = function () {
300 Object.create = function create(prototype, properties) {
303 var Type = function Type() {}; // An empty constructor.
305 if (prototype === null) {
306 object = createEmpty();
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
316 Type.prototype = prototype;
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 */
327 if (properties !== void 0) {
328 Object.defineProperties(object, properties);
336 // http://es5.github.com/#x15.2.3.6
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
342 // http://msdn.microsoft.com/en-us/library/dd282900.aspx
343 // http://msdn.microsoft.com/en-us/library/dd229916.aspx
345 // https://bugs.webkit.org/show_bug.cgi?id=36423
347 var doesDefinePropertyWork = function doesDefinePropertyWork(object) {
349 Object.defineProperty(object, 'sentinel', {});
350 return 'sentinel' in object;
351 } catch (exception) {
356 // check whether defineProperty works if it's given. Otherwise,
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;
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';
373 Object.defineProperty = function defineProperty(object, property, descriptor) {
374 if (isPrimitive(object)) {
375 throw new TypeError(ERR_NON_OBJECT_TARGET + object);
377 if (isPrimitive(descriptor)) {
378 throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
380 // make a valiant attempt to use the real defineProperty
381 // for I8's DOM elements.
382 if (definePropertyFallback) {
384 return definePropertyFallback.call(Object, object, property, descriptor);
385 } catch (exception) {
386 // try the shim if the real one doesn't work
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
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)
401 throw new RangeError(
402 'This implementation of Object.defineProperty does not support configurable, enumerable, or writable.'
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
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 */
422 object[property] = descriptor.value;
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);
430 // If we got that far then getters and setters can be defined !!
432 defineGetter(object, property, descriptor.get);
435 defineSetter(object, property, descriptor.set);
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) {
449 return definePropertiesFallback.call(Object, object, properties);
450 } catch (exception) {
451 // try the shim if the real one doesn't work
455 Object.keys(properties).forEach(function (property) {
456 if (property !== '__proto__') {
457 Object.defineProperty(object, property, properties[property]);
465 // http://es5.github.com/#x15.2.3.8
467 Object.seal = function seal(object) {
468 if (Object(object) !== object) {
469 throw new TypeError('Object.seal can only be called on Objects.');
471 // this is misleading and breaks feature-detection, but
472 // allows "securable" code to "gracefully" degrade to working
473 // but insecure code.
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.');
485 // this is misleading and breaks feature-detection, but
486 // allows "securable" code to "gracefully" degrade to working
487 // but insecure code.
492 // detect a Rhino bug and patch it
494 Object.freeze(function () {});
495 } catch (exception) {
496 Object.freeze = (function (freezeObject) {
497 return function freeze(object) {
498 if (typeof object === 'function') {
501 return freezeObject(object);
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.');
514 // this is misleading and breaks feature-detection, but
515 // allows "securable" code to "gracefully" degrade to working
516 // but insecure code.
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.');
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.');
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.');
551 // 2. Return the Boolean value of the [[Extensible]] internal property of O.
553 while (owns(object, name)) {
557 var returnValue = owns(object, name);