1
0

es5-sham.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // ES5 15.2.3.2
  17. // http://es5.github.com/#x15.2.3.2
  18. if (!Object.getPrototypeOf) {
  19. // https://github.com/kriskowal/es5-shim/issues#issue/2
  20. // http://ejohn.org/blog/objectgetprototypeof/
  21. // recommended by fschaefer on github
  22. Object.getPrototypeOf = function getPrototypeOf(object) {
  23. return object.__proto__ || (
  24. object.constructor
  25. ? object.constructor.prototype
  26. : prototypeOfObject
  27. );
  28. };
  29. }
  30. // ES5 15.2.3.3
  31. // http://es5.github.com/#x15.2.3.3
  32. if (!Object.getOwnPropertyDescriptor) {
  33. var ERR_NON_OBJECT = "Object.getOwnPropertyDescriptor called on a non-object: ";
  34. Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
  35. if ((typeof object != "object" && typeof object != "function") || object === null) {
  36. throw new TypeError(ERR_NON_OBJECT + object);
  37. }
  38. // If object does not owns property return undefined immediately.
  39. if (!owns(object, property)) {
  40. return;
  41. }
  42. // If object has a property then it's for sure both `enumerable` and
  43. // `configurable`.
  44. var descriptor = { enumerable: true, configurable: true };
  45. // If JS engine supports accessor properties then property may be a
  46. // getter or setter.
  47. if (supportsAccessors) {
  48. // Unfortunately `__lookupGetter__` will return a getter even
  49. // if object has own non getter property along with a same named
  50. // inherited getter. To avoid misbehavior we temporary remove
  51. // `__proto__` so that `__lookupGetter__` will return getter only
  52. // if it's owned by an object.
  53. var prototype = object.__proto__;
  54. object.__proto__ = prototypeOfObject;
  55. var getter = lookupGetter(object, property);
  56. var setter = lookupSetter(object, property);
  57. // Once we have getter and setter we can put values back.
  58. object.__proto__ = prototype;
  59. if (getter || setter) {
  60. if (getter) {
  61. descriptor.get = getter;
  62. }
  63. if (setter) {
  64. descriptor.set = setter;
  65. }
  66. // If it was accessor property we're done and return here
  67. // in order to avoid adding `value` to the descriptor.
  68. return descriptor;
  69. }
  70. }
  71. // If we got this far we know that object has an own property that is
  72. // not an accessor so we set it as a value and return descriptor.
  73. descriptor.value = object[property];
  74. return descriptor;
  75. };
  76. }
  77. // ES5 15.2.3.4
  78. // http://es5.github.com/#x15.2.3.4
  79. if (!Object.getOwnPropertyNames) {
  80. Object.getOwnPropertyNames = function getOwnPropertyNames(object) {
  81. return Object.keys(object);
  82. };
  83. }
  84. // ES5 15.2.3.5
  85. // http://es5.github.com/#x15.2.3.5
  86. if (!Object.create) {
  87. Object.create = function create(prototype, properties) {
  88. var object;
  89. function Type() {} // An empty constructor.
  90. if (prototype === null) {
  91. object = { "__proto__": null };
  92. } else {
  93. if (typeof prototype !== "object" && typeof prototype !== "function") {
  94. // In the native implementation `parent` can be `null`
  95. // OR *any* `instanceof Object` (Object|Function|Array|RegExp|etc)
  96. // Use `typeof` tho, b/c in old IE, DOM elements are not `instanceof Object`
  97. // like they are in modern browsers. Using `Object.create` on DOM elements
  98. // is...err...probably inappropriate, but the native version allows for it.
  99. throw new TypeError("Object prototype may only be an Object or null"); // same msg as Chrome
  100. }
  101. Type.prototype = prototype;
  102. object = new Type();
  103. // IE has no built-in implementation of `Object.getPrototypeOf`
  104. // neither `__proto__`, but this manually setting `__proto__` will
  105. // guarantee that `Object.getPrototypeOf` will work as expected with
  106. // objects created using `Object.create`
  107. object.__proto__ = prototype;
  108. }
  109. if (properties !== void 0) {
  110. Object.defineProperties(object, properties);
  111. }
  112. return object;
  113. };
  114. }
  115. // ES5 15.2.3.6
  116. // http://es5.github.com/#x15.2.3.6
  117. // Patch for WebKit and IE8 standard mode
  118. // Designed by hax <hax.github.com>
  119. // related issue: https://github.com/kriskowal/es5-shim/issues#issue/5
  120. // IE8 Reference:
  121. // http://msdn.microsoft.com/en-us/library/dd282900.aspx
  122. // http://msdn.microsoft.com/en-us/library/dd229916.aspx
  123. // WebKit Bugs:
  124. // https://bugs.webkit.org/show_bug.cgi?id=36423
  125. function doesDefinePropertyWork(object) {
  126. try {
  127. Object.defineProperty(object, "sentinel", {});
  128. return "sentinel" in object;
  129. } catch (exception) {
  130. // returns falsy
  131. }
  132. }
  133. // check whether defineProperty works if it's given. Otherwise,
  134. // shim partially.
  135. if (Object.defineProperty) {
  136. var definePropertyWorksOnObject = doesDefinePropertyWork({});
  137. var definePropertyWorksOnDom = typeof document == "undefined" ||
  138. doesDefinePropertyWork(document.createElement("div"));
  139. if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
  140. var definePropertyFallback = Object.defineProperty;
  141. }
  142. }
  143. if (!Object.defineProperty || definePropertyFallback) {
  144. var ERR_NON_OBJECT_DESCRIPTOR = "Property description must be an object: ";
  145. var ERR_NON_OBJECT_TARGET = "Object.defineProperty called on non-object: "
  146. var ERR_ACCESSORS_NOT_SUPPORTED = "getters & setters can not be defined " +
  147. "on this javascript engine";
  148. Object.defineProperty = function defineProperty(object, property, descriptor) {
  149. if ((typeof object != "object" && typeof object != "function") || object === null) {
  150. throw new TypeError(ERR_NON_OBJECT_TARGET + object);
  151. }
  152. if ((typeof descriptor != "object" && typeof descriptor != "function") || descriptor === null) {
  153. throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
  154. }
  155. // make a valiant attempt to use the real defineProperty
  156. // for I8's DOM elements.
  157. if (definePropertyFallback) {
  158. try {
  159. return definePropertyFallback.call(Object, object, property, descriptor);
  160. } catch (exception) {
  161. // try the shim if the real one doesn't work
  162. }
  163. }
  164. // If it's a data property.
  165. if (owns(descriptor, "value")) {
  166. // fail silently if "writable", "enumerable", or "configurable"
  167. // are requested but not supported
  168. /*
  169. // alternate approach:
  170. if ( // can't implement these features; allow false but not true
  171. !(owns(descriptor, "writable") ? descriptor.writable : true) ||
  172. !(owns(descriptor, "enumerable") ? descriptor.enumerable : true) ||
  173. !(owns(descriptor, "configurable") ? descriptor.configurable : true)
  174. )
  175. throw new RangeError(
  176. "This implementation of Object.defineProperty does not " +
  177. "support configurable, enumerable, or writable."
  178. );
  179. */
  180. if (supportsAccessors && (lookupGetter(object, property) ||
  181. lookupSetter(object, property)))
  182. {
  183. // As accessors are supported only on engines implementing
  184. // `__proto__` we can safely override `__proto__` while defining
  185. // a property to make sure that we don't hit an inherited
  186. // accessor.
  187. var prototype = object.__proto__;
  188. object.__proto__ = prototypeOfObject;
  189. // Deleting a property anyway since getter / setter may be
  190. // defined on object itself.
  191. delete object[property];
  192. object[property] = descriptor.value;
  193. // Setting original `__proto__` back now.
  194. object.__proto__ = prototype;
  195. } else {
  196. object[property] = descriptor.value;
  197. }
  198. } else {
  199. if (!supportsAccessors) {
  200. throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
  201. }
  202. // If we got that far then getters and setters can be defined !!
  203. if (owns(descriptor, "get")) {
  204. defineGetter(object, property, descriptor.get);
  205. }
  206. if (owns(descriptor, "set")) {
  207. defineSetter(object, property, descriptor.set);
  208. }
  209. }
  210. return object;
  211. };
  212. }
  213. // ES5 15.2.3.7
  214. // http://es5.github.com/#x15.2.3.7
  215. if (!Object.defineProperties) {
  216. Object.defineProperties = function defineProperties(object, properties) {
  217. for (var property in properties) {
  218. if (owns(properties, property) && property != "__proto__") {
  219. Object.defineProperty(object, property, properties[property]);
  220. }
  221. }
  222. return object;
  223. };
  224. }
  225. // ES5 15.2.3.8
  226. // http://es5.github.com/#x15.2.3.8
  227. if (!Object.seal) {
  228. Object.seal = function seal(object) {
  229. // this is misleading and breaks feature-detection, but
  230. // allows "securable" code to "gracefully" degrade to working
  231. // but insecure code.
  232. return object;
  233. };
  234. }
  235. // ES5 15.2.3.9
  236. // http://es5.github.com/#x15.2.3.9
  237. if (!Object.freeze) {
  238. Object.freeze = function freeze(object) {
  239. // this is misleading and breaks feature-detection, but
  240. // allows "securable" code to "gracefully" degrade to working
  241. // but insecure code.
  242. return object;
  243. };
  244. }
  245. // detect a Rhino bug and patch it
  246. try {
  247. Object.freeze(function () {});
  248. } catch (exception) {
  249. Object.freeze = (function freeze(freezeObject) {
  250. return function freeze(object) {
  251. if (typeof object == "function") {
  252. return object;
  253. } else {
  254. return freezeObject(object);
  255. }
  256. };
  257. })(Object.freeze);
  258. }
  259. // ES5 15.2.3.10
  260. // http://es5.github.com/#x15.2.3.10
  261. if (!Object.preventExtensions) {
  262. Object.preventExtensions = function preventExtensions(object) {
  263. // this is misleading and breaks feature-detection, but
  264. // allows "securable" code to "gracefully" degrade to working
  265. // but insecure code.
  266. return object;
  267. };
  268. }
  269. // ES5 15.2.3.11
  270. // http://es5.github.com/#x15.2.3.11
  271. if (!Object.isSealed) {
  272. Object.isSealed = function isSealed(object) {
  273. return false;
  274. };
  275. }
  276. // ES5 15.2.3.12
  277. // http://es5.github.com/#x15.2.3.12
  278. if (!Object.isFrozen) {
  279. Object.isFrozen = function isFrozen(object) {
  280. return false;
  281. };
  282. }
  283. // ES5 15.2.3.13
  284. // http://es5.github.com/#x15.2.3.13
  285. if (!Object.isExtensible) {
  286. Object.isExtensible = function isExtensible(object) {
  287. // 1. If Type(O) is not Object throw a TypeError exception.
  288. if (Object(object) !== object) {
  289. throw new TypeError(); // TODO message
  290. }
  291. // 2. Return the Boolean value of the [[Extensible]] internal property of O.
  292. var name = '';
  293. while (owns(object, name)) {
  294. name += '?';
  295. }
  296. object[name] = true;
  297. var returnValue = owns(object, name);
  298. delete object[name];
  299. return returnValue;
  300. };
  301. }
  302. });