Skip to content

Commit 5f0b0dc

Browse files
RedDwarfianjtenner
authored andcommitted
[Implement] Read/Write BigInt and Double (#21)
* [Implement] Read/Write BigInt and Double * [Fix] Small changes
1 parent f1056aa commit 5f0b0dc

3 files changed

Lines changed: 234 additions & 0 deletions

File tree

assembly/buffer/index.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,72 @@ export class Buffer extends Uint8Array {
171171
store<i32>(this.dataStart + offset, bswap<i32>(reinterpret<i32>(value)));
172172
return offset + 4;
173173
}
174+
175+
readBigInt64LE(offset: i32 = 0): i64 {
176+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
177+
return load<i64>(this.dataStart + <usize>offset);
178+
}
179+
180+
readBigInt64BE(offset: i32 = 0): i64 {
181+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
182+
return bswap<i64>(load<i64>(this.dataStart + <usize>offset));
183+
}
184+
185+
readBigUInt64LE(offset: i32 = 0): u64 {
186+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
187+
return load<u64>(this.dataStart + <usize>offset);
188+
}
189+
190+
readBigUInt64BE(offset: i32 = 0): u64 {
191+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
192+
return bswap<u64>(load<u64>(this.dataStart + <usize>offset));
193+
}
194+
195+
writeBigInt64LE(value: i64, offset: i32 = 0): i32 {
196+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
197+
store<i64>(this.dataStart + offset, value);
198+
return offset + 8;
199+
}
200+
201+
writeBigInt64BE(value: i64, offset: i32 = 0): i32 {
202+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
203+
store<i64>(this.dataStart + offset, bswap<i64>(value));
204+
return offset + 8;
205+
}
206+
207+
writeBigUInt64LE(value: u64, offset: i32 = 0): i32 {
208+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
209+
store<u64>(this.dataStart + offset, value);
210+
return offset + 8;
211+
}
212+
213+
writeBigUInt64BE(value: u64, offset: i32 = 0): i32 {
214+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
215+
store<u64>(this.dataStart + offset, bswap<u64>(value));
216+
return offset + 8;
217+
}
218+
219+
readDoubleLE(offset: i32 = 0): f64 {
220+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
221+
return load<f64>(this.dataStart + <usize>offset);
222+
}
223+
224+
readDoubleBE(offset: i32 = 0): f64 {
225+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
226+
return reinterpret<f64>(bswap<i64>(load<i64>(this.dataStart + <usize>offset)));
227+
}
228+
229+
writeDoubleLE(value: f64, offset: i32 = 0): i32 {
230+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
231+
store<f64>(this.dataStart + offset, value);
232+
return offset + 8;
233+
}
234+
235+
writeDoubleBE(value: f64, offset: i32 = 0): i32 {
236+
if (i32(offset < 0) | i32(<u32>offset + 8 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
237+
store<i64>(this.dataStart + offset, bswap<i64>(reinterpret<i64>(value)));
238+
return offset + 8;
239+
}
174240
}
175241

176242
export namespace Buffer {

assembly/node.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ declare class Buffer extends Uint8Array {
5353
writeFloatLE(value: f32, offset?: i32): i32;
5454
/** Writes an inputted 32-bit float at the designated offset, stored in Big Endian format */
5555
writeFloatBE(value: f32, offset?: i32): i32;
56+
/** Reads a signed 64-bit integer, stored in Little Endian format at the designated offset. */
57+
readBigInt64LE(offset?: i32): i64;
58+
/** Reads a signed 64-bit integer, stored in Big Endian format at the designated offset. */
59+
readBigInt64BE(offset?: i32): i64;
60+
/** Reads an unsigned 64-bit integer, stored in Little Endian format at the designated offset. */
61+
readBigUInt64LE(offset?: i32): u64;
62+
/** Reads an unsigned 64-bit integer, stored in Big Endian format at the designated offset. */
63+
readBigUInt64BE(offset?: i32): u64;
64+
/** Writes an inputted 64-bit integer at the designated offset, stored in Little Endian format */
65+
writeBigInt64LE(value: i64, offset?: i32): i32;
66+
/** Writes an inputted 64-bit integer at the designated offset, stored in Big Endian format */
67+
writeBigInt64BE(value: i64, offset?: i32): i32;
68+
/** Writes an inputted unsigned 64-bit integer at the designated offset, stored in Little Endian format */
69+
writeBigUInt64LE(value: u64, offset?: i32): i32;
70+
/** Writes an inputted unsigned 64-bit integer at the designated offset, stored in Big Endian format */
71+
writeBigUInt64BE(value: u64, offset?: i32): i32;
72+
/** Reads a signed 64-bit double, stored in Little Endian format at the designated offset. */
73+
readDoubleLE(offset?: i32): f64;
74+
/** Reads a signed 64-bit double, stored in Big Endian format at the designated offset. */
75+
readDoubleBE(offset?: i32): f64;
76+
/** Writes an inputted 64-bit double at the designated offset, stored in Little Endian format */
77+
writeDoubleLE(value: f64, offset?: i32): i32;
78+
/** Writes an inputted 64-bit double at the designated offset, stored in Big Endian format */
79+
writeDoubleBE(value: f64, offset?: i32): i32;
5680
}
5781

5882
declare namespace Buffer {

tests/buffer.spec.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,150 @@ describe("buffer", () => {
358358
// }).toThrow();
359359
});
360360

361+
test("#readBigInt64LE", () => {
362+
let buff = create<Buffer>([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00]);
363+
expect<i64>(buff.readBigInt64LE()).toBe(-4294967296);
364+
expect<i64>(buff.readBigInt64LE(8)).toBe(4294967295);
365+
// TODO:
366+
// expectFn(() => {
367+
// let newBuff = new Buffer(1);
368+
// newBuff.readBigInt64LE(0);
369+
// }).toThrow();
370+
});
371+
372+
test("#readBigInt64BE", () => {
373+
let buff = create<Buffer>([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00]);
374+
expect<i64>(buff.readBigInt64BE()).toBe(4294967295);
375+
expect<i64>(buff.readBigInt64BE(8)).toBe(-4294967296);
376+
// TODO:
377+
// expectFn(() => {
378+
// let newBuff = new Buffer(1);
379+
// newBuff.readBigInt64BE(0);
380+
// }).toThrow();
381+
});
382+
383+
test("#readBigUInt64LE", () => {
384+
let buff = create<Buffer>([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00]);
385+
expect<u64>(buff.readBigUInt64LE()).toBe(18446744069414584320);
386+
expect<u64>(buff.readBigUInt64LE(8)).toBe(4294967295);
387+
// TODO:
388+
// expectFn(() => {
389+
// let newBuff = new Buffer(1);
390+
// newBuff.readBigUInt64LE(0);
391+
// }).toThrow();
392+
});
393+
394+
test("#readBigUInt64BE", () => {
395+
let buff = create<Buffer>([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00]);
396+
expect<u64>(buff.readBigUInt64BE()).toBe(4294967295);
397+
expect<u64>(buff.readBigUInt64BE(8)).toBe(18446744069414584320);
398+
// TODO:
399+
// expectFn(() => {
400+
// let newBuff = new Buffer(1);
401+
// newBuff.readBigUInt64BE(0);
402+
// }).toThrow();
403+
});
404+
405+
test("#writeBigInt64LE", () => {
406+
let buff = new Buffer(16);
407+
expect<i64>(buff.writeBigInt64LE(-559038737)).toBe(8);
408+
expect<i64>(buff.writeBigInt64LE(283033613,8)).toBe(16);
409+
let result = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0xFF,0xFF,0xFF,0xFF,0x0d,0xc0,0xde,0x10,0x00,0x00,0x00,0x00]);
410+
expect<Buffer>(buff).toStrictEqual(result);
411+
// TODO:
412+
// expectFn(() => {
413+
// let newBuff = new Buffer(1);
414+
// newBuff.writeBigInt64LE(0);
415+
// }).toThrow();
416+
});
417+
418+
test("#writeBigInt64BE", () => {
419+
let buff = new Buffer(16);
420+
expect<i64>(buff.writeBigInt64BE(-559038737)).toBe(8);
421+
expect<i64>(buff.writeBigInt64BE(283033613,8)).toBe(16);
422+
let result = create<Buffer>([0xFF,0xFF,0xFF,0xFF,0xDE,0xAD,0xBE,0xEF,0x00,0x00,0x00,0x00,0x10,0xde,0xc0,0x0d]);
423+
expect<Buffer>(buff).toStrictEqual(result);
424+
// TODO:
425+
// expectFn(() => {
426+
// let newBuff = new Buffer(1);
427+
// newBuff.writeBigInt64BE(0);
428+
// }).toThrow();
429+
});
430+
431+
test("#writeBigUInt64LE", () => {
432+
let buff = new Buffer(16);
433+
expect<i64>(buff.writeBigUInt64LE(3735928559)).toBe(8);
434+
expect<i64>(buff.writeBigUInt64LE(283033613,8)).toBe(16);
435+
let result = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x00,0x00,0x00,0x00,0x0d,0xc0,0xde,0x10,0x00,0x00,0x00,0x00]);
436+
expect<Buffer>(buff).toStrictEqual(result);
437+
// TODO:
438+
// expectFn(() => {
439+
// let newBuff = new Buffer(1);
440+
// newBuff.writeBigUInt64LE(0);
441+
// }).toThrow();
442+
});
443+
444+
test("#writeBigUInt64BE", () => {
445+
let buff = new Buffer(16);
446+
expect<i64>(buff.writeBigUInt64BE(3735928559)).toBe(8);
447+
expect<i64>(buff.writeBigUInt64BE(283033613,8)).toBe(16);
448+
let result = create<Buffer>([0x00,0x00,0x00,0x00,0xDE,0xAD,0xBE,0xEF,0x00,0x00,0x00,0x00,0x10,0xde,0xc0,0x0d]);
449+
expect<Buffer>(buff).toStrictEqual(result);
450+
// TODO:
451+
// expectFn(() => {
452+
// let newBuff = new Buffer(1);
453+
// newBuff.writeBigUInt64BE(0);
454+
// }).toThrow();
455+
});
456+
457+
test("#readDoubleLE", () => {
458+
let buff = create<Buffer>([0x77, 0xbe, 0x9f, 0x1a, 0x2f, 0xdd, 0x5e, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
459+
expect<f64>(buff.readDoubleLE()).toBe(123.456);
460+
expect<f64>(buff.readDoubleLE(8)).toBe(5.447603722011605e-270);
461+
// TODO:
462+
// expectFn(() => {
463+
// let newBuff = new Buffer(1);
464+
// newBuff.readDoubleLE(0);
465+
// }).toThrow();
466+
});
467+
468+
test("#readDoubleBE", () => {
469+
let buff = create<Buffer>([0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f, 0xbe, 0x77, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
470+
expect<f64>(buff.readDoubleBE()).toBe(123.456);
471+
expect<f64>(buff.readDoubleBE(8)).toBe(8.20788039913184e-304);
472+
// TODO:
473+
// expectFn(() => {
474+
// let newBuff = new Buffer(1);
475+
// newBuff.readDoubleBE(0);
476+
// }).toThrow();
477+
});
478+
479+
test("#writeDoubleLE", () => {
480+
let buff = new Buffer(16);
481+
expect<i32>(buff.writeDoubleLE(123.456)).toBe(8);
482+
expect<i32>(buff.writeDoubleLE(5.447603722011605e-270,8)).toBe(16);
483+
let result = create<Buffer>([0x77, 0xbe, 0x9f, 0x1a, 0x2f, 0xdd, 0x5e, 0x40, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
484+
expect<Buffer>(buff).toStrictEqual(result);
485+
// TODO:
486+
// expectFn(() => {
487+
// let newBuff = new Buffer(1);
488+
// newBuff.writeDoubleLE(0);
489+
// }).toThrow();
490+
});
491+
492+
test("#writeDoubleBE", () => {
493+
let buff = new Buffer(16);
494+
expect<i32>(buff.writeDoubleBE(123.456)).toBe(8);
495+
expect<i32>(buff.writeDoubleBE(8.20788039913184e-304,8)).toBe(16);
496+
let result = create<Buffer>([0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f, 0xbe, 0x77, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
497+
expect<Buffer>(buff).toStrictEqual(result);
498+
// TODO:
499+
// expectFn(() => {
500+
// let newBuff = new Buffer(1);
501+
// newBuff.writeDoubleBE(0);
502+
// }).toThrow();
503+
});
504+
361505
test("#subarray", () => {
362506
let example = create<Buffer>([1, 2, 3, 4, 5, 6, 7, 8]);
363507

0 commit comments

Comments
 (0)