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

Filter by extension

Filter by extension

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

readInt8(offset: i32 = 0): i8 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + usize(offset));
}
}
2 changes: 2 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +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;
}
15 changes: 15 additions & 0 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ 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<i8>(buff.readInt8(0)).toBe(5);
expect<i8>(buff.readInt8()).toBe(5);
// Testing offset, and casting between u8 and i8.
expect<i8>(buff.readInt8(9)).toBe(-1);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readInt8(5);
// }).toThrow();
})
});