Skip to content

Commit 8613b14

Browse files
RedDwarfianjtenner
authored andcommitted
[Implement] Buffer.writeInt8 (#13)
1 parent 364fe63 commit 8613b14

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

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+
writeInt8(value: i8, offset: i32 = 0): i32 {
27+
if(<u32>offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
28+
store<i8>(this.dataStart + offset, value);
29+
return offset + 1;
30+
}
31+
2632
readInt8(offset: i32 = 0): i8 {
2733
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
2834
return load<i8>(this.dataStart + usize(offset));

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 value to the buffer, at the desired offset. */
7+
writeInt8(value:i8, offset?:i32): i32;
68
/** Reads a signed integer at the designated offset. */
79
readInt8(offset?: i32): i8;
810
}

tests/buffer.spec.ts

Lines changed: 9 additions & 0 deletions
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("#writeInt8", () => {
47+
let buff = new Buffer(5);
48+
expect<i32>(buff.writeInt8(9)).toBe(1);
49+
expect<i32>(buff.writeInt8(-3,4)).toBe(5);
50+
expect<i8>(buff[0]).toBe(9);
51+
expect<i8>(buff[4]).toBe(-3);
52+
});
53+
4654
test("#readInt8", () => {
4755
let buff = new Buffer(10);
4856
buff[0] = 5;
@@ -57,4 +65,5 @@ describe("buffer", () => {
5765
// newBuff.readInt8(5);
5866
// }).toThrow();
5967
})
68+
6069
});

0 commit comments

Comments
 (0)