1
0

es5-sham.js 17 KB

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