oasis-std
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -62,2 +62,6 @@ /// <reference types="node" /> | ||
private writeNumericArray; | ||
/** Write a little-endian unsigned BigInt | ||
* max and min are non-inclusive bounds | ||
*/ | ||
private writeBigUInt64LE; | ||
/** Writes a fixed-length `Uint8Array` */ | ||
@@ -115,2 +119,6 @@ writeU8Array(arr: Uint8Array | Uint8ClampedArray): void; | ||
private readNumericArray; | ||
/** Read a little-endian unsigned BigInt */ | ||
private readBigUInt64LE; | ||
/** Read a little-endian signed BigInt */ | ||
private readBigInt64LE; | ||
readU8Array(length: number): Uint8Array; | ||
@@ -117,0 +125,0 @@ readI8Array(length: number): Int16Array; |
@@ -460,6 +460,8 @@ "use strict"; | ||
writeU64(num) { | ||
this.writeNumber(buffer_1.Buffer.prototype.writeBigUInt64LE, 8, num); | ||
// allow input of x: -1 < x < Number.MAX_VALUE | ||
this.writeBigUInt64LE(num, BigInt('-1'), BigInt(Number.MAX_VALUE)); | ||
} | ||
writeI64(num) { | ||
this.writeNumber(buffer_1.Buffer.prototype.writeBigInt64LE, 8, num); | ||
// allow input of x: Number.MIN_SAFE_INTEGER < x < Number.MAX_VALUE | ||
this.writeBigUInt64LE(num, BigInt(Number.MIN_SAFE_INTEGER), BigInt(Number.MAX_VALUE)); | ||
} | ||
@@ -487,2 +489,27 @@ /* istanbul ignore next */ | ||
} | ||
// TODO: Refactor to feross | ||
// https://github.com/feross/buffer/pull/267 | ||
/** Write a little-endian unsigned BigInt | ||
* max and min are non-inclusive bounds | ||
*/ | ||
writeBigUInt64LE(value, min, max = BigInt(Number.MAX_VALUE)) { | ||
if (value >= max || value <= min) | ||
throw new Error(`trying to write value: ${value} out of range`); | ||
let lo = Number(value & BigInt('0xffffffff')); | ||
this.buf[this.offset++] = lo; | ||
lo = lo >> 8; | ||
this.buf[this.offset++] = lo; | ||
lo = lo >> 8; | ||
this.buf[this.offset++] = lo; | ||
lo = lo >> 8; | ||
this.buf[this.offset++] = lo; | ||
let hi = Number((value >> BigInt('32')) & BigInt('0xffffffff')); | ||
this.buf[this.offset++] = hi; | ||
hi = hi >> 8; | ||
this.buf[this.offset++] = hi; | ||
hi = hi >> 8; | ||
this.buf[this.offset++] = hi; | ||
hi = hi >> 8; | ||
this.buf[this.offset++] = hi; | ||
} | ||
/** Writes a fixed-length `Uint8Array` */ | ||
@@ -591,3 +618,3 @@ writeU8Array(arr) { | ||
readU64() { | ||
const u64 = this.buf.readBigUInt64LE(this.offset); | ||
const u64 = this.readBigUInt64LE(this.offset); | ||
this.offset += 8; | ||
@@ -597,3 +624,3 @@ return u64; | ||
readI64() { | ||
const i64 = this.buf.readBigInt64LE(this.offset); | ||
const i64 = this.readBigInt64LE(this.offset); | ||
this.offset += 8; | ||
@@ -628,2 +655,38 @@ return i64; | ||
} | ||
// TODO: Refactor to feross | ||
// https://github.com/feross/buffer/pull/267 | ||
/** Read a little-endian unsigned BigInt */ | ||
readBigUInt64LE(offset = 0) { | ||
const first = this.buf[offset]; | ||
const last = this.buf[offset + 7]; | ||
if (first === undefined || last === undefined) | ||
throw new Error('buffer bound error'); | ||
const lo = first + | ||
this.buf[++offset] * Math.pow(2, 8) + | ||
this.buf[++offset] * Math.pow(2, 16) + | ||
this.buf[++offset] * Math.pow(2, 24); | ||
const hi = this.buf[++offset] + | ||
this.buf[++offset] * Math.pow(2, 8) + | ||
this.buf[++offset] * Math.pow(2, 16) + | ||
last * Math.pow(2, 24); | ||
return BigInt(lo) + (BigInt(hi) << BigInt('32')); | ||
} | ||
// TODO: Refactor to feross | ||
// https://github.com/feross/buffer/pull/267 | ||
/** Read a little-endian signed BigInt */ | ||
readBigInt64LE(offset = 0) { | ||
const first = this.buf[offset]; | ||
const last = this.buf[offset + 7]; | ||
if (first === undefined || last === undefined) | ||
throw new Error('buffer bound error'); | ||
const hi = this.buf[offset + 4] + | ||
this.buf[offset + 5] * Math.pow(2, 8) + | ||
this.buf[offset + 6] * Math.pow(2, 16) + | ||
(last << 24); // Overflow | ||
const lo = first + | ||
this.buf[++offset] * Math.pow(2, 8) + | ||
this.buf[++offset] * Math.pow(2, 16) + | ||
this.buf[++offset] * Math.pow(2, 24); | ||
return BigInt(lo) + (BigInt(hi) << BigInt('32')); | ||
} | ||
readU8Array(length) { | ||
@@ -630,0 +693,0 @@ return this.readNumericArray(Uint8Array, length); |
{ | ||
"name": "oasis-std", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Oasis platform standard library", | ||
@@ -29,3 +29,3 @@ "license": "Apache-2.0", | ||
}, | ||
"gitHead": "f6f0431a1546493faa4bd9064b3184a76e248550" | ||
"gitHead": "8126667cdf23ffa97ccb7b7b410170ac0d6f4336" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2067
300935