From 541df5ba28d282dfcef45ad5441d19cf9b391bbc Mon Sep 17 00:00:00 2001 From: MierenManz <63878374+MierenManz@users.noreply.github.com> Date: Tue, 12 Nov 2024 23:38:45 +0100 Subject: [PATCH] Fix errors (#44) --- .github/workflows/checks.yml | 2 +- src/array/array.ts | 4 ++-- src/array/array_buffer.ts | 4 ---- src/array/sized_array.ts | 8 ++++++-- src/array/typed_array.ts | 4 ---- src/bitflags/bitflags16.ts | 4 ---- src/bitflags/bitflags32.ts | 4 ---- src/bitflags/bitflags64.ts | 4 ---- src/bitflags/bitflags8.ts | 4 ---- src/compound/sized_struct.ts | 8 ++++++-- src/compound/struct.ts | 8 ++++++-- src/compound/tagged_union.ts | 4 ++-- src/compound/tuple.ts | 8 ++++++-- src/compound/union.ts | 8 ++++++-- src/primitives/bool.ts | 6 +----- src/primitives/f32.ts | 2 -- src/primitives/f64.ts | 2 -- src/primitives/i16.ts | 2 -- src/primitives/i32.ts | 2 -- src/primitives/i64.ts | 2 -- src/primitives/i8.ts | 2 -- src/primitives/u16.ts | 2 -- src/primitives/u32.ts | 2 -- src/primitives/u64.ts | 2 -- src/primitives/u8.ts | 2 -- src/string/fixed_length.ts | 4 ---- src/string/prefixed_length.ts | 4 ++-- src/types/sized.ts | 7 ------- 28 files changed, 38 insertions(+), 77 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 6aa1dd1..2552159 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -28,4 +28,4 @@ jobs: uses: denoland/setup-deno@main - name: Run deno test - run: deno test --allow-none + run: deno test diff --git a/src/array/array.ts b/src/array/array.ts index 44ceedb..1cd6118 100644 --- a/src/array/array.ts +++ b/src/array/array.ts @@ -18,7 +18,7 @@ export class ArrayType extends UnsizedType { return result; } - read(dt: DataView, options: Options = { byteOffset: 0 }): T[] { + override read(dt: DataView, options: Options = { byteOffset: 0 }): T[] { if (this.length === 0) return []; const result = new Array(this.length); const { type } = this; @@ -47,7 +47,7 @@ export class ArrayType extends UnsizedType { } } - write( + override write( value: T[], dt: DataView, options: Options = { byteOffset: 0 }, diff --git a/src/array/array_buffer.ts b/src/array/array_buffer.ts index 87b9cc9..acc93fe 100644 --- a/src/array/array_buffer.ts +++ b/src/array/array_buffer.ts @@ -9,8 +9,6 @@ export class ArrayBufferType extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): ArrayBuffer { - super.rangeCheck(dt.byteLength, options.byteOffset); - const resultAB = new ArrayBuffer(this.byteSize); const resultView = new Uint8Array(resultAB); @@ -32,8 +30,6 @@ export class ArrayBufferType extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - const view = new Uint8Array( dt.buffer, dt.byteOffset + options.byteOffset, diff --git a/src/array/sized_array.ts b/src/array/sized_array.ts index ff39632..9409818 100644 --- a/src/array/sized_array.ts +++ b/src/array/sized_array.ts @@ -13,7 +13,7 @@ export class SizedArrayType extends SizedType implements ArrayType { return this.#inner.readPacked(dt, options); } - read(dt: DataView, options: Options = { byteOffset: 0 }): T[] { + override read(dt: DataView, options: Options = { byteOffset: 0 }): T[] { return this.#inner.read(dt, options); } @@ -25,7 +25,11 @@ export class SizedArrayType extends SizedType implements ArrayType { this.#inner.writePacked(value, dt, options); } - write(value: T[], dt: DataView, options: Options = { byteOffset: 0 }): void { + override write( + value: T[], + dt: DataView, + options: Options = { byteOffset: 0 }, + ): void { this.#inner.write(value, dt, options); } } diff --git a/src/array/typed_array.ts b/src/array/typed_array.ts index 0ef1579..5150996 100644 --- a/src/array/typed_array.ts +++ b/src/array/typed_array.ts @@ -39,8 +39,6 @@ export class TypedArray extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): T { - super.rangeCheck(dt.byteLength, options.byteOffset); - const value = new this.arrayConstructor( dt.buffer, dt.byteOffset + options.byteOffset, @@ -56,8 +54,6 @@ export class TypedArray extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - const view = new this.arrayConstructor( dt.buffer, dt.byteOffset + options.byteOffset, diff --git a/src/bitflags/bitflags16.ts b/src/bitflags/bitflags16.ts index 369a61c..8955762 100644 --- a/src/bitflags/bitflags16.ts +++ b/src/bitflags/bitflags16.ts @@ -13,8 +13,6 @@ export class BitFlags16< } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O { - super.rangeCheck(dt.byteLength, options.byteOffset); - const returnObject: Record = {}; const byteBag = dt.getUint16(options.byteOffset); @@ -32,8 +30,6 @@ export class BitFlags16< dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - let flags = 0; for (const { 0: key, 1: flagValue } of this.#recordEntries) { diff --git a/src/bitflags/bitflags32.ts b/src/bitflags/bitflags32.ts index c66fffc..e0a5e5c 100644 --- a/src/bitflags/bitflags32.ts +++ b/src/bitflags/bitflags32.ts @@ -13,8 +13,6 @@ export class BitFlags32< } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O { - super.rangeCheck(dt.byteLength, options.byteOffset); - const returnObject: Record = {}; const byteBag = dt.getUint32(options.byteOffset); @@ -32,8 +30,6 @@ export class BitFlags32< dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - let flags = 0; for (const { 0: key, 1: flagValue } of this.#recordEntries) { diff --git a/src/bitflags/bitflags64.ts b/src/bitflags/bitflags64.ts index 08db206..6b5f742 100644 --- a/src/bitflags/bitflags64.ts +++ b/src/bitflags/bitflags64.ts @@ -13,8 +13,6 @@ export class BitFlags64< } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O { - super.rangeCheck(dt.byteLength, options.byteOffset); - const returnObject: Record = {}; const byteBag = dt.getBigUint64(options.byteOffset); @@ -32,8 +30,6 @@ export class BitFlags64< dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - let flags = 0n; for (const { 0: key, 1: flagValue } of this.#recordEntries) { diff --git a/src/bitflags/bitflags8.ts b/src/bitflags/bitflags8.ts index 6d90b05..a083392 100644 --- a/src/bitflags/bitflags8.ts +++ b/src/bitflags/bitflags8.ts @@ -13,8 +13,6 @@ export class BitFlags8< } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): O { - super.rangeCheck(dt.byteLength, options.byteOffset); - const returnObject: Record = {}; const byteBag = dt.getUint8(options.byteOffset); @@ -32,8 +30,6 @@ export class BitFlags8< dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - let flags = 0; for (const { 0: key, 1: flagValue } of this.#recordEntries) { diff --git a/src/compound/sized_struct.ts b/src/compound/sized_struct.ts index c9a7f67..a77749d 100644 --- a/src/compound/sized_struct.ts +++ b/src/compound/sized_struct.ts @@ -66,7 +66,7 @@ export class SizedStruct< return this.#readPacked(dt, options); } - read(dt: DataView, options: Options = { byteOffset: 0 }): V { + override read(dt: DataView, options: Options = { byteOffset: 0 }): V { return this.#read(dt, options); } @@ -78,7 +78,11 @@ export class SizedStruct< this.#writePacked(dt, options, value); } - write(value: V, dt: DataView, options: Options = { byteOffset: 0 }): void { + override write( + value: V, + dt: DataView, + options: Options = { byteOffset: 0 }, + ): void { this.#write(dt, options, value); } } diff --git a/src/compound/struct.ts b/src/compound/struct.ts index 194dfa6..fa2d57a 100644 --- a/src/compound/struct.ts +++ b/src/compound/struct.ts @@ -27,7 +27,7 @@ export class Struct< return result as V; } - read(dt: DataView, options: Options = { byteOffset: 0 }): V { + override read(dt: DataView, options: Options = { byteOffset: 0 }): V { if (this.#record.length === 0) return {} as V; const result: Record = {}; @@ -53,7 +53,11 @@ export class Struct< } } - write(value: V, dt: DataView, options: Options = { byteOffset: 0 }): void { + override write( + value: V, + dt: DataView, + options: Options = { byteOffset: 0 }, + ): void { if (this.#record.length === 0) return; for (let i = 0; i < this.#record.length; i++) { diff --git a/src/compound/tagged_union.ts b/src/compound/tagged_union.ts index fa247b2..27d808a 100644 --- a/src/compound/tagged_union.ts +++ b/src/compound/tagged_union.ts @@ -52,7 +52,7 @@ export class TaggedUnion< return codec.readPacked(dt, options) as V; } - read(dt: DataView, options: Options = { byteOffset: 0 }): V { + override read(dt: DataView, options: Options = { byteOffset: 0 }): V { const discriminant = this.#discriminant.read(dt, options); super.alignOffset(options); const codec = this.#record[discriminant]; @@ -73,7 +73,7 @@ export class TaggedUnion< codec.writePacked(variant, dt, options); } - write( + override write( variant: V, dt: DataView, options: Options = { byteOffset: 0 }, diff --git a/src/compound/tuple.ts b/src/compound/tuple.ts index f0064cd..c59f870 100644 --- a/src/compound/tuple.ts +++ b/src/compound/tuple.ts @@ -25,7 +25,7 @@ export class Tuple< return result as V; } - read(dt: DataView, options: Options = { byteOffset: 0 }): V { + override read(dt: DataView, options: Options = { byteOffset: 0 }): V { if (this.#length === 0) return [] as unknown as V; const result: unknown[] = new Array(this.#length); @@ -55,7 +55,11 @@ export class Tuple< } } - write(value: V, dt: DataView, options: Options = { byteOffset: 0 }): void { + override write( + value: V, + dt: DataView, + options: Options = { byteOffset: 0 }, + ): void { if (value.length !== this.#length) { throw new TypeError( `value V has more entries than expected\nExpected:${this.#length} but got ${value.length}`, diff --git a/src/compound/union.ts b/src/compound/union.ts index ca4dd14..12b672a 100644 --- a/src/compound/union.ts +++ b/src/compound/union.ts @@ -38,7 +38,7 @@ export class Union< return codec.readPacked(dt, options) as V; } - read(dt: DataView, options: Options = { byteOffset: 0 }): V { + override read(dt: DataView, options: Options = { byteOffset: 0 }): V { const discriminant = this.#discriminant.read(dt, { byteOffset: options.byteOffset, }); @@ -58,7 +58,11 @@ export class Union< codec.writePacked(variant, dt, options); } - write(variant: V, dt: DataView, options: Options = { byteOffset: 0 }): void { + override write( + variant: V, + dt: DataView, + options: Options = { byteOffset: 0 }, + ): void { const discriminant = this.#variantFinder(variant); const codec = this.#record[discriminant]; if (!codec) throw new TypeError("Unknown discriminant"); diff --git a/src/primitives/bool.ts b/src/primitives/bool.ts index c7b5e03..1e16f00 100644 --- a/src/primitives/bool.ts +++ b/src/primitives/bool.ts @@ -6,9 +6,7 @@ export class Bool extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): boolean { - super.rangeCheck(dt.byteLength, options.byteOffset); - - const value = Boolean(dt.getInt8(options.byteOffset)); + const value = !!dt.getInt8(options.byteOffset); super.incrementOffset(options); return value; } @@ -18,8 +16,6 @@ export class Bool extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - dt.setInt8(options.byteOffset, Number(value)); super.incrementOffset(options); } diff --git a/src/primitives/f32.ts b/src/primitives/f32.ts index 1ffc008..ff8d61c 100644 --- a/src/primitives/f32.ts +++ b/src/primitives/f32.ts @@ -7,7 +7,6 @@ export class F32 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getFloat32(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class F32 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setFloat32(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/f64.ts b/src/primitives/f64.ts index 1fa1695..9f1075c 100644 --- a/src/primitives/f64.ts +++ b/src/primitives/f64.ts @@ -7,7 +7,6 @@ export class F64 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getFloat64(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class F64 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setFloat64(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/i16.ts b/src/primitives/i16.ts index b43f563..481008c 100644 --- a/src/primitives/i16.ts +++ b/src/primitives/i16.ts @@ -7,7 +7,6 @@ export class I16 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getInt16(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class I16 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setInt16(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/i32.ts b/src/primitives/i32.ts index d56adb4..12fdfd7 100644 --- a/src/primitives/i32.ts +++ b/src/primitives/i32.ts @@ -7,7 +7,6 @@ export class I32 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getInt32(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class I32 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setInt32(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/i64.ts b/src/primitives/i64.ts index 513d267..eb66fcd 100644 --- a/src/primitives/i64.ts +++ b/src/primitives/i64.ts @@ -7,7 +7,6 @@ export class I64 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): bigint { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getBigInt64(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class I64 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setBigInt64(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/i8.ts b/src/primitives/i8.ts index 635951a..1783811 100644 --- a/src/primitives/i8.ts +++ b/src/primitives/i8.ts @@ -6,7 +6,6 @@ export class I8 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getInt8(options.byteOffset); super.incrementOffset(options); return value; @@ -17,7 +16,6 @@ export class I8 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setInt8(options.byteOffset, value); super.incrementOffset(options); } diff --git a/src/primitives/u16.ts b/src/primitives/u16.ts index 32ac527..d5c3547 100644 --- a/src/primitives/u16.ts +++ b/src/primitives/u16.ts @@ -7,7 +7,6 @@ export class U16 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getUint16(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class U16 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setUint16(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/u32.ts b/src/primitives/u32.ts index 32016af..75b1207 100644 --- a/src/primitives/u32.ts +++ b/src/primitives/u32.ts @@ -7,7 +7,6 @@ export class U32 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getUint32(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class U32 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setUint32(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/u64.ts b/src/primitives/u64.ts index f7bf151..490a251 100644 --- a/src/primitives/u64.ts +++ b/src/primitives/u64.ts @@ -7,7 +7,6 @@ export class U64 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): bigint { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getBigUint64(options.byteOffset, this.littleEndian); super.incrementOffset(options); return value; @@ -18,7 +17,6 @@ export class U64 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setBigUint64(options.byteOffset, value, this.littleEndian); super.incrementOffset(options); } diff --git a/src/primitives/u8.ts b/src/primitives/u8.ts index 63156e7..160d694 100644 --- a/src/primitives/u8.ts +++ b/src/primitives/u8.ts @@ -6,7 +6,6 @@ export class U8 extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): number { - super.rangeCheck(dt.byteLength, options.byteOffset); const value = dt.getUint8(options.byteOffset); super.incrementOffset(options); return value; @@ -17,7 +16,6 @@ export class U8 extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); dt.setUint8(options.byteOffset, value); super.incrementOffset(options); } diff --git a/src/string/fixed_length.ts b/src/string/fixed_length.ts index a4f79ba..7cc9960 100644 --- a/src/string/fixed_length.ts +++ b/src/string/fixed_length.ts @@ -7,8 +7,6 @@ export class FixedLengthString extends SizedType { } readPacked(dt: DataView, options: Options = { byteOffset: 0 }): string { - super.rangeCheck(dt.byteLength, options.byteOffset); - const view = new Uint8Array( dt.buffer, dt.byteOffset + options.byteOffset, @@ -25,8 +23,6 @@ export class FixedLengthString extends SizedType { dt: DataView, options: Options = { byteOffset: 0 }, ): void { - super.rangeCheck(dt.byteLength, options.byteOffset); - const view = new Uint8Array( dt.buffer, dt.byteOffset + options.byteOffset, diff --git a/src/string/prefixed_length.ts b/src/string/prefixed_length.ts index d0ef221..1fc1e1c 100644 --- a/src/string/prefixed_length.ts +++ b/src/string/prefixed_length.ts @@ -27,7 +27,7 @@ export class PrefixedString extends UnsizedType { super.incrementOffset(options, value.length); } - write( + override write( value: string, dt: DataView, options: Options = { byteOffset: 0 }, @@ -57,7 +57,7 @@ export class PrefixedString extends UnsizedType { return TEXT_DECODER.decode(view); } - read(dt: DataView, options: Options = { byteOffset: 0 }): string { + override read(dt: DataView, options: Options = { byteOffset: 0 }): string { const length = this.#prefixCodec.read(dt, options); super.alignOffset(options); diff --git a/src/types/sized.ts b/src/types/sized.ts index 1d2aafe..5fc8c14 100644 --- a/src/types/sized.ts +++ b/src/types/sized.ts @@ -20,11 +20,4 @@ export abstract class SizedType extends UnsizedType implements Sized { protected override incrementOffset(options: Options): void { super.incrementOffset(options, this.byteSize); } - - /** Allows you to check upfront if you will go out of bound */ - protected rangeCheck(byteLength: number, offset: number): void { - if (this.byteSize > (byteLength - offset)) { - throw new RangeError("Out of bound"); - } - } }