node-ctypes 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts ADDED
@@ -0,0 +1,527 @@
1
+ /**
2
+ * node-ctypes - Python ctypes-like FFI for Node.js
3
+ * TypeScript type definitions
4
+ */
5
+
6
+ /// <reference types="node" />
7
+
8
+ // =============================================================================
9
+ // Basic Types
10
+ // =============================================================================
11
+
12
+ /** C type enumeration */
13
+ export type CTypeString =
14
+ | "void"
15
+ | "int8"
16
+ | "uint8"
17
+ | "int16"
18
+ | "uint16"
19
+ | "int32"
20
+ | "uint32"
21
+ | "int64"
22
+ | "uint64"
23
+ | "float"
24
+ | "double"
25
+ | "bool"
26
+ | "pointer"
27
+ | "string"
28
+ | "wstring"
29
+ | "wchar"
30
+ | "size_t"
31
+ | "ssize_t"
32
+ | "long"
33
+ | "ulong"
34
+ | "char"
35
+ | "uchar"
36
+ | "short"
37
+ | "ushort"
38
+ | "int"
39
+ | "uint";
40
+
41
+ /** Native CType object */
42
+ export interface CType {
43
+ readonly size: number;
44
+ readonly name: string;
45
+ }
46
+
47
+ /** Predefined types object */
48
+ export interface Types {
49
+ readonly void: CType;
50
+ readonly int8: CType;
51
+ readonly uint8: CType;
52
+ readonly int16: CType;
53
+ readonly uint16: CType;
54
+ readonly int32: CType;
55
+ readonly uint32: CType;
56
+ readonly int64: CType;
57
+ readonly uint64: CType;
58
+ readonly float: CType;
59
+ readonly double: CType;
60
+ readonly bool: CType;
61
+ readonly pointer: CType;
62
+ readonly string: CType;
63
+ readonly wstring: CType;
64
+ readonly wchar: CType;
65
+ readonly size_t: CType;
66
+ readonly long: CType;
67
+ readonly ulong: CType;
68
+ // Python-style aliases
69
+ readonly c_void: CType;
70
+ readonly c_int8: CType;
71
+ readonly c_uint8: CType;
72
+ readonly c_int16: CType;
73
+ readonly c_uint16: CType;
74
+ readonly c_int32: CType;
75
+ readonly c_uint32: CType;
76
+ readonly c_int64: CType;
77
+ readonly c_uint64: CType;
78
+ readonly c_float: CType;
79
+ readonly c_double: CType;
80
+ readonly c_bool: CType;
81
+ readonly c_char_p: CType;
82
+ readonly c_wchar_p: CType;
83
+ readonly c_wchar: CType;
84
+ readonly c_void_p: CType;
85
+ readonly c_size_t: CType;
86
+ readonly c_long: CType;
87
+ readonly c_ulong: CType;
88
+ readonly c_int: CType;
89
+ readonly c_uint: CType;
90
+ readonly c_char: CType;
91
+ readonly c_uchar: CType;
92
+ readonly c_short: CType;
93
+ readonly c_ushort: CType;
94
+ }
95
+
96
+ // =============================================================================
97
+ // Library & Function
98
+ // =============================================================================
99
+
100
+ /** Version information */
101
+ export class Version {
102
+ static get major(): number;
103
+ static get minor(): number;
104
+ static get patch(): number;
105
+ static toString(): string;
106
+ }
107
+
108
+ /** Native library handle */
109
+ export class Library {
110
+ constructor(path: string | null);
111
+ func(
112
+ name: string,
113
+ returnType: CTypeString | CType,
114
+ argTypes?: (CTypeString | CType)[],
115
+ options?: FunctionOptions,
116
+ ): FFIFunction;
117
+ symbol(name: string): bigint;
118
+ close(): void;
119
+ readonly path: string;
120
+ readonly loaded: boolean;
121
+ }
122
+
123
+ /** Function call options */
124
+ export interface FunctionOptions {
125
+ abi?: "cdecl" | "stdcall" | "fastcall" | "thiscall" | "default";
126
+ }
127
+
128
+ /** Errcheck callback type */
129
+ export type ErrcheckCallback = (
130
+ result: any,
131
+ func: CallableFunction,
132
+ args: any[],
133
+ ) => any;
134
+
135
+ /** FFI function wrapper */
136
+ export interface FFIFunction {
137
+ (...args: any[]): any;
138
+ readonly funcName: string;
139
+ readonly address: bigint;
140
+ errcheck: ErrcheckCallback | null;
141
+ }
142
+
143
+ /** CDLL - C calling convention library */
144
+ export class CDLL {
145
+ constructor(path: string | null);
146
+ func(
147
+ name: string,
148
+ returnType: CTypeString | CType,
149
+ argTypes?: (CTypeString | CType)[],
150
+ options?: FunctionOptions,
151
+ ): CallableFunction & { errcheck: ErrcheckCallback | null };
152
+ symbol(name: string): bigint;
153
+ close(): void;
154
+ readonly path: string;
155
+ readonly loaded: boolean;
156
+ }
157
+
158
+ /** WinDLL - stdcall calling convention library (Windows) */
159
+ export class WinDLL extends CDLL {
160
+ constructor(path: string);
161
+ }
162
+
163
+ // =============================================================================
164
+ // Callbacks
165
+ // =============================================================================
166
+
167
+ /** Callback wrapper for main-thread callbacks */
168
+ export interface CallbackWrapper {
169
+ readonly pointer: bigint;
170
+ release(): void;
171
+ }
172
+
173
+ /** Native Callback class */
174
+ export class Callback {
175
+ constructor(
176
+ fn: Function,
177
+ returnType: CTypeString | CType,
178
+ argTypes?: (CTypeString | CType)[],
179
+ );
180
+ readonly pointer: bigint;
181
+ release(): void;
182
+ }
183
+
184
+ /** Thread-safe callback class */
185
+ export class ThreadSafeCallback extends Callback {}
186
+
187
+ // =============================================================================
188
+ // Structures
189
+ // =============================================================================
190
+
191
+ /** Field definition for struct */
192
+ export interface FieldDef {
193
+ name: string;
194
+ type: CTypeString | CType | StructDef | ArrayTypeDef | BitFieldDef;
195
+ offset: number;
196
+ size: number;
197
+ alignment: number;
198
+ isNested?: boolean;
199
+ isAnonymous?: boolean;
200
+ isArray?: boolean;
201
+ isBitField?: boolean;
202
+ bitOffset?: number;
203
+ bitSize?: number;
204
+ baseSize?: number;
205
+ }
206
+
207
+ /** Struct definition */
208
+ export interface StructDef {
209
+ readonly size: number;
210
+ readonly alignment: number;
211
+ readonly fields: FieldDef[];
212
+ readonly packed: boolean;
213
+ readonly _isStructType: true;
214
+
215
+ create(values?: Record<string, any>): Buffer;
216
+ get(buf: Buffer, fieldName: string): any;
217
+ set(buf: Buffer, fieldName: string, value: any): void;
218
+ toObject(buf: Buffer): Record<string, any>;
219
+ getNestedBuffer(buf: Buffer, fieldName: string): Buffer;
220
+ }
221
+
222
+ // Helper types for improved typed structs
223
+ export type FieldSpec =
224
+ | CTypeString
225
+ | CType
226
+ | StructDef
227
+ | ArrayTypeDef
228
+ | BitFieldDef
229
+ | AnonymousField;
230
+
231
+ type JsFromCType<T> = T extends "int64" | "uint64" | "size_t"
232
+ ? bigint
233
+ : T extends
234
+ | "int8"
235
+ | "uint8"
236
+ | "int16"
237
+ | "uint16"
238
+ | "int32"
239
+ | "uint32"
240
+ | "int"
241
+ | "uint"
242
+ | "short"
243
+ | "ushort"
244
+ | "char"
245
+ | "uchar"
246
+ | "float"
247
+ | "double"
248
+ ? number
249
+ : T extends "bool"
250
+ ? boolean
251
+ : T extends "string" | "c_char_p" | "c_char"
252
+ ? string | Buffer
253
+ : T extends "wstring" | "c_wchar_p"
254
+ ? string
255
+ : T extends StructDef
256
+ ? any
257
+ : T extends ArrayTypeDef
258
+ ? any
259
+ : T extends BitFieldDef
260
+ ? number
261
+ : any;
262
+
263
+ type FieldsToInstance<F extends Record<string, FieldSpec>> = {
264
+ [K in keyof F]: JsFromCType<F[K]>;
265
+ };
266
+
267
+ /** Union definition (same interface as struct) */
268
+ export interface UnionDef extends StructDef {
269
+ readonly isUnion: true;
270
+ }
271
+
272
+ /** Bit field definition */
273
+ export interface BitFieldDef {
274
+ readonly _isBitField: true;
275
+ readonly baseType: CTypeString;
276
+ readonly bits: number;
277
+ readonly baseSize: number;
278
+ }
279
+
280
+ /** Array type definition */
281
+ export interface ArrayTypeDef {
282
+ readonly elementType: CTypeString;
283
+ readonly length: number;
284
+ readonly _isArrayType: true;
285
+
286
+ getSize(): number;
287
+ getLength(): number;
288
+ getAlignment(): number;
289
+ create(values?: any[]): Buffer & ArrayProxy;
290
+ wrap(buffer: Buffer): Buffer & ArrayProxy;
291
+ }
292
+
293
+ /** Array proxy for index access */
294
+ export interface ArrayProxy {
295
+ [index: number]: any;
296
+ [Symbol.iterator](): Iterator<any>;
297
+ }
298
+
299
+ /** Struct options */
300
+ export interface StructOptions {
301
+ packed?: boolean;
302
+ }
303
+
304
+ /** Anonymous field wrapper */
305
+ export interface AnonymousField {
306
+ type: StructDef;
307
+ anonymous: true;
308
+ }
309
+
310
+ // =============================================================================
311
+ // Pointers
312
+ // =============================================================================
313
+
314
+ /** POINTER type definition */
315
+ export interface PointerTypeDef {
316
+ readonly _pointerTo: CTypeString | StructDef;
317
+ readonly _baseSize: number;
318
+ readonly size: number;
319
+
320
+ create(): Buffer;
321
+ fromBuffer(targetBuf: Buffer): Buffer;
322
+ deref(ptrBuf: Buffer): bigint | null;
323
+ set(ptrBuf: Buffer, value: Buffer | bigint): void;
324
+ toString(): string;
325
+ }
326
+
327
+ // =============================================================================
328
+ // Native Struct/Array Types
329
+ // =============================================================================
330
+
331
+ /** Native StructType class */
332
+ export class StructType {
333
+ constructor(isUnion?: boolean);
334
+ addField(
335
+ name: string,
336
+ type: CTypeString | CType | StructType | ArrayType,
337
+ ): this;
338
+ getSize(): number;
339
+ getAlignment(): number;
340
+ create(values?: Record<string, any>): Buffer;
341
+ read(buffer: Buffer): Record<string, any>;
342
+ }
343
+
344
+ /** Native ArrayType class */
345
+ export class ArrayType {
346
+ constructor(elementType: CTypeString | CType, count: number);
347
+ getSize(): number;
348
+ getLength(): number;
349
+ getAlignment(): number;
350
+ create(values?: any[]): Buffer;
351
+ }
352
+
353
+ /**
354
+ * Python-like Structure base class.
355
+ * Subclasses should define `static _fields_` as array of [name, type]
356
+ * or an object map { name: type }.
357
+ */
358
+ export class Structure<
359
+ F extends Record<string, FieldSpec> = Record<string, any>,
360
+ > {
361
+ constructor(...args: any[]);
362
+ static _fields_?: Array<[string, FieldSpec]> | Record<string, FieldSpec>;
363
+ static _pack_?: boolean;
364
+ static _anonymous_?: string[];
365
+ static create<ThisT extends Structure<F>>(
366
+ this: new (...args: any[]) => ThisT,
367
+ values?: Partial<FieldsToInstance<F>> | Buffer,
368
+ ): ThisT;
369
+ static toObject<ThisT extends Structure<F>>(
370
+ this: new (...args: any[]) => ThisT,
371
+ buf: Buffer | any,
372
+ ): FieldsToInstance<F>;
373
+ _buffer: Buffer;
374
+ _structDef: StructDef;
375
+ get<K extends keyof F & string>(fieldName: K): FieldsToInstance<F>[K];
376
+ set<K extends keyof F & string>(
377
+ fieldName: K,
378
+ value: FieldsToInstance<F>[K],
379
+ ): void;
380
+ toObject(): FieldsToInstance<F>;
381
+ [field: string]: any; // instance has dynamic properties for fields
382
+ }
383
+
384
+ /**
385
+ * Python-like Union base class.
386
+ */
387
+ export class Union<
388
+ F extends Record<string, FieldSpec> = Record<string, any>,
389
+ > extends Structure<F> {}
390
+
391
+ // =============================================================================
392
+ // Functions
393
+ // =============================================================================
394
+
395
+ // Library loading
396
+ export function load(path: string | null): Library;
397
+
398
+ // Callbacks
399
+ export function callback(
400
+ fn: Function,
401
+ returnType: CTypeString | CType,
402
+ argTypes?: (CTypeString | CType)[],
403
+ ): CallbackWrapper;
404
+ export function threadSafeCallback(
405
+ fn: Function,
406
+ returnType: CTypeString | CType,
407
+ argTypes?: (CTypeString | CType)[],
408
+ ): CallbackWrapper;
409
+
410
+ // Memory management - Python-compatible
411
+ export function create_string_buffer(init: number | string | Buffer): Buffer;
412
+ export function create_unicode_buffer(init: number | string): Buffer;
413
+ export function string_at(
414
+ address: Buffer | bigint | number,
415
+ size?: number,
416
+ ): string | null;
417
+ export function wstring_at(
418
+ address: Buffer | bigint | number,
419
+ size?: number,
420
+ ): string | null;
421
+ export function addressof(ptr: Buffer | bigint | number): bigint;
422
+ export function memmove(dst: Buffer, src: Buffer | bigint, count: number): void;
423
+ export function memset(dst: Buffer, value: number, count: number): void;
424
+
425
+ // Memory - utility (no direct Python equivalent)
426
+ export function readValue(
427
+ ptr: Buffer | bigint | number,
428
+ type: CTypeString | CType,
429
+ offset?: number,
430
+ ): any;
431
+ export function writeValue(
432
+ ptr: Buffer | bigint | number,
433
+ type: CTypeString | CType,
434
+ value: any,
435
+ offset?: number,
436
+ ): number;
437
+ export function sizeof(
438
+ type: CTypeString | CType | StructDef | ArrayTypeDef,
439
+ ): number;
440
+ export function ptrToBuffer(address: bigint | number, size: number): Buffer;
441
+
442
+ // Structures
443
+ export function struct(
444
+ fields: Record<
445
+ string,
446
+ | CTypeString
447
+ | CType
448
+ | StructDef
449
+ | ArrayTypeDef
450
+ | BitFieldDef
451
+ | AnonymousField
452
+ >,
453
+ options?: StructOptions,
454
+ ): StructDef;
455
+ export function union(
456
+ fields: Record<
457
+ string,
458
+ CTypeString | CType | StructDef | ArrayTypeDef | BitFieldDef
459
+ >,
460
+ ): UnionDef;
461
+ // Factory helpers that return a typed class extending Structure/Union
462
+ export function defineStruct<F extends Record<string, FieldSpec>>(
463
+ fields: F,
464
+ options?: StructOptions,
465
+ ): new (...args: any[]) => Structure<F>;
466
+ export function defineUnion<F extends Record<string, FieldSpec>>(
467
+ fields: F,
468
+ ): new (...args: any[]) => Union<F>;
469
+ export function array(elementType: CTypeString, count: number): ArrayTypeDef;
470
+ export function bitfield(baseType: CTypeString, bits: number): BitFieldDef;
471
+
472
+ // Pointers
473
+ export function byref(obj: Buffer): Buffer;
474
+ export function cast(
475
+ ptr: Buffer | bigint,
476
+ targetType: CTypeString | StructDef,
477
+ ): any;
478
+ export function POINTER(baseType: CTypeString | StructDef): PointerTypeDef;
479
+
480
+ // Error handling
481
+ export function get_errno(): number;
482
+ export function set_errno(value: number): void;
483
+ export function GetLastError(): number;
484
+ export function SetLastError(code: number): void;
485
+ export function FormatError(code?: number): string;
486
+ export function WinError(code?: number): Error & { winerror: number };
487
+
488
+ // =============================================================================
489
+ // Type Aliases (Python-compatible)
490
+ // =============================================================================
491
+
492
+ export const types: Types;
493
+ export const c_void: CType;
494
+ export const c_int: CType;
495
+ export const c_uint: CType;
496
+ export const c_int8: CType;
497
+ export const c_uint8: CType;
498
+ export const c_int16: CType;
499
+ export const c_uint16: CType;
500
+ export const c_int32: CType;
501
+ export const c_uint32: CType;
502
+ export const c_int64: CType;
503
+ export const c_uint64: CType;
504
+ export const c_float: CType;
505
+ export const c_double: CType;
506
+ export const c_char: CType;
507
+ export const c_char_p: CType;
508
+ export const c_wchar: CType;
509
+ export const c_wchar_p: CType;
510
+ export const c_void_p: CType;
511
+ export const c_bool: CType;
512
+ export const c_size_t: CType;
513
+ export const c_long: CType;
514
+ export const c_ulong: CType;
515
+
516
+ // =============================================================================
517
+ // Constants
518
+ // =============================================================================
519
+
520
+ /** Size of a pointer in bytes (4 on 32-bit, 8 on 64-bit) */
521
+ export const POINTER_SIZE: number;
522
+
523
+ /** Size of wchar_t in bytes (2 on Windows, 4 on Unix) */
524
+ export const WCHAR_SIZE: number;
525
+
526
+ /** Null pointer constant */
527
+ export const NULL: null;