Skip to content

Commit

Permalink
Use BigInt conversion function instead of literals, #141
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Jul 30, 2024
1 parent c8a9cd5 commit 164764d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,18 @@ export class Packr extends Unpackr {
if (this.largeBigIntToFloat) {
target[position++] = 0xcb
targetView.setFloat64(position, Number(value))
} else if (this.useBigIntExtension && value < 2n**(1023n) && value > -(2n**(1023n))) {
} else if (this.useBigIntExtension && value < BigInt(2)**BigInt(1023) && value > -(BigInt(2)**BigInt(1023))) {
target[position++] = 0xc7
position++;
target[position++] = 0x42 // "B" for BigInt
let bytes = [];
let alignedSign;
do {
let byte = value & 0xffn;
alignedSign = (byte & 0x80n) === (value < 0n ? 0x80n : 0n);
alignedSign = (byte & BigInt(0x80)) === (value < BigInt(0) ? BigInt(0x80) : BigInt(0));
bytes.push(byte);
value >>= 8n;
} while (!((value === 0n || value === -1n) && alignedSign));
value >>= BigInt(8);
} while (!((value === BigInt(0) || value === BigInt(-1)) && alignedSign));
target[position-2] = bytes.length;
for (let i = bytes.length; i > 0;) {
target[position++] = Number(bytes[--i]);
Expand Down
2 changes: 1 addition & 1 deletion unpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ currentExtensions[0x42] = (data) => {
let length = data.length;
let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
for (let i = 1; i < length; i++) {
value <<= 8n;
value <<= BigInt(8);
value += BigInt(data[i]);
}
return value;
Expand Down

0 comments on commit 164764d

Please sign in to comment.