Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 3 additions & 115 deletions lib/classes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
StructTypeof,
UnionTypeof,
UnpackedNativeArray,
UnpackedStruct,
UnpackedUnion,
} from "./types/generics.ts";
Expand All @@ -12,120 +11,10 @@ import { Alignment, Endianness } from "./types/enums.ts";
import { endianness } from "./helpers/endianness.ts";
import { alignof } from "./helpers/alignof.ts";
import { sizeof } from "./helpers/sizeof.ts";
import { format } from "./format/main.ts";
import { format } from "./helpers/format.ts";
import { write } from "./helpers/write.ts";
import { read } from "./helpers/read.ts";

export class NativeArray<
const Declaration extends ValueDeclarationType = ValueDeclarationType,
> {
readonly isLittleEndian: boolean;
readonly isBigEndian: boolean;
readonly endianness: Endianness;

readonly align: Alignment;
readonly type: Declaration;

readonly byteLength: number;
readonly alignment: number;
readonly padding: number;
readonly length: number;
readonly size: number;

constructor(
opts: {
length: number;
type: Declaration;

endianness?: Endianness;
align?: Alignment;
size?: number;
},
) {
opts = { ...opts };

opts.endianness ??= Endianness.System;
opts.size = Math.max(opts.size ?? 1, 1);

this.isLittleEndian = endianness(opts.endianness);
this.isBigEndian = !this.isLittleEndian;
this.endianness = this.isLittleEndian
? Endianness.Little
: Endianness.Big;

this.align = opts.align ?? Alignment.Natural;
this.size = sizeof(opts.type) * opts.length;
this.type = opts.type;

this.alignment = alignof(this.type, this.align);
this.byteLength = Math.max(this.size, opts.size);
this.padding = this.byteLength - this.size;
this.length = opts.length;
}

static isNativeArray(value: unknown): value is NativeArray {
return !!(value && typeof value === "object" &&
value instanceof NativeArray);
}

[Symbol.for("Deno.customInspect")](
_: () => void,
opts: Deno.InspectOptions,
): string {
return format(this, { colors: opts.colors ? true : false });
}

toString(): string {
return format(this, { colors: false });
}

offsetof(index: number): number {
return sizeof(this.type) * index;
}

typeof(): Declaration {
return this.type;
}

alignof(): number {
return this.alignment;
}

pack(
array: UnpackedNativeArray<Declaration>,
buff: ArrayBuffer = new ArrayBuffer(this.byteLength),
offset = 0,
): ArrayBuffer {
for (let i = 0; i < this.length; i++) {
const value = array[i];
const index = this.offsetof(i);

write(this.type, value, buff, index + offset, this.endianness);
}

return buff;
}

unpack(buff: ArrayBuffer, offset = 0): UnpackedNativeArray<Declaration> {
const array: UnpackedNativeArray<Declaration> = [];

for (let i = 0; i < this.length; i++) {
const index = this.offsetof(i);
const value = read(
this.type,
buff,
index + offset,
this.endianness,
);

// @ts-ignore - Yeah this works on runtime properly.
array.push(value);
}

return array;
}
}

export class Struct<
const Declaration extends readonly {
name: string;
Expand Down Expand Up @@ -200,8 +89,7 @@ export class Struct<
}

static isStruct(value: unknown): value is Struct {
return !!(value && typeof value === "object" &&
value instanceof Struct);
return value instanceof Struct;
}

[Symbol.for("Deno.customInspect")](
Expand Down Expand Up @@ -407,7 +295,7 @@ export class Union<
}

static isUnion(value: unknown): value is Union {
return !!(value && typeof value === "object" && value instanceof Union);
return value instanceof Union;
}

[Symbol.for("Deno.customInspect")](
Expand Down
7 changes: 3 additions & 4 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ const nativeChar = isWindows ? "i8" : "u8";
const testBuffer: ArrayBuffer = new ArrayBuffer(4);
new DataView(testBuffer).setUint32(0, 0x00_00_00_01, true);

export const SystemEndianness: enums.Endianness =
new Uint8Array(testBuffer)[0] === 0x01
? enums.Endianness.Little
: enums.Endianness.Big;
export const SystemEndianness: enums.Endianness = new Uint8Array(testBuffer)[0] === 0x01
? enums.Endianness.Little
: enums.Endianness.Big;

export const Platform = {
os: Deno.build.os,
Expand Down
54 changes: 0 additions & 54 deletions lib/format/colors.ts

This file was deleted.

36 changes: 0 additions & 36 deletions lib/format/generate-description.ts

This file was deleted.

10 changes: 0 additions & 10 deletions lib/format/generate-prefix.ts

This file was deleted.

39 changes: 0 additions & 39 deletions lib/format/internal-format.ts

This file was deleted.

29 changes: 0 additions & 29 deletions lib/format/main.ts

This file was deleted.

4 changes: 4 additions & 0 deletions lib/helpers/alignof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ValueDeclarationType } from "../types/unions.ts";

import { Alignment } from "../types/enums.ts";
import { sizeof } from "./sizeof.ts";
import { isArrayField } from "./checkers.ts";

export function alignof(
type: ValueDeclarationType,
Expand All @@ -15,6 +16,9 @@ export function alignof(
case "string":
return size;
case "object":
if (isArrayField(type)) return alignof(type.type, align);
return type.alignment;
}

return size;
}
8 changes: 8 additions & 0 deletions lib/helpers/checkers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ValueDeclarationType } from "../types/unions.ts";

export function isArrayField(
type: unknown,
): type is { readonly length: number; readonly type: ValueDeclarationType } {
return type !== null && typeof type === "object" && "length" in type &&
"type" in type;
}
20 changes: 20 additions & 0 deletions lib/helpers/create-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Alignment, Endianness } from "../types/enums.ts";
import type { ValueDeclarationType } from "../types/unions.ts";
import { Union } from "../classes.ts";

export function createArray<
const Declaration extends ValueDeclarationType = ValueDeclarationType,
>(
type: Declaration,
length: number,
opts: { size?: number; endianness?: Endianness; align?: Alignment } = {},
): Union<{
readonly "[ARRAY_ITEMS]": {
readonly length: number;
readonly type: Declaration;
};
}> {
return new Union({
"[ARRAY_ITEMS]": { length, type },
}, opts);
}
Loading
Loading