From 3e70f0db816833ee3b1b62733bc9f720bab90e2a Mon Sep 17 00:00:00 2001 From: Holly Date: Mon, 31 Jul 2023 12:01:07 +0100 Subject: [PATCH] start work on the web buffer implementation --- tooling/fileSmasher.ts | 2 +- web/Buffer.ts | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 web/Buffer.ts diff --git a/tooling/fileSmasher.ts b/tooling/fileSmasher.ts index 6026c37..a2cd23e 100644 --- a/tooling/fileSmasher.ts +++ b/tooling/fileSmasher.ts @@ -12,7 +12,7 @@ const toolinglessFolderPath = __dirname.replace("/tooling", "/"); function readDir(nam:string) { const files = readdirSync(nam); for (const file of files) { - if (nam == toolinglessFolderPath && (file.startsWith(".") || file == "tooling" || file == "lib" || file == "node_modules" || file == "combined.ts")) { + if (nam == toolinglessFolderPath && (file.startsWith(".") || file == "tooling" || file == "lib" || file == "web" || file == "node_modules" || file == "combined.ts")) { continue; } diff --git a/web/Buffer.ts b/web/Buffer.ts new file mode 100644 index 0000000..574c53a --- /dev/null +++ b/web/Buffer.ts @@ -0,0 +1,69 @@ +export class Buffer { + public data:Uint8Array; + + public constructor(size:number, data?:Array|Uint8Array) { + if (data !== undefined) { + let usableData:Uint8Array; + if (data instanceof Array) { + usableData = new Uint8Array(data); + } else { + usableData = data; + } + + this.data = usableData; + } else { + this.data = new Uint8Array(size); + } + } + + public get length() { + return this.data.length; + } + + // TODO: toString + + // TODO: Check correctness of this. + public static concat(buffers:Array, totalLength?:number) { + let joinedArrays:Uint8Array; + if (totalLength !== undefined) { + joinedArrays = new Uint8Array(totalLength); + } else { + let arraysLength = 0; + for (const buffer of buffers) { + arraysLength += buffer.length; + } + joinedArrays = new Uint8Array(arraysLength); + } + + let offset = 0; + for (const buffer of buffers) { + joinedArrays.set(buffer.data, offset); + offset += buffer.length; + } + + return new Buffer(0, joinedArrays); + } + + // Little Endian + + // Writer methods + public writeInt8(value:number, offset?:number) { + if (offset === undefined) { + // TODO: Handle writing without an offset + throw new Error("Writing without offset is currently unimplemented"); + } + if (value >= 0) { + this.data[offset] = value; + } else { + this.data[offset] = 256 + value; + } + } + + public writeUInt8(value:number, offset?:number) { + if (offset === undefined) { + // TODO: Handle writing without an offset + throw new Error("Writing without offset is currently unimplemented"); + } + this.data[offset] = value; + } +} \ No newline at end of file