int.js.html 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: int.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: int.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/* jslint nomen: true, bitwise: true */
  20. /* global Sk: true */
  21. /**
  22. * @namespace Sk.builtin
  23. */
  24. /**
  25. * @constructor
  26. * Sk.builtin.int_
  27. *
  28. * @description
  29. * Constructor for Python int. If provided number is greater than integer threshold, will return a Python long instead.
  30. *
  31. * type int, all integers are created with this method, it is also used
  32. * for the builtin int()
  33. *
  34. * Takes also implemented `__int__` and `__trunc__` methods for x into account
  35. * and tries to use `__index__` and/or `__int__` if base is not a number
  36. *
  37. * @extends {Sk.builtin.numtype}
  38. *
  39. * @param {!(Object|number)} x Python object or Javascript number to convert to Python int
  40. * @param {!(Object|number)=} base Optional base, can only be used when x is Sk.builtin.str
  41. * @return {(Sk.builtin.int_|Sk.builtin.lng)} Python int (or long, if overflow)
  42. */
  43. Sk.builtin.int_ = function (x, base) {
  44. "use strict";
  45. var val;
  46. var ret; // return value
  47. var magicName; // name of magic method
  48. if (!(this instanceof Sk.builtin.int_)) {
  49. return new Sk.builtin.int_(x, base);
  50. }
  51. if (this instanceof Sk.builtin.bool) {
  52. return this;
  53. }
  54. if (x instanceof Sk.builtin.int_ &amp;&amp; base === undefined) {
  55. this.v = x.v;
  56. return this;
  57. }
  58. // if base is not of type int, try calling .__index__
  59. if(base !== undefined &amp;&amp; !Sk.builtin.checkInt(base)) {
  60. if (Sk.builtin.checkFloat(base)) {
  61. throw new Sk.builtin.TypeError("integer argument expected, got " + Sk.abstr.typeName(base));
  62. } else if (base.__index__) {
  63. base = Sk.misceval.callsim(base.__index__, base);
  64. } else if(base.__int__) {
  65. base = Sk.misceval.callsim(base.__int__, base);
  66. } else {
  67. throw new Sk.builtin.AttributeError(Sk.abstr.typeName(base) + " instance has no attribute '__index__' or '__int__'");
  68. }
  69. }
  70. if (x instanceof Sk.builtin.str) {
  71. base = Sk.builtin.asnum$(base);
  72. val = Sk.str2number(x.v, base, parseInt, function (x) {
  73. return -x;
  74. }, "int");
  75. if ((val > Sk.builtin.int_.threshold$) || (val &lt; -Sk.builtin.int_.threshold$)) {
  76. // Too big for int, convert to long
  77. return new Sk.builtin.lng(x, base);
  78. }
  79. this.v = val;
  80. return this;
  81. }
  82. if (base !== undefined) {
  83. throw new Sk.builtin.TypeError("int() can't convert non-string with explicit base");
  84. }
  85. if (x === undefined || x === Sk.builtin.none) {
  86. x = 0;
  87. }
  88. /**
  89. * try calling special methods:
  90. * 1. __int__
  91. * 2. __trunc__
  92. */
  93. if(x !== undefined &amp;&amp; (x.tp$getattr &amp;&amp; x.tp$getattr("__int__"))) {
  94. // calling a method which contains im_self and im_func
  95. // causes skulpt to automatically map the im_self as first argument
  96. ret = Sk.misceval.callsim(x.tp$getattr("__int__"));
  97. magicName = "__int__";
  98. } else if(x !== undefined &amp;&amp; x.__int__) {
  99. // required for internal types
  100. // __int__ method is on prototype
  101. ret = Sk.misceval.callsim(x.__int__, x);
  102. magicName = "__int__";
  103. } else if(x !== undefined &amp;&amp; (x.tp$getattr &amp;&amp; x.tp$getattr("__trunc__"))) {
  104. ret = Sk.misceval.callsim(x.tp$getattr("__trunc__"));
  105. magicName = "__trunc__";
  106. } else if(x !== undefined &amp;&amp; x.__trunc__) {
  107. ret = Sk.misceval.callsim(x.__trunc__, x);
  108. magicName = "__trunc__";
  109. }
  110. // check return type of magic methods
  111. if(ret !== undefined &amp;&amp; !Sk.builtin.checkInt(ret)) {
  112. throw new Sk.builtin.TypeError(magicName + " returned non-Integral (type " + Sk.abstr.typeName(ret)+")");
  113. } else if(ret !== undefined){
  114. x = ret; // valid return value, proceed in function
  115. }
  116. // check type even without magic numbers
  117. if(!Sk.builtin.checkNumber(x)) {
  118. throw new Sk.builtin.TypeError("int() argument must be a string or a number, not '" + Sk.abstr.typeName(x) + "'");
  119. }
  120. x = Sk.builtin.asnum$(x);
  121. if (x > Sk.builtin.int_.threshold$ || x &lt; -Sk.builtin.int_.threshold$) {
  122. return new Sk.builtin.lng(x);
  123. }
  124. if ((x > -1) &amp;&amp; (x &lt; 1)) {
  125. x = 0;
  126. }
  127. this.v = parseInt(x, base);
  128. return this;
  129. };
  130. Sk.abstr.setUpInheritance("int", Sk.builtin.int_, Sk.builtin.numtype);
  131. /* NOTE: See constants used for kwargs in constants.js */
  132. Sk.builtin.int_.prototype.nb$int_ = function () {
  133. return this;
  134. };
  135. Sk.builtin.int_.prototype.nb$float_ = function() {
  136. return new Sk.builtin.float_(this.v);
  137. };
  138. Sk.builtin.int_.prototype.nb$lng = function () {
  139. return new Sk.builtin.lng(this.v);
  140. };
  141. /**
  142. * Python wrapper of `__trunc__` dunder method.
  143. *
  144. * @instance
  145. */
  146. Sk.builtin.int_.prototype.__trunc__ = new Sk.builtin.func(function(self) {
  147. return self;
  148. });
  149. /**
  150. * Python wrapper of `__index__` dunder method.
  151. *
  152. * @instance
  153. */
  154. Sk.builtin.int_.prototype.__index__ = new Sk.builtin.func(function(self) {
  155. return self;
  156. });
  157. /**
  158. * Python wrapper of `__complex__` dunder method.
  159. *
  160. * @instance
  161. */
  162. Sk.builtin.int_.prototype.__complex__ = new Sk.builtin.func(function(self) {
  163. return Sk.builtin.NotImplemented.NotImplemented$;
  164. });
  165. /**
  166. * Return this instance's Javascript value.
  167. *
  168. * Javascript function, returns Javascript object.
  169. *
  170. * @return {number} This instance's value.
  171. */
  172. Sk.builtin.int_.prototype.tp$index = function () {
  173. return this.v;
  174. };
  175. /** @override */
  176. Sk.builtin.int_.prototype.tp$hash = function () {
  177. //the hash of all numbers should be an int and since javascript doesn't really
  178. //care every number can be an int.
  179. return new Sk.builtin.int_(this.v);
  180. };
  181. /**
  182. * Threshold to determine when types should be converted to long.
  183. *
  184. * Note: be sure to check against threshold in both positive and negative directions.
  185. *
  186. * @type {number}
  187. */
  188. Sk.builtin.int_.threshold$ = Math.pow(2, 53) - 1;
  189. /**
  190. * Returns a copy of this instance.
  191. *
  192. * Javascript function, returns Python object.
  193. *
  194. * @return {Sk.builtin.int_} The copy
  195. */
  196. Sk.builtin.int_.prototype.clone = function () {
  197. return new Sk.builtin.int_(this.v);
  198. };
  199. /** @override */
  200. Sk.builtin.int_.prototype.nb$add = function (other) {
  201. var thisAsLong, thisAsFloat;
  202. if (other instanceof Sk.builtin.int_) {
  203. return new Sk.builtin.int_(this.v + other.v);
  204. }
  205. if (other instanceof Sk.builtin.lng) {
  206. thisAsLong = new Sk.builtin.lng(this.v);
  207. return thisAsLong.nb$add(other);
  208. }
  209. if (other instanceof Sk.builtin.float_) {
  210. thisAsFloat = new Sk.builtin.float_(this.v);
  211. return thisAsFloat.nb$add(other);
  212. }
  213. return Sk.builtin.NotImplemented.NotImplemented$;
  214. };
  215. /** @override */
  216. Sk.builtin.int_.prototype.nb$reflected_add = function (other) {
  217. // Should not automatically call this.nb$add, as nb$add may have
  218. // been overridden by a subclass
  219. return Sk.builtin.int_.prototype.nb$add.call(this, other);
  220. };
  221. /** @override */
  222. Sk.builtin.int_.prototype.nb$subtract = function (other) {
  223. var thisAsLong, thisAsFloat;
  224. if (other instanceof Sk.builtin.int_) {
  225. return new Sk.builtin.int_(this.v - other.v);
  226. }
  227. if (other instanceof Sk.builtin.lng) {
  228. thisAsLong = new Sk.builtin.lng(this.v);
  229. return thisAsLong.nb$subtract(other);
  230. }
  231. if (other instanceof Sk.builtin.float_) {
  232. thisAsFloat = new Sk.builtin.float_(this.v);
  233. return thisAsFloat.nb$subtract(other);
  234. }
  235. return Sk.builtin.NotImplemented.NotImplemented$;
  236. };
  237. /** @override */
  238. Sk.builtin.int_.prototype.nb$reflected_subtract = function (other) {
  239. // Should not automatically call this.nb$add, as nb$add may have
  240. // been overridden by a subclass
  241. var negative_this = this.nb$negative();
  242. return Sk.builtin.int_.prototype.nb$add.call(negative_this, other);
  243. };
  244. /** @override */
  245. Sk.builtin.int_.prototype.nb$multiply = function (other) {
  246. var product, thisAsLong, thisAsFloat;
  247. if (other instanceof Sk.builtin.int_) {
  248. product = this.v * other.v;
  249. if (product > Sk.builtin.int_.threshold$ ||
  250. product &lt; -Sk.builtin.int_.threshold$) {
  251. thisAsLong = new Sk.builtin.lng(this.v);
  252. return thisAsLong.nb$multiply(other);
  253. } else {
  254. return new Sk.builtin.int_(product);
  255. }
  256. }
  257. if (other instanceof Sk.builtin.lng) {
  258. thisAsLong = new Sk.builtin.lng(this.v);
  259. return thisAsLong.nb$multiply(other);
  260. }
  261. if (other instanceof Sk.builtin.float_) {
  262. thisAsFloat = new Sk.builtin.float_(this.v);
  263. return thisAsFloat.nb$multiply(other);
  264. }
  265. return Sk.builtin.NotImplemented.NotImplemented$;
  266. };
  267. /** @override */
  268. Sk.builtin.int_.prototype.nb$reflected_multiply = function (other) {
  269. // Should not automatically call this.nb$multiply, as nb$multiply may have
  270. // been overridden by a subclass
  271. return Sk.builtin.int_.prototype.nb$multiply.call(this, other);
  272. };
  273. /** @override */
  274. Sk.builtin.int_.prototype.nb$divide = function (other) {
  275. var thisAsFloat;
  276. if (Sk.python3) {
  277. thisAsFloat = new Sk.builtin.float_(this.v);
  278. return thisAsFloat.nb$divide(other);
  279. }
  280. return this.nb$floor_divide(other);
  281. };
  282. /** @override */
  283. Sk.builtin.int_.prototype.nb$reflected_divide = function (other) {
  284. return this.nb$reflected_floor_divide(other);
  285. };
  286. /** @override */
  287. Sk.builtin.int_.prototype.nb$floor_divide = function (other) {
  288. var thisAsLong, thisAsFloat;
  289. if (other instanceof Sk.builtin.int_) {
  290. if (other.v === 0) {
  291. throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
  292. }
  293. return new Sk.builtin.int_(Math.floor(this.v / other.v));
  294. }
  295. if (other instanceof Sk.builtin.lng) {
  296. thisAsLong = new Sk.builtin.lng(this.v);
  297. return thisAsLong.nb$divide(other);
  298. }
  299. if (other instanceof Sk.builtin.float_) {
  300. thisAsFloat = new Sk.builtin.float_(this.v);
  301. return thisAsFloat.nb$divide(other);
  302. }
  303. return Sk.builtin.NotImplemented.NotImplemented$;
  304. };
  305. /** @override */
  306. Sk.builtin.int_.prototype.nb$reflected_floor_divide = function (other) {
  307. if (other instanceof Sk.builtin.int_) {
  308. return other.nb$divide(this);
  309. }
  310. return Sk.builtin.NotImplemented.NotImplemented$;
  311. };
  312. /** @override */
  313. Sk.builtin.int_.prototype.nb$remainder = function (other) {
  314. var thisAsLong, thisAsFloat;
  315. var tmp;
  316. if (other instanceof Sk.builtin.int_) {
  317. // Javacript logic on negatives doesn't work for Python... do this instead
  318. tmp = this.v % other.v;
  319. if (this.v &lt; 0) {
  320. if (other.v > 0 &amp;&amp; tmp &lt; 0) {
  321. tmp = tmp + other.v;
  322. }
  323. } else {
  324. if (other.v &lt; 0 &amp;&amp; tmp !== 0) {
  325. tmp = tmp + other.v;
  326. }
  327. }
  328. if (other.v &lt; 0 &amp;&amp; tmp === 0) {
  329. tmp = -0.0; // otherwise the sign gets lost by javascript modulo
  330. } else if (tmp === 0 &amp;&amp; Infinity/tmp === -Infinity) {
  331. tmp = 0.0;
  332. }
  333. return new Sk.builtin.int_(tmp);
  334. }
  335. if (other instanceof Sk.builtin.lng) {
  336. thisAsLong = new Sk.builtin.lng(this.v);
  337. return thisAsLong.nb$remainder(other);
  338. }
  339. if (other instanceof Sk.builtin.float_) {
  340. thisAsFloat = new Sk.builtin.float_(this.v);
  341. return thisAsFloat.nb$remainder(other);
  342. }
  343. return Sk.builtin.NotImplemented.NotImplemented$;
  344. };
  345. /** @override */
  346. Sk.builtin.int_.prototype.nb$reflected_remainder = function (other) {
  347. if (other instanceof Sk.builtin.int_) {
  348. return other.nb$remainder(this);
  349. }
  350. return Sk.builtin.NotImplemented.NotImplemented$;
  351. };
  352. /** @override */
  353. Sk.builtin.int_.prototype.nb$divmod = function (other) {
  354. var thisAsLong, thisAsFloat;
  355. if (other instanceof Sk.builtin.int_) {
  356. return new Sk.builtin.tuple([
  357. this.nb$floor_divide(other),
  358. this.nb$remainder(other)
  359. ]);
  360. }
  361. if (other instanceof Sk.builtin.lng) {
  362. thisAsLong = new Sk.builtin.lng(this.v);
  363. return thisAsLong.nb$divmod(other);
  364. }
  365. if (other instanceof Sk.builtin.float_) {
  366. thisAsFloat = new Sk.builtin.float_(this.v);
  367. return thisAsFloat.nb$divmod(other);
  368. }
  369. return Sk.builtin.NotImplemented.NotImplemented$;
  370. };
  371. /** @override */
  372. Sk.builtin.int_.prototype.nb$reflected_divmod = function (other) {
  373. if (other instanceof Sk.builtin.int_) {
  374. return new Sk.builtin.tuple([
  375. other.nb$floor_divide(this),
  376. other.nb$remainder(this)
  377. ]);
  378. }
  379. return Sk.builtin.NotImplemented.NotImplemented$;
  380. };
  381. /** @override */
  382. Sk.builtin.int_.prototype.nb$power = function (other, mod) {
  383. var power, ret, thisAsLong, thisAsFloat;
  384. if (other instanceof Sk.builtin.int_ &amp;&amp; (mod === undefined || mod instanceof Sk.builtin.int_)) {
  385. power = Math.pow(this.v, other.v);
  386. if (power > Sk.builtin.int_.threshold$ ||
  387. power &lt; -Sk.builtin.int_.threshold$) {
  388. thisAsLong = new Sk.builtin.lng(this.v);
  389. ret = thisAsLong.nb$power(other, mod);
  390. } else if (other.v &lt; 0) {
  391. ret = new Sk.builtin.float_(power);
  392. } else {
  393. ret = new Sk.builtin.int_(power);
  394. }
  395. if (mod !== undefined) {
  396. if (other.v &lt; 0) {
  397. throw new Sk.builtin.TypeError("pow() 2nd argument cannot be negative when 3rd argument specified");
  398. }
  399. return ret.nb$remainder(mod);
  400. } else {
  401. return ret;
  402. }
  403. }
  404. if (other instanceof Sk.builtin.lng) {
  405. thisAsLong = new Sk.builtin.lng(this.v);
  406. return thisAsLong.nb$power(other);
  407. }
  408. if (other instanceof Sk.builtin.float_) {
  409. thisAsFloat = new Sk.builtin.float_(this.v);
  410. return thisAsFloat.nb$power(other);
  411. }
  412. return Sk.builtin.NotImplemented.NotImplemented$;
  413. };
  414. /** @override */
  415. Sk.builtin.int_.prototype.nb$reflected_power = function (other, mod) {
  416. if (other instanceof Sk.builtin.int_) {
  417. return other.nb$power(this, mod);
  418. }
  419. return Sk.builtin.NotImplemented.NotImplemented$;
  420. };
  421. /** @override */
  422. Sk.builtin.int_.prototype.nb$abs = function () {
  423. return new Sk.builtin.int_(Math.abs(this.v));
  424. };
  425. /**
  426. * Compute the bitwise AND of this instance and a Python object (i.e. this &amp; other).
  427. *
  428. * Returns NotImplemented if bitwise AND operation between int and other type is unsupported.
  429. *
  430. * Javscript function, returns Python object.
  431. *
  432. * @param {!Sk.builtin.object} other The Python object to AND with this one
  433. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the conjunction
  434. */
  435. Sk.builtin.int_.prototype.nb$and = function (other) {
  436. var thisAsLong, thisAsFloat;
  437. if (other instanceof Sk.builtin.int_) {
  438. var tmp;
  439. other = Sk.builtin.asnum$(other);
  440. tmp = this.v &amp; other;
  441. if ((tmp !== undefined) &amp;&amp; (tmp &lt; 0)) {
  442. tmp = tmp + 4294967296; // convert back to unsigned
  443. }
  444. if (tmp !== undefined) {
  445. return new Sk.builtin.int_(tmp);
  446. }
  447. }
  448. if (other instanceof Sk.builtin.lng) {
  449. thisAsLong = new Sk.builtin.lng(this.v);
  450. return thisAsLong.nb$and(other);
  451. }
  452. return Sk.builtin.NotImplemented.NotImplemented$;
  453. };
  454. Sk.builtin.int_.prototype.nb$reflected_and = Sk.builtin.int_.prototype.nb$and;
  455. /**
  456. * Compute the bitwise OR of this instance and a Python object (i.e. this | other).
  457. *
  458. * Returns NotImplemented if bitwise OR operation between int and other type is unsupported.
  459. *
  460. * Javscript function, returns Python object.
  461. *
  462. * @param {!Sk.builtin.object} other The Python object to OR with this one
  463. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the disjunction
  464. */
  465. Sk.builtin.int_.prototype.nb$or = function (other) {
  466. var thisAsLong;
  467. if (other instanceof Sk.builtin.int_) {
  468. var tmp;
  469. other = Sk.builtin.asnum$(other);
  470. tmp = this.v | other;
  471. if ((tmp !== undefined) &amp;&amp; (tmp &lt; 0)) {
  472. tmp = tmp + 4294967296; // convert back to unsigned
  473. }
  474. if (tmp !== undefined) {
  475. return new Sk.builtin.int_(tmp);
  476. }
  477. }
  478. if (other instanceof Sk.builtin.lng) {
  479. thisAsLong = new Sk.builtin.lng(this.v);
  480. return thisAsLong.nb$and(other);
  481. }
  482. return Sk.builtin.NotImplemented.NotImplemented$;
  483. };
  484. Sk.builtin.int_.prototype.nb$reflected_or = Sk.builtin.int_.prototype.nb$or;
  485. /**
  486. * Compute the bitwise XOR of this instance and a Python object (i.e. this ^ other).
  487. *
  488. * Returns NotImplemented if bitwise XOR operation between int and other type is unsupported.
  489. *
  490. * Javscript function, returns Python object.
  491. *
  492. * @param {!Sk.builtin.object} other The Python object to XOR with this one
  493. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the exclusive disjunction
  494. */
  495. Sk.builtin.int_.prototype.nb$xor = function (other) {
  496. var thisAsLong;
  497. if (other instanceof Sk.builtin.int_) {
  498. var tmp;
  499. other = Sk.builtin.asnum$(other);
  500. tmp = this.v ^ other;
  501. if ((tmp !== undefined) &amp;&amp; (tmp &lt; 0)) {
  502. tmp = tmp + 4294967296; // convert back to unsigned
  503. }
  504. if (tmp !== undefined) {
  505. return new Sk.builtin.int_(tmp);
  506. }
  507. }
  508. if (other instanceof Sk.builtin.lng) {
  509. thisAsLong = new Sk.builtin.lng(this.v);
  510. return thisAsLong.nb$xor(other);
  511. }
  512. return Sk.builtin.NotImplemented.NotImplemented$;
  513. };
  514. Sk.builtin.int_.prototype.nb$reflected_xor = Sk.builtin.int_.prototype.nb$xor;
  515. /**
  516. * Compute the bitwise left shift of this instance by a Python object (i.e. this &lt;&lt; other).
  517. *
  518. * Returns NotImplemented if bitwise left shift operation between int and other type is unsupported.
  519. *
  520. * Javscript function, returns Python object.
  521. *
  522. * @param {!Sk.builtin.object} other The Python object by which to left shift
  523. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the left shift
  524. */
  525. Sk.builtin.int_.prototype.nb$lshift = function (other) {
  526. var thisAsLong;
  527. if (other instanceof Sk.builtin.int_) {
  528. var tmp;
  529. var shift = Sk.builtin.asnum$(other);
  530. if (shift !== undefined) {
  531. if (shift &lt; 0) {
  532. throw new Sk.builtin.ValueError("negative shift count");
  533. }
  534. tmp = this.v &lt;&lt; shift;
  535. if (tmp &lt;= this.v) {
  536. // Fail, recompute with longs
  537. return new Sk.builtin.lng(this.v).nb$lshift(other);
  538. }
  539. }
  540. if (tmp !== undefined) {
  541. tmp = /** @type {number} */ (tmp);
  542. return new Sk.builtin.int_(tmp);
  543. }
  544. }
  545. if (other instanceof Sk.builtin.lng) {
  546. thisAsLong = new Sk.builtin.lng(this.v);
  547. return thisAsLong.nb$lshift(other);
  548. }
  549. return Sk.builtin.NotImplemented.NotImplemented$;
  550. };
  551. Sk.builtin.int_.prototype.nb$reflected_lshift = function (other) {
  552. if (other instanceof Sk.builtin.int_) {
  553. return other.nb$lshift(this);
  554. }
  555. return Sk.builtin.NotImplemented.NotImplemented$;
  556. };
  557. /**
  558. * Compute the bitwise right shift of this instance by a Python object (i.e. this >> other).
  559. *
  560. * Returns NotImplemented if bitwise right shift operation between int and other type is unsupported.
  561. *
  562. * Javscript function, returns Python object.
  563. *
  564. * @param {!Sk.builtin.object} other The Python object by which to right shift
  565. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the right shift
  566. */
  567. Sk.builtin.int_.prototype.nb$rshift = function (other) {
  568. var thisAsLong;
  569. if (other instanceof Sk.builtin.int_) {
  570. var tmp;
  571. var shift = Sk.builtin.asnum$(other);
  572. if (shift !== undefined) {
  573. if (shift &lt; 0) {
  574. throw new Sk.builtin.ValueError("negative shift count");
  575. }
  576. tmp = this.v >> shift;
  577. if ((this.v > 0) &amp;&amp; (tmp &lt; 0)) {
  578. // Fix incorrect sign extension
  579. tmp = tmp &amp; (Math.pow(2, 32 - shift) - 1);
  580. }
  581. }
  582. if (tmp !== undefined) {
  583. tmp = /** @type {number} */ (tmp);
  584. return new Sk.builtin.int_(tmp);
  585. }
  586. }
  587. if (other instanceof Sk.builtin.lng) {
  588. thisAsLong = new Sk.builtin.lng(this.v);
  589. return thisAsLong.nb$rshift(other);
  590. }
  591. return Sk.builtin.NotImplemented.NotImplemented$;
  592. };
  593. Sk.builtin.int_.prototype.nb$reflected_rshift = function (other) {
  594. if (other instanceof Sk.builtin.int_) {
  595. return other.nb$rshift(this);
  596. }
  597. return Sk.builtin.NotImplemented.NotImplemented$;
  598. };
  599. /**
  600. * Compute the bitwise inverse of this instance (i.e. ~this).
  601. *
  602. * Javscript function, returns Python object.
  603. *
  604. * @return {Sk.builtin.int_} The result of the inversion
  605. */
  606. Sk.builtin.int_.prototype.nb$invert = function () {
  607. return new Sk.builtin.int_(~this.v);
  608. };
  609. /** @override */
  610. Sk.builtin.int_.prototype.nb$inplace_add = Sk.builtin.int_.prototype.nb$add;
  611. /** @override */
  612. Sk.builtin.int_.prototype.nb$inplace_subtract = Sk.builtin.int_.prototype.nb$subtract;
  613. /** @override */
  614. Sk.builtin.int_.prototype.nb$inplace_multiply = Sk.builtin.int_.prototype.nb$multiply;
  615. /** @override */
  616. Sk.builtin.int_.prototype.nb$inplace_divide = Sk.builtin.int_.prototype.nb$divide;
  617. /** @override */
  618. Sk.builtin.int_.prototype.nb$inplace_remainder = Sk.builtin.int_.prototype.nb$remainder;
  619. /** @override */
  620. Sk.builtin.int_.prototype.nb$inplace_floor_divide = Sk.builtin.int_.prototype.nb$floor_divide;
  621. /** @override */
  622. Sk.builtin.int_.prototype.nb$inplace_power = Sk.builtin.int_.prototype.nb$power;
  623. /**
  624. * @function
  625. * @name nb$inplace_and
  626. * @memberOf Sk.builtin.int_.prototype
  627. * @description
  628. * Compute the bitwise AND of this instance and a Python object (i.e. this &amp;= other).
  629. *
  630. * Returns NotImplemented if inplace bitwise AND operation between int and other type is unsupported.
  631. *
  632. * Javscript function, returns Python object.
  633. *
  634. * @param {!Sk.builtin.object} other The Python object to AND with this one
  635. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the conjunction
  636. */
  637. Sk.builtin.int_.prototype.nb$inplace_and = Sk.builtin.int_.prototype.nb$and;
  638. /**
  639. * @function
  640. * @name nb$inplace_or
  641. * @memberOf Sk.builtin.int_.prototype
  642. * @description
  643. * Compute the bitwise OR of this instance and a Python object (i.e. this |= other).
  644. *
  645. * Returns NotImplemented if inplace bitwise OR operation between int and other type is unsupported.
  646. *
  647. * Javscript function, returns Python object.
  648. *
  649. * @param {!Sk.builtin.object} other The Python object to OR with this one
  650. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the disjunction
  651. */
  652. Sk.builtin.int_.prototype.nb$inplace_or = Sk.builtin.int_.prototype.nb$or;
  653. /**
  654. * @function
  655. * @name nb$inplace_xor
  656. * @memberOf Sk.builtin.int_.prototype
  657. * @description
  658. * Compute the bitwise XOR of this instance and a Python object (i.e. this ^= other).
  659. *
  660. * Returns NotImplemented if inplace bitwise XOR operation between int and other type is unsupported.
  661. *
  662. * Javscript function, returns Python object.
  663. *
  664. * @param {!Sk.builtin.object} other The Python object to XOR with this one
  665. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the exclusive disjunction
  666. */
  667. Sk.builtin.int_.prototype.nb$inplace_xor = Sk.builtin.int_.prototype.nb$xor;
  668. /**
  669. * @function
  670. * @name nb$inplace_lshift
  671. * @memberOf Sk.builtin.int_.prototype
  672. * @description
  673. * Compute the bitwise left shift of this instance by a Python object (i.e. this &lt;&lt;= other).
  674. *
  675. * Returns NotImplemented if inplace bitwise left shift operation between int and other type is unsupported.
  676. *
  677. * Javscript function, returns Python object.
  678. *
  679. * @param {!Sk.builtin.object} other The Python object by which to left shift
  680. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the left shift
  681. */
  682. Sk.builtin.int_.prototype.nb$inplace_lshift = Sk.builtin.int_.prototype.nb$lshift;
  683. /**
  684. * @function
  685. * @name nb$inplace_rshift
  686. * @memberOf Sk.builtin.int_.prototype
  687. * @description
  688. * Compute the bitwise right shift of this instance by a Python object (i.e. this >>= other).
  689. *
  690. * Returns NotImplemented if inplace bitwise right shift operation between int and other type is unsupported.
  691. *
  692. * Javscript function, returns Python object.
  693. *
  694. * @param {!Sk.builtin.object} other The Python object by which to right shift
  695. * @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the right shift
  696. */
  697. Sk.builtin.int_.prototype.nb$inplace_rshift = Sk.builtin.int_.prototype.nb$rshift;
  698. /**
  699. * @override
  700. *
  701. * @return {Sk.builtin.int_} A copy of this instance with the value negated.
  702. */
  703. Sk.builtin.int_.prototype.nb$negative = function () {
  704. return new Sk.builtin.int_(-this.v);
  705. };
  706. /** @override */
  707. Sk.builtin.int_.prototype.nb$positive = function () {
  708. return this.clone();
  709. };
  710. /** @override */
  711. Sk.builtin.int_.prototype.nb$nonzero = function () {
  712. return this.v !== 0;
  713. };
  714. /** @override */
  715. Sk.builtin.int_.prototype.nb$isnegative = function () {
  716. return this.v &lt; 0;
  717. };
  718. /** @override */
  719. Sk.builtin.int_.prototype.nb$ispositive = function () {
  720. return this.v >= 0;
  721. };
  722. /**
  723. * Compare this instance's value to another Python object's value.
  724. *
  725. * Returns NotImplemented if comparison between int and other type is unsupported.
  726. *
  727. * Javscript function, returns Javascript object or Sk.builtin.NotImplemented.
  728. *
  729. * @return {(number|Sk.builtin.NotImplemented)} negative if this &lt; other, zero if this == other, positive if this > other
  730. */
  731. Sk.builtin.int_.prototype.numberCompare = function (other) {
  732. if (other instanceof Sk.builtin.int_) {
  733. return this.v - other.v;
  734. }
  735. if (other instanceof Sk.builtin.lng) {
  736. return -other.longCompare(this);
  737. }
  738. if (other instanceof Sk.builtin.float_) {
  739. return -other.numberCompare(this);
  740. }
  741. return Sk.builtin.NotImplemented.NotImplemented$;
  742. };
  743. // Despite what jshint may want us to do, these two functions need to remain
  744. // as == and != Unless you modify the logic of numberCompare do not change
  745. // these.
  746. /** @override */
  747. Sk.builtin.int_.prototype.ob$eq = function (other) {
  748. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  749. other instanceof Sk.builtin.float_) {
  750. return new Sk.builtin.bool(this.numberCompare(other) == 0); //jshint ignore:line
  751. } else if (other instanceof Sk.builtin.none) {
  752. return Sk.builtin.bool.false$;
  753. } else {
  754. return Sk.builtin.NotImplemented.NotImplemented$;
  755. }
  756. };
  757. /** @override */
  758. Sk.builtin.int_.prototype.ob$ne = function (other) {
  759. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  760. other instanceof Sk.builtin.float_) {
  761. return new Sk.builtin.bool(this.numberCompare(other) != 0); //jshint ignore:line
  762. } else if (other instanceof Sk.builtin.none) {
  763. return Sk.builtin.bool.true$;
  764. } else {
  765. return Sk.builtin.NotImplemented.NotImplemented$;
  766. }
  767. };
  768. /** @override */
  769. Sk.builtin.int_.prototype.ob$lt = function (other) {
  770. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  771. other instanceof Sk.builtin.float_) {
  772. return new Sk.builtin.bool(this.numberCompare(other) &lt; 0);
  773. } else {
  774. return Sk.builtin.NotImplemented.NotImplemented$;
  775. }
  776. };
  777. /** @override */
  778. Sk.builtin.int_.prototype.ob$le = function (other) {
  779. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  780. other instanceof Sk.builtin.float_) {
  781. return new Sk.builtin.bool(this.numberCompare(other) &lt;= 0);
  782. } else {
  783. return Sk.builtin.NotImplemented.NotImplemented$;
  784. }
  785. };
  786. /** @override */
  787. Sk.builtin.int_.prototype.ob$gt = function (other) {
  788. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  789. other instanceof Sk.builtin.float_) {
  790. return new Sk.builtin.bool(this.numberCompare(other) > 0);
  791. } else {
  792. return Sk.builtin.NotImplemented.NotImplemented$;
  793. }
  794. };
  795. /** @override */
  796. Sk.builtin.int_.prototype.ob$ge = function (other) {
  797. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  798. other instanceof Sk.builtin.float_) {
  799. return new Sk.builtin.bool(this.numberCompare(other) >= 0);
  800. } else {
  801. return Sk.builtin.NotImplemented.NotImplemented$;
  802. }
  803. };
  804. /**
  805. * Round this instance to a given number of digits, or zero if omitted.
  806. *
  807. * Implements `__round__` dunder method.
  808. *
  809. * Javascript function, returns Python object.
  810. *
  811. * @param {Sk.builtin.int_} self This instance.
  812. * @param {Object|number=} ndigits The number of digits after the decimal point to which to round.
  813. * @return {Sk.builtin.int_} The rounded integer.
  814. */
  815. Sk.builtin.int_.prototype.__round__ = function (self, ndigits) {
  816. Sk.builtin.pyCheckArgs("__round__", arguments, 1, 2);
  817. var result, multiplier, number;
  818. if ((ndigits !== undefined) &amp;&amp; !Sk.misceval.isIndex(ndigits)) {
  819. throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(ndigits) + "' object cannot be interpreted as an index");
  820. }
  821. if (ndigits === undefined) {
  822. ndigits = 0;
  823. }
  824. number = Sk.builtin.asnum$(self);
  825. ndigits = Sk.misceval.asIndex(ndigits);
  826. multiplier = Math.pow(10, ndigits);
  827. result = Math.round(number * multiplier) / multiplier;
  828. return new Sk.builtin.int_(result);
  829. };
  830. /** @override */
  831. Sk.builtin.int_.prototype["$r"] = function () {
  832. return new Sk.builtin.str(this.str$(10, true));
  833. };
  834. /**
  835. * Return the string representation of this instance.
  836. *
  837. * Javascript function, returns Python object.
  838. *
  839. * @return {Sk.builtin.str} The Python string representation of this instance.
  840. */
  841. Sk.builtin.int_.prototype.tp$str = function () {
  842. return new Sk.builtin.str(this.str$(10, true));
  843. };
  844. /**
  845. * Convert this instance's value to a Javascript string.
  846. *
  847. * Javascript function, returns Javascript object.
  848. *
  849. * @param {number} base The base of the value.
  850. * @param {boolean} sign true if the value should be signed, false otherwise.
  851. * @return {string} The Javascript string representation of this instance.
  852. */
  853. Sk.builtin.int_.prototype.str$ = function (base, sign) {
  854. var tmp;
  855. var work;
  856. if (sign === undefined) {
  857. sign = true;
  858. }
  859. work = sign ? this.v : Math.abs(this.v);
  860. if (base === undefined || base === 10) {
  861. tmp = work.toString();
  862. } else {
  863. tmp = work.toString(base);
  864. }
  865. return tmp;
  866. };
  867. /**
  868. * Takes a JavaScript string and returns a number using the parser and negater
  869. * functions (for int/long right now)
  870. * @param {string} s Javascript string to convert to a number.
  871. * @param {number} base The base of the number.
  872. * @param {function(string, number): number} parser Function which should take
  873. * a string that is a postive number which only contains characters that are
  874. * valid in the given base and a base and return a number.
  875. * @param {function((number|Sk.builtin.biginteger)): number} negater Function which should take a
  876. * number and return its negation
  877. * @param {string} fname The name of the calling function, to be used in error messages
  878. * @return {number} The number equivalent of the string in the given base
  879. */
  880. Sk.str2number = function (s, base, parser, negater, fname) {
  881. "use strict";
  882. var origs = s,
  883. neg = false,
  884. i,
  885. ch,
  886. val;
  887. // strip whitespace from ends
  888. // s = s.trim();
  889. s = s.replace(/^\s+|\s+$/g, "");
  890. // check for minus sign
  891. if (s.charAt(0) === "-") {
  892. neg = true;
  893. s = s.substring(1);
  894. }
  895. // check for plus sign
  896. if (s.charAt(0) === "+") {
  897. s = s.substring(1);
  898. }
  899. if (base === undefined) {
  900. base = 10;
  901. } // default radix is 10, not dwim
  902. if (base &lt; 2 || base > 36) {
  903. if (base !== 0) {
  904. throw new Sk.builtin.ValueError(fname + "() base must be >= 2 and &lt;= 36");
  905. }
  906. }
  907. if (s.substring(0, 2).toLowerCase() === "0x") {
  908. if (base === 16 || base === 0) {
  909. s = s.substring(2);
  910. base = 16;
  911. } else if (base &lt; 34) {
  912. throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
  913. }
  914. } else if (s.substring(0, 2).toLowerCase() === "0b") {
  915. if (base === 2 || base === 0) {
  916. s = s.substring(2);
  917. base = 2;
  918. } else if (base &lt; 12) {
  919. throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
  920. }
  921. } else if (s.substring(0, 2).toLowerCase() === "0o") {
  922. if (base === 8 || base === 0) {
  923. s = s.substring(2);
  924. base = 8;
  925. } else if (base &lt; 25) {
  926. throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
  927. }
  928. } else if (s.charAt(0) === "0") {
  929. if (s === "0") {
  930. return 0;
  931. }
  932. if (base === 8 || base === 0) {
  933. base = 8;
  934. }
  935. }
  936. if (base === 0) {
  937. base = 10;
  938. }
  939. if (s.length === 0) {
  940. throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
  941. }
  942. // check all characters are valid
  943. for (i = 0; i &lt; s.length; i = i + 1) {
  944. ch = s.charCodeAt(i);
  945. val = base;
  946. if ((ch >= 48) &amp;&amp; (ch &lt;= 57)) {
  947. // 0-9
  948. val = ch - 48;
  949. } else if ((ch >= 65) &amp;&amp; (ch &lt;= 90)) {
  950. // A-Z
  951. val = ch - 65 + 10;
  952. } else if ((ch >= 97) &amp;&amp; (ch &lt;= 122)) {
  953. // a-z
  954. val = ch - 97 + 10;
  955. }
  956. if (val >= base) {
  957. throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
  958. }
  959. }
  960. // parse number
  961. val = parser(s, base);
  962. if (neg) {
  963. val = negater(val);
  964. }
  965. return val;
  966. };
  967. goog.exportSymbol("Sk.builtin.int_", Sk.builtin.int_);</code></pre>
  968. </article>
  969. </section>
  970. </div>
  971. <nav>
  972. <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>
  973. </nav>
  974. <br class="clear">
  975. <footer>
  976. 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)
  977. </footer>
  978. <script> prettyPrint(); </script>
  979. <script src="scripts/linenumber.js"> </script>
  980. </body>
  981. </html>