Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional BigInt support: map BigInt to int64/uint64 when useBigInt64 is set to true #223

Merged
merged 1 commit into from
Mar 6, 2023
Merged
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
25 changes: 23 additions & 2 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class Decoder<ContextType = undefined> {
public constructor(
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
private readonly context: ContextType = undefined as any,
private readonly useBigInt64 = false,
private readonly maxStrLength = UINT32_MAX,
private readonly maxBinLength = UINT32_MAX,
private readonly maxArrayLength = UINT32_MAX,
Expand Down Expand Up @@ -277,7 +278,11 @@ export class Decoder<ContextType = undefined> {
object = this.readU32();
} else if (headByte === 0xcf) {
// uint 64
object = this.readU64();
if (this.useBigInt64) {
object = this.readU64AsBigInt();
} else {
object = this.readU64();
}
} else if (headByte === 0xd0) {
// int 8
object = this.readI8();
Expand All @@ -289,7 +294,11 @@ export class Decoder<ContextType = undefined> {
object = this.readI32();
} else if (headByte === 0xd3) {
// int 64
object = this.readI64();
if (this.useBigInt64) {
object = this.readI64AsBigInt();
} else {
object = this.readI64();
}
} else if (headByte === 0xd9) {
// str 8
const byteLength = this.lookU8();
Expand Down Expand Up @@ -605,6 +614,18 @@ export class Decoder<ContextType = undefined> {
return value;
}

private readU64AsBigInt(): bigint {
const value = this.view.getBigUint64(this.pos);
this.pos += 8;
return value;
}

private readI64AsBigInt(): bigint {
const value = this.view.getBigInt64(this.pos);
this.pos += 8;
return value;
}

private readF32() {
const value = this.view.getFloat32(this.pos);
this.pos += 4;
Expand Down
73 changes: 58 additions & 15 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class Encoder<ContextType = undefined> {
public constructor(
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
private readonly context: ContextType = undefined as any,
private readonly useBigInt64 = false,
private readonly maxDepth = DEFAULT_MAX_DEPTH,
private readonly initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE,
private readonly sortKeys = false,
Expand Down Expand Up @@ -57,9 +58,15 @@ export class Encoder<ContextType = undefined> {
} else if (typeof object === "boolean") {
this.encodeBoolean(object);
} else if (typeof object === "number") {
this.encodeNumber(object);
if (!this.forceIntegerToFloat) {
this.encodeNumber(object);
} else {
this.encodeNumberAsFloat(object);
}
} else if (typeof object === "string") {
this.encodeString(object);
} else if (this.useBigInt64 && typeof object === "bigint") {
this.encodeBigInt64(object);
} else {
this.encodeObject(object, depth);
}
Expand Down Expand Up @@ -95,8 +102,9 @@ export class Encoder<ContextType = undefined> {
this.writeU8(0xc3);
}
}
private encodeNumber(object: number) {
if (Number.isSafeInteger(object) && !this.forceIntegerToFloat) {

private encodeNumber(object: number): void {
if (!this.forceIntegerToFloat && Number.isSafeInteger(object)) {
if (object >= 0) {
if (object < 0x80) {
// positive fixint
Expand All @@ -113,10 +121,12 @@ export class Encoder<ContextType = undefined> {
// uint 32
this.writeU8(0xce);
this.writeU32(object);
} else {
} else if (!this.useBigInt64) {
// uint 64
this.writeU8(0xcf);
this.writeU64(object);
} else {
this.encodeNumberAsFloat(object);
}
} else {
if (object >= -0x20) {
Expand All @@ -134,23 +144,40 @@ export class Encoder<ContextType = undefined> {
// int 32
this.writeU8(0xd2);
this.writeI32(object);
} else {
} else if (!this.useBigInt64) {
// int 64
this.writeU8(0xd3);
this.writeI64(object);
} else {
this.encodeNumberAsFloat(object);
}
}
} else {
// non-integer numbers
if (this.forceFloat32) {
// float 32
this.writeU8(0xca);
this.writeF32(object);
} else {
// float 64
this.writeU8(0xcb);
this.writeF64(object);
}
this.encodeNumberAsFloat(object);
}
}

private encodeNumberAsFloat(object: number): void {
if (this.forceFloat32) {
// float 32
this.writeU8(0xca);
this.writeF32(object);
} else {
// float 64
this.writeU8(0xcb);
this.writeF64(object);
}
}

private encodeBigInt64(object: bigint): void {
if (object >= BigInt(0)) {
// uint 64
this.writeU8(0xcf);
this.writeBigUint64(object);
} else {
// int 64
this.writeU8(0xd3);
this.writeBigInt64(object);
}
}

Expand Down Expand Up @@ -377,12 +404,14 @@ export class Encoder<ContextType = undefined> {

private writeF32(value: number) {
this.ensureBufferSizeToWrite(4);

this.view.setFloat32(this.pos, value);
this.pos += 4;
}

private writeF64(value: number) {
this.ensureBufferSizeToWrite(8);

this.view.setFloat64(this.pos, value);
this.pos += 8;
}
Expand All @@ -400,4 +429,18 @@ export class Encoder<ContextType = undefined> {
setInt64(this.view, this.pos, value);
this.pos += 8;
}

private writeBigUint64(value: bigint) {
this.ensureBufferSizeToWrite(8);

this.view.setBigUint64(this.pos, value);
this.pos += 8;
}

private writeBigInt64(value: bigint) {
this.ensureBufferSizeToWrite(8);

this.view.setBigInt64(this.pos, value);
this.pos += 8;
}
}
11 changes: 11 additions & 0 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
Partial<{
extensionCodec: ExtensionCodecType<ContextType>;

/**
* Decodes Int64 and Uint64 as bigint if it's set to true.
* Depends on ES2020's {@link DataView#getBigInt64} and
* {@link DataView#getBigUint64}.
*
* Defaults to false.
*/
useBigInt64: boolean;

/**
* Maximum string length.
*
Expand Down Expand Up @@ -58,6 +67,7 @@ export function decode<ContextType = undefined>(
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.useBigInt64,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
Expand All @@ -81,6 +91,7 @@ export function decodeMulti<ContextType = undefined>(
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.useBigInt64,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
Expand Down
3 changes: 3 additions & 0 deletions src/decodeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { SplitUndefined } from "./context";
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.useBigInt64,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
Expand All @@ -40,6 +41,7 @@ import type { SplitUndefined } from "./context";
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.useBigInt64,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
Expand All @@ -63,6 +65,7 @@ export function decodeMultiStream<ContextType>(
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.useBigInt64,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
Expand Down
11 changes: 11 additions & 0 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ export type EncodeOptions<ContextType = undefined> = Partial<
Readonly<{
extensionCodec: ExtensionCodecType<ContextType>;

/**
* Encodes bigint as Int64 or Uint64 if it's set to true.
* {@link forceIntegerToFloat} does not affect bigint.
* Depends on ES2020's {@link DataView#setBigInt64} and
* {@link DataView#setBigUint64}.
*
* Defaults to false.
*/
useBigInt64: boolean;

/**
* The maximum depth in nested objects and arrays.
*
Expand Down Expand Up @@ -70,6 +80,7 @@ export function encode<ContextType = undefined>(
const encoder = new Encoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.useBigInt64,
options.maxDepth,
options.initialBufferSize,
options.sortKeys,
Expand Down
38 changes: 38 additions & 0 deletions test/bigint64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import assert from "assert";
import { encode, decode } from "../src";

describe("useBigInt64: true", () => {
before(function () {
if (typeof BigInt === "undefined") {
this.skip();
}
});

it("encodes and decodes 0n", () => {
const value = BigInt(0);
const encoded = encode(value, { useBigInt64: true });
assert.deepStrictEqual(decode(encoded, { useBigInt64: true }), value);
});

it("encodes and decodes MAX_SAFE_INTEGER+1", () => {
const value = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
const encoded = encode(value, { useBigInt64: true });
assert.deepStrictEqual(decode(encoded, { useBigInt64: true }), value);
});

it("encodes and decodes MIN_SAFE_INTEGER-1", () => {
const value = BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1);
const encoded = encode(value, { useBigInt64: true });
assert.deepStrictEqual(decode(encoded, { useBigInt64: true }), value);
});

it("encodes and decodes values with numbers and bigints", () => {
const value = {
ints: [0, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER],
nums: [Number.NaN, Math.PI, Math.E, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
bigints: [BigInt(0), BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1), BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1)],
};
const encoded = encode(value, { useBigInt64: true });
assert.deepStrictEqual(decode(encoded, { useBigInt64: true }), value);
});
});
3 changes: 3 additions & 0 deletions test/codec-bigint.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import assert from "assert";
import { encode, decode, ExtensionCodec, DecodeError } from "../src";

// This test is provided for backward compatibility since this library now has
// native bigint support with `useBigInt64: true` option.

const extensionCodec = new ExtensionCodec();
extensionCodec.register({
type: 0,
Expand Down