From afbf5c1d8f5a7b2e0fa01a5841f1a8e07a485242 Mon Sep 17 00:00:00 2001 From: RedDwarfian Date: Sat, 20 Jul 2019 22:14:55 -0400 Subject: [PATCH 1/2] [Implement] Buffer.readUInt8 --- assembly/buffer/index.ts | 10 ++++++++++ assembly/node.d.ts | 3 +++ tests/buffer.spec.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index 66866bf..710ca67 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -22,4 +22,14 @@ export class Buffer extends Uint8Array { result.dataLength = size; return result; } + + readInt8(offset: i32 = 0): i8 { + if(offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE); + return load(this.dataStart + usize(offset)); + } + + readUInt8(offset: i32 = 0): u8 { + if(offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE); + return load(this.dataStart + usize(offset)); + } } diff --git a/assembly/node.d.ts b/assembly/node.d.ts index 80075a8..702db84 100644 --- a/assembly/node.d.ts +++ b/assembly/node.d.ts @@ -3,4 +3,7 @@ declare class Buffer extends Uint8Array { static alloc(size: i32): Buffer; /** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */ static allocUnsafe(size: i32): Buffer; + /** Reads a signed integer at the designated offset. */ + readInt8(offset?: i32): i8; + readUInt8(offset?: i32): u8; } diff --git a/tests/buffer.spec.ts b/tests/buffer.spec.ts index 1596845..ab8096d 100644 --- a/tests/buffer.spec.ts +++ b/tests/buffer.spec.ts @@ -42,4 +42,35 @@ describe("buffer", () => { // TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow(); // TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow(); }); + + test("#readInt8", () => { + let buff = new Buffer(10); + buff[0] = 5; + buff[9] = 255; + expect(buff.readInt8(0)).toBe(5); + expect(buff.readInt8()).toBe(5); + // Testing offset, and casting between u8 and i8. + expect(buff.readInt8(9)).toBe(-1); + // TODO: + // expectFn(() => { + // let newBuff = new Buffer(1); + // newBuff.readInt8(5); + // }).toThrow(); + }); + + test("#readUInt8", () => { + let buff = new Buffer(10); + buff[0] = -2; + buff[9] = 47; + // Testing casting between u8 and i8. + expect(buff.readUInt8(0)).toBe(254); + expect(buff.readUInt8()).toBe(254); + // Testing offset + expect(buff.readUInt8(9)).toBe(47); + // TODO: + // expectFn(() => { + // let newBuff = new Buffer(1); + // newBuff.readUInt8(5); + // }).toThrow(); + }); }); From e6de6c3003a7331249e653110b7a707a0bcac0fa Mon Sep 17 00:00:00 2001 From: RedDwarfian Date: Sun, 21 Jul 2019 11:51:07 -0400 Subject: [PATCH 2/2] [Cleanup] Removing readInt8 Clearing out a stray piece of code left over from a different branch. --- assembly/buffer/index.ts | 5 ----- assembly/node.d.ts | 3 +-- tests/buffer.spec.ts | 15 --------------- 3 files changed, 1 insertion(+), 22 deletions(-) diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index 710ca67..0fe0967 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -23,11 +23,6 @@ export class Buffer extends Uint8Array { return result; } - readInt8(offset: i32 = 0): i8 { - if(offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE); - return load(this.dataStart + usize(offset)); - } - readUInt8(offset: i32 = 0): u8 { if(offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE); return load(this.dataStart + usize(offset)); diff --git a/assembly/node.d.ts b/assembly/node.d.ts index 702db84..2614e4e 100644 --- a/assembly/node.d.ts +++ b/assembly/node.d.ts @@ -3,7 +3,6 @@ declare class Buffer extends Uint8Array { static alloc(size: i32): Buffer; /** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */ static allocUnsafe(size: i32): Buffer; - /** Reads a signed integer at the designated offset. */ - readInt8(offset?: i32): i8; + /** Reads an unsigned integer at the designated offset. */ readUInt8(offset?: i32): u8; } diff --git a/tests/buffer.spec.ts b/tests/buffer.spec.ts index ab8096d..86b610c 100644 --- a/tests/buffer.spec.ts +++ b/tests/buffer.spec.ts @@ -43,21 +43,6 @@ describe("buffer", () => { // TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow(); }); - test("#readInt8", () => { - let buff = new Buffer(10); - buff[0] = 5; - buff[9] = 255; - expect(buff.readInt8(0)).toBe(5); - expect(buff.readInt8()).toBe(5); - // Testing offset, and casting between u8 and i8. - expect(buff.readInt8(9)).toBe(-1); - // TODO: - // expectFn(() => { - // let newBuff = new Buffer(1); - // newBuff.readInt8(5); - // }).toThrow(); - }); - test("#readUInt8", () => { let buff = new Buffer(10); buff[0] = -2;