long.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Long = void 0;
  4. var utils_1 = require("./parser/utils");
  5. /**
  6. * wasm optimizations, to do native i64 multiplication and divide
  7. */
  8. var wasm = undefined;
  9. try {
  10. wasm = new WebAssembly.Instance(new WebAssembly.Module(
  11. // prettier-ignore
  12. new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11])), {}).exports;
  13. }
  14. catch (_a) {
  15. // no wasm support
  16. }
  17. var TWO_PWR_16_DBL = 1 << 16;
  18. var TWO_PWR_24_DBL = 1 << 24;
  19. var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
  20. var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
  21. var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
  22. /** A cache of the Long representations of small integer values. */
  23. var INT_CACHE = {};
  24. /** A cache of the Long representations of small unsigned integer values. */
  25. var UINT_CACHE = {};
  26. /**
  27. * A class representing a 64-bit integer
  28. * @public
  29. * @category BSONType
  30. * @remarks
  31. * The internal representation of a long is the two given signed, 32-bit values.
  32. * We use 32-bit pieces because these are the size of integers on which
  33. * Javascript performs bit-operations. For operations like addition and
  34. * multiplication, we split each number into 16 bit pieces, which can easily be
  35. * multiplied within Javascript's floating-point representation without overflow
  36. * or change in sign.
  37. * In the algorithms below, we frequently reduce the negative case to the
  38. * positive case by negating the input(s) and then post-processing the result.
  39. * Note that we must ALWAYS check specially whether those values are MIN_VALUE
  40. * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
  41. * a positive number, it overflows back into a negative). Not handling this
  42. * case would often result in infinite recursion.
  43. * Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
  44. */
  45. var Long = /** @class */ (function () {
  46. /**
  47. * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
  48. * See the from* functions below for more convenient ways of constructing Longs.
  49. *
  50. * Acceptable signatures are:
  51. * - Long(low, high, unsigned?)
  52. * - Long(bigint, unsigned?)
  53. * - Long(string, unsigned?)
  54. *
  55. * @param low - The low (signed) 32 bits of the long
  56. * @param high - The high (signed) 32 bits of the long
  57. * @param unsigned - Whether unsigned or not, defaults to signed
  58. */
  59. function Long(low, high, unsigned) {
  60. if (low === void 0) { low = 0; }
  61. if (!(this instanceof Long))
  62. return new Long(low, high, unsigned);
  63. if (typeof low === 'bigint') {
  64. Object.assign(this, Long.fromBigInt(low, !!high));
  65. }
  66. else if (typeof low === 'string') {
  67. Object.assign(this, Long.fromString(low, !!high));
  68. }
  69. else {
  70. this.low = low | 0;
  71. this.high = high | 0;
  72. this.unsigned = !!unsigned;
  73. }
  74. Object.defineProperty(this, '__isLong__', {
  75. value: true,
  76. configurable: false,
  77. writable: false,
  78. enumerable: false
  79. });
  80. }
  81. /**
  82. * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits.
  83. * Each is assumed to use 32 bits.
  84. * @param lowBits - The low 32 bits
  85. * @param highBits - The high 32 bits
  86. * @param unsigned - Whether unsigned or not, defaults to signed
  87. * @returns The corresponding Long value
  88. */
  89. Long.fromBits = function (lowBits, highBits, unsigned) {
  90. return new Long(lowBits, highBits, unsigned);
  91. };
  92. /**
  93. * Returns a Long representing the given 32 bit integer value.
  94. * @param value - The 32 bit integer in question
  95. * @param unsigned - Whether unsigned or not, defaults to signed
  96. * @returns The corresponding Long value
  97. */
  98. Long.fromInt = function (value, unsigned) {
  99. var obj, cachedObj, cache;
  100. if (unsigned) {
  101. value >>>= 0;
  102. if ((cache = 0 <= value && value < 256)) {
  103. cachedObj = UINT_CACHE[value];
  104. if (cachedObj)
  105. return cachedObj;
  106. }
  107. obj = Long.fromBits(value, (value | 0) < 0 ? -1 : 0, true);
  108. if (cache)
  109. UINT_CACHE[value] = obj;
  110. return obj;
  111. }
  112. else {
  113. value |= 0;
  114. if ((cache = -128 <= value && value < 128)) {
  115. cachedObj = INT_CACHE[value];
  116. if (cachedObj)
  117. return cachedObj;
  118. }
  119. obj = Long.fromBits(value, value < 0 ? -1 : 0, false);
  120. if (cache)
  121. INT_CACHE[value] = obj;
  122. return obj;
  123. }
  124. };
  125. /**
  126. * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
  127. * @param value - The number in question
  128. * @param unsigned - Whether unsigned or not, defaults to signed
  129. * @returns The corresponding Long value
  130. */
  131. Long.fromNumber = function (value, unsigned) {
  132. if (isNaN(value))
  133. return unsigned ? Long.UZERO : Long.ZERO;
  134. if (unsigned) {
  135. if (value < 0)
  136. return Long.UZERO;
  137. if (value >= TWO_PWR_64_DBL)
  138. return Long.MAX_UNSIGNED_VALUE;
  139. }
  140. else {
  141. if (value <= -TWO_PWR_63_DBL)
  142. return Long.MIN_VALUE;
  143. if (value + 1 >= TWO_PWR_63_DBL)
  144. return Long.MAX_VALUE;
  145. }
  146. if (value < 0)
  147. return Long.fromNumber(-value, unsigned).neg();
  148. return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
  149. };
  150. /**
  151. * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
  152. * @param value - The number in question
  153. * @param unsigned - Whether unsigned or not, defaults to signed
  154. * @returns The corresponding Long value
  155. */
  156. Long.fromBigInt = function (value, unsigned) {
  157. return Long.fromString(value.toString(), unsigned);
  158. };
  159. /**
  160. * Returns a Long representation of the given string, written using the specified radix.
  161. * @param str - The textual representation of the Long
  162. * @param unsigned - Whether unsigned or not, defaults to signed
  163. * @param radix - The radix in which the text is written (2-36), defaults to 10
  164. * @returns The corresponding Long value
  165. */
  166. Long.fromString = function (str, unsigned, radix) {
  167. if (str.length === 0)
  168. throw Error('empty string');
  169. if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
  170. return Long.ZERO;
  171. if (typeof unsigned === 'number') {
  172. // For goog.math.long compatibility
  173. (radix = unsigned), (unsigned = false);
  174. }
  175. else {
  176. unsigned = !!unsigned;
  177. }
  178. radix = radix || 10;
  179. if (radix < 2 || 36 < radix)
  180. throw RangeError('radix');
  181. var p;
  182. if ((p = str.indexOf('-')) > 0)
  183. throw Error('interior hyphen');
  184. else if (p === 0) {
  185. return Long.fromString(str.substring(1), unsigned, radix).neg();
  186. }
  187. // Do several (8) digits each time through the loop, so as to
  188. // minimize the calls to the very expensive emulated div.
  189. var radixToPower = Long.fromNumber(Math.pow(radix, 8));
  190. var result = Long.ZERO;
  191. for (var i = 0; i < str.length; i += 8) {
  192. var size = Math.min(8, str.length - i), value = parseInt(str.substring(i, i + size), radix);
  193. if (size < 8) {
  194. var power = Long.fromNumber(Math.pow(radix, size));
  195. result = result.mul(power).add(Long.fromNumber(value));
  196. }
  197. else {
  198. result = result.mul(radixToPower);
  199. result = result.add(Long.fromNumber(value));
  200. }
  201. }
  202. result.unsigned = unsigned;
  203. return result;
  204. };
  205. /**
  206. * Creates a Long from its byte representation.
  207. * @param bytes - Byte representation
  208. * @param unsigned - Whether unsigned or not, defaults to signed
  209. * @param le - Whether little or big endian, defaults to big endian
  210. * @returns The corresponding Long value
  211. */
  212. Long.fromBytes = function (bytes, unsigned, le) {
  213. return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
  214. };
  215. /**
  216. * Creates a Long from its little endian byte representation.
  217. * @param bytes - Little endian byte representation
  218. * @param unsigned - Whether unsigned or not, defaults to signed
  219. * @returns The corresponding Long value
  220. */
  221. Long.fromBytesLE = function (bytes, unsigned) {
  222. return new Long(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24), bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24), unsigned);
  223. };
  224. /**
  225. * Creates a Long from its big endian byte representation.
  226. * @param bytes - Big endian byte representation
  227. * @param unsigned - Whether unsigned or not, defaults to signed
  228. * @returns The corresponding Long value
  229. */
  230. Long.fromBytesBE = function (bytes, unsigned) {
  231. return new Long((bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7], (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3], unsigned);
  232. };
  233. /**
  234. * Tests if the specified object is a Long.
  235. */
  236. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  237. Long.isLong = function (value) {
  238. return utils_1.isObjectLike(value) && value['__isLong__'] === true;
  239. };
  240. /**
  241. * Converts the specified value to a Long.
  242. * @param unsigned - Whether unsigned or not, defaults to signed
  243. */
  244. Long.fromValue = function (val, unsigned) {
  245. if (typeof val === 'number')
  246. return Long.fromNumber(val, unsigned);
  247. if (typeof val === 'string')
  248. return Long.fromString(val, unsigned);
  249. // Throws for non-objects, converts non-instanceof Long:
  250. return Long.fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
  251. };
  252. /** Returns the sum of this and the specified Long. */
  253. Long.prototype.add = function (addend) {
  254. if (!Long.isLong(addend))
  255. addend = Long.fromValue(addend);
  256. // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
  257. var a48 = this.high >>> 16;
  258. var a32 = this.high & 0xffff;
  259. var a16 = this.low >>> 16;
  260. var a00 = this.low & 0xffff;
  261. var b48 = addend.high >>> 16;
  262. var b32 = addend.high & 0xffff;
  263. var b16 = addend.low >>> 16;
  264. var b00 = addend.low & 0xffff;
  265. var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
  266. c00 += a00 + b00;
  267. c16 += c00 >>> 16;
  268. c00 &= 0xffff;
  269. c16 += a16 + b16;
  270. c32 += c16 >>> 16;
  271. c16 &= 0xffff;
  272. c32 += a32 + b32;
  273. c48 += c32 >>> 16;
  274. c32 &= 0xffff;
  275. c48 += a48 + b48;
  276. c48 &= 0xffff;
  277. return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
  278. };
  279. /**
  280. * Returns the sum of this and the specified Long.
  281. * @returns Sum
  282. */
  283. Long.prototype.and = function (other) {
  284. if (!Long.isLong(other))
  285. other = Long.fromValue(other);
  286. return Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned);
  287. };
  288. /**
  289. * Compares this Long's value with the specified's.
  290. * @returns 0 if they are the same, 1 if the this is greater and -1 if the given one is greater
  291. */
  292. Long.prototype.compare = function (other) {
  293. if (!Long.isLong(other))
  294. other = Long.fromValue(other);
  295. if (this.eq(other))
  296. return 0;
  297. var thisNeg = this.isNegative(), otherNeg = other.isNegative();
  298. if (thisNeg && !otherNeg)
  299. return -1;
  300. if (!thisNeg && otherNeg)
  301. return 1;
  302. // At this point the sign bits are the same
  303. if (!this.unsigned)
  304. return this.sub(other).isNegative() ? -1 : 1;
  305. // Both are positive if at least one is unsigned
  306. return other.high >>> 0 > this.high >>> 0 ||
  307. (other.high === this.high && other.low >>> 0 > this.low >>> 0)
  308. ? -1
  309. : 1;
  310. };
  311. /** This is an alias of {@link Long.compare} */
  312. Long.prototype.comp = function (other) {
  313. return this.compare(other);
  314. };
  315. /**
  316. * Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned.
  317. * @returns Quotient
  318. */
  319. Long.prototype.divide = function (divisor) {
  320. if (!Long.isLong(divisor))
  321. divisor = Long.fromValue(divisor);
  322. if (divisor.isZero())
  323. throw Error('division by zero');
  324. // use wasm support if present
  325. if (wasm) {
  326. // guard against signed division overflow: the largest
  327. // negative number / -1 would be 1 larger than the largest
  328. // positive number, due to two's complement.
  329. if (!this.unsigned &&
  330. this.high === -0x80000000 &&
  331. divisor.low === -1 &&
  332. divisor.high === -1) {
  333. // be consistent with non-wasm code path
  334. return this;
  335. }
  336. var low = (this.unsigned ? wasm.div_u : wasm.div_s)(this.low, this.high, divisor.low, divisor.high);
  337. return Long.fromBits(low, wasm.get_high(), this.unsigned);
  338. }
  339. if (this.isZero())
  340. return this.unsigned ? Long.UZERO : Long.ZERO;
  341. var approx, rem, res;
  342. if (!this.unsigned) {
  343. // This section is only relevant for signed longs and is derived from the
  344. // closure library as a whole.
  345. if (this.eq(Long.MIN_VALUE)) {
  346. if (divisor.eq(Long.ONE) || divisor.eq(Long.NEG_ONE))
  347. return Long.MIN_VALUE;
  348. // recall that -MIN_VALUE == MIN_VALUE
  349. else if (divisor.eq(Long.MIN_VALUE))
  350. return Long.ONE;
  351. else {
  352. // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
  353. var halfThis = this.shr(1);
  354. approx = halfThis.div(divisor).shl(1);
  355. if (approx.eq(Long.ZERO)) {
  356. return divisor.isNegative() ? Long.ONE : Long.NEG_ONE;
  357. }
  358. else {
  359. rem = this.sub(divisor.mul(approx));
  360. res = approx.add(rem.div(divisor));
  361. return res;
  362. }
  363. }
  364. }
  365. else if (divisor.eq(Long.MIN_VALUE))
  366. return this.unsigned ? Long.UZERO : Long.ZERO;
  367. if (this.isNegative()) {
  368. if (divisor.isNegative())
  369. return this.neg().div(divisor.neg());
  370. return this.neg().div(divisor).neg();
  371. }
  372. else if (divisor.isNegative())
  373. return this.div(divisor.neg()).neg();
  374. res = Long.ZERO;
  375. }
  376. else {
  377. // The algorithm below has not been made for unsigned longs. It's therefore
  378. // required to take special care of the MSB prior to running it.
  379. if (!divisor.unsigned)
  380. divisor = divisor.toUnsigned();
  381. if (divisor.gt(this))
  382. return Long.UZERO;
  383. if (divisor.gt(this.shru(1)))
  384. // 15 >>> 1 = 7 ; with divisor = 8 ; true
  385. return Long.UONE;
  386. res = Long.UZERO;
  387. }
  388. // Repeat the following until the remainder is less than other: find a
  389. // floating-point that approximates remainder / other *from below*, add this
  390. // into the result, and subtract it from the remainder. It is critical that
  391. // the approximate value is less than or equal to the real value so that the
  392. // remainder never becomes negative.
  393. rem = this;
  394. while (rem.gte(divisor)) {
  395. // Approximate the result of division. This may be a little greater or
  396. // smaller than the actual value.
  397. approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
  398. // We will tweak the approximate result by changing it in the 48-th digit or
  399. // the smallest non-fractional digit, whichever is larger.
  400. var log2 = Math.ceil(Math.log(approx) / Math.LN2);
  401. var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48);
  402. // Decrease the approximation until it is smaller than the remainder. Note
  403. // that if it is too large, the product overflows and is negative.
  404. var approxRes = Long.fromNumber(approx);
  405. var approxRem = approxRes.mul(divisor);
  406. while (approxRem.isNegative() || approxRem.gt(rem)) {
  407. approx -= delta;
  408. approxRes = Long.fromNumber(approx, this.unsigned);
  409. approxRem = approxRes.mul(divisor);
  410. }
  411. // We know the answer can't be zero... and actually, zero would cause
  412. // infinite recursion since we would make no progress.
  413. if (approxRes.isZero())
  414. approxRes = Long.ONE;
  415. res = res.add(approxRes);
  416. rem = rem.sub(approxRem);
  417. }
  418. return res;
  419. };
  420. /**This is an alias of {@link Long.divide} */
  421. Long.prototype.div = function (divisor) {
  422. return this.divide(divisor);
  423. };
  424. /**
  425. * Tests if this Long's value equals the specified's.
  426. * @param other - Other value
  427. */
  428. Long.prototype.equals = function (other) {
  429. if (!Long.isLong(other))
  430. other = Long.fromValue(other);
  431. if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1)
  432. return false;
  433. return this.high === other.high && this.low === other.low;
  434. };
  435. /** This is an alias of {@link Long.equals} */
  436. Long.prototype.eq = function (other) {
  437. return this.equals(other);
  438. };
  439. /** Gets the high 32 bits as a signed integer. */
  440. Long.prototype.getHighBits = function () {
  441. return this.high;
  442. };
  443. /** Gets the high 32 bits as an unsigned integer. */
  444. Long.prototype.getHighBitsUnsigned = function () {
  445. return this.high >>> 0;
  446. };
  447. /** Gets the low 32 bits as a signed integer. */
  448. Long.prototype.getLowBits = function () {
  449. return this.low;
  450. };
  451. /** Gets the low 32 bits as an unsigned integer. */
  452. Long.prototype.getLowBitsUnsigned = function () {
  453. return this.low >>> 0;
  454. };
  455. /** Gets the number of bits needed to represent the absolute value of this Long. */
  456. Long.prototype.getNumBitsAbs = function () {
  457. if (this.isNegative()) {
  458. // Unsigned Longs are never negative
  459. return this.eq(Long.MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
  460. }
  461. var val = this.high !== 0 ? this.high : this.low;
  462. var bit;
  463. for (bit = 31; bit > 0; bit--)
  464. if ((val & (1 << bit)) !== 0)
  465. break;
  466. return this.high !== 0 ? bit + 33 : bit + 1;
  467. };
  468. /** Tests if this Long's value is greater than the specified's. */
  469. Long.prototype.greaterThan = function (other) {
  470. return this.comp(other) > 0;
  471. };
  472. /** This is an alias of {@link Long.greaterThan} */
  473. Long.prototype.gt = function (other) {
  474. return this.greaterThan(other);
  475. };
  476. /** Tests if this Long's value is greater than or equal the specified's. */
  477. Long.prototype.greaterThanOrEqual = function (other) {
  478. return this.comp(other) >= 0;
  479. };
  480. /** This is an alias of {@link Long.greaterThanOrEqual} */
  481. Long.prototype.gte = function (other) {
  482. return this.greaterThanOrEqual(other);
  483. };
  484. /** This is an alias of {@link Long.greaterThanOrEqual} */
  485. Long.prototype.ge = function (other) {
  486. return this.greaterThanOrEqual(other);
  487. };
  488. /** Tests if this Long's value is even. */
  489. Long.prototype.isEven = function () {
  490. return (this.low & 1) === 0;
  491. };
  492. /** Tests if this Long's value is negative. */
  493. Long.prototype.isNegative = function () {
  494. return !this.unsigned && this.high < 0;
  495. };
  496. /** Tests if this Long's value is odd. */
  497. Long.prototype.isOdd = function () {
  498. return (this.low & 1) === 1;
  499. };
  500. /** Tests if this Long's value is positive. */
  501. Long.prototype.isPositive = function () {
  502. return this.unsigned || this.high >= 0;
  503. };
  504. /** Tests if this Long's value equals zero. */
  505. Long.prototype.isZero = function () {
  506. return this.high === 0 && this.low === 0;
  507. };
  508. /** Tests if this Long's value is less than the specified's. */
  509. Long.prototype.lessThan = function (other) {
  510. return this.comp(other) < 0;
  511. };
  512. /** This is an alias of {@link Long#lessThan}. */
  513. Long.prototype.lt = function (other) {
  514. return this.lessThan(other);
  515. };
  516. /** Tests if this Long's value is less than or equal the specified's. */
  517. Long.prototype.lessThanOrEqual = function (other) {
  518. return this.comp(other) <= 0;
  519. };
  520. /** This is an alias of {@link Long.lessThanOrEqual} */
  521. Long.prototype.lte = function (other) {
  522. return this.lessThanOrEqual(other);
  523. };
  524. /** Returns this Long modulo the specified. */
  525. Long.prototype.modulo = function (divisor) {
  526. if (!Long.isLong(divisor))
  527. divisor = Long.fromValue(divisor);
  528. // use wasm support if present
  529. if (wasm) {
  530. var low = (this.unsigned ? wasm.rem_u : wasm.rem_s)(this.low, this.high, divisor.low, divisor.high);
  531. return Long.fromBits(low, wasm.get_high(), this.unsigned);
  532. }
  533. return this.sub(this.div(divisor).mul(divisor));
  534. };
  535. /** This is an alias of {@link Long.modulo} */
  536. Long.prototype.mod = function (divisor) {
  537. return this.modulo(divisor);
  538. };
  539. /** This is an alias of {@link Long.modulo} */
  540. Long.prototype.rem = function (divisor) {
  541. return this.modulo(divisor);
  542. };
  543. /**
  544. * Returns the product of this and the specified Long.
  545. * @param multiplier - Multiplier
  546. * @returns Product
  547. */
  548. Long.prototype.multiply = function (multiplier) {
  549. if (this.isZero())
  550. return Long.ZERO;
  551. if (!Long.isLong(multiplier))
  552. multiplier = Long.fromValue(multiplier);
  553. // use wasm support if present
  554. if (wasm) {
  555. var low = wasm.mul(this.low, this.high, multiplier.low, multiplier.high);
  556. return Long.fromBits(low, wasm.get_high(), this.unsigned);
  557. }
  558. if (multiplier.isZero())
  559. return Long.ZERO;
  560. if (this.eq(Long.MIN_VALUE))
  561. return multiplier.isOdd() ? Long.MIN_VALUE : Long.ZERO;
  562. if (multiplier.eq(Long.MIN_VALUE))
  563. return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
  564. if (this.isNegative()) {
  565. if (multiplier.isNegative())
  566. return this.neg().mul(multiplier.neg());
  567. else
  568. return this.neg().mul(multiplier).neg();
  569. }
  570. else if (multiplier.isNegative())
  571. return this.mul(multiplier.neg()).neg();
  572. // If both longs are small, use float multiplication
  573. if (this.lt(Long.TWO_PWR_24) && multiplier.lt(Long.TWO_PWR_24))
  574. return Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
  575. // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
  576. // We can skip products that would overflow.
  577. var a48 = this.high >>> 16;
  578. var a32 = this.high & 0xffff;
  579. var a16 = this.low >>> 16;
  580. var a00 = this.low & 0xffff;
  581. var b48 = multiplier.high >>> 16;
  582. var b32 = multiplier.high & 0xffff;
  583. var b16 = multiplier.low >>> 16;
  584. var b00 = multiplier.low & 0xffff;
  585. var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
  586. c00 += a00 * b00;
  587. c16 += c00 >>> 16;
  588. c00 &= 0xffff;
  589. c16 += a16 * b00;
  590. c32 += c16 >>> 16;
  591. c16 &= 0xffff;
  592. c16 += a00 * b16;
  593. c32 += c16 >>> 16;
  594. c16 &= 0xffff;
  595. c32 += a32 * b00;
  596. c48 += c32 >>> 16;
  597. c32 &= 0xffff;
  598. c32 += a16 * b16;
  599. c48 += c32 >>> 16;
  600. c32 &= 0xffff;
  601. c32 += a00 * b32;
  602. c48 += c32 >>> 16;
  603. c32 &= 0xffff;
  604. c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
  605. c48 &= 0xffff;
  606. return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
  607. };
  608. /** This is an alias of {@link Long.multiply} */
  609. Long.prototype.mul = function (multiplier) {
  610. return this.multiply(multiplier);
  611. };
  612. /** Returns the Negation of this Long's value. */
  613. Long.prototype.negate = function () {
  614. if (!this.unsigned && this.eq(Long.MIN_VALUE))
  615. return Long.MIN_VALUE;
  616. return this.not().add(Long.ONE);
  617. };
  618. /** This is an alias of {@link Long.negate} */
  619. Long.prototype.neg = function () {
  620. return this.negate();
  621. };
  622. /** Returns the bitwise NOT of this Long. */
  623. Long.prototype.not = function () {
  624. return Long.fromBits(~this.low, ~this.high, this.unsigned);
  625. };
  626. /** Tests if this Long's value differs from the specified's. */
  627. Long.prototype.notEquals = function (other) {
  628. return !this.equals(other);
  629. };
  630. /** This is an alias of {@link Long.notEquals} */
  631. Long.prototype.neq = function (other) {
  632. return this.notEquals(other);
  633. };
  634. /** This is an alias of {@link Long.notEquals} */
  635. Long.prototype.ne = function (other) {
  636. return this.notEquals(other);
  637. };
  638. /**
  639. * Returns the bitwise OR of this Long and the specified.
  640. */
  641. Long.prototype.or = function (other) {
  642. if (!Long.isLong(other))
  643. other = Long.fromValue(other);
  644. return Long.fromBits(this.low | other.low, this.high | other.high, this.unsigned);
  645. };
  646. /**
  647. * Returns this Long with bits shifted to the left by the given amount.
  648. * @param numBits - Number of bits
  649. * @returns Shifted Long
  650. */
  651. Long.prototype.shiftLeft = function (numBits) {
  652. if (Long.isLong(numBits))
  653. numBits = numBits.toInt();
  654. if ((numBits &= 63) === 0)
  655. return this;
  656. else if (numBits < 32)
  657. return Long.fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
  658. else
  659. return Long.fromBits(0, this.low << (numBits - 32), this.unsigned);
  660. };
  661. /** This is an alias of {@link Long.shiftLeft} */
  662. Long.prototype.shl = function (numBits) {
  663. return this.shiftLeft(numBits);
  664. };
  665. /**
  666. * Returns this Long with bits arithmetically shifted to the right by the given amount.
  667. * @param numBits - Number of bits
  668. * @returns Shifted Long
  669. */
  670. Long.prototype.shiftRight = function (numBits) {
  671. if (Long.isLong(numBits))
  672. numBits = numBits.toInt();
  673. if ((numBits &= 63) === 0)
  674. return this;
  675. else if (numBits < 32)
  676. return Long.fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
  677. else
  678. return Long.fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
  679. };
  680. /** This is an alias of {@link Long.shiftRight} */
  681. Long.prototype.shr = function (numBits) {
  682. return this.shiftRight(numBits);
  683. };
  684. /**
  685. * Returns this Long with bits logically shifted to the right by the given amount.
  686. * @param numBits - Number of bits
  687. * @returns Shifted Long
  688. */
  689. Long.prototype.shiftRightUnsigned = function (numBits) {
  690. if (Long.isLong(numBits))
  691. numBits = numBits.toInt();
  692. numBits &= 63;
  693. if (numBits === 0)
  694. return this;
  695. else {
  696. var high = this.high;
  697. if (numBits < 32) {
  698. var low = this.low;
  699. return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned);
  700. }
  701. else if (numBits === 32)
  702. return Long.fromBits(high, 0, this.unsigned);
  703. else
  704. return Long.fromBits(high >>> (numBits - 32), 0, this.unsigned);
  705. }
  706. };
  707. /** This is an alias of {@link Long.shiftRightUnsigned} */
  708. Long.prototype.shr_u = function (numBits) {
  709. return this.shiftRightUnsigned(numBits);
  710. };
  711. /** This is an alias of {@link Long.shiftRightUnsigned} */
  712. Long.prototype.shru = function (numBits) {
  713. return this.shiftRightUnsigned(numBits);
  714. };
  715. /**
  716. * Returns the difference of this and the specified Long.
  717. * @param subtrahend - Subtrahend
  718. * @returns Difference
  719. */
  720. Long.prototype.subtract = function (subtrahend) {
  721. if (!Long.isLong(subtrahend))
  722. subtrahend = Long.fromValue(subtrahend);
  723. return this.add(subtrahend.neg());
  724. };
  725. /** This is an alias of {@link Long.subtract} */
  726. Long.prototype.sub = function (subtrahend) {
  727. return this.subtract(subtrahend);
  728. };
  729. /** Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. */
  730. Long.prototype.toInt = function () {
  731. return this.unsigned ? this.low >>> 0 : this.low;
  732. };
  733. /** Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). */
  734. Long.prototype.toNumber = function () {
  735. if (this.unsigned)
  736. return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0);
  737. return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
  738. };
  739. /** Converts the Long to a BigInt (arbitrary precision). */
  740. Long.prototype.toBigInt = function () {
  741. return BigInt(this.toString());
  742. };
  743. /**
  744. * Converts this Long to its byte representation.
  745. * @param le - Whether little or big endian, defaults to big endian
  746. * @returns Byte representation
  747. */
  748. Long.prototype.toBytes = function (le) {
  749. return le ? this.toBytesLE() : this.toBytesBE();
  750. };
  751. /**
  752. * Converts this Long to its little endian byte representation.
  753. * @returns Little endian byte representation
  754. */
  755. Long.prototype.toBytesLE = function () {
  756. var hi = this.high, lo = this.low;
  757. return [
  758. lo & 0xff,
  759. (lo >>> 8) & 0xff,
  760. (lo >>> 16) & 0xff,
  761. lo >>> 24,
  762. hi & 0xff,
  763. (hi >>> 8) & 0xff,
  764. (hi >>> 16) & 0xff,
  765. hi >>> 24
  766. ];
  767. };
  768. /**
  769. * Converts this Long to its big endian byte representation.
  770. * @returns Big endian byte representation
  771. */
  772. Long.prototype.toBytesBE = function () {
  773. var hi = this.high, lo = this.low;
  774. return [
  775. hi >>> 24,
  776. (hi >>> 16) & 0xff,
  777. (hi >>> 8) & 0xff,
  778. hi & 0xff,
  779. lo >>> 24,
  780. (lo >>> 16) & 0xff,
  781. (lo >>> 8) & 0xff,
  782. lo & 0xff
  783. ];
  784. };
  785. /**
  786. * Converts this Long to signed.
  787. */
  788. Long.prototype.toSigned = function () {
  789. if (!this.unsigned)
  790. return this;
  791. return Long.fromBits(this.low, this.high, false);
  792. };
  793. /**
  794. * Converts the Long to a string written in the specified radix.
  795. * @param radix - Radix (2-36), defaults to 10
  796. * @throws RangeError If `radix` is out of range
  797. */
  798. Long.prototype.toString = function (radix) {
  799. radix = radix || 10;
  800. if (radix < 2 || 36 < radix)
  801. throw RangeError('radix');
  802. if (this.isZero())
  803. return '0';
  804. if (this.isNegative()) {
  805. // Unsigned Longs are never negative
  806. if (this.eq(Long.MIN_VALUE)) {
  807. // We need to change the Long value before it can be negated, so we remove
  808. // the bottom-most digit in this base and then recurse to do the rest.
  809. var radixLong = Long.fromNumber(radix), div = this.div(radixLong), rem1 = div.mul(radixLong).sub(this);
  810. return div.toString(radix) + rem1.toInt().toString(radix);
  811. }
  812. else
  813. return '-' + this.neg().toString(radix);
  814. }
  815. // Do several (6) digits each time through the loop, so as to
  816. // minimize the calls to the very expensive emulated div.
  817. var radixToPower = Long.fromNumber(Math.pow(radix, 6), this.unsigned);
  818. // eslint-disable-next-line @typescript-eslint/no-this-alias
  819. var rem = this;
  820. var result = '';
  821. // eslint-disable-next-line no-constant-condition
  822. while (true) {
  823. var remDiv = rem.div(radixToPower);
  824. var intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0;
  825. var digits = intval.toString(radix);
  826. rem = remDiv;
  827. if (rem.isZero()) {
  828. return digits + result;
  829. }
  830. else {
  831. while (digits.length < 6)
  832. digits = '0' + digits;
  833. result = '' + digits + result;
  834. }
  835. }
  836. };
  837. /** Converts this Long to unsigned. */
  838. Long.prototype.toUnsigned = function () {
  839. if (this.unsigned)
  840. return this;
  841. return Long.fromBits(this.low, this.high, true);
  842. };
  843. /** Returns the bitwise XOR of this Long and the given one. */
  844. Long.prototype.xor = function (other) {
  845. if (!Long.isLong(other))
  846. other = Long.fromValue(other);
  847. return Long.fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
  848. };
  849. /** This is an alias of {@link Long.isZero} */
  850. Long.prototype.eqz = function () {
  851. return this.isZero();
  852. };
  853. /** This is an alias of {@link Long.lessThanOrEqual} */
  854. Long.prototype.le = function (other) {
  855. return this.lessThanOrEqual(other);
  856. };
  857. /*
  858. ****************************************************************
  859. * BSON SPECIFIC ADDITIONS *
  860. ****************************************************************
  861. */
  862. Long.prototype.toExtendedJSON = function (options) {
  863. if (options && options.relaxed)
  864. return this.toNumber();
  865. return { $numberLong: this.toString() };
  866. };
  867. Long.fromExtendedJSON = function (doc, options) {
  868. var result = Long.fromString(doc.$numberLong);
  869. return options && options.relaxed ? result.toNumber() : result;
  870. };
  871. /** @internal */
  872. Long.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  873. return this.inspect();
  874. };
  875. Long.prototype.inspect = function () {
  876. return "new Long(\"" + this.toString() + "\"" + (this.unsigned ? ', true' : '') + ")";
  877. };
  878. Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
  879. /** Maximum unsigned value. */
  880. Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
  881. /** Signed zero */
  882. Long.ZERO = Long.fromInt(0);
  883. /** Unsigned zero. */
  884. Long.UZERO = Long.fromInt(0, true);
  885. /** Signed one. */
  886. Long.ONE = Long.fromInt(1);
  887. /** Unsigned one. */
  888. Long.UONE = Long.fromInt(1, true);
  889. /** Signed negative one. */
  890. Long.NEG_ONE = Long.fromInt(-1);
  891. /** Maximum signed value. */
  892. Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
  893. /** Minimum signed value. */
  894. Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
  895. return Long;
  896. }());
  897. exports.Long = Long;
  898. Object.defineProperty(Long.prototype, '__isLong__', { value: true });
  899. Object.defineProperty(Long.prototype, '_bsontype', { value: 'Long' });
  900. //# sourceMappingURL=long.js.map