From c035ce6e07629e4a016eb823664ccd8e4bada7e4 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 26 Aug 2019 14:32:28 +0300 Subject: [PATCH 1/2] refactor Buffer --- assembly/buffer/index.ts | 94 ++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index 91be52d..0db71af 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -7,28 +7,28 @@ export class Buffer extends Uint8Array { super(size); } - public static alloc(size: i32): Buffer { + @inline static alloc(size: i32): Buffer { return new Buffer(size); } - @unsafe public static allocUnsafe(size: i32): Buffer { + @unsafe static allocUnsafe(size: i32): Buffer { // Node throws an error if size is less than 0 - if (u32(size) > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH); + if (size > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH); let buffer = __alloc(size, idof()); // This retains the pointer to the result Buffer. let result = changetype(__alloc(offsetof(), idof())); result.data = changetype(buffer); - result.dataStart = changetype(buffer); + result.dataStart = buffer; result.dataLength = size; return result; } - public static isBuffer(value: T): bool { + @inline static isBuffer(value: T): bool { return value instanceof Buffer; } // Adapted from https://github.com/AssemblyScript/assemblyscript/blob/master/std/assembly/typedarray.ts - public subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Buffer { + subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Buffer { var len = this.dataLength; begin = begin < 0 ? max(len + begin, 0) : min(begin, len); end = end < 0 ? max(len + end, 0) : min(end, len); @@ -69,7 +69,7 @@ export class Buffer extends Uint8Array { readInt16BE(offset: i32 = 0): i16 { if (i32(offset < 0) | i32(offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return bswap(load(this.dataStart + offset)); + return bswap(load(this.dataStart + offset)); } readUInt16LE(offset: i32 = 0): u16 { @@ -79,7 +79,7 @@ export class Buffer extends Uint8Array { readUInt16BE(offset: i32 = 0): u16 { if (i32(offset < 0) | i32(offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return bswap(load(this.dataStart + offset)); + return bswap(load(this.dataStart + offset)); } writeInt16LE(value: i16, offset: i32 = 0): i32 { @@ -90,7 +90,7 @@ export class Buffer extends Uint8Array { writeInt16BE(value: i16, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(value)); + store(this.dataStart + offset, bswap(value)); return offset + 2; } @@ -102,7 +102,7 @@ export class Buffer extends Uint8Array { writeUInt16BE(value: u16, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(value)); + store(this.dataStart + offset, bswap(value)); return offset + 2; } @@ -113,7 +113,7 @@ export class Buffer extends Uint8Array { readInt32BE(offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return bswap(load(this.dataStart + offset)); + return bswap(load(this.dataStart + offset)); } readUInt32LE(offset: i32 = 0): u32 { @@ -123,7 +123,7 @@ export class Buffer extends Uint8Array { readUInt32BE(offset: i32 = 0): u32 { if (i32(offset < 0) | i32(offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return bswap(load(this.dataStart + offset)); + return bswap(load(this.dataStart + offset)); } writeInt32LE(value: i32, offset: i32 = 0): i32 { @@ -134,7 +134,7 @@ export class Buffer extends Uint8Array { writeInt32BE(value: i32, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(value)); + store(this.dataStart + offset, bswap(value)); return offset + 4; } @@ -146,7 +146,7 @@ export class Buffer extends Uint8Array { writeUInt32BE(value: u32, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(value)); + store(this.dataStart + offset, bswap(value)); return offset + 4; } @@ -157,7 +157,7 @@ export class Buffer extends Uint8Array { readFloatBE(offset: i32 = 0): f32 { if (i32(offset < 0) | i32(offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return reinterpret(bswap(load(this.dataStart + offset))); + return reinterpret(bswap(load(this.dataStart + offset))); } writeFloatLE(value: f32, offset: i32 = 0): i32 { @@ -168,7 +168,7 @@ export class Buffer extends Uint8Array { writeFloatBE(value: f32, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(reinterpret(value))); + store(this.dataStart + offset, bswap(reinterpret(value))); return offset + 4; } @@ -179,7 +179,7 @@ export class Buffer extends Uint8Array { readBigInt64BE(offset: i32 = 0): i64 { if (i32(offset < 0) | i32(offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return bswap(load(this.dataStart + offset)); + return bswap(load(this.dataStart + offset)); } readBigUInt64LE(offset: i32 = 0): u64 { @@ -189,7 +189,7 @@ export class Buffer extends Uint8Array { readBigUInt64BE(offset: i32 = 0): u64 { if (i32(offset < 0) | i32(offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return bswap(load(this.dataStart + offset)); + return bswap(load(this.dataStart + offset)); } writeBigInt64LE(value: i64, offset: i32 = 0): i32 { @@ -200,7 +200,7 @@ export class Buffer extends Uint8Array { writeBigInt64BE(value: i64, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(value)); + store(this.dataStart + offset, bswap(value)); return offset + 8; } @@ -212,7 +212,7 @@ export class Buffer extends Uint8Array { writeBigUInt64BE(value: u64, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(value)); + store(this.dataStart + offset, bswap(value)); return offset + 8; } @@ -223,7 +223,7 @@ export class Buffer extends Uint8Array { readDoubleBE(offset: i32 = 0): f64 { if (i32(offset < 0) | i32(offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - return reinterpret(bswap(load(this.dataStart + offset))); + return reinterpret(bswap(load(this.dataStart + offset))); } writeDoubleLE(value: f64, offset: i32 = 0): i32 { @@ -234,7 +234,7 @@ export class Buffer extends Uint8Array { writeDoubleBE(value: f64, offset: i32 = 0): i32 { if (i32(offset < 0) | i32(offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + offset, bswap(reinterpret(value))); + store(this.dataStart + offset, bswap(reinterpret(value))); return offset + 8; } @@ -245,12 +245,12 @@ export class Buffer extends Uint8Array { let dataStart = this.dataStart; dataLength += dataStart; while (dataStart < dataLength) { - store(dataStart, bswap(load(dataStart))); + store(dataStart, bswap(load(dataStart))); dataStart += 2; } return this; } - + swap32(): Buffer { let dataLength = this.dataLength; // Make sure dataLength is divisible by 4 @@ -258,12 +258,12 @@ export class Buffer extends Uint8Array { let dataStart = this.dataStart; dataLength += dataStart; while (dataStart < dataLength) { - store(dataStart, bswap(load(dataStart))); + store(dataStart, bswap(load(dataStart))); dataStart += 4; } return this; } - + swap64(): Buffer { let dataLength = this.dataLength; // Make sure dataLength is divisible by 8 @@ -271,7 +271,7 @@ export class Buffer extends Uint8Array { let dataStart = this.dataStart; dataLength += dataStart; while (dataStart < dataLength) { - store(dataStart, bswap(load(dataStart))); + store(dataStart, bswap(load(dataStart))); dataStart += 8; } return this; @@ -280,29 +280,20 @@ export class Buffer extends Uint8Array { export namespace Buffer { export namespace HEX { - /** Calculates the two char combination from the byte. */ - @inline export function charsFromByte(byte: u32): u32 { - let top = (byte >>> 4) & 0xF; - let bottom = (0xF & byte); - top += select(0x57, 0x30, top > 9); - bottom += select(0x57, 0x30, bottom > 9); - return (bottom << 16) | top; - } - /** Calculates the byte length of the specified string when encoded as HEX. */ export function byteLength(str: string): i32 { let ptr = changetype(str); let byteCount = changetype(changetype(str) - BLOCK_OVERHEAD).rtSize; let length = byteCount >> 2; // The string length must be even because the bytes come in pairs of characters two wide - if (byteCount & 0x3) return 0; // encoding fails and returns an empty ArrayBuffer + if (byteCount & 3) return 0; // encoding fails and returns an empty ArrayBuffer byteCount += ptr; while (ptr < byteCount) { var char = load(ptr); - if ( ((char - 0x30) <= 0x9) - || ((char - 0x61) <= 0x5) - || ((char - 0x41) <= 0x5)) { + if ( ((char - 0x30) <= 9) + || ((char - 0x61) <= 5) + || ((char - 0x41) <= 5)) { ptr += 2; continue; } else { @@ -325,15 +316,14 @@ export namespace Buffer { let b: u32 = 0; let outChar = 0; for (let i: usize = 0; ptr < byteEnd; i++) { - let odd = i & 1; - if (odd) { + if (i & 1) { outChar <<= 4; b >>>= 16; if ((b - 0x30) <= 9) { outChar |= b - 0x30; - } else if ((b - 0x61) <= 0x5) { + } else if ((b - 0x61) <= 5) { outChar |= b - 0x57; - } else if (b - 0x41 <= 0x5) { + } else if (b - 0x41 <= 5) { outChar |= b - 0x37; } store(result + (i >> 1), (outChar & 0xFF)); @@ -344,9 +334,9 @@ export namespace Buffer { let c = b & 0xFF; if ((c - 0x30) <= 9) { outChar |= c - 0x30; - } else if ((c - 0x61) <= 0x5) { + } else if ((c - 0x61) <= 5) { outChar |= c - 0x57; - } else if (c - 0x41 <= 0x5) { + } else if (c - 0x41 <= 5) { outChar |= c - 0x37; } } @@ -368,12 +358,20 @@ export namespace Buffer { // loop over each byte and store a `u32` for each one while (ptr < inputByteLength) { - store(result + i, charsFromByte(load(ptr))); + store(result + i, charsFromByte(load(ptr++))); i += 4; - ptr++; } return changetype(result); } + + /** Calculates the two char combination from the byte. */ + @inline function charsFromByte(byte: u32): u32 { + let hi = (byte >>> 4) & 0xF; + let lo = byte & 0xF; + hi += select(0x57, 0x30, hi > 9); + lo += select(0x57, 0x30, lo > 9); + return (lo << 16) | hi; + } } } From 23294299e3ee87ffc32b08d805221ba11f226b78 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 26 Aug 2019 21:21:32 +0300 Subject: [PATCH 2/2] remove inline for exported methods --- assembly/buffer/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index 0db71af..363ab5e 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -7,7 +7,7 @@ export class Buffer extends Uint8Array { super(size); } - @inline static alloc(size: i32): Buffer { + static alloc(size: i32): Buffer { return new Buffer(size); } @@ -23,7 +23,7 @@ export class Buffer extends Uint8Array { return result; } - @inline static isBuffer(value: T): bool { + static isBuffer(value: T): bool { return value instanceof Buffer; }