Skip to content

Commit

Permalink
Add support for BigInts larger than 64-bit
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Nov 19, 2023
1 parent 0e960cb commit 6978440
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,26 @@ 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))) {
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);
bytes.push(byte);
value >>= 8n;
} while (!((value === 0n || value === -1n) && alignedSign));
target[position-2] = bytes.length;
for (let i = bytes.length; i > 0;) {
target[position++] = Number(bytes[--i]);
}
return
} else {
throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
' useBigIntExtension or set largeBigIntToFloat to convert to float-64')
}
}
position += 8
Expand Down
16 changes: 16 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,22 @@ suite('msgpackr basic tests', function() {
assert.equal(pack(123).length, 1)
})

test('BigInt', function() {
let packr = new Packr({useBigIntExtension: true})
let data = {
a: 3333333333333333333333333333n,
b: 1234567890123456789012345678901234567890n,
c: -3333333333333333333333333333n,
d: -352523523642364364364264264264264264262642642n,
e: 0xffffffffffffffffffffffffffn,
f: -0xffffffffffffffffffffffffffn,
}
let serialized = packr.pack(data)
let deserialized = packr.unpack(serialized)
assert.deepEqual(data, deserialized)
})


test('extended class pack/unpack', function(){
function Extended() {

Expand Down
11 changes: 11 additions & 0 deletions unpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,17 @@ const recordDefinition = (id, highByte) => {
currentExtensions[0] = () => {} // notepack defines extension 0 to mean undefined, so use that as the default here
currentExtensions[0].noBuffer = true

currentExtensions[0x42] = (data) => {
// decode bigint
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(data[i]);
}
return value;
}

let errors = { Error, TypeError, ReferenceError };
currentExtensions[0x65] = () => {
let data = read()
Expand Down

0 comments on commit 6978440

Please sign in to comment.