From e81cf1e241d920c4538bec4a48a27f4cb7e05517 Mon Sep 17 00:00:00 2001 From: MierenManz Date: Tue, 12 Dec 2023 09:40:38 +0100 Subject: [PATCH] x --- src/array/array.ts | 12 ++++++------ src/compound/struct.ts | 8 ++++---- src/compound/tagged_union.ts | 14 +++++++------- src/compound/tuple.ts | 6 +++--- src/string/cstring.ts | 4 ++-- src/types/aligned.ts | 35 ----------------------------------- src/types/mod.ts | 1 - src/types/sized.ts | 8 ++++---- src/types/unsized.ts | 22 ++++++++++++++++++++++ src/util.ts | 4 ++-- src/varint/i32_leb128.ts | 4 ++-- src/varint/i64_leb128.ts | 4 ++-- 12 files changed, 54 insertions(+), 68 deletions(-) delete mode 100644 src/types/aligned.ts diff --git a/src/array/array.ts b/src/array/array.ts index 45bb7fc..086aa80 100644 --- a/src/array/array.ts +++ b/src/array/array.ts @@ -1,7 +1,7 @@ -import { AlignedType, type Options } from "../types/mod.ts"; +import { UnsizedType, type Options } from "../types/mod.ts"; -export class ArrayType extends AlignedType { - constructor(readonly type: AlignedType, readonly length: number) { +export class ArrayType extends UnsizedType { + constructor(readonly type: UnsizedType, readonly length: number) { super(type.byteAlignment); } @@ -25,7 +25,7 @@ export class ArrayType extends AlignedType { for (let i = 0; i < this.length; i++) { result[i] = this.type.read(dt, options); - // No need for the increment offset. This is handled by the `type.readPacked` function + // No need for the increment offset. This is handled by the `type.read` function } return result as T[]; @@ -43,7 +43,7 @@ export class ArrayType extends AlignedType { for (let i = 0; i < this.length; i++) { this.type.writePacked(value[i], dt, options); - // No need for the increment offset. This is handled by the `type.writeUnaligned` function + // No need for the increment offset. This is handled by the `type.writePacked` function } } @@ -59,7 +59,7 @@ export class ArrayType extends AlignedType { for (let i = 0; i < this.length; i++) { this.type.write(value[i], dt, options); - // No need for the increment offset. This is handled by the `type.writeUnaligned` function + // No need for the increment offset. This is handled by the `type.write` function } } } diff --git a/src/compound/struct.ts b/src/compound/struct.ts index d5d0deb..f616bb3 100644 --- a/src/compound/struct.ts +++ b/src/compound/struct.ts @@ -1,13 +1,13 @@ -import { AlignedType, type InnerType, type Options } from "../types/mod.ts"; +import { UnsizedType, type InnerType, type Options } from "../types/mod.ts"; import { getBiggestAlignment } from "../util.ts"; export class Struct< - T extends Record>, + T extends Record>, V extends { [K in keyof T]: InnerType } = { [K in keyof T]: InnerType; }, -> extends AlignedType { - #record: Array<[string, AlignedType]>; +> extends UnsizedType { + #record: Array<[string, UnsizedType]>; constructor(input: T) { super(getBiggestAlignment(input)); diff --git a/src/compound/tagged_union.ts b/src/compound/tagged_union.ts index 82b5ec4..228b6af 100644 --- a/src/compound/tagged_union.ts +++ b/src/compound/tagged_union.ts @@ -1,6 +1,6 @@ import { u8 } from "../primitives/mod.ts"; import { - AlignedType, + UnsizedType, type InnerType, type Options, type ValueOf, @@ -12,31 +12,31 @@ type FindDiscriminant = (variant: V) => D; type Keys = Exclude; export class TaggedUnion< - T extends Record>, + T extends Record>, V extends ValueOf<{ [K in keyof T]: InnerType }> = ValueOf< { [K in keyof T]: InnerType } >, -> extends AlignedType { +> extends UnsizedType { #record: T; #variantFinder: FindDiscriminant>; - #discriminant: AlignedType; + #discriminant: UnsizedType; constructor( input: T, variantFinder: FindDiscriminant>, - discriminant: Keys extends string ? AlignedType : never, + discriminant: Keys extends string ? UnsizedType : never, ); constructor( input: T, variantFinder: FindDiscriminant>, - discriminant?: Keys extends number ? AlignedType : never, + discriminant?: Keys extends number ? UnsizedType : never, ); constructor( input: T, variantFinder: FindDiscriminant>, - discriminant: AlignedType = u8, + discriminant: UnsizedType = u8, ) { super(getBiggestAlignment(input)); this.#record = input; diff --git a/src/compound/tuple.ts b/src/compound/tuple.ts index 2d961d5..c2fecd9 100644 --- a/src/compound/tuple.ts +++ b/src/compound/tuple.ts @@ -1,10 +1,10 @@ -import { AlignedType, type InnerType, type Options } from "../types/mod.ts"; +import { UnsizedType, type InnerType, type Options } from "../types/mod.ts"; import { getBiggestAlignment } from "../util.ts"; export class Tuple< - T extends [...AlignedType[]], + T extends [...UnsizedType[]], V extends [...unknown[]] = { [I in keyof T]: InnerType }, -> extends AlignedType { +> extends UnsizedType { #tupleTypes: T; constructor(types: T) { diff --git a/src/string/cstring.ts b/src/string/cstring.ts index 76af2b5..ecbac03 100644 --- a/src/string/cstring.ts +++ b/src/string/cstring.ts @@ -1,7 +1,7 @@ -import { AlignedType, type Options } from "../types/mod.ts"; +import { UnsizedType, type Options } from "../types/mod.ts"; import { TEXT_DECODER, TEXT_ENCODER } from "./_common.ts"; -export class CString extends AlignedType { +export class CString extends UnsizedType { constructor() { super(1); } diff --git a/src/types/aligned.ts b/src/types/aligned.ts deleted file mode 100644 index 205f482..0000000 --- a/src/types/aligned.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { align } from "../util.ts"; -import { type Unsized, UnsizedType } from "./unsized.ts"; -import type { Options } from "./_common.ts"; - -export interface Aligned extends Unsized { - readonly byteAlignment: number; - - /** In most cases you don't need to reimplement read. as long as your `readUnaligned` is correct */ - read(dt: DataView, options?: Options): T; - /** In most cases you don't need to reimplement write. as long as your `writeUnaligned` is correct */ - write(value: T, dt: DataView, options?: Options): void; -} - -export abstract class AlignedType extends UnsizedType - implements Aligned { - constructor(readonly byteAlignment: number) { - super(); - } - - /** In most cases you don't need to reimplement read. as long as your `readPacked` is correct */ - read(dt: DataView, options: Options = { byteOffset: 0 }): T { - this.alignOffset(options); - return this.readPacked(dt, options); - } - - /** In most cases you don't need to reimplement write. as long as your `readPacked` is correct */ - write(value: T, dt: DataView, options: Options = { byteOffset: 0 }): void { - this.alignOffset(options); - this.writePacked(value, dt, options); - } - - protected alignOffset(options: Options) { - options.byteOffset = align(options.byteOffset, this.byteAlignment); - } -} diff --git a/src/types/mod.ts b/src/types/mod.ts index 6b76ecf..64f8c69 100644 --- a/src/types/mod.ts +++ b/src/types/mod.ts @@ -1,4 +1,3 @@ export * from "./_common.ts"; -export { AlignedType } from "./aligned.ts"; export { SizedType } from "./sized.ts"; export { UnsizedType } from "./unsized.ts"; diff --git a/src/types/sized.ts b/src/types/sized.ts index e8d50a6..9d8141a 100644 --- a/src/types/sized.ts +++ b/src/types/sized.ts @@ -1,12 +1,12 @@ -import { type Aligned, AlignedType } from "./aligned.ts"; +import { type Unsized, UnsizedType } from "./unsized.ts"; import type { Options } from "./_common.ts"; -interface Sized extends Aligned { +interface Sized extends Unsized { readonly byteSize: number; } -export abstract class SizedType extends AlignedType implements Sized { - constructor(readonly byteSize: number, byteAlignment: number) { +export abstract class SizedType extends UnsizedType implements Sized { + constructor(readonly byteSize: number, byteAlignment: number = 1) { super(byteAlignment); } diff --git a/src/types/unsized.ts b/src/types/unsized.ts index 17518c5..f9f58e4 100644 --- a/src/types/unsized.ts +++ b/src/types/unsized.ts @@ -1,14 +1,36 @@ + +import {align} from "../util.ts"; import type { Options } from "./_common.ts"; export interface Unsized { + readonly byteAlignment: number; + readPacked(dt: DataView, options?: Options): T; writePacked(value: T, dt: DataView, options?: Options): void; } export abstract class UnsizedType implements Unsized { + constructor(readonly byteAlignment: number) {} + abstract readPacked(dt: DataView, options?: Options): T; abstract writePacked(value: T, dt: DataView, options?: Options): void; + /** In most cases you don't need to reimplement read. as long as your `readPacked` is correct */ + read(dt: DataView, options: Options = { byteOffset: 0 }): T { + this.alignOffset(options); + return this.readPacked(dt, options); + } + + /** In most cases you don't need to reimplement write. as long as your `writePacked` is correct */ + write(value: T, dt: DataView, options: Options = { byteOffset: 0 }): void { + this.alignOffset(options); + this.writePacked(value, dt, options); + } + + protected alignOffset(options: Options) { + options.byteOffset = align(options.byteOffset, this.byteAlignment); + } + protected incrementOffset(options: Options, byteSize: number) { options.byteOffset += byteSize; } diff --git a/src/util.ts b/src/util.ts index 07976bb..dc35bf4 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,4 @@ -import type { AlignedType } from "./mod.ts"; +import type { UnsizedType } from "./mod.ts"; /** * The endianess of your machine, true if little endian and false if big endian. @@ -15,7 +15,7 @@ export const align = (unaligned: number, alignment: number) => type ArrayOrRecord = T[] | Record; export const getBiggestAlignment = ( - input: ArrayOrRecord>, + input: ArrayOrRecord>, ) => Object.values(input) .reduce((acc, x) => Math.max(acc, x.byteAlignment), 0); diff --git a/src/varint/i32_leb128.ts b/src/varint/i32_leb128.ts index 6336b36..2a9dca0 100644 --- a/src/varint/i32_leb128.ts +++ b/src/varint/i32_leb128.ts @@ -1,7 +1,7 @@ -import { AlignedType, type Options } from "../types/mod.ts"; +import { UnsizedType, type Options } from "../types/mod.ts"; import { CONTINUE_BIT, SEGMENT_BITS } from "./_common.ts"; -export class I32Leb128 extends AlignedType { +export class I32Leb128 extends UnsizedType { constructor() { super(1); } diff --git a/src/varint/i64_leb128.ts b/src/varint/i64_leb128.ts index b49373f..fcd515c 100644 --- a/src/varint/i64_leb128.ts +++ b/src/varint/i64_leb128.ts @@ -1,4 +1,4 @@ -import { AlignedType, type Options } from "../types/mod.ts"; +import { UnsizedType, type Options } from "../types/mod.ts"; import { CONTINUE_BIT, SEGMENT_BITS } from "./_common.ts"; const SEGMENT_BITS_N = BigInt(SEGMENT_BITS); @@ -9,7 +9,7 @@ const U32_VIEW = new Uint32Array(AB); const I64_VIEW = new BigInt64Array(AB); const U64_VIEW = new BigUint64Array(AB); -export class I64Leb128 extends AlignedType { +export class I64Leb128 extends UnsizedType { constructor() { super(1); }