diff --git a/index.js b/index.js index 4983233..5fca138 100644 --- a/index.js +++ b/index.js @@ -368,12 +368,13 @@ Long.fromString = fromString; /** * @function - * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val + * @param {!Long|number|bigint|string|!{low: number, high: number, unsigned: boolean}} val * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromValue(val, unsigned) { + if (typeof val === "bigint") return Long.fromBigInt(val, unsigned); if (typeof val === "number") return fromNumber(val, unsigned); if (typeof val === "string") return fromString(val, unsigned); // Throws for non-objects, converts non-instanceof Long: @@ -1560,12 +1561,6 @@ if (typeof BigInt === "function") { return fromBits(lowBits, highBits, unsigned); }; - // Override - Long.fromValue = function fromValueWithBigInt(value, unsigned) { - if (typeof value === "bigint") return Long.fromBigInt(value, unsigned); - return fromValue(value, unsigned); - }; - /** * Converts the Long to its big integer representation. * @this {!Long} diff --git a/tests/index.js b/tests/index.js index 3eab85e..bf5ad1b 100644 --- a/tests/index.js +++ b/tests/index.js @@ -279,6 +279,79 @@ var tests = [ } }, + function testBigIntOperands() { + if (typeof BigInt !== "function") return; + + var values = [7n, -7n, 9007199254740993n]; + var operands = [ + 0n, + 1n, + -1n, + 2n, + -2n, + 7n, + 9007199254740993n, + 18446744073709551615n, + (1n << 80n) + 3n, + ]; + for (var i = 0; i < values.length; i++) { + var a = values[i]; + var value = Long.fromBigInt(a); + for (var j = 0; j < operands.length; j++) { + var operand = operands[j]; + var b = BigInt.asIntN(64, operand); + assert.strictEqual( + value.add(operand).toBigInt(), + BigInt.asIntN(64, a + b), + ); + assert.strictEqual( + value.sub(operand).toBigInt(), + BigInt.asIntN(64, a - b), + ); + assert.strictEqual( + value.mul(operand).toBigInt(), + BigInt.asIntN(64, a * b), + ); + assert.strictEqual( + value.and(operand).toBigInt(), + BigInt.asIntN(64, a & b), + ); + assert.strictEqual( + value.or(operand).toBigInt(), + BigInt.asIntN(64, a | b), + ); + assert.strictEqual( + value.xor(operand).toBigInt(), + BigInt.asIntN(64, a ^ b), + ); + assert.strictEqual(value.eq(operand), a === b); + assert.strictEqual(value.neq(operand), a !== b); + assert.strictEqual(value.lt(operand), a < b); + assert.strictEqual(value.lte(operand), a <= b); + assert.strictEqual(value.gt(operand), a > b); + assert.strictEqual(value.gte(operand), a >= b); + assert.strictEqual(value.comp(operand), a < b ? -1 : a > b ? 1 : 0); + if (b === 0n) { + assert.throws(function () { + value.div(operand); + }); + assert.throws(function () { + value.mod(operand); + }); + } else { + assert.strictEqual( + value.div(operand).toBigInt(), + BigInt.asIntN(64, a / b), + ); + assert.strictEqual( + value.mod(operand).toBigInt(), + BigInt.asIntN(64, a % b), + ); + } + } + } + }, + function testSafeInteger() { assert(Long.fromNumber(0).isSafeInteger()); assert(Long.fromNumber(1).isSafeInteger());