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
90 changes: 78 additions & 12 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,70 +41,136 @@ export class Buffer extends Uint8Array {
}

readInt8(offset: i32 = 0): i8 {
if(i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + <usize>offset);
}

readUInt8(offset: i32 = 0): u8 {
if(i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u8>(this.dataStart + <usize>offset);
}

writeInt8(value: i8, offset: i32 = 0): i32 {
if(i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i8>(this.dataStart + offset, value);
return offset + 1;
}

writeUInt8(value: u8, offset: i32 = 0): i32 {
if(i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u8>(this.dataStart + offset, value);
return offset + 1;
}

readInt16LE(offset: i32 = 0): i16 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i16>(this.dataStart + <usize>offset);
}

readInt16BE(offset: i32 = 0): i16 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<i16>(load<i16>(this.dataStart + <usize>offset));
}

readUInt16LE(offset: i32 = 0): u16 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u16>(this.dataStart + <usize>offset);
}

readUInt16BE(offset: i32 = 0): u16 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<u16>(load<u16>(this.dataStart + <usize>offset));
}

writeInt16LE(value: i16, offset: i32 = 0): i32 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i16>(this.dataStart + offset, value);
return offset + 2;
}

writeInt16BE(value: i16, offset: i32 = 0): i32 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i16>(this.dataStart + offset, bswap<i16>(value));
return offset + 2;
}

writeUInt16LE(value: u16, offset: i32 = 0): i32 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u16>(this.dataStart + offset, value);
return offset + 2;
}

writeUInt16BE(value: u16, offset: i32 = 0): i32 {
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u16>(this.dataStart + offset, bswap<u16>(value));
return offset + 2;
}

readInt32LE(offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i32>(this.dataStart + <usize>offset);
}

readInt32BE(offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<i32>(load<i32>(this.dataStart + <usize>offset));
}

readUInt32LE(offset: i32 = 0): u32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u32>(this.dataStart + <usize>offset);
}

readUInt32BE(offset: i32 = 0): u32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return bswap<u32>(load<u32>(this.dataStart + <usize>offset));
}

writeInt32LE(value: i32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i32>(this.dataStart + offset, value);
return offset + 4;
}

writeInt32BE(value: i32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i32>(this.dataStart + offset, bswap<i32>(value));
return offset + 4;
}

writeUInt32LE(value: u32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u32>(this.dataStart + offset, value);
return offset + 4;
}

writeUInt32BE(value: u32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<u32>(this.dataStart + offset, bswap<u32>(value));
return offset + 4;
}

readFloatLE(offset: i32 = 0): f32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<f32>(this.dataStart + <usize>offset);
}

readFloatBE(offset: i32 = 0): f32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return reinterpret<f32>(bswap<i32>(load<i32>(this.dataStart + <usize>offset)));
}

writeFloatLE(value: f32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<f32>(this.dataStart + offset, value);
return offset + 4;
}

writeFloatBE(value: f32, offset: i32 = 0): i32 {
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
store<i32>(this.dataStart + offset, bswap<i32>(reinterpret<i32>(value)));
return offset + 4;
}
}

export namespace Buffer {
Expand Down
24 changes: 24 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ declare class Buffer extends Uint8Array {
writeUInt16LE(value: u16, offset?: i32): i32;
/** Writes an inputted unsigned 16-bit integer at the designated offset, stored in Big Endian format */
writeUInt16BE(value: u16, offset?: i32): i32;
/** Reads a signed 32-bit integer, stored in Little Endian format at the designated offset. */
readInt32LE(offset?: i32): i32;
/** Reads a signed 32-bit integer, stored in Big Endian format at the designated offset. */
readInt32BE(offset?: i32): i32;
/** Reads an unsigned 32-bit integer, stored in Little Endian format at the designated offset. */
readUInt32LE(offset?: i32): u32;
/** Reads an unsigned 32-bit integer, stored in Big Endian format at the designated offset. */
readUInt32BE(offset?: i32): u32;
/** Writes an inputted 32-bit integer at the designated offset, stored in Little Endian format */
writeInt32LE(value: i32, offset?: i32): i32;
/** Writes an inputted 32-bit integer at the designated offset, stored in Big Endian format */
writeInt32BE(value: i32, offset?: i32): i32;
/** Writes an inputted unsigned 32-bit integer at the designated offset, stored in Little Endian format */
writeUInt32LE(value: u32, offset?: i32): i32;
/** Writes an inputted unsigned 32-bit integer at the designated offset, stored in Big Endian format */
writeUInt32BE(value: u32, offset?: i32): i32;
/** Reads a signed 32-bit float, stored in Little Endian format at the designated offset. */
readFloatLE(offset?: i32): f32;
/** Reads a signed 32-bit float, stored in Big Endian format at the designated offset. */
readFloatBE(offset?: i32): f32;
/** Writes an inputted 32-bit float at the designated offset, stored in Little Endian format */
writeFloatLE(value: f32, offset?: i32): i32;
/** Writes an inputted 32-bit float at the designated offset, stored in Big Endian format */
writeFloatBE(value: f32, offset?: i32): i32;
}

declare namespace Buffer {
Expand Down
144 changes: 144 additions & 0 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,150 @@ describe("buffer", () => {
// }).toThrow();
});

test("#readInt32LE", () => {
let buff = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);
expect<i32>(buff.readInt32LE()).toBe(-559038737);
expect<i32>(buff.readInt32LE(4)).toBe(283033613);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readInt32LE(0);
// }).toThrow();
});

test("#readInt32BE", () => {
let buff = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
expect<i32>(buff.readInt32BE()).toBe(-559038737);
expect<i32>(buff.readInt32BE(4)).toBe(283033613);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readInt32BE(0);
// }).toThrow();
});

test("#readUInt32LE", () => {
let buff = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);
expect<u32>(buff.readUInt32LE()).toBe(3735928559);
expect<u32>(buff.readUInt32LE(4)).toBe(283033613);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readUInt32LE(0);
// }).toThrow();
});

