PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/es5-shim/2.0.7/es5-sham.js

https://gitlab.com/alidz1982/cdnjs
JavaScript | 386 lines | 234 code | 38 blank | 114 comment | 75 complexity | 51b8df3438da9dc9d51af9b16f8b16a5 MD5 | raw file
  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. // Contributed by Brandon Benvie, October, 2012
  88. var createEmpty;
  89. var supportsProto = Object.prototype.__proto__ === null;
  90. if (supportsProto || typeof document == 'undefined') {
  91. createEmpty = function () {
  92. return { "__proto__": null };
  93. };
  94. } else {
  95. // In old IE __proto__ can't be used to manually set `null`, nor does
  96. // any other method exist to make an object that inherits from nothing,
  97. // aside from Object.prototype itself. Instead, create a new global
  98. // object and *steal* its Object.prototype and strip it bare. This is
  99. // used as the prototype to create nullary objects.
  100. createEmpty = (function () {
  101. var iframe = document.createElement('iframe');
  102. var parent = document.body || document.documentElement;
  103. iframe.style.display = 'none';
  104. parent.appendChild(iframe);
  105. iframe.src = 'javascript:';
  106. var empty = iframe.contentWindow.Object.prototype;
  107. parent.removeChild(iframe);
  108. iframe = null;
  109. delete empty.constructor;
  110. delete empty.hasOwnProperty;
  111. delete empty.propertyIsEnumerable;
  112. delete empty.isPrototypeOf;
  113. delete empty.toLocaleString;
  114. delete empty.toString;
  115. delete empty.valueOf;
  116. empty.__proto__ = null;
  117. function Empty() {}
  118. Empty.prototype = empty;
  119. return function () {
  120. return new Empty();
  121. };
  122. })();
  123. }
  124. Object.create = function create(prototype, properties) {
  125. var object;
  126. function Type() {} // An empty constructor.
  127. if (prototype === null) {
  128. object = createEmpty();
  129. } else {
  130. if (typeof prototype !== "object" && typeof prototype !== "function") {
  131. // In the native implementation `parent` can be `null`
  132. // OR *any* `instanceof Object` (Object|Function|Array|RegExp|etc)
  133. // Use `typeof` tho, b/c in old IE, DOM elements are not `instanceof Object`
  134. // like they are in modern browsers. Using `Object.create` on DOM elements
  135. // is...err...probably inappropriate, but the native version allows for it.
  136. throw new TypeError("Object prototype may only be an Object or null"); // same msg as Chrome
  137. }
  138. Type.prototype = prototype;
  139. object = new Type();
  140. // IE has no built-in implementation of `Object.getPrototypeOf`
  141. // neither `__proto__`, but this manually setting `__proto__` will
  142. // guarantee that `Object.getPrototypeOf` will work as expected with
  143. // objects created using `Object.create`
  144. object.__proto__ = prototype;
  145. }
  146. if (properties !== void 0) {
  147. Object.defineProperties(object, properties);
  148. }
  149. return object;
  150. };
  151. }
  152. // ES5 15.2.3.6
  153. // http://es5.github.com/#x15.2.3.6
  154. // Patch for WebKit and IE8 standard mode
  155. // Designed by hax <hax.github.com>
  156. // related issue: https://github.com/kriskowal/es5-shim/issues#issue/5
  157. // IE8 Reference:
  158. // http://msdn.microsoft.com/en-us/library/dd282900.aspx
  159. // http://msdn.microsoft.com/en-us/library/dd229916.aspx
  160. // WebKit Bugs:
  161. // https://bugs.webkit.org/show_bug.cgi?id=36423
  162. function doesDefinePropertyWork(object) {
  163. try {
  164. Object.defineProperty(object, "sentinel", {});
  165. return "sentinel" in object;
  166. } catch (exception) {
  167. // returns falsy
  168. }
  169. }
  170. // check whether defineProperty works if it's given. Otherwise,
  171. // shim partially.
  172. if (Object.defineProperty) {
  173. var definePropertyWorksOnObject = doesDefinePropertyWork({});
  174. var definePropertyWorksOnDom = typeof document == "undefined" ||
  175. doesDefinePropertyWork(document.createElement("div"));
  176. if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
  177. var definePropertyFallback = Object.defineProperty,
  178. definePropertiesFallback = Object.defineProperties;
  179. }
  180. }
  181. if (!Object.defineProperty || definePropertyFallback) {
  182. var ERR_NON_OBJECT_DESCRIPTOR = "Property description must be an object: ";
  183. var ERR_NON_OBJECT_TARGET = "Object.defineProperty called on non-object: "
  184. var ERR_ACCESSORS_NOT_SUPPORTED = "getters & setters can not be defined " +
  185. "on this javascript engine";
  186. Object.defineProperty = function defineProperty(object, property, descriptor) {
  187. if ((typeof object != "object" && typeof object != "function") || object === null) {
  188. throw new TypeError(ERR_NON_OBJECT_TARGET + object);
  189. }
  190. if ((typeof descriptor != "object" && typeof descriptor != "function") || descriptor === null) {
  191. throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);
  192. }
  193. // make a valiant attempt to use the real defineProperty
  194. // for I8's DOM elements.
  195. if (definePropertyFallback) {
  196. try {
  197. return definePropertyFallback.call(Object, object, property, descriptor);
  198. } catch (exception) {
  199. // try the shim if the real one doesn't work
  200. }
  201. }
  202. // If it's a data property.
  203. if (owns(descriptor, "value")) {
  204. // fail silently if "writable", "enumerable", or "configurable"
  205. // are requested but not supported
  206. /*
  207. // alternate approach:
  208. if ( // can't implement these features; allow false but not true
  209. !(owns(descriptor, "writable") ? descriptor.writable : true) ||
  210. !(owns(descriptor, "enumerable") ? descriptor.enumerable : true) ||
  211. !(owns(descriptor, "configurable") ? descriptor.configurable : true)
  212. )
  213. throw new RangeError(
  214. "This implementation of Object.defineProperty does not " +
  215. "support configurable, enumerable, or writable."
  216. );
  217. */
  218. if (supportsAccessors && (lookupGetter(object, property) ||
  219. lookupSetter(object, property)))
  220. {
  221. // As accessors are supported only on engines implementing
  222. // `__proto__` we can safely override `__proto__` while defining
  223. // a property to make sure that we don't hit an inherited
  224. // accessor.
  225. var prototype = object.__proto__;
  226. object.__proto__ = prototypeOfObject;
  227. // Deleting a property anyway since getter / setter may be
  228. // defined on object itself.
  229. delete object[property];
  230. object[property] = descriptor.value;
  231. // Setting original `__proto__` back now.
  232. object.__proto__ = prototype;
  233. } else {
  234. object[property] = descriptor.value;
  235. }
  236. } else {
  237. if (!supportsAccessors) {
  238. throw new TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
  239. }
  240. // If we got that far then getters and setters can be defined !!
  241. if (owns(descriptor, "get")) {
  242. defineGetter(object, property, descriptor.get);
  243. }
  244. if (owns(descriptor, "set")) {
  245. defineSetter(object, property, descriptor.set);
  246. }
  247. }
  248. return object;
  249. };
  250. }
  251. // ES5 15.2.3.7
  252. // http://es5.github.com/#x15.2.3.7
  253. if (!Object.defineProperties || definePropertiesFallback) {
  254. Object.defineProperties = function defineProperties(object, properties) {
  255. // make a valiant attempt to use the real defineProperties
  256. if (definePropertiesFallback) {
  257. try {
  258. return definePropertiesFallback.call(Object, object, properties);
  259. } catch (exception) {
  260. // try the shim if the real one doesn't work
  261. }
  262. }
  263. for (var property in properties) {
  264. if (owns(properties, property) && property != "__proto__") {
  265. Object.defineProperty(object, property, properties[property]);
  266. }
  267. }
  268. return object;
  269. };
  270. }
  271. // ES5 15.2.3.8
  272. // http://es5.github.com/#x15.2.3.8
  273. if (!Object.seal) {
  274. Object.seal = function seal(object) {
  275. // this is misleading and breaks feature-detection, but
  276. // allows "securable" code to "gracefully" degrade to working
  277. // but insecure code.
  278. return object;
  279. };
  280. }
  281. // ES5 15.2.3.9
  282. // http://es5.github.com/#x15.2.3.9
  283. if (!Object.freeze) {
  284. Object.freeze = function freeze(object) {
  285. // this is misleading and breaks feature-detection, but
  286. // allows "securable" code to "gracefully" degrade to working
  287. // but insecure code.
  288. return object;
  289. };
  290. }
  291. // detect a Rhino bug and patch it
  292. try {
  293. Object.freeze(function () {});
  294. } catch (exception) {
  295. Object.freeze = (function freeze(freezeObject) {
  296. return function freeze(object) {
  297. if (typeof object == "function") {
  298. return object;
  299. } else {
  300. return freezeObject(object);
  301. }
  302. };
  303. })(Object.freeze);
  304. }
  305. // ES5 15.2.3.10
  306. // http://es5.github.com/#x15.2.3.10
  307. if (!Object.preventExtensions) {
  308. Object.preventExtensions = function preventExtensions(object) {
  309. // this is misleading and breaks feature-detection, but
  310. // allows "securable" code to "gracefully" degrade to working
  311. // but insecure code.
  312. return object;
  313. };
  314. }
  315. // ES5 15.2.3.11
  316. // http://es5.github.com/#x15.2.3.11
  317. if (!Object.isSealed) {
  318. Object.isSealed = function isSealed(object) {
  319. return false;
  320. };
  321. }
  322. // ES5 15.2.3.12
  323. // http://es5.github.com/#x15.2.3.12
  324. if (!Object.isFrozen) {
  325. Object.isFrozen = function isFrozen(object) {
  326. return false;
  327. };
  328. }
  329. // ES5 15.2.3.13
  330. // http://es5.github.com/#x15.2.3.13
  331. if (!Object.isExtensible) {
  332. Object.isExtensible = function isExtensible(object) {
  333. // 1. If Type(O) is not Object throw a TypeError exception.
  334. if (Object(object) !== object) {
  335. throw new TypeError(); // TODO message
  336. }
  337. // 2. Return the Boolean value of the [[Extensible]] internal property of O.
  338. var name = '';
  339. while (owns(object, name)) {
  340. name += '?';
  341. }
  342. object[name] = true;
  343. var returnValue = owns(object, name);
  344. delete object[name];
  345. return returnValue;
  346. };
  347. }
  348. });