long.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /* global Sk: true, goog:true */
  2. // long aka "bignumber" implementation
  3. //
  4. // Using javascript BigInteger by Tom Wu
  5. /**
  6. * @constructor
  7. * Sk.builtin.lng
  8. *
  9. * @description
  10. * Constructor for Python long. Also used for builtin long().
  11. *
  12. * @extends {Sk.builtin.numtype}
  13. *
  14. * @param {*} x Object or number to convert to Python long.
  15. * @param {number=} base Optional base.
  16. * @return {Sk.builtin.lng} Python long
  17. */
  18. Sk.builtin.lng = function (x, base) { /* long is a reserved word */
  19. base = Sk.builtin.asnum$(base);
  20. if (!(this instanceof Sk.builtin.lng)) {
  21. return new Sk.builtin.lng(x, base);
  22. }
  23. if (x === undefined) {
  24. this.biginteger = new Sk.builtin.biginteger(0);
  25. return this;
  26. }
  27. if (x instanceof Sk.builtin.lng) {
  28. this.biginteger = x.biginteger.clone();
  29. return this;
  30. }
  31. if (x instanceof Sk.builtin.biginteger) {
  32. this.biginteger = x;
  33. return this;
  34. }
  35. if (x instanceof String || typeof x === "string") {
  36. return Sk.longFromStr(x, base);
  37. }
  38. if (x instanceof Sk.builtin.str) {
  39. return Sk.longFromStr(x.v, base);
  40. }
  41. if ((x !== undefined) && (!Sk.builtin.checkString(x) && !Sk.builtin.checkNumber(x))) {
  42. if (x === true) {
  43. x = 1;
  44. } else if (x === false) {
  45. x = 0;
  46. } else {
  47. throw new Sk.builtin.TypeError("long() argument must be a string or a number, not '" + Sk.abstr.typeName(x) + "'");
  48. }
  49. }
  50. x = Sk.builtin.asnum$nofloat(x);
  51. this.biginteger = new Sk.builtin.biginteger(x);
  52. return this;
  53. };
  54. Sk.abstr.setUpInheritance("long", Sk.builtin.lng, Sk.builtin.numtype);
  55. /* NOTE: See constants used for kwargs in constants.js */
  56. Sk.builtin.lng.prototype.tp$index = function () {
  57. return parseInt(this.str$(10, true), 10);
  58. };
  59. Sk.builtin.lng.prototype.tp$hash = function () {
  60. return new Sk.builtin.int_(this.tp$index());
  61. };
  62. Sk.builtin.lng.prototype.nb$int_ = function() {
  63. if (this.cantBeInt()) {
  64. return new Sk.builtin.lng(this);
  65. }
  66. return new Sk.builtin.int_(this.toInt$());
  67. };
  68. Sk.builtin.lng.prototype.__index__ = new Sk.builtin.func(function(self) {
  69. return self.nb$int_(self);
  70. });
  71. Sk.builtin.lng.prototype.nb$lng_ = function () {
  72. return this;
  73. };
  74. Sk.builtin.lng.prototype.nb$float_ = function() {
  75. return new Sk.builtin.float_(Sk.ffi.remapToJs(this));
  76. };
  77. // Threshold to determine when types should be converted to long
  78. //Sk.builtin.lng.threshold$ = Sk.builtin.int_.threshold$;
  79. Sk.builtin.lng.MAX_INT$ = new Sk.builtin.lng(Sk.builtin.int_.threshold$);
  80. Sk.builtin.lng.MIN_INT$ = new Sk.builtin.lng(-Sk.builtin.int_.threshold$);
  81. Sk.builtin.lng.prototype.cantBeInt = function () {
  82. return (this.longCompare(Sk.builtin.lng.MAX_INT$) > 0) || (this.longCompare(Sk.builtin.lng.MIN_INT$) < 0);
  83. };
  84. Sk.builtin.lng.fromInt$ = function (ival) {
  85. return new Sk.builtin.lng(ival);
  86. };
  87. // js string (not Sk.builtin.str) -> long. used to create longs in transformer, respects
  88. // 0x, 0o, 0b, etc.
  89. Sk.longFromStr = function (s, base) {
  90. // l/L are valid digits with base >= 22
  91. // goog.asserts.assert(s.charAt(s.length - 1) !== "L" && s.charAt(s.length - 1) !== 'l', "L suffix should be removed before here");
  92. var parser = function (s, base) {
  93. if (base === 10) {
  94. return new Sk.builtin.biginteger(s);
  95. }
  96. return new Sk.builtin.biginteger(s, base);
  97. },
  98. biginteger = Sk.str2number(s, base, parser, function (x) {
  99. return x.negate();
  100. }, "long");
  101. return new Sk.builtin.lng(biginteger);
  102. };
  103. goog.exportSymbol("Sk.longFromStr", Sk.longFromStr);
  104. Sk.builtin.lng.prototype.toInt$ = function () {
  105. return this.biginteger.intValue();
  106. };
  107. Sk.builtin.lng.prototype.clone = function () {
  108. return new Sk.builtin.lng(this);
  109. };
  110. Sk.builtin.lng.prototype.conjugate = new Sk.builtin.func(function (self) {
  111. return self.clone();
  112. });
  113. Sk.builtin.lng.prototype.nb$add = function (other) {
  114. var thisAsFloat;
  115. if (other instanceof Sk.builtin.float_) {
  116. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  117. return thisAsFloat.nb$add(other);
  118. }
  119. if (other instanceof Sk.builtin.int_) {
  120. // Promote an int to long
  121. other = new Sk.builtin.lng(other.v);
  122. }
  123. if (other instanceof Sk.builtin.lng) {
  124. return new Sk.builtin.lng(this.biginteger.add(other.biginteger));
  125. }
  126. if (other instanceof Sk.builtin.biginteger) {
  127. return new Sk.builtin.lng(this.biginteger.add(other));
  128. }
  129. return Sk.builtin.NotImplemented.NotImplemented$;
  130. };
  131. /** @override */
  132. Sk.builtin.lng.prototype.nb$reflected_add = function (other) {
  133. // Should not automatically call this.nb$add, as nb$add may have
  134. // been overridden by a subclass
  135. return Sk.builtin.lng.prototype.nb$add.call(this, other);
  136. };
  137. Sk.builtin.lng.prototype.nb$inplace_add = Sk.builtin.lng.prototype.nb$add;
  138. Sk.builtin.lng.prototype.nb$subtract = function (other) {
  139. var thisAsFloat;
  140. if (other instanceof Sk.builtin.float_) {
  141. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  142. return thisAsFloat.nb$subtract(other);
  143. }
  144. if (other instanceof Sk.builtin.int_) {
  145. // Promote an int to long
  146. other = new Sk.builtin.lng(other.v);
  147. }
  148. if (other instanceof Sk.builtin.lng) {
  149. return new Sk.builtin.lng(this.biginteger.subtract(other.biginteger));
  150. }
  151. if (other instanceof Sk.builtin.biginteger) {
  152. return new Sk.builtin.lng(this.biginteger.subtract(other));
  153. }
  154. return Sk.builtin.NotImplemented.NotImplemented$;
  155. };
  156. /** @override */
  157. Sk.builtin.lng.prototype.nb$reflected_subtract = function (other) {
  158. // Should not automatically call this.nb$add, as nb$add may have
  159. // been overridden by a subclass
  160. var negative_this = this.nb$negative();
  161. return Sk.builtin.lng.prototype.nb$add.call(negative_this, other);
  162. };
  163. Sk.builtin.lng.prototype.nb$inplace_subtract = Sk.builtin.lng.prototype.nb$subtract;
  164. Sk.builtin.lng.prototype.nb$multiply = function (other) {
  165. var thisAsFloat;
  166. if (other instanceof Sk.builtin.float_) {
  167. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  168. return thisAsFloat.nb$multiply(other);
  169. }
  170. if (other instanceof Sk.builtin.int_) {
  171. other = new Sk.builtin.lng(other.v);
  172. }
  173. if (other instanceof Sk.builtin.lng) {
  174. return new Sk.builtin.lng(this.biginteger.multiply(other.biginteger));
  175. }
  176. if (other instanceof Sk.builtin.biginteger) {
  177. return new Sk.builtin.lng(this.biginteger.multiply(other));
  178. }
  179. return Sk.builtin.NotImplemented.NotImplemented$;
  180. };
  181. /** @override */
  182. Sk.builtin.lng.prototype.nb$reflected_multiply = function (other) {
  183. // Should not automatically call this.nb$multiply, as nb$multiply may have
  184. // been overridden by a subclass
  185. return Sk.builtin.lng.prototype.nb$multiply.call(this, other);
  186. };
  187. Sk.builtin.lng.prototype.nb$inplace_multiply = Sk.builtin.lng.prototype.nb$multiply;
  188. Sk.builtin.lng.prototype.nb$divide = function (other) {
  189. var thisAsFloat, thisneg, otherneg, result;
  190. if (other instanceof Sk.builtin.float_) {
  191. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  192. return thisAsFloat.nb$divide(other);
  193. }
  194. if (other instanceof Sk.builtin.int_) {
  195. // Promote an int to long
  196. other = new Sk.builtin.lng(other.v);
  197. }
  198. // Standard, long result mode
  199. if (other instanceof Sk.builtin.lng) {
  200. // Special logic to round DOWN towards negative infinity for negative results
  201. thisneg = this.nb$isnegative();
  202. otherneg = other.nb$isnegative();
  203. if ((thisneg && !otherneg) || (otherneg && !thisneg)) {
  204. result = this.biginteger.divideAndRemainder(other.biginteger);
  205. // If remainder is zero or positive, just return division result
  206. if (result[1].trueCompare(Sk.builtin.biginteger.ZERO) === 0) {
  207. // No remainder, just return result
  208. return new Sk.builtin.lng(result[0]);
  209. }
  210. // Reminder... subtract 1 from the result (like rounding to neg infinity)
  211. result = result[0].subtract(Sk.builtin.biginteger.ONE);
  212. return new Sk.builtin.lng(result);
  213. }
  214. return new Sk.builtin.lng(this.biginteger.divide(other.biginteger));
  215. }
  216. return Sk.builtin.NotImplemented.NotImplemented$;
  217. };
  218. Sk.builtin.lng.prototype.nb$reflected_divide = function (other) {
  219. var thisneg, otherneg, result;
  220. if (other instanceof Sk.builtin.int_) {
  221. // Promote an int to long
  222. other = new Sk.builtin.lng(other.v);
  223. }
  224. // Standard, long result mode
  225. if (other instanceof Sk.builtin.lng) {
  226. return other.nb$divide(this);
  227. }
  228. return Sk.builtin.NotImplemented.NotImplemented$;
  229. };
  230. Sk.builtin.lng.prototype.nb$floor_divide = function (other) {
  231. var thisAsFloat;
  232. if (other instanceof Sk.builtin.float_) {
  233. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  234. return thisAsFloat.nb$floor_divide(other);
  235. }
  236. if (other instanceof Sk.builtin.int_) {
  237. // Promote an int to long
  238. other = new Sk.builtin.lng(other.v);
  239. }
  240. // Standard, long result mode
  241. if (other instanceof Sk.builtin.lng) {
  242. return other.nb$divide(this);
  243. }
  244. return Sk.builtin.NotImplemented.NotImplemented$;
  245. };
  246. Sk.builtin.lng.prototype.nb$divmod = function (other) {
  247. if (other instanceof Sk.builtin.int_) {
  248. // Promote an int to long
  249. other = new Sk.builtin.lng(other.v);
  250. }
  251. if (other instanceof Sk.builtin.lng) {
  252. return new Sk.builtin.tuple([
  253. this.nb$floor_divide(other),
  254. this.nb$remainder(other)
  255. ]);
  256. }
  257. return Sk.builtin.NotImplemented.NotImplemented$;
  258. };
  259. Sk.builtin.lng.prototype.nb$reflected_divmod = function (other) {
  260. if (other instanceof Sk.builtin.int_) {
  261. // Promote an int to long
  262. other = new Sk.builtin.lng(other.v);
  263. }
  264. if (other instanceof Sk.builtin.lng) {
  265. return new Sk.builtin.tuple([
  266. other.nb$floor_divide(this),
  267. other.nb$remainder(this)
  268. ]);
  269. }
  270. return Sk.builtin.NotImplemented.NotImplemented$;
  271. };
  272. Sk.builtin.lng.prototype.nb$inplace_divide = Sk.builtin.lng.prototype.nb$divide;
  273. Sk.builtin.lng.prototype.nb$floor_divide = Sk.builtin.lng.prototype.nb$divide;
  274. Sk.builtin.lng.prototype.nb$reflected_floor_divide = Sk.builtin.lng.prototype.nb$reflected_divide;
  275. Sk.builtin.lng.prototype.nb$inplace_floor_divide = Sk.builtin.lng.prototype.nb$floor_divide;
  276. Sk.builtin.lng.prototype.nb$remainder = function (other) {
  277. var thisAsFloat, tmp;
  278. if (this.biginteger.trueCompare(Sk.builtin.biginteger.ZERO) === 0) {
  279. if (other instanceof Sk.builtin.float_) {
  280. return new Sk.builtin.float_(0);
  281. }
  282. return new Sk.builtin.lng(0);
  283. }
  284. if (other instanceof Sk.builtin.float_) {
  285. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  286. return thisAsFloat.nb$remainder(other);
  287. }
  288. if (other instanceof Sk.builtin.int_) {
  289. // Promote an int to long
  290. other = new Sk.builtin.lng(other.v);
  291. }
  292. if (other instanceof Sk.builtin.lng) {
  293. tmp = new Sk.builtin.lng(this.biginteger.remainder(other.biginteger));
  294. if (this.nb$isnegative()) {
  295. if (other.nb$ispositive() && tmp.nb$nonzero()) {
  296. tmp = tmp.nb$add(other).nb$remainder(other);
  297. }
  298. } else {
  299. if (other.nb$isnegative() && tmp.nb$nonzero()) {
  300. tmp = tmp.nb$add(other);
  301. }
  302. }
  303. return tmp;
  304. }
  305. return Sk.builtin.NotImplemented.NotImplemented$;
  306. };
  307. Sk.builtin.lng.prototype.nb$reflected_remainder = function (other) {
  308. if (other instanceof Sk.builtin.int_) {
  309. other = new Sk.builtin.lng(other.v);
  310. }
  311. if (other instanceof Sk.builtin.lng) {
  312. return other.nb$remainder(this);
  313. }
  314. return Sk.builtin.NotImplemented.NotImplemented$;
  315. };
  316. Sk.builtin.lng.prototype.nb$inplace_remainder = Sk.builtin.lng.prototype.nb$remainder;
  317. Sk.builtin.lng.prototype.nb$divmod = function (other) {
  318. var thisAsFloat;
  319. if (other === Sk.builtin.bool.true$) {
  320. other = new Sk.builtin.lng(1);
  321. }
  322. if (other === Sk.builtin.bool.false$) {
  323. other = new Sk.builtin.lng(0);
  324. }
  325. if (other instanceof Sk.builtin.int_) {
  326. other = new Sk.builtin.lng(other.v);
  327. }
  328. if (other instanceof Sk.builtin.lng) {
  329. return new Sk.builtin.tuple([
  330. this.nb$floor_divide(other),
  331. this.nb$remainder(other)
  332. ]);
  333. }
  334. if (other instanceof Sk.builtin.float_) {
  335. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  336. return thisAsFloat.nb$divmod(other);
  337. }
  338. return Sk.builtin.NotImplemented.NotImplemented$;
  339. };
  340. /**
  341. * @param {number|Object} n
  342. * @param {number|Object=} mod
  343. * @suppress {checkTypes}
  344. */
  345. Sk.builtin.lng.prototype.nb$power = function (n, mod) {
  346. var thisAsFloat;
  347. if (mod !== undefined) {
  348. n = new Sk.builtin.biginteger(Sk.builtin.asnum$(n));
  349. mod = new Sk.builtin.biginteger(Sk.builtin.asnum$(mod));
  350. return new Sk.builtin.lng(this.biginteger.modPowInt(n, mod));
  351. }
  352. if (n instanceof Sk.builtin.float_ ||
  353. (n instanceof Sk.builtin.int_ && n.v < 0)) {
  354. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  355. return thisAsFloat.nb$power(n);
  356. }
  357. if (n instanceof Sk.builtin.int_) {
  358. // Promote an int to long
  359. n = new Sk.builtin.lng(n.v);
  360. }
  361. if (n instanceof Sk.builtin.lng) {
  362. if (mod !== undefined) {
  363. n = new Sk.builtin.biginteger(Sk.builtin.asnum$(n));
  364. mod = new Sk.builtin.biginteger(Sk.builtin.asnum$(mod));
  365. return new Sk.builtin.lng(this.biginteger.modPowInt(n, mod));
  366. }
  367. if (n.nb$isnegative()) {
  368. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  369. return thisAsFloat.nb$power(n);
  370. }
  371. return new Sk.builtin.lng(this.biginteger.pow(n.biginteger));
  372. }
  373. if (n instanceof Sk.builtin.biginteger) {
  374. if (mod !== undefined) {
  375. mod = new Sk.builtin.biginteger(Sk.builtin.asnum$(mod));
  376. return new Sk.builtin.lng(this.biginteger.modPowInt(n, mod));
  377. }
  378. if (n.isnegative()) {
  379. thisAsFloat = new Sk.builtin.float_(this.str$(10, true));
  380. return thisAsFloat.nb$power(n);
  381. }
  382. return new Sk.builtin.lng(this.biginteger.pow(n));
  383. }
  384. return Sk.builtin.NotImplemented.NotImplemented$;
  385. };
  386. Sk.builtin.lng.prototype.nb$reflected_power = function (n, mod) {
  387. if (n instanceof Sk.builtin.int_) {
  388. // Promote an int to long
  389. n = new Sk.builtin.lng(n.v);
  390. }
  391. if (n instanceof Sk.builtin.lng) {
  392. return n.nb$power(this, mod);
  393. }
  394. return Sk.builtin.NotImplemented.NotImplemented$;
  395. };
  396. Sk.builtin.lng.prototype.nb$inplace_power = Sk.builtin.lng.prototype.nb$power;
  397. /**
  398. * Compute the absolute value of this instance and return.
  399. *
  400. * Javascript function, returns Python object.
  401. *
  402. * @return {Sk.builtin.lng} The absolute value
  403. */
  404. Sk.builtin.lng.prototype.nb$abs = function () {
  405. return new Sk.builtin.lng(this.biginteger.bnAbs());
  406. };
  407. Sk.builtin.lng.prototype.nb$lshift = function (other) {
  408. if (other instanceof Sk.builtin.int_) {
  409. // Promote an int to long
  410. other = new Sk.builtin.lng(other.v);
  411. }
  412. if (other instanceof Sk.builtin.lng) {
  413. if (other.biginteger.signum() < 0) {
  414. throw new Sk.builtin.ValueError("negative shift count");
  415. }
  416. return new Sk.builtin.lng(this.biginteger.shiftLeft(other.biginteger));
  417. }
  418. if (other instanceof Sk.builtin.biginteger) {
  419. if (other.signum() < 0) {
  420. throw new Sk.builtin.ValueError("negative shift count");
  421. }
  422. return new Sk.builtin.lng(this.biginteger.shiftLeft(other));
  423. }
  424. return Sk.builtin.NotImplemented.NotImplemented$;
  425. };
  426. Sk.builtin.lng.prototype.nb$reflected_lshift = function (other) {
  427. if (other instanceof Sk.builtin.int_) {
  428. // Promote an int to long
  429. other = new Sk.builtin.lng(other.v);
  430. }
  431. if (other instanceof Sk.builtin.lng) {
  432. return other.nb$lshift(this);
  433. }
  434. return Sk.builtin.NotImplemented.NotImplemented$;
  435. };
  436. Sk.builtin.lng.prototype.nb$inplace_lshift = Sk.builtin.lng.prototype.nb$lshift;
  437. Sk.builtin.lng.prototype.nb$rshift = function (other) {
  438. if (other instanceof Sk.builtin.int_) {
  439. // Promote an int to long
  440. other = new Sk.builtin.lng(other.v);
  441. }
  442. if (other instanceof Sk.builtin.lng) {
  443. if (other.biginteger.signum() < 0) {
  444. throw new Sk.builtin.ValueError("negative shift count");
  445. }
  446. return new Sk.builtin.lng(this.biginteger.shiftRight(other.biginteger));
  447. }
  448. if (other instanceof Sk.builtin.biginteger) {
  449. if (other.signum() < 0) {
  450. throw new Sk.builtin.ValueError("negative shift count");
  451. }
  452. return new Sk.builtin.lng(this.biginteger.shiftRight(other));
  453. }
  454. return Sk.builtin.NotImplemented.NotImplemented$;
  455. };
  456. Sk.builtin.lng.prototype.nb$reflected_rshift = function (other) {
  457. if (other instanceof Sk.builtin.int_) {
  458. // Promote an int to long
  459. other = new Sk.builtin.lng(other.v);
  460. }
  461. if (other instanceof Sk.builtin.lng) {
  462. return other.nb$rshift(this);
  463. }
  464. return Sk.builtin.NotImplemented.NotImplemented$;
  465. };
  466. Sk.builtin.lng.prototype.nb$inplace_rshift = Sk.builtin.lng.prototype.nb$rshift;
  467. Sk.builtin.lng.prototype.nb$and = function (other) {
  468. if (other instanceof Sk.builtin.int_) {
  469. // Promote an int to long
  470. other = new Sk.builtin.lng(other.v);
  471. }
  472. if (other instanceof Sk.builtin.lng) {
  473. return new Sk.builtin.lng(this.biginteger.and(other.biginteger));
  474. }
  475. if (other instanceof Sk.builtin.biginteger) {
  476. return new Sk.builtin.lng(this.biginteger.and(other));
  477. }
  478. return Sk.builtin.NotImplemented.NotImplemented$;
  479. };
  480. Sk.builtin.lng.prototype.nb$reflected_and = Sk.builtin.lng.prototype.nb$and;
  481. Sk.builtin.lng.prototype.nb$inplace_and = Sk.builtin.lng.prototype.nb$and;
  482. Sk.builtin.lng.prototype.nb$or = function (other) {
  483. if (other instanceof Sk.builtin.int_) {
  484. // Promote an int to long
  485. other = new Sk.builtin.lng(other.v);
  486. }
  487. if (other instanceof Sk.builtin.lng) {
  488. return new Sk.builtin.lng(this.biginteger.or(other.biginteger));
  489. }
  490. if (other instanceof Sk.builtin.biginteger) {
  491. return new Sk.builtin.lng(this.biginteger.or(other));
  492. }
  493. return Sk.builtin.NotImplemented.NotImplemented$;
  494. };
  495. Sk.builtin.lng.prototype.nb$reflected_or = Sk.builtin.lng.prototype.nb$or;
  496. Sk.builtin.lng.prototype.nb$inplace_or = Sk.builtin.lng.prototype.nb$or;
  497. Sk.builtin.lng.prototype.nb$xor = function (other) {
  498. if (other instanceof Sk.builtin.int_) {
  499. // Promote an int to long
  500. other = new Sk.builtin.lng(other.v);
  501. }
  502. if (other instanceof Sk.builtin.lng) {
  503. return new Sk.builtin.lng(this.biginteger.xor(other.biginteger));
  504. }
  505. if (other instanceof Sk.builtin.biginteger) {
  506. return new Sk.builtin.lng(this.biginteger.xor(other));
  507. }
  508. return Sk.builtin.NotImplemented.NotImplemented$;
  509. };
  510. Sk.builtin.lng.prototype.nb$reflected_xor = Sk.builtin.lng.prototype.nb$xor;
  511. Sk.builtin.lng.prototype.nb$inplace_xor = Sk.builtin.lng.prototype.nb$xor;
  512. /**
  513. * @override
  514. *
  515. * @return {Sk.builtin.lng} A copy of this instance with the value negated.
  516. */
  517. Sk.builtin.lng.prototype.nb$negative = function () {
  518. return new Sk.builtin.lng(this.biginteger.negate());
  519. };
  520. Sk.builtin.lng.prototype.nb$invert = function () {
  521. return new Sk.builtin.lng(this.biginteger.not());
  522. };
  523. Sk.builtin.lng.prototype.nb$positive = function () {
  524. return this.clone();
  525. };
  526. Sk.builtin.lng.prototype.nb$nonzero = function () {
  527. return this.biginteger.trueCompare(Sk.builtin.biginteger.ZERO) !== 0;
  528. };
  529. Sk.builtin.lng.prototype.nb$isnegative = function () {
  530. return this.biginteger.isnegative();
  531. };
  532. Sk.builtin.lng.prototype.nb$ispositive = function () {
  533. return !this.biginteger.isnegative();
  534. };
  535. Sk.builtin.lng.prototype.longCompare = function (other) {
  536. var otherAsLong, thisAsFloat;
  537. if (typeof other === "number") {
  538. other = new Sk.builtin.lng(other);
  539. }
  540. if (other instanceof Sk.builtin.int_ ||
  541. (other instanceof Sk.builtin.float_ && other.v % 1 === 0)) {
  542. otherAsLong = new Sk.builtin.lng(other.v);
  543. return this.longCompare(otherAsLong);
  544. }
  545. if (other instanceof Sk.builtin.float_) {
  546. thisAsFloat = new Sk.builtin.float_(this);
  547. return thisAsFloat.numberCompare(other);
  548. }
  549. if (other instanceof Sk.builtin.lng) {
  550. return this.biginteger.subtract(other.biginteger);
  551. } else if (other instanceof Sk.builtin.biginteger) {
  552. return this.biginteger.subtract(other);
  553. }
  554. return Sk.builtin.NotImplemented.NotImplemented$;
  555. };
  556. //tests fail if ===
  557. Sk.builtin.lng.prototype.ob$eq = function (other) {
  558. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  559. other instanceof Sk.builtin.float_) {
  560. return new Sk.builtin.bool(this.longCompare(other) == 0); //jshint ignore:line
  561. } else if (other instanceof Sk.builtin.none) {
  562. return Sk.builtin.bool.false$;
  563. } else {
  564. return Sk.builtin.NotImplemented.NotImplemented$;
  565. }
  566. };
  567. Sk.builtin.lng.prototype.ob$ne = function (other) {
  568. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  569. other instanceof Sk.builtin.float_) {
  570. return new Sk.builtin.bool(this.longCompare(other) != 0); //jshint ignore:line
  571. } else if (other instanceof Sk.builtin.none) {
  572. return Sk.builtin.bool.true$;
  573. } else {
  574. return Sk.builtin.NotImplemented.NotImplemented$;
  575. }
  576. };
  577. Sk.builtin.lng.prototype.ob$lt = function (other) {
  578. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  579. other instanceof Sk.builtin.float_) {
  580. return new Sk.builtin.bool(this.longCompare(other) < 0);
  581. } else {
  582. return Sk.builtin.NotImplemented.NotImplemented$;
  583. }
  584. };
  585. Sk.builtin.lng.prototype.ob$le = function (other) {
  586. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  587. other instanceof Sk.builtin.float_) {
  588. return new Sk.builtin.bool(this.longCompare(other) <= 0);
  589. } else {
  590. return Sk.builtin.NotImplemented.NotImplemented$;
  591. }
  592. };
  593. Sk.builtin.lng.prototype.ob$gt = function (other) {
  594. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  595. other instanceof Sk.builtin.float_) {
  596. return new Sk.builtin.bool(this.longCompare(other) > 0);
  597. } else {
  598. return Sk.builtin.NotImplemented.NotImplemented$;
  599. }
  600. };
  601. Sk.builtin.lng.prototype.ob$ge = function (other) {
  602. if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
  603. other instanceof Sk.builtin.float_) {
  604. return new Sk.builtin.bool(this.longCompare(other) >= 0);
  605. } else {
  606. return Sk.builtin.NotImplemented.NotImplemented$;
  607. }
  608. };
  609. Sk.builtin.lng.prototype.$r = function () {
  610. return new Sk.builtin.str(this.str$(10, true) + "L");
  611. };
  612. Sk.builtin.lng.prototype.tp$str = function () {
  613. return new Sk.builtin.str(this.str$(10, true));
  614. };
  615. Sk.builtin.lng.prototype.str$ = function (base, sign) {
  616. var work;
  617. if (sign === undefined) {
  618. sign = true;
  619. }
  620. work = sign ? this.biginteger : this.biginteger.abs();
  621. if (base === undefined || base === 10) {
  622. return work.toString();
  623. }
  624. // Another base... convert...
  625. return work.toString(base);
  626. };