Skip to content

Commit 00e1d25

Browse files
RedDwarfianjtenner
authored andcommitted
[Implement] Buffer.writeUInt8 (#12)
1 parent 8613b14 commit 00e1d25

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

assembly/buffer/index.ts

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

26+
writeUInt8(value: u8, offset: i32 = 0): i32 {
27+
if(<u32>offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
28+
store<u8>(this.dataStart + offset, value);
29+
return offset + 1;
30+
}
31+
2632
writeInt8(value: i8, offset: i32 = 0): i32 {
2733
if(<u32>offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
2834
store<i8>(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+
/** Writes an inputted u8 value to the buffer, at the desired offset. */
7+
writeUInt8(value:u8, offset?:i32): i32;
68
/** Writes an inputted value to the buffer, at the desired offset. */
79
writeInt8(value:i8, offset?:i32): i32;
810
/** Reads a signed integer at the designated offset. */

tests/buffer.spec.ts

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

46+
test("#writeUInt8", () => {
47+
let buff = new Buffer(5);
48+
expect<i32>(buff.writeUInt8(4)).toBe(1);
49+
expect<i32>(buff.writeUInt8(252,4)).toBe(5);
50+
expect<u8>(buff[0]).toBe(4);
51+
expect<u8>(buff[4]).toBe(252);
52+
});
53+
4654
test("#writeInt8", () => {
4755
let buff = new Buffer(5);
4856
expect<i32>(buff.writeInt8(9)).toBe(1);
@@ -65,5 +73,4 @@ describe("buffer", () => {
6573
// newBuff.readInt8(5);
6674
// }).toThrow();
6775
})
68-
6976
});

0 commit comments

Comments
 (0)