Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}
Expand Down
73 changes: 73 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down