diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index b42edf7..11ee8c5 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -23,6 +23,12 @@ export class Buffer extends Uint8Array { return result; } + writeInt8(value: i8, offset: i32 = 0): i32 { + if(offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE); + store(this.dataStart + offset, value); + return offset + 1; + } + readInt8(offset: i32 = 0): i8 { 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 52f1eba..1e4f387 100644 --- a/assembly/node.d.ts +++ b/assembly/node.d.ts @@ -3,6 +3,8 @@ 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; + /** Writes an inputted value to the buffer, at the desired offset. */ + writeInt8(value:i8, offset?:i32): i32; /** Reads a signed integer at the designated offset. */ readInt8(offset?: i32): i8; } diff --git a/tests/buffer.spec.ts b/tests/buffer.spec.ts index d202073..eb0d0b1 100644 --- a/tests/buffer.spec.ts +++ b/tests/buffer.spec.ts @@ -43,6 +43,14 @@ describe("buffer", () => { // TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow(); }); + test("#writeInt8", () => { + let buff = new Buffer(5); + expect(buff.writeInt8(9)).toBe(1); + expect(buff.writeInt8(-3,4)).toBe(5); + expect(buff[0]).toBe(9); + expect(buff[4]).toBe(-3); + }); + test("#readInt8", () => { let buff = new Buffer(10); buff[0] = 5; @@ -57,4 +65,5 @@ describe("buffer", () => { // newBuff.readInt8(5); // }).toThrow(); }) + });