start work on the web buffer implementation

This commit is contained in:
Holly Stubbs 2023-07-31 12:01:07 +01:00
parent 558c3c2025
commit 3e70f0db81
2 changed files with 70 additions and 1 deletions

View file

@ -12,7 +12,7 @@ const toolinglessFolderPath = __dirname.replace("/tooling", "/");
function readDir(nam:string) { function readDir(nam:string) {
const files = readdirSync(nam); const files = readdirSync(nam);
for (const file of files) { 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; continue;
} }

69
web/Buffer.ts Normal file
View file

@ -0,0 +1,69 @@
export class Buffer {
public data:Uint8Array;
public constructor(size:number, data?:Array<number>|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<Buffer>, 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;
}
}