es5-sham.js 16 KB

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