es5-sham.js 17 KB

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