object.js.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: object.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: object.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @constructor
  21. * Sk.builtin.object
  22. *
  23. * @description
  24. * Constructor for Python object. All Python classes (builtin and user-defined)
  25. * should inherit from this class.
  26. *
  27. * @return {Sk.builtin.object} Python object
  28. */
  29. Sk.builtin.object = function () {
  30. if (!(this instanceof Sk.builtin.object)) {
  31. return new Sk.builtin.object();
  32. }
  33. return this;
  34. };
  35. /**
  36. * @return {undefined}
  37. */
  38. Sk.builtin.object.prototype.GenericGetAttr = function (name) {
  39. var res;
  40. var f;
  41. var descr;
  42. var tp;
  43. var dict;
  44. var pyName = new Sk.builtin.str(name);
  45. goog.asserts.assert(typeof name === "string");
  46. tp = this.ob$type;
  47. goog.asserts.assert(tp !== undefined, "object has no ob$type!");
  48. dict = this["$d"] || this.constructor["$d"];
  49. // todo; assert? force?
  50. if (dict) {
  51. if (dict.mp$lookup) {
  52. res = dict.mp$lookup(pyName);
  53. } else if (dict.mp$subscript) {
  54. try {
  55. res = dict.mp$subscript(pyName);
  56. } catch (x) {
  57. res = undefined;
  58. }
  59. } else if (typeof dict === "object") {
  60. // todo; definitely the wrong place for this. other custom tp$getattr won't work on object -- bnm -- implemented custom __getattr__ in abstract.js
  61. res = dict[name];
  62. }
  63. if (res !== undefined) {
  64. return res;
  65. }
  66. }
  67. descr = Sk.builtin.type.typeLookup(tp, name);
  68. // otherwise, look in the type for a descr
  69. if (descr !== undefined &amp;&amp; descr !== null &amp;&amp; descr.ob$type !== undefined) {
  70. f = descr.ob$type.tp$descr_get;
  71. // todo;
  72. //if (f &amp;&amp; descr.tp$descr_set) // is a data descriptor if it has a set
  73. //return f.call(descr, this, this.ob$type);
  74. }
  75. if (f) {
  76. // non-data descriptor
  77. return f.call(descr, this, this.ob$type);
  78. }
  79. if (descr !== undefined) {
  80. return descr;
  81. }
  82. return undefined;
  83. };
  84. goog.exportSymbol("Sk.builtin.object.prototype.GenericGetAttr", Sk.builtin.object.prototype.GenericGetAttr);
  85. Sk.builtin.object.prototype.GenericPythonGetAttr = function(self, name) {
  86. return Sk.builtin.object.prototype.GenericGetAttr.call(self, name.v);
  87. };
  88. goog.exportSymbol("Sk.builtin.object.prototype.GenericPythonGetAttr", Sk.builtin.object.prototype.GenericPythonGetAttr);
  89. Sk.builtin.object.prototype.GenericSetAttr = function (name, value) {
  90. var objname = Sk.abstr.typeName(this);
  91. var pyname;
  92. var dict;
  93. goog.asserts.assert(typeof name === "string");
  94. // todo; lots o' stuff
  95. dict = this["$d"] || this.constructor["$d"];
  96. if (dict.mp$ass_subscript) {
  97. pyname = new Sk.builtin.str(name);
  98. if (this instanceof Sk.builtin.object &amp;&amp; !(this.ob$type.sk$klass) &amp;&amp;
  99. dict.mp$lookup(pyname) === undefined) {
  100. // Cannot add new attributes to a builtin object
  101. throw new Sk.builtin.AttributeError("'" + objname + "' object has no attribute '" + name + "'");
  102. }
  103. dict.mp$ass_subscript(new Sk.builtin.str(name), value);
  104. } else if (typeof dict === "object") {
  105. dict[name] = value;
  106. }
  107. };
  108. goog.exportSymbol("Sk.builtin.object.prototype.GenericSetAttr", Sk.builtin.object.prototype.GenericSetAttr);
  109. Sk.builtin.object.prototype.GenericPythonSetAttr = function(self, name, value) {
  110. return Sk.builtin.object.prototype.GenericSetAttr.call(self, name.v, value);
  111. };
  112. goog.exportSymbol("Sk.builtin.object.prototype.GenericPythonSetAttr", Sk.builtin.object.prototype.GenericPythonSetAttr);
  113. Sk.builtin.object.prototype.HashNotImplemented = function () {
  114. throw new Sk.builtin.TypeError("unhashable type: '" + Sk.abstr.typeName(this) + "'");
  115. };
  116. Sk.builtin.object.prototype.tp$getattr = Sk.builtin.object.prototype.GenericGetAttr;
  117. Sk.builtin.object.prototype.tp$setattr = Sk.builtin.object.prototype.GenericSetAttr;
  118. // Although actual attribute-getting happens in pure Javascript via tp$getattr, classes
  119. // overriding __getattr__ etc need to be able to call object.__getattr__ etc from Python
  120. Sk.builtin.object.prototype["__getattr__"] = Sk.builtin.object.prototype.GenericPythonGetAttr;
  121. Sk.builtin.object.prototype["__setattr__"] = Sk.builtin.object.prototype.GenericPythonSetAttr;
  122. /**
  123. * The name of this class.
  124. * @type {string}
  125. */
  126. Sk.builtin.object.prototype.tp$name = "object";
  127. /**
  128. * The type object of this class.
  129. * @type {Sk.builtin.type}
  130. */
  131. Sk.builtin.object.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("object", Sk.builtin.object);
  132. Sk.builtin.object.prototype.ob$type.sk$klass = undefined; // Nonsense for closure compiler
  133. /** Default implementations of dunder methods found in all Python objects */
  134. /**
  135. * Python wrapper for `__repr__` method.
  136. * @name __repr__
  137. * @memberOf Sk.builtin.object.prototype
  138. * @instance
  139. */
  140. Sk.builtin.object.prototype["__repr__"] = function (self) {
  141. Sk.builtin.pyCheckArgs("__repr__", arguments, 0, 0, false, true);
  142. return self["$r"]();
  143. };
  144. /**
  145. * Python wrapper for `__str__` method.
  146. * @name __str__
  147. * @memberOf Sk.builtin.object.prototype
  148. * @instance
  149. */
  150. Sk.builtin.object.prototype["__str__"] = function (self) {
  151. Sk.builtin.pyCheckArgs("__str__", arguments, 0, 0, false, true);
  152. return self["$r"]();
  153. };
  154. /**
  155. * Python wrapper for `__hash__` method.
  156. * @name __hash__
  157. * @memberOf Sk.builtin.object.prototype
  158. * @instance
  159. */
  160. Sk.builtin.object.prototype["__hash__"] = function (self) {
  161. Sk.builtin.pyCheckArgs("__hash__", arguments, 0, 0, false, true);
  162. return self.tp$hash();
  163. };
  164. /**
  165. * Python wrapper for `__eq__` method.
  166. * @name __eq__
  167. * @memberOf Sk.builtin.object.prototype
  168. * @instance
  169. */
  170. Sk.builtin.object.prototype["__eq__"] = function (self, other) {
  171. Sk.builtin.pyCheckArgs("__eq__", arguments, 1, 1, false, true);
  172. return self.ob$eq(other);
  173. };
  174. /**
  175. * Python wrapper for `__ne__` method.
  176. * @name __ne__
  177. * @memberOf Sk.builtin.object.prototype
  178. * @instance
  179. */
  180. Sk.builtin.object.prototype["__ne__"] = function (self, other) {
  181. Sk.builtin.pyCheckArgs("__ne__", arguments, 1, 1, false, true);
  182. return self.ob$ne(other);
  183. };
  184. /**
  185. * Python wrapper for `__lt__` method.
  186. * @name __lt__
  187. * @memberOf Sk.builtin.object.prototype
  188. * @instance
  189. */
  190. Sk.builtin.object.prototype["__lt__"] = function (self, other) {
  191. Sk.builtin.pyCheckArgs("__lt__", arguments, 1, 1, false, true);
  192. return self.ob$lt(other);
  193. };
  194. /**
  195. * Python wrapper for `__le__` method.
  196. * @name __le__
  197. * @memberOf Sk.builtin.object.prototype
  198. * @instance
  199. */
  200. Sk.builtin.object.prototype["__le__"] = function (self, other) {
  201. Sk.builtin.pyCheckArgs("__le__", arguments, 1, 1, false, true);
  202. return self.ob$le(other);
  203. };
  204. /**
  205. * Python wrapper for `__gt__` method.
  206. * @name __gt__
  207. * @memberOf Sk.builtin.object.prototype
  208. * @instance
  209. */
  210. Sk.builtin.object.prototype["__gt__"] = function (self, other) {
  211. Sk.builtin.pyCheckArgs("__gt__", arguments, 1, 1, false, true);
  212. return self.ob$gt(other);
  213. };
  214. /**
  215. * Python wrapper for `__ge__` method.
  216. * @name __ge__
  217. * @memberOf Sk.builtin.object.prototype
  218. * @instance
  219. */
  220. Sk.builtin.object.prototype["__ge__"] = function (self, other) {
  221. Sk.builtin.pyCheckArgs("__ge__", arguments, 1, 1, false, true);
  222. return self.ob$ge(other);
  223. };
  224. /** Default implementations of Javascript functions used in dunder methods */
  225. /**
  226. * Return the string representation of this instance.
  227. *
  228. * Javascript function, returns Python object.
  229. *
  230. * @name $r
  231. * @memberOf Sk.builtin.object.prototype
  232. * @return {Sk.builtin.str} The Python string representation of this instance.
  233. */
  234. Sk.builtin.object.prototype["$r"] = function () {
  235. return new Sk.builtin.str("&lt;object>");
  236. };
  237. Sk.builtin.hashCount = 1;
  238. /**
  239. * Return the hash value of this instance.
  240. *
  241. * Javascript function, returns Python object.
  242. *
  243. * @return {Sk.builtin.int_} The hash value
  244. */
  245. Sk.builtin.object.prototype.tp$hash = function () {
  246. if (!this.$savedHash_) {
  247. this.$savedHash_ = new Sk.builtin.int_(Sk.builtin.hashCount++);
  248. }
  249. return this.$savedHash_;
  250. };
  251. /**
  252. * Perform equality check between this instance and a Python object (i.e. this == other).
  253. *
  254. * Implements `__eq__` dunder method.
  255. *
  256. * Javascript function, returns Python object.
  257. *
  258. * @param {Object} other The Python object to check for equality.
  259. * @return {(Sk.builtin.bool|Sk.builtin.NotImplemented)} true if equal, false otherwise
  260. */
  261. Sk.builtin.object.prototype.ob$eq = function (other) {
  262. if (this === other) {
  263. return Sk.builtin.bool.true$;
  264. }
  265. return Sk.builtin.NotImplemented.NotImplemented$;
  266. };
  267. /**
  268. * Perform non-equality check between this instance and a Python object (i.e. this != other).
  269. *
  270. * Implements `__ne__` dunder method.
  271. *
  272. * Javascript function, returns Python object.
  273. *
  274. * @param {Object} other The Python object to check for non-equality.
  275. * @return {(Sk.builtin.bool|Sk.builtin.NotImplemented)} true if not equal, false otherwise
  276. */
  277. Sk.builtin.object.prototype.ob$ne = function (other) {
  278. if (this === other) {
  279. return Sk.builtin.bool.false$;
  280. }
  281. return Sk.builtin.NotImplemented.NotImplemented$;
  282. };
  283. /**
  284. * Determine if this instance is less than a Python object (i.e. this &lt; other).
  285. *
  286. * Implements `__lt__` dunder method.
  287. *
  288. * Javascript function, returns Python object.
  289. *
  290. * @param {Object} other The Python object to compare.
  291. * @return {(Sk.builtin.bool|Sk.builtin.NotImplemented)} true if this &lt; other, false otherwise
  292. */
  293. Sk.builtin.object.prototype.ob$lt = function (other) {
  294. return Sk.builtin.NotImplemented.NotImplemented$;
  295. };
  296. /**
  297. * Determine if this instance is less than or equal to a Python object (i.e. this &lt;= other).
  298. *
  299. * Implements `__le__` dunder method.
  300. *
  301. * Javascript function, returns Python object.
  302. *
  303. * @param {Object} other The Python object to compare.
  304. * @return {(Sk.builtin.bool|Sk.builtin.NotImplemented)} true if this &lt;= other, false otherwise
  305. */
  306. Sk.builtin.object.prototype.ob$le = function (other) {
  307. return Sk.builtin.NotImplemented.NotImplemented$;
  308. };
  309. /**
  310. * Determine if this instance is greater than a Python object (i.e. this > other).
  311. *
  312. * Implements `__gt__` dunder method.
  313. *
  314. * Javascript function, returns Python object.
  315. *
  316. * @param {Object} other The Python object to compare.
  317. * @return {(Sk.builtin.bool|Sk.builtin.NotImplemented)} true if this > other, false otherwise
  318. */
  319. Sk.builtin.object.prototype.ob$gt = function (other) {
  320. return Sk.builtin.NotImplemented.NotImplemented$;
  321. };
  322. /**
  323. * Determine if this instance is greater than or equal to a Python object (i.e. this >= other).
  324. *
  325. * Implements `__ge__` dunder method.
  326. *
  327. * Javascript function, returns Python object.
  328. *
  329. * @param {Object} other The Python object to compare.
  330. * @return {(Sk.builtin.bool|Sk.builtin.NotImplemented)} true if this >= other, false otherwise
  331. */
  332. Sk.builtin.object.prototype.ob$ge = function (other) {
  333. return Sk.builtin.NotImplemented.NotImplemented$;
  334. };
  335. // Wrap the following functions in Sk.builtin.func once that class is initialized
  336. /**
  337. * Array of all the Python functions which are methods of this class.
  338. * @type {Array}
  339. */
  340. Sk.builtin.object.pythonFunctions = ["__repr__", "__str__", "__hash__",
  341. "__eq__", "__ne__", "__lt__", "__le__", "__gt__", "__ge__", "__getattr__", "__setattr__"];
  342. /**
  343. * @constructor
  344. * Sk.builtin.none
  345. *
  346. * @extends {Sk.builtin.object}
  347. */
  348. Sk.builtin.none = function () {
  349. this.v = null;
  350. };
  351. Sk.abstr.setUpInheritance("NoneType", Sk.builtin.none, Sk.builtin.object);
  352. /** @override */
  353. Sk.builtin.none.prototype["$r"] = function () { return new Sk.builtin.str("None"); };
  354. /** @override */
  355. Sk.builtin.none.prototype.tp$hash = function () {
  356. return new Sk.builtin.int_(0);
  357. };
  358. /**
  359. * Python None constant.
  360. * @type {Sk.builtin.none}
  361. */
  362. Sk.builtin.none.none$ = new Sk.builtin.none();
  363. /**
  364. * @constructor
  365. * Sk.builtin.NotImplemented
  366. *
  367. * @extends {Sk.builtin.object}
  368. */
  369. Sk.builtin.NotImplemented = function() { };
  370. Sk.abstr.setUpInheritance("NotImplementedType", Sk.builtin.NotImplemented, Sk.builtin.object);
  371. /** @override */
  372. Sk.builtin.NotImplemented.prototype["$r"] = function () { return new Sk.builtin.str("NotImplemented"); };
  373. /**
  374. * Python NotImplemented constant.
  375. * @type {Sk.builtin.NotImplemented}
  376. */
  377. Sk.builtin.NotImplemented.NotImplemented$ = new Sk.builtin.NotImplemented();
  378. goog.exportSymbol("Sk.builtin.none", Sk.builtin.none);
  379. goog.exportSymbol("Sk.builtin.NotImplemented", Sk.builtin.NotImplemented);
  380. </code></pre>
  381. </article>
  382. </section>
  383. </div>
  384. <nav>
  385. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sk.abstr.iter-seqIter.html">seqIter</a></li><li><a href="Sk.builtin.bool.html">bool</a></li><li><a href="Sk.builtin.float_.html">float_</a></li><li><a href="Sk.builtin.func.html">func</a></li><li><a href="Sk.builtin.int_.html">int_</a></li><li><a href="Sk.builtin.none.html">none</a></li><li><a href="Sk.builtin.NotImplemented.html">NotImplemented</a></li><li><a href="Sk.builtin.numtype.html">numtype</a></li><li><a href="Sk.builtin.object.html">object</a></li><li><a href="Sk.builtin.seqtype.html">seqtype</a></li><li><a href="Sk.misceval.Suspension.html">Suspension</a></li></ul><h3>Namespaces</h3><ul><li><a href="Sk.html">Sk</a></li><li><a href="Sk.abstr.html">abstr</a></li><li><a href="Sk.builtin.html">builtin</a></li><li><a href="Sk.ffi.html">ffi</a></li><li><a href="Sk.misceval.html">misceval</a></li></ul>
  386. </nav>
  387. <br class="clear">
  388. <footer>
  389. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0</a> on Thu Aug 13 2015 08:14:27 GMT-0500 (CDT)
  390. </footer>
  391. <script> prettyPrint(); </script>
  392. <script src="scripts/linenumber.js"> </script>
  393. </body>
  394. </html>