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: 115 additions & 3 deletions lib/classes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
StructTypeof,
UnionTypeof,
UnpackedNativeArray,
UnpackedStruct,
UnpackedUnion,
} from "./types/generics.ts";
Expand All @@ -11,10 +12,120 @@ 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 "./helpers/format.ts";
import { format } from "./format/main.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 @@ -89,7 +200,8 @@ export class Struct<
}

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

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

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

[Symbol.for("Deno.customInspect")](
Expand Down
7 changes: 4 additions & 3 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ 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: 54 additions & 0 deletions lib/format/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export const ANSIColors = {
// Reset
Reset: "\x1b[0m",

// Text styles
Bold: "\x1b[1m",
Dim: "\x1b[2m",
Italic: "\x1b[3m",
Underline: "\x1b[4m",
Blink: "\x1b[5m",
Inverse: "\x1b[7m",
Hidden: "\x1b[8m",
Strikethrough: "\x1b[9m",

// Foreground colors
Black: "\x1b[30m",
Red: "\x1b[31m",
Green: "\x1b[32m",
Yellow: "\x1b[33m",
Blue: "\x1b[34m",
Magenta: "\x1b[35m",
Cyan: "\x1b[36m",
White: "\x1b[37m",

// Bright foreground colors
BrightBlack: "\x1b[90m",
BrightRed: "\x1b[91m",
BrightGreen: "\x1b[92m",
BrightYellow: "\x1b[93m",
BrightBlue: "\x1b[94m",
BrightMagenta: "\x1b[95m",
BrightCyan: "\x1b[96m",
BrightWhite: "\x1b[97m",

// Background colors
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",

// Bright background colors
BgBrightBlack: "\x1b[100m",
BgBrightRed: "\x1b[101m",
BgBrightGreen: "\x1b[102m",
BgBrightYellow: "\x1b[103m",
BgBrightBlue: "\x1b[104m",
BgBrightMagenta: "\x1b[105m",
BgBrightCyan: "\x1b[106m",
BgBrightWhite: "\x1b[107m",
} as const;
36 changes: 36 additions & 0 deletions lib/format/generate-description.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { NativeArray, Struct, Union } from "../classes.ts";

import { Alignment, Endianness } from "../types/enums.ts";
import { ANSIColors as C } from "./colors.ts";
import { hexify } from "../helpers/hexify.ts";

export function generateDescription(
value: Struct | Union | NativeArray,
tab: string,
): string {
let formatted = "";

formatted += `\n${tab}${C.BrightBlack}Endianness: ${C.Yellow}${
value.endianness === Endianness.Little ? "Little" : "Big"
}`;

formatted += `\n${tab}${C.BrightBlack}Alignment: ${C.Yellow}${
hexify(value.alignment)
}:${value.alignment} ${
value.align === Alignment.Natural ? "Natural" : "Packed"
}`;

formatted += `\n${tab}${C.BrightBlack}Padding: ${C.Yellow}${
hexify(value.padding)
}:${value.padding}`;

formatted += `\n${tab}${C.BrightBlack}Total Size: ${C.Yellow}${
hexify(value.byteLength)
}:${value.byteLength}`;

formatted += `\n${tab}${C.BrightBlack}Size: ${C.Yellow}${
hexify(value.size)
}:${value.size}`;

return formatted;
}
10 changes: 10 additions & 0 deletions lib/format/generate-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ANSIColors as C } from "./colors.ts";
import { hexify } from "../helpers/hexify.ts";

export function generatePrefix(offset: number, size: number): string {
return `${C.Reset}${C.BrightBlack}Offset:${C.Yellow}${
hexify(offset)
}:${offset} ${C.White}Size:${C.Yellow}${
hexify(size)
}:${size}${C.BrightBlack})`;
}
39 changes: 39 additions & 0 deletions lib/format/internal-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ValueDeclarationType } from "../types/unions.ts";

import { NativeArray, Struct, Union } from "../classes.ts";
import { generateDescription } from "./generate-description.ts";
import { ANSIColors as C } from "./colors.ts";
import { generatePrefix } from "./generate-prefix.ts";
import { sizeof } from "../helpers/sizeof.ts";

export function internalFormat(
type: ValueDeclarationType,
opts: {
name: string;
ident: number;
spaces: number;
offset: number;
colors: boolean;
},
): string {
const spaces = {
initial: " ".repeat(opts.spaces * opts.ident++),
secondary: " ".repeat(opts.spaces * opts.ident),
}

let formatted = spaces.initial + generatePrefix(opts.offset, sizeof(type));

switch (true) {
case typeof type === "string": {
return `${formatted + C.Magenta} ${type} ${C.Yellow + opts.name}`;
}

case NativeArray.isNativeArray(type): {
formatted +=
formatted += generateDescription(type, spaces.secondary);
break;
}
}

return "";
}
29 changes: 29 additions & 0 deletions lib/format/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ValueDeclarationType } from "../types/unions.ts";
import { internalFormat } from "./internal-format.ts";

export function format(
type: ValueDeclarationType,
opts: {
name?: string;
ident?: number;
spaces?: number;
offset?: number;
colors?: boolean;
} = {},
): string {
const newOpts = {
name: "unnamed",
ident: 0,
spaces: 4,
offset: 0,
colors: false,
...opts,
};

const formatted = internalFormat(type, newOpts);

// deno-lint-ignore no-control-regex
if (!opts.colors) return formatted.replace(/\x1b\[\d*m/g, "");

return formatted;
}
4 changes: 0 additions & 4 deletions lib/helpers/alignof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 @@ -16,9 +15,6 @@ export function alignof(
case "string":
return size;
case "object":
if (isArrayField(type)) return alignof(type.type, align);
return type.alignment;
}

return size;
}
8 changes: 0 additions & 8 deletions lib/helpers/checkers.ts

This file was deleted.

20 changes: 0 additions & 20 deletions lib/helpers/create-array.ts

This file was deleted.

File renamed without changes.
Loading
Loading