Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export class Buffer extends Uint8Array {
return result;
}

writeInt8(value: i8, offset: i32 = 0): i32 {
if(<u32>offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be offset >= this.dataLength?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I missed that. Thanks for the heads up.

store<i8>(this.dataStart + offset, value);
return offset + 1;
}

readInt8(offset: i32 = 0): i8 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + usize(offset));
Expand Down
2 changes: 2 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe("buffer", () => {
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
});

test("#writeInt8", () => {
let buff = new Buffer(5);
expect<i32>(buff.writeInt8(9)).toBe(1);
expect<i32>(buff.writeInt8(-3,4)).toBe(5);
expect<i8>(buff[0]).toBe(9);
expect<i8>(buff[4]).toBe(-3);
});

test("#readInt8", () => {
let buff = new Buffer(10);
buff[0] = 5;
Expand All @@ -57,4 +65,5 @@ describe("buffer", () => {
// newBuff.readInt8(5);
// }).toThrow();
})

});