Skip to content

Commit f1056aa

Browse files
RedDwarfianjtenner
authored andcommitted
[Implement] Buffer Read/Write 32 Bit Int/Float (#19)
* [Implement] Buffer Handling (U)Int32 * [Cleanup] Actually using signed int 32, now. * [Implement] Buffer Handling Float * [Bug] Copy-Paste Issue * [Cleanup] Simplified load and store for LE Floats * [Chore] fix space between if keywords
1 parent 04668b8 commit f1056aa

4 files changed

Lines changed: 249 additions & 13 deletions

File tree

assembly/buffer/index.ts

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,70 +41,136 @@ export class Buffer extends Uint8Array {
4141
}
4242

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

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

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

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

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

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

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

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

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

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

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

103103
writeUInt16BE(value: u16, offset: i32 = 0): i32 {
104-
if(i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
104+
if (i32(offset < 0) | i32(<u32>offset + 2 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
105105
store<u16>(this.dataStart + offset, bswap<u16>(value));
106106
return offset + 2;
107107
}
108+
109+
readInt32LE(offset: i32 = 0): i32 {
110+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
111+
return load<i32>(this.dataStart + <usize>offset);
112+
}
113+
114+
readInt32BE(offset: i32 = 0): i32 {
115+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
116+
return bswap<i32>(load<i32>(this.dataStart + <usize>offset));
117+
}
118+
119+
readUInt32LE(offset: i32 = 0): u32 {
120+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
121+
return load<u32>(this.dataStart + <usize>offset);
122+
}
123+
124+
readUInt32BE(offset: i32 = 0): u32 {
125+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
126+
return bswap<u32>(load<u32>(this.dataStart + <usize>offset));
127+
}
128+
129+
writeInt32LE(value: i32, offset: i32 = 0): i32 {
130+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
131+
store<i32>(this.dataStart + offset, value);
132+
return offset + 4;
133+
}
134+
135+
writeInt32BE(value: i32, offset: i32 = 0): i32 {
136+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
137+
store<i32>(this.dataStart + offset, bswap<i32>(value));
138+
return offset + 4;
139+
}
140+
141+
writeUInt32LE(value: u32, offset: i32 = 0): i32 {
142+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
143+
store<u32>(this.dataStart + offset, value);
144+
return offset + 4;
145+
}
146+
147+
writeUInt32BE(value: u32, offset: i32 = 0): i32 {
148+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
149+
store<u32>(this.dataStart + offset, bswap<u32>(value));
150+
return offset + 4;
151+
}
152+
153+
readFloatLE(offset: i32 = 0): f32 {
154+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
155+
return load<f32>(this.dataStart + <usize>offset);
156+
}
157+
158+
readFloatBE(offset: i32 = 0): f32 {
159+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
160+
return reinterpret<f32>(bswap<i32>(load<i32>(this.dataStart + <usize>offset)));
161+
}
162+
163+
writeFloatLE(value: f32, offset: i32 = 0): i32 {
164+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
165+
store<f32>(this.dataStart + offset, value);
166+
return offset + 4;
167+
}
168+
169+
writeFloatBE(value: f32, offset: i32 = 0): i32 {
170+
if (i32(offset < 0) | i32(<u32>offset + 4 > this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
171+
store<i32>(this.dataStart + offset, bswap<i32>(reinterpret<i32>(value)));
172+
return offset + 4;
173+
}
108174
}
109175

110176
export namespace Buffer {

assembly/node.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ declare class Buffer extends Uint8Array {
2929
writeUInt16LE(value: u16, offset?: i32): i32;
3030
/** Writes an inputted unsigned 16-bit integer at the designated offset, stored in Big Endian format */
3131
writeUInt16BE(value: u16, offset?: i32): i32;
32+
/** Reads a signed 32-bit integer, stored in Little Endian format at the designated offset. */
33+
readInt32LE(offset?: i32): i32;
34+
/** Reads a signed 32-bit integer, stored in Big Endian format at the designated offset. */
35+
readInt32BE(offset?: i32): i32;
36+
/** Reads an unsigned 32-bit integer, stored in Little Endian format at the designated offset. */
37+
readUInt32LE(offset?: i32): u32;
38+
/** Reads an unsigned 32-bit integer, stored in Big Endian format at the designated offset. */
39+
readUInt32BE(offset?: i32): u32;
40+
/** Writes an inputted 32-bit integer at the designated offset, stored in Little Endian format */
41+
writeInt32LE(value: i32, offset?: i32): i32;
42+
/** Writes an inputted 32-bit integer at the designated offset, stored in Big Endian format */
43+
writeInt32BE(value: i32, offset?: i32): i32;
44+
/** Writes an inputted unsigned 32-bit integer at the designated offset, stored in Little Endian format */
45+
writeUInt32LE(value: u32, offset?: i32): i32;
46+
/** Writes an inputted unsigned 32-bit integer at the designated offset, stored in Big Endian format */
47+
writeUInt32BE(value: u32, offset?: i32): i32;
48+
/** Reads a signed 32-bit float, stored in Little Endian format at the designated offset. */
49+
readFloatLE(offset?: i32): f32;
50+
/** Reads a signed 32-bit float, stored in Big Endian format at the designated offset. */
51+
readFloatBE(offset?: i32): f32;
52+
/** Writes an inputted 32-bit float at the designated offset, stored in Little Endian format */
53+
writeFloatLE(value: f32, offset?: i32): i32;
54+
/** Writes an inputted 32-bit float at the designated offset, stored in Big Endian format */
55+
writeFloatBE(value: f32, offset?: i32): i32;
3256
}
3357

3458
declare namespace Buffer {

tests/buffer.spec.ts

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

217+
test("#readInt32LE", () => {
218+
let buff = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);
219+
expect<i32>(buff.readInt32LE()).toBe(-559038737);
220+
expect<i32>(buff.readInt32LE(4)).toBe(283033613);
221+
// TODO:
222+
// expectFn(() => {
223+
// let newBuff = new Buffer(1);
224+
// newBuff.readInt32LE(0);
225+
// }).toThrow();
226+
});
227+
228+
test("#readInt32BE", () => {
229+
let buff = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
230+
expect<i32>(buff.readInt32BE()).toBe(-559038737);
231+
expect<i32>(buff.readInt32BE(4)).toBe(283033613);
232+
// TODO:
233+
// expectFn(() => {
234+
// let newBuff = new Buffer(1);
235+
// newBuff.readInt32BE(0);
236+
// }).toThrow();
237+
});
238+
239+
test("#readUInt32LE", () => {
240+
let buff = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);
241+
expect<u32>(buff.readUInt32LE()).toBe(3735928559);
242+
expect<u32>(buff.readUInt32LE(4)).toBe(283033613);
243+
// TODO:
244+
// expectFn(() => {
245+
// let newBuff = new Buffer(1);
246+
// newBuff.readUInt32LE(0);
247+
// }).toThrow();
248+
});
249+
250+
test("#readUInt32BE", () => {
251+
let buff = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
252+
expect<u32>(buff.readUInt32BE()).toBe(3735928559);
253+
expect<u32>(buff.readUInt32BE(4)).toBe(283033613);
254+
// TODO:
255+
// expectFn(() => {
256+
// let newBuff = new Buffer(1);
257+
// newBuff.readUInt32BE(0);
258+
// }).toThrow();
259+
});
260+
261+
test("#writeInt32LE", () => {
262+
let buff = new Buffer(8);
263+
expect<i32>(buff.writeInt32LE(-559038737)).toBe(4);
264+
expect<i32>(buff.writeInt32LE(283033613,4)).toBe(8);
265+
let result = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);
266+
expect<Buffer>(buff).toStrictEqual(result);
267+
// TODO:
268+
// expectFn(() => {
269+
// let newBuff = new Buffer(1);
270+
// newBuff.writeInt32LE(0);
271+
// }).toThrow();
272+
});
273+
274+
test("#writeInt32BE", () => {
275+
let buff = new Buffer(8);
276+
expect<i32>(buff.writeInt32BE(-559038737)).toBe(4);
277+
expect<i32>(buff.writeInt32BE(283033613,4)).toBe(8);
278+
let result = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
279+
expect<Buffer>(buff).toStrictEqual(result);
280+
// TODO:
281+
// expectFn(() => {
282+
// let newBuff = new Buffer(1);
283+
// newBuff.writeInt32BE(0);
284+
// }).toThrow();
285+
});
286+
287+
test("#writeUInt32LE", () => {
288+
let buff = new Buffer(8);
289+
expect<i32>(buff.writeUInt32LE(3735928559)).toBe(4);
290+
expect<i32>(buff.writeUInt32LE(283033613,4)).toBe(8);
291+
let result = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);;
292+
expect<Buffer>(buff).toStrictEqual(result);
293+
// TODO:
294+
// expectFn(() => {
295+
// let newBuff = new Buffer(1);
296+
// newBuff.writeUInt32LE(0);
297+
// }).toThrow();
298+
});
299+
300+
test("#writeUInt32BE", () => {
301+
let buff = new Buffer(8);
302+
expect<i32>(buff.writeUInt32BE(3735928559)).toBe(4);
303+
expect<i32>(buff.writeUInt32BE(283033613,4)).toBe(8);
304+
let result = create<Buffer>([0xDE,0xAD,0xBE,0xEF,0x10,0xde,0xc0,0x0d]);
305+
expect<Buffer>(buff).toStrictEqual(result);
306+
// TODO:
307+
// expectFn(() => {
308+
// let newBuff = new Buffer(1);
309+
// newBuff.writeUInt32BE(0);
310+
// }).toThrow();
311+
});
312+
313+
test("#readFloatLE", () => {
314+
let buff = create<Buffer>([0xbb,0xfe,0x4a,0x4f,0x01,0x02,0x03,0x04]);
315+
expect<f32>(buff.readFloatLE()).toBe(0xcafebabe);
316+
expect<f32>(buff.readFloatLE(4)).toBe(1.539989614439558e-36);
317+
// TODO:
318+
// expectFn(() => {
319+
// let newBuff = new Buffer(1);
320+
// newBuff.readFloatLE(0);
321+
// }).toThrow();
322+
});
323+
324+
test("#readFloatBE", () => {
325+
let buff = create<Buffer>([0x4f,0x4a,0xfe,0xbb,0x01,0x02,0x03,0x04]);
326+
expect<f32>(buff.readFloatBE()).toBe(0xcafebabe);
327+
expect<f32>(buff.readFloatBE(4)).toBe(2.387939260590663e-38);
328+
// TODO:
329+
// expectFn(() => {
330+
// let newBuff = new Buffer(1);
331+
// newBuff.readFloatBE(0);
332+
// }).toThrow();
333+
});
334+
335+
test("#writeFloatLE", () => {
336+
let buff = new Buffer(8);
337+
expect<i32>(buff.writeFloatLE(0xcafebabe)).toBe(4);
338+
expect<i32>(buff.writeFloatLE(1.539989614439558e-36,4)).toBe(8);
339+
let result = create<Buffer>([0xbb,0xfe,0x4a,0x4f,0x01,0x02,0x03,0x04]);
340+
expect<Buffer>(buff).toStrictEqual(result);
341+
// TODO:
342+
// expectFn(() => {
343+
// let newBuff = new Buffer(1);
344+
// newBuff.writeFloatLE(0);
345+
// }).toThrow();
346+
});
347+
348+
test("#writeFloatBE", () => {
349+
let buff = new Buffer(8);
350+
expect<i32>(buff.writeFloatBE(0xcafebabe)).toBe(4);
351+
expect<i32>(buff.writeFloatBE(2.387939260590663e-38,4)).toBe(8);
352+
let result = create<Buffer>([0x4f,0x4a,0xfe,0xbb,0x01,0x02,0x03,0x04]);
353+
expect<Buffer>(buff).toStrictEqual(result);
354+
// TODO:
355+
// expectFn(() => {
356+
// let newBuff = new Buffer(1);
357+
// newBuff.writeFloatBE(0);
358+
// }).toThrow();
359+
});
360+
217361
test("#subarray", () => {
218362
let example = create<Buffer>([1, 2, 3, 4, 5, 6, 7, 8]);
219363

tests/node.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ function runTest(file, type, binary, wat) {
109109
+ "." + type + ".wat";
110110

111111
// should not block testing
112-
fs.writeFileSync(watPath, wat);
112+
fs.writeFile(watPath, wat, (err) => {
113+
if (err) console.warn(err);
114+
});
113115

114116
const context = new TestContext({
115117
fileName: file,

0 commit comments

Comments
 (0)