[TS/JS] BigInt implementation (#6998)

* BigInt implementation

* Unit test reading long from existing bytebuffer

* Code review
This commit is contained in:
Alex E
2022-01-06 21:35:37 -05:00
committed by GitHub
parent f28c2b2936
commit ace4a37f22
22 changed files with 225 additions and 256 deletions

View File

@@ -3,15 +3,13 @@ import { toByteWidth, fromByteWidth } from './bit-width-util'
import { toUTF8Array, fromUTF8Array } from './flexbuffers-util'
import { Reference } from './reference'
import { Long } from '../long'
export function validateOffset(dataView: DataView, offset: number, width: number): void {
if (dataView.byteLength <= offset + width || (offset & (toByteWidth(width) - 1)) !== 0) {
throw "Bad offset: " + offset + ", width: " + width;
}
}
export function readInt(dataView: DataView, offset: number, width: number): number | Long | bigint {
export function readInt(dataView: DataView, offset: number, width: number): number | bigint {
if (width < 2) {
if (width < 1) {
return dataView.getInt8(offset);
@@ -23,14 +21,14 @@ export function readInt(dataView: DataView, offset: number, width: number): numb
return dataView.getInt32(offset, true)
} else {
if (dataView.setBigInt64 === undefined) {
return new Long(dataView.getUint32(offset, true), dataView.getUint32(offset + 4, true))
return BigInt(dataView.getUint32(offset, true)) + (BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32));
}
return dataView.getBigInt64(offset, true)
}
}
}
export function readUInt(dataView: DataView, offset: number, width: number): number | Long | bigint {
export function readUInt(dataView: DataView, offset: number, width: number): number | bigint {
if (width < 2) {
if (width < 1) {
return dataView.getUint8(offset);
@@ -42,7 +40,7 @@ export function readUInt(dataView: DataView, offset: number, width: number): num
return dataView.getUint32(offset, true)
} else {
if (dataView.getBigUint64 === undefined) {
return new Long(dataView.getUint32(offset, true), dataView.getUint32(offset + 4, true))
return BigInt(dataView.getUint32(offset, true)) + (BigInt(dataView.getUint32(offset + 4, true)) << BigInt(32));
}
return dataView.getBigUint64(offset, true)
}
@@ -116,4 +114,4 @@ export function keyForIndex(index: number, dataView: DataView, offset: number, p
length++;
}
return fromUTF8Array(new Uint8Array(dataView.buffer, keyIndirectOffset, length));
}
}

View File

@@ -2,7 +2,6 @@ import { fromByteWidth } from './bit-width-util'
import { ValueType } from './value-type'
import { isNumber, isIndirectNumber, isAVector, fixedTypedVectorElementSize, isFixedTypedVector, isTypedVector, typedVectorElementType, packedType, fixedTypedVectorElementType } from './value-type-util'
import { indirect, keyForIndex, keyIndex, readFloat, readInt, readUInt, valueForIndexWithKey } from './reference-util'
import { Long } from '../long';
import { fromUTF8Array } from './flexbuffers-util';
import { BitWidth } from './bit-width';
@@ -48,7 +47,7 @@ export class Reference {
return null;
}
intValue(): number | Long | bigint | null {
intValue(): number | bigint | null {
if (this.valueType === ValueType.INT) {
return readInt(this.dataView, this.offset, this.parentWidth);
}
@@ -74,7 +73,7 @@ export class Reference {
return null;
}
numericValue(): number | Long | bigint | null { return this.floatValue() || this.intValue()}
numericValue(): number | bigint | null { return this.floatValue() || this.intValue()}
stringValue(): string | null {
if (this.valueType === ValueType.STRING || this.valueType === ValueType.KEY) {