test("#readUInt32BE", () => {
let buff = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
expect<u32>(buff.readUInt32BE()).toBe(3735928559);
expect<u32>(buff.readUInt32BE(4)).toBe(283033613);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readUInt32BE(0);
// }).toThrow();
});

test("#writeInt32LE", () => {
let buff = new Buffer(8);
expect<i32>(buff.writeInt32LE(-559038737)).toBe(4);
expect<i32>(buff.writeInt32LE(283033613,4)).toBe(8);
let result = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);
expect<Buffer>(buff).toStrictEqual(result);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeInt32LE(0);
// }).toThrow();
});

test("#writeInt32BE", () => {
let buff = new Buffer(8);
expect<i32>(buff.writeInt32BE(-559038737)).toBe(4);
expect<i32>(buff.writeInt32BE(283033613,4)).toBe(8);
let result = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
expect<Buffer>(buff).toStrictEqual(result);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeInt32BE(0);
// }).toThrow();
});

test("#writeUInt32LE", () => {
let buff = new Buffer(8);
expect<i32>(buff.writeUInt32LE(3735928559)).toBe(4);
expect<i32>(buff.writeUInt32LE(283033613,4)).toBe(8);
let result = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);;
expect<Buffer>(buff).toStrictEqual(result);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeUInt32LE(0);
// }).toThrow();
});

test("#writeUInt32BE", () => {
let buff = new Buffer(8);
expect<i32>(buff.writeUInt32BE(3735928559)).toBe(4);
expect<i32>(buff.writeUInt32BE(283033613,4)).toBe(8);
let result = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
expect<Buffer>(buff).toStrictEqual(result);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeUInt32BE(0);
// }).toThrow();
});

test("#readFloatLE", () => {
let buff = create<Buffer>([0xbb,0xfe,0x4a,0x4f,0x01,0x02,0x03,0x04]);
expect<f32>(buff.readFloatLE()).toBe(0xcafebabe);
expect<f32>(buff.readFloatLE(4)).toBe(1.539989614439558e-36);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readFloatLE(0);
// }).toThrow();
});

test("#readFloatBE", () => {
let buff = create<Buffer>([0x4f,0x4a,0xfe,0xbb,0x01,0x02,0x03,0x04]);
expect<f32>(buff.readFloatBE()).toBe(0xcafebabe);
expect<f32>(buff.readFloatBE(4)).toBe(2.387939260590663e-38);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readFloatBE(0);
// }).toThrow();
});

test("#writeFloatLE", () => {
let buff = new Buffer(8);
expect<i32>(buff.writeFloatLE(0xcafebabe)).toBe(4);
expect<i32>(buff.writeFloatLE(1.539989614439558e-36,4)).toBe(8);
let result = create<Buffer>([0xbb,0xfe,0x4a,0x4f,0x01,0x02,0x03,0x04]);
expect<Buffer>(buff).toStrictEqual(result);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeFloatLE(0);
// }).toThrow();
});

test("#writeFloatBE", () => {
let buff = new Buffer(8);
expect<i32>(buff.writeFloatBE(0xcafebabe)).toBe(4);
expect<i32>(buff.writeFloatBE(2.387939260590663e-38,4)).toBe(8);
let result = create<Buffer>([0x4f,0x4a,0xfe,0xbb,0x01,0x02,0x03,0x04]);
expect<Buffer>(buff).toStrictEqual(result);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeFloatBE(0);
// }).toThrow();
});

test("#subarray", () => {
let example = create<Buffer>([1, 2, 3, 4, 5, 6, 7, 8]);

Expand Down
4 changes: 3 additions & 1 deletion tests/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ function runTest(file, type, binary, wat) {
+ "." + type + ".wat";

// should not block testing
fs.writeFileSync(watPath, wat);
fs.writeFile(watPath, wat, (err) => {
if (err) console.warn(err);
});

const context = new TestContext({
fileName: file,
Expand Down