feat(Struct)

This commit is contained in:
2025-08-03 23:18:27 +03:00
parent 72f287a4e4
commit 38f0d922aa
15 changed files with 410 additions and 150 deletions

8
types/ConstArray.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
import { Type } from ".";
/**
* constructs type of array with constant byte size
* @param {number} size number of items
* @returns {Type}
*/
export function ConstArray(size: number): Type;

View File

@ -1,4 +1,4 @@
export type Type = unknown
import { Type } from ".";
/**
* constructs type of utf8-string with constant byte size
@ -6,10 +6,3 @@ export type Type = unknown
* @returns {Type}
*/
export function ConstString(byte_size: number): Type;
/**
* constructs type of array with constant byte size
* @param {number} size number of items
* @returns {Type}
*/
export function ConstArray(size: number): Type;

7
types/Struct.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
import { SerializableType, Type } from "."
/**
* constructs type of c-like structure. if field is headless, inside of structure will be stored u32 offset, outside of structure will be stored value of field
* @returns {Type}
*/
export function Struct(type_obj: Record<string, SerializableType | SerializableType[]>): Type;

1
types/Type.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export type Type = unknown

24
types/index.d.ts vendored
View File

@ -1,10 +1,11 @@
import { memcpy } from "./mem";
import { limits } from "./limits";
import { ConstString, ConstArray } from "./type";
import { Type } from "./Type"
import { ConstString } from "./ConstString"
import { ConstArray } from "./ConstArray";
import { Struct } from "./Struct";
export { memcpy, limits, ConstString, ConstArray }
import { Type } from "./type";
export { memcpy, limits, Type, ConstString, ConstArray, Struct }
export type SerializableType = NumberConstructor | StringConstructor | ArrayConstructor | Type
export type Serializable = number | string | array
@ -28,13 +29,14 @@ export function parse(dv: DataView, ...types: SerializableType[]): Serializable;
/**
* some types, like Array, String, has no fixed size. So in Structure they are stored as u32 offset, which points to their beginning
* @param {Serializable} obj to check
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @returns {boolean}
*/
export function isHeadless(obj: Serializable): boolean;
export function isHeadless(obj: Serializable, ...types: SerializableType[]): boolean;
/**
* some types, like Array, String, has no fixed size. So in Structure they are stored as u32 offset, which points to their beginning
* @param {SerializableType[]} ...args primary and inner types. eg: Array, Number
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @returns {boolean}
*/
export function isHeadless(...types: SerializableType[]): boolean;
@ -42,25 +44,27 @@ export function isHeadless(...types: SerializableType[]): boolean;
/**
* if obj has no fixed size, return 4 (sizeof u32 offset)
* @param {Serializable} obj to check
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @returns {number}
*/
export function sizeofHead(obj: Serializable): number;
export function sizeofHead(obj: Serializable, ...types: SerializableType[]): number;
/**
* if obj has no fixed size, return 4 (sizeof u32 offset)
* @param {SerializableType[]} ...args primary and inner types. eg: Array, Number
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @returns {number}
*/
export function sizeofHead(...types: SerializableType[]): number;
/**
* @param {Serializable} obj to check
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @returns {number}
*/
export function sizeof(obj: Serializable): number;
export function sizeof(obj: Serializable, ...types: SerializableType[]): number;
/**
* @param {SerializableType[]} ...args primary and inner types. eg: Array, Number
* @param {SerializableType[]} ...types primary and inner types. eg: Array, Number
* @returns {number}
* @throws {Error} if passed Array or String type (unknown sizeof)
*/