Skip to content

Commit 1b2fe13

Browse files
RedDwarfianjtenner
authored andcommitted
Buffer readuint8 (#11)
* [Implement] Buffer.readUInt8 * [Cleanup] Removing readInt8 Clearing out a stray piece of code left over from a different branch.
1 parent 00e1d25 commit 1b2fe13

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

assembly/buffer/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export class Buffer extends Uint8Array {
2323
return result;
2424
}
2525

26+
readUInt8(offset: i32 = 0): u8 {
27+
if(<u32>offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
28+
return load<u8>(this.dataStart + usize(offset));
29+
}
30+
2631
writeUInt8(value: u8, offset: i32 = 0): i32 {
2732
if(<u32>offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
2833
store<u8>(this.dataStart + offset, value);

assembly/node.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ declare class Buffer extends Uint8Array {
33
static alloc(size: i32): Buffer;
44
/** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */
55
static allocUnsafe(size: i32): Buffer;
6+
/** Reads an unsigned integer at the designated offset. */
7+
readUInt8(offset?: i32): u8;
68
/** Writes an inputted u8 value to the buffer, at the desired offset. */
79
writeUInt8(value:u8, offset?:i32): i32;
810
/** Writes an inputted value to the buffer, at the desired offset. */

tests/buffer.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ describe("buffer", () => {
4343
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
4444
});
4545

46+
test("#readUInt8", () => {
47+
let buff = new Buffer(10);
48+
buff[0] = -2;
49+
buff[9] = 47;
50+
// Testing casting between u8 and i8.
51+
expect<u8>(buff.readUInt8(0)).toBe(254);
52+
expect<u8>(buff.readUInt8()).toBe(254);
53+
// Testing offset
54+
expect<u8>(buff.readUInt8(9)).toBe(47);
55+
// TODO:
56+
// expectFn(() => {
57+
// let newBuff = new Buffer(1);
58+
// newBuff.readUInt8(5);
59+
// }).toThrow();
60+
});
61+
4662
test("#writeUInt8", () => {
4763
let buff = new Buffer(5);
4864
expect<i32>(buff.writeUInt8(4)).toBe(1);
@@ -72,5 +88,5 @@ describe("buffer", () => {
7288
// let newBuff = new Buffer(1);
7389
// newBuff.readInt8(5);
7490
// }).toThrow();
75-
})
91+
});
7692
});

0 commit comments

Comments
 (0)