node-ctypes 1.5.0 → 1.7.0

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 CHANGED
@@ -1,6 +1,56 @@
1
1
  /**
2
2
  * node-ctypes - Python ctypes-like FFI for Node.js
3
- * TypeScript type definitions
3
+ *
4
+ * A foreign function interface library that mirrors the Python ctypes API,
5
+ * built on libffi and N-API.
6
+ *
7
+ * @example Loading a library and calling a function
8
+ * ```javascript
9
+ * import { CDLL, c_int } from 'node-ctypes';
10
+ *
11
+ * const libc = new CDLL('libc.so.6'); // Linux
12
+ * // const libc = new CDLL('msvcrt.dll'); // Windows
13
+ * // const libc = new CDLL('libc.dylib'); // macOS
14
+ * const abs_func = libc.abs;
15
+ * abs_func.argtypes = [c_int];
16
+ * abs_func.restype = c_int;
17
+ * console.log(abs_func(-42)); // 42
18
+ * ```
19
+ *
20
+ * @example Defining a struct
21
+ * ```javascript
22
+ * import { Structure, c_int } from 'node-ctypes';
23
+ *
24
+ * class Point extends Structure {
25
+ * static _fields_ = [["x", c_int], ["y", c_int]];
26
+ * }
27
+ *
28
+ * const p = new Point(10, 20);
29
+ * console.log(p.x, p.y); // 10 20
30
+ * ```
31
+ *
32
+ * @example Callbacks
33
+ * ```javascript
34
+ * import { callback, c_int32, c_void_p, readValue } from 'node-ctypes';
35
+ *
36
+ * const compare = callback(
37
+ * (a, b) => readValue(a, c_int32) - readValue(b, c_int32),
38
+ * c_int32, [c_void_p, c_void_p]
39
+ * );
40
+ * // Pass compare.pointer to native functions
41
+ * compare.release(); // Release when done
42
+ * ```
43
+ *
44
+ * @example Pointers
45
+ * ```javascript
46
+ * import { POINTER, pointer, c_int32 } from 'node-ctypes';
47
+ *
48
+ * const x = new c_int32(42);
49
+ * const p = pointer(x);
50
+ * console.log(p.contents); // 42
51
+ * ```
52
+ *
53
+ * @packageDocumentation
4
54
  */
5
55
 
6
56
  /// <reference types="node" />
@@ -10,8 +60,12 @@
10
60
  // =============================================================================
11
61
 
12
62
  /**
13
- * CType - Object containing numeric type identifiers
14
- * Single source of truth for type values
63
+ * Numeric type identifiers used internally by the native module.
64
+ *
65
+ * These constants identify C data types and are the single source of truth
66
+ * for type values throughout the library.
67
+ *
68
+ * @category Types
15
69
  */
16
70
  export interface CTypeValues {
17
71
  readonly VOID: number;
@@ -40,17 +94,33 @@ export interface CTypeValues {
40
94
  readonly COUNT: number;
41
95
  }
42
96
 
43
- /** CType object with numeric type identifiers */
97
+ /**
98
+ * CType enum with numeric type identifiers.
99
+ * @category Types
100
+ */
44
101
  export const CType: CTypeValues;
45
102
 
46
- /** Any accepted type specification (SimpleCData class, CType value, or struct/union) */
103
+ /**
104
+ * Any accepted type specification — a SimpleCData class, CType numeric value, or struct/union definition.
105
+ * @category Types
106
+ */
47
107
  export type AnyType = SimpleCDataConstructor | number | StructDef | UnionDef;
48
108
 
49
109
  // =============================================================================
50
110
  // Library & Function
51
111
  // =============================================================================
52
112
 
53
- /** Version information */
113
+ /**
114
+ * Version information for the native module.
115
+ *
116
+ * @example
117
+ * ```javascript
118
+ * import { Version } from 'node-ctypes';
119
+ * console.log(Version.toString()); // "1.5.0"
120
+ * ```
121
+ *
122
+ * @category Library Loading
123
+ */
54
124
  export class Version {
55
125
  static get major(): number;
56
126
  static get minor(): number;
@@ -58,57 +128,198 @@ export class Version {
58
128
  static toString(): string;
59
129
  }
60
130
 
61
- /** Native library handle */
131
+ /**
132
+ * Low-level native library handle.
133
+ *
134
+ * For most use cases, prefer {@link CDLL} or {@link WinDLL} instead.
135
+ *
136
+ * @category Library Loading
137
+ */
62
138
  export class Library {
63
139
  constructor(path: string | null);
64
140
  func(name: string, returnType: AnyType, argTypes?: AnyType[], options?: FunctionOptions): FFIFunction;
141
+
142
+ /**
143
+ * Get the address of a symbol in the library.
144
+ * @param name - Symbol name
145
+ * @returns Address as BigInt
146
+ */
65
147
  symbol(name: string): bigint;
148
+
149
+ /** Close the library and release resources. */
66
150
  close(): void;
151
+
152
+ /** Library file path. */
67
153
  readonly path: string;
154
+
155
+ /** Whether the library is currently loaded. */
68
156
  readonly loaded: boolean;
69
157
  }
70
158
 
71
- /** Function call options */
159
+ /**
160
+ * Options for FFI function calls.
161
+ * @category Library Loading
162
+ */
72
163
  export interface FunctionOptions {
164
+ /** Calling convention. Defaults to `"cdecl"` for CDLL, `"stdcall"` for WinDLL. */
73
165
  abi?: "cdecl" | "stdcall" | "fastcall" | "thiscall" | "default";
74
166
  }
75
167
 
76
- /** Errcheck callback type */
168
+ /**
169
+ * Callback for intercepting function return values.
170
+ *
171
+ * Python equivalent: `func.errcheck`
172
+ *
173
+ * @param result - The raw return value
174
+ * @param func - The function that was called
175
+ * @param args - The arguments that were passed
176
+ * @returns The (possibly transformed) return value
177
+ *
178
+ * @category Library Loading
179
+ */
77
180
  export type ErrcheckCallback = (result: any, func: CallableFunction, args: any[]) => any;
78
181
 
79
- /** FFI function wrapper */
182
+ /**
183
+ * A callable FFI function returned by {@link Library.func} or {@link CDLL.func}.
184
+ * @category Library Loading
185
+ */
80
186
  export interface FFIFunction {
187
+ /** Call the native function synchronously. */
81
188
  (...args: any[]): any;
82
- /** Async version: executes the native call on a worker thread, returns a Promise */
189
+
190
+ /** Call the native function asynchronously on a worker thread. */
83
191
  callAsync(...args: any[]): Promise<any>;
192
+
193
+ /** The function name in the native library. */
84
194
  readonly funcName: string;
195
+
196
+ /** The function address as BigInt. */
85
197
  readonly address: bigint;
198
+
199
+ /**
200
+ * Error checking callback, called after every invocation.
201
+ * @see {@link ErrcheckCallback}
202
+ */
86
203
  errcheck: ErrcheckCallback | null;
87
204
  }
88
205
 
89
- /** CDLL - C calling convention library */
206
+ /**
207
+ * Load a shared library using the C calling convention (cdecl).
208
+ *
209
+ * This is the primary way to load and call functions from shared libraries.
210
+ * Supports both a traditional syntax and a Python ctypes-compatible syntax.
211
+ *
212
+ * Python equivalent: `ctypes.CDLL`
213
+ *
214
+ * @example Traditional syntax
215
+ * ```javascript
216
+ * import { CDLL, c_int } from 'node-ctypes';
217
+ *
218
+ * const libc = new CDLL('libc.so.6'); // Linux
219
+ * const abs = libc.func('abs', c_int, [c_int]);
220
+ * console.log(abs(-42)); // 42
221
+ * ```
222
+ *
223
+ * @example Python ctypes-like syntax
224
+ * ```javascript
225
+ * const libc = new CDLL('msvcrt.dll'); // Windows
226
+ * const abs_func = libc.abs;
227
+ * abs_func.argtypes = [c_int];
228
+ * abs_func.restype = c_int;
229
+ * console.log(abs_func(-42)); // 42
230
+ * ```
231
+ *
232
+ * @category Library Loading
233
+ */
90
234
  export class CDLL {
235
+ /**
236
+ * @param path - Path to the shared library, or `null` to load the current process
237
+ */
91
238
  constructor(path: string | null);
239
+
240
+ /**
241
+ * Get a callable function from the library.
242
+ *
243
+ * @param name - Function name in the library
244
+ * @param returnType - Return type (e.g. `c_int`, `c_double`, `c_void`)
245
+ * @param argTypes - Argument types array
246
+ * @param options - Calling convention options
247
+ * @returns A callable function
248
+ */
92
249
  func(name: string, returnType: AnyType, argTypes?: AnyType[], options?: FunctionOptions): CallableFunction & { callAsync(...args: any[]): Promise<any>; errcheck: ErrcheckCallback | null };
250
+
251
+ /**
252
+ * Get the address of a symbol.
253
+ * @param name - Symbol name
254
+ * @returns Address as BigInt
255
+ */
93
256
  symbol(name: string): bigint;
257
+
258
+ /** Close the library and clear the function cache. */
94
259
  close(): void;
260
+
261
+ /** Library file path. */
95
262
  readonly path: string;
263
+
264
+ /** Whether the library is loaded. */
96
265
  readonly loaded: boolean;
97
- /** Python-like function access: lib.FuncName returns a FunctionWrapper */
266
+
267
+ /**
268
+ * Python-like function access. Accessing any property returns a {@link FunctionWrapper}
269
+ * that supports `argtypes`/`restype` assignment.
270
+ */
98
271
  [funcName: string]: FunctionWrapper | any;
99
272
  }
100
273
 
101
- /** FunctionWrapper for Python-like argtypes/restype syntax */
274
+ /**
275
+ * Wrapper returned by Python-like property access on {@link CDLL}.
276
+ *
277
+ * Supports the `argtypes`/`restype` pattern from Python ctypes.
278
+ *
279
+ * @example
280
+ * ```javascript
281
+ * const abs_func = libc.abs;
282
+ * abs_func.argtypes = [c_int];
283
+ * abs_func.restype = c_int;
284
+ * console.log(abs_func(-42)); // 42
285
+ * ```
286
+ *
287
+ * @category Library Loading
288
+ */
102
289
  export interface FunctionWrapper {
290
+ /** Call the function. */
103
291
  (...args: any[]): any;
104
- /** Async version: executes the native call on a worker thread, returns a Promise */
292
+
293
+ /** Call the function asynchronously on a worker thread. */
105
294
  callAsync(...args: any[]): Promise<any>;
295
+
296
+ /** Argument types. Set before calling. */
106
297
  argtypes: AnyType[];
298
+
299
+ /** Return type. Set before calling. */
107
300
  restype: AnyType;
301
+
302
+ /** Optional error checking callback. */
108
303
  errcheck: ErrcheckCallback | null;
109
304
  }
110
305
 
111
- /** WinDLL - stdcall calling convention library (Windows) */
306
+ /**
307
+ * Load a shared library using the stdcall calling convention.
308
+ *
309
+ * Used for Windows API functions (kernel32, user32, etc.).
310
+ *
311
+ * Python equivalent: `ctypes.WinDLL`
312
+ *
313
+ * @example
314
+ * ```javascript
315
+ * import { WinDLL, c_void, c_void_p } from 'node-ctypes';
316
+ *
317
+ * const kernel32 = new WinDLL('kernel32.dll');
318
+ * const GetLocalTime = kernel32.func('GetLocalTime', c_void, [c_void_p]);
319
+ * ```
320
+ *
321
+ * @category Library Loading
322
+ */
112
323
  export class WinDLL extends CDLL {
113
324
  constructor(path: string);
114
325
  }
@@ -117,30 +328,52 @@ export class WinDLL extends CDLL {
117
328
  // Callbacks
118
329
  // =============================================================================
119
330
 
120
- /** Callback wrapper for main-thread callbacks */
331
+ /**
332
+ * Wrapper around a native callback.
333
+ *
334
+ * **Important:** Always call {@link CallbackWrapper.release | release()} when the callback
335
+ * is no longer needed to prevent memory leaks.
336
+ *
337
+ * @category Callbacks
338
+ */
121
339
  export interface CallbackWrapper {
340
+ /** The callback function pointer, passable to native functions. */
122
341
  readonly pointer: bigint;
342
+
343
+ /** Release the callback and free native resources. Must be called when done. */
123
344
  release(): void;
124
345
  }
125
346
 
126
- /** Native Callback class */
347
+ /**
348
+ * Native callback class for main-thread callbacks.
349
+ * @category Callbacks
350
+ */
127
351
  export class Callback {
128
352
  constructor(fn: Function, returnType: AnyType, argTypes?: AnyType[]);
129
353
  readonly pointer: bigint;
130
354
  release(): void;
131
355
  }
132
356
 
133
- /** Thread-safe callback class */
357
+ /**
358
+ * Thread-safe callback that can be invoked from any thread.
359
+ *
360
+ * Use this when native code may call the callback from a thread other than the main thread.
361
+ *
362
+ * @category Callbacks
363
+ */
134
364
  export class ThreadSafeCallback extends Callback {}
135
365
 
136
366
  // =============================================================================
137
367
  // Structures
138
368
  // =============================================================================
139
369
 
140
- /** Field definition for struct */
370
+ /**
371
+ * Metadata for a single field in a struct or union.
372
+ * @category Structures
373
+ */
141
374
  export interface FieldDef {
142
375
  name: string;
143
- type: AnyType | StructDef | ArrayTypeDef | BitFieldDef;
376
+ type: AnyType | StructDef | ArrayTypeDef | BitFieldDef | PointerTypeDef;
144
377
  offset: number;
145
378
  size: number;
146
379
  alignment: number;
@@ -153,23 +386,40 @@ export interface FieldDef {
153
386
  baseSize?: number;
154
387
  }
155
388
 
156
- /** Struct definition */
389
+ /**
390
+ * A struct type definition created by the {@link struct} function.
391
+ *
392
+ * Provides low-level `create()`, `get()`, `set()`, and `toObject()` methods.
393
+ * For Python-like syntax, use {@link Structure} class instead.
394
+ *
395
+ * @category Structures
396
+ */
157
397
  export interface StructDef {
398
+ /** Total size of the struct in bytes. */
158
399
  readonly size: number;
400
+ /** Alignment requirement in bytes. */
159
401
  readonly alignment: number;
402
+ /** Field definitions. */
160
403
  readonly fields: FieldDef[];
404
+ /** Whether the struct is packed (no padding). */
161
405
  readonly packed: boolean;
406
+ /** @internal */
162
407
  readonly _isStructType: true;
163
408
 
409
+ /** Allocate a buffer and optionally initialize field values. */
164
410
  create(values?: Record<string, any>): Buffer;
411
+ /** Read a field value from a buffer. */
165
412
  get(buf: Buffer, fieldName: string): any;
413
+ /** Write a field value to a buffer. */
166
414
  set(buf: Buffer, fieldName: string, value: any): void;
415
+ /** Convert all fields to a plain JavaScript object. */
167
416
  toObject(buf: Buffer): Record<string, any>;
417
+ /** Get a view of a nested struct's buffer region. */
168
418
  getNestedBuffer(buf: Buffer, fieldName: string): Buffer;
169
419
  }
170
420
 
171
- // Helper types for improved typed structs
172
- export type FieldSpec = AnyType | StructDef | ArrayTypeDef | BitFieldDef | AnonymousField;
421
+ /** @category Structures */
422
+ export type FieldSpec = AnyType | StructDef | ArrayTypeDef | BitFieldDef | AnonymousField | PointerTypeDef;
173
423
 
174
424
  type JsFromCType<T> = T extends "int64" | "uint64" | "size_t"
175
425
  ? bigint
@@ -193,44 +443,98 @@ type FieldsToInstance<F extends Record<string, FieldSpec>> = {
193
443
  [K in keyof F]: JsFromCType<F[K]>;
194
444
  };
195
445
 
196
- /** Union definition (same interface as struct) */
446
+ /**
447
+ * A union type definition created by the {@link union} function.
448
+ *
449
+ * All fields share the same memory region. The total size equals the
450
+ * size of the largest field.
451
+ *
452
+ * @category Structures
453
+ */
197
454
  export interface UnionDef extends StructDef {
198
455
  readonly isUnion: true;
199
456
  }
200
457
 
201
- /** Bit field definition */
458
+ /**
459
+ * A bit field definition created by the {@link bitfield} function.
460
+ *
461
+ * @example
462
+ * ```javascript
463
+ * class Flags extends Structure {
464
+ * static _fields_ = [
465
+ * ["enabled", bitfield(c_uint32, 1)],
466
+ * ["mode", bitfield(c_uint32, 3)],
467
+ * ];
468
+ * }
469
+ * ```
470
+ *
471
+ * @category Structures
472
+ */
202
473
  export interface BitFieldDef {
474
+ /** @internal */
203
475
  readonly _isBitField: true;
476
+ /** The underlying integer type. */
204
477
  readonly baseType: AnyType;
478
+ /** Number of bits. */
205
479
  readonly bits: number;
480
+ /** Size of the base type in bytes. */
206
481
  readonly baseSize: number;
207
482
  }
208
483
 
209
- /** Array type definition */
484
+ /**
485
+ * An array type definition created by the {@link array} function.
486
+ *
487
+ * @example
488
+ * ```javascript
489
+ * const IntArray5 = array(c_int32, 5);
490
+ * const arr = IntArray5.create([1, 2, 3, 4, 5]);
491
+ * console.log(arr[0]); // 1
492
+ * ```
493
+ *
494
+ * @category Structures
495
+ */
210
496
  export interface ArrayTypeDef {
497
+ /** Element type. */
211
498
  readonly elementType: AnyType;
499
+ /** Number of elements. */
212
500
  readonly length: number;
501
+ /** @internal */
213
502
  readonly _isArrayType: true;
214
503
 
504
+ /** Total size in bytes (elementSize * length). */
215
505
  getSize(): number;
506
+ /** Number of elements. */
216
507
  getLength(): number;
508
+ /** Element alignment. */
217
509
  getAlignment(): number;
510
+ /** Create a new array, optionally initialized with values. Returns a Proxy with index access. */
218
511
  create(values?: any[]): Buffer & ArrayProxy;
512
+ /** Wrap an existing buffer as an array with index access. */
219
513
  wrap(buffer: Buffer): Buffer & ArrayProxy;
220
514
  }
221
515
 
222
- /** Array proxy for index access */
516
+ /**
517
+ * Proxy interface for array index access and iteration.
518
+ * @category Structures
519
+ */
223
520
  export interface ArrayProxy {
224
521
  [index: number]: any;
225
522
  [Symbol.iterator](): Iterator<any>;
226
523
  }
227
524
 
228
- /** Struct options */
525
+ /**
526
+ * Options for struct creation.
527
+ * @category Structures
528
+ */
229
529
  export interface StructOptions {
530
+ /** If `true`, disables padding between fields. */
230
531
  packed?: boolean;
231
532
  }
232
533
 
233
- /** Anonymous field wrapper */
534
+ /**
535
+ * Wrapper for anonymous fields in structs/unions.
536
+ * @category Structures
537
+ */
234
538
  export interface AnonymousField {
235
539
  type: StructDef;
236
540
  anonymous: true;
@@ -240,16 +544,178 @@ export interface AnonymousField {
240
544
  // Pointers
241
545
  // =============================================================================
242
546
 
243
- /** POINTER type definition */
547
+ /**
548
+ * A pointer instance created by {@link PointerTypeDef.create}, {@link PointerTypeDef.fromBuffer},
549
+ * or {@link PointerTypeDef.fromAddress}.
550
+ *
551
+ * Supports Python ctypes-compatible `.contents` property and C-like `[index]` pointer arithmetic.
552
+ *
553
+ * @example
554
+ * ```javascript
555
+ * const IntPtr = POINTER(c_int32);
556
+ * const p = IntPtr.fromBuffer(buf);
557
+ * console.log(p.contents); // dereferenced value (like *p in C)
558
+ * console.log(p[0]); // same as p.contents
559
+ * console.log(p[5]); // pointer arithmetic (like p[5] in C)
560
+ * ```
561
+ *
562
+ * @example Access a native array via pointer from a struct field
563
+ * ```javascript
564
+ * // When a struct has a c_void_p field pointing to an array of structs:
565
+ * const pValues = POINTER(ADSVALUE).fromAddress(col.pADsValues);
566
+ * for (let i = 0; i < col.dwNumValues; i++) {
567
+ * console.log(pValues[i].dwType); // struct view at pValues + i * sizeof(ADSVALUE)
568
+ * }
569
+ * ```
570
+ *
571
+ * @category Pointers
572
+ */
573
+ export interface PointerInstance {
574
+ /** The pointer type definition this instance was created from. */
575
+ readonly _pointerType: PointerTypeDef;
576
+ /** The base type this pointer points to. */
577
+ readonly _baseType: AnyType | StructDef;
578
+ /** Size of the pointed-to type in bytes. */
579
+ readonly _baseSize: number;
580
+
581
+ /** Raw memory address as BigInt. */
582
+ readonly address: bigint;
583
+
584
+ /**
585
+ * Dereference the pointer — read/write the value at the pointed location.
586
+ *
587
+ * Python equivalent: `p.contents`
588
+ *
589
+ * @example
590
+ * ```javascript
591
+ * const p = POINTER(c_int32).fromBuffer(buf);
592
+ * console.log(p.contents); // read value
593
+ * p.contents = 42; // write value
594
+ * ```
595
+ */
596
+ contents: any;
597
+
598
+ /** The underlying buffer (if created from a buffer), or null. */
599
+ readonly _buffer: Buffer | null;
600
+
601
+ /** Alias for contents getter. */
602
+ deref(): any;
603
+
604
+ /**
605
+ * Set pointer to a new target (buffer or address).
606
+ * @param value - Buffer to point to, or BigInt/number address
607
+ */
608
+ set(value: Buffer | bigint | number): void;
609
+
610
+ /** Check if this is a NULL pointer. */
611
+ readonly isNull: boolean;
612
+
613
+ /** Get pointer as an 8-byte buffer containing the address. */
614
+ toBuffer(): Buffer;
615
+
616
+ toString(): string;
617
+
618
+ /**
619
+ * Pointer arithmetic — access elements at offset from the pointer.
620
+ *
621
+ * `p[i]` reads `sizeof(baseType)` bytes at `address + i * sizeof(baseType)`,
622
+ * exactly like C pointer indexing.
623
+ *
624
+ * @example
625
+ * ```javascript
626
+ * const arr = POINTER(c_int32).fromAddress(arrayAddr);
627
+ * arr[0] // first element
628
+ * arr[5] // sixth element
629
+ * arr[2] = 99; // write to third element
630
+ * ```
631
+ */
632
+ [index: number]: any;
633
+ }
634
+
635
+ /**
636
+ * A pointer type created by {@link POINTER}.
637
+ *
638
+ * @example
639
+ * ```javascript
640
+ * const IntPtr = POINTER(c_int32);
641
+ *
642
+ * // From existing buffer
643
+ * const p = IntPtr.fromBuffer(buf);
644
+ * console.log(p.contents); // 42
645
+ * console.log(p[0]); // 42
646
+ *
647
+ * // From raw address (e.g., from a struct's c_void_p field)
648
+ * const pValues = POINTER(MyStruct).fromAddress(someStruct.pData);
649
+ * console.log(pValues[0].field1);
650
+ * ```
651
+ *
652
+ * @category Pointers
653
+ */
244
654
  export interface PointerTypeDef {
655
+ /** The type this pointer points to. */
245
656
  readonly _pointerTo: AnyType | StructDef;
657
+ /** Size of the pointed-to type. */
246
658
  readonly _baseSize: number;
659
+ /** Pointer size (always 8 on 64-bit). */
247
660
  readonly size: number;
248
661
 
249
- create(): Buffer;
250
- fromBuffer(targetBuf: Buffer): Buffer;
662
+ /**
663
+ * Create a NULL pointer instance.
664
+ *
665
+ * @example
666
+ * ```javascript
667
+ * const p = POINTER(c_int32).create();
668
+ * console.log(p.isNull); // true
669
+ * ```
670
+ */
671
+ create(value?: Buffer | bigint | number): PointerInstance;
672
+
673
+ /**
674
+ * Create a pointer from an existing buffer (points to that buffer's memory).
675
+ *
676
+ * @param targetBuf - Buffer to point to
677
+ *
678
+ * @example
679
+ * ```javascript
680
+ * const buf = Buffer.alloc(4);
681
+ * buf.writeInt32LE(42, 0);
682
+ * const p = POINTER(c_int32).fromBuffer(buf);
683
+ * console.log(p.contents); // 42
684
+ * ```
685
+ */
686
+ fromBuffer(targetBuf: Buffer): PointerInstance;
687
+
688
+ /**
689
+ * Create a pointer from a raw memory address.
690
+ *
691
+ * Use this to create typed access to native memory, especially useful with
692
+ * `c_void_p` struct fields that point to arrays of structs.
693
+ *
694
+ * @param address - Memory address as BigInt or number
695
+ *
696
+ * @example
697
+ * ```javascript
698
+ * // Access array of structs from a c_void_p field
699
+ * const pValues = POINTER(ADSVALUE).fromAddress(col.pADsValues);
700
+ * for (let i = 0; i < col.dwNumValues; i++) {
701
+ * console.log(pValues[i].dwType);
702
+ * }
703
+ * ```
704
+ */
705
+ fromAddress(address: bigint | number): PointerInstance;
706
+
707
+ /**
708
+ * @deprecated Use pointer instance `.contents` instead.
709
+ * Legacy API: Dereferences a pointer buffer.
710
+ */
251
711
  deref(ptrBuf: Buffer): bigint | null;
712
+
713
+ /**
714
+ * @deprecated Use pointer instance `.set()` instead.
715
+ * Legacy API: Sets pointer buffer value.
716
+ */
252
717
  set(ptrBuf: Buffer, value: Buffer | bigint): void;
718
+
253
719
  toString(): string;
254
720
  }
255
721
 
@@ -257,7 +723,10 @@ export interface PointerTypeDef {
257
723
  // Native Struct/Array Types
258
724
  // =============================================================================
259
725
 
260
- /** Native StructType class */
726
+ /**
727
+ * Low-level native struct type. Prefer {@link Structure} for most use cases.
728
+ * @category Structures
729
+ */
261
730
  export class StructType {
262
731
  constructor(isUnion?: boolean);
263
732
  addField(name: string, type: AnyType | StructType | ArrayType): this;
@@ -267,7 +736,10 @@ export class StructType {
267
736
  read(buffer: Buffer): Record<string, any>;
268
737
  }
269
738
 
270
- /** Native ArrayType class */
739
+ /**
740
+ * Low-level native array type. Prefer {@link array} for most use cases.
741
+ * @category Structures
742
+ */
271
743
  export class ArrayType {
272
744
  constructor(elementType: AnyType, count: number);
273
745
  getSize(): number;
@@ -277,27 +749,109 @@ export class ArrayType {
277
749
  }
278
750
 
279
751
  /**
280
- * Python-like Structure base class.
281
- * Subclasses should define `static _fields_` as array of [name, type]
282
- * or an object map { name: type }.
752
+ * Base class for Python-like struct definitions.
753
+ *
754
+ * Subclasses define fields via `static _fields_`. Field access is transparent —
755
+ * `point.x` reads directly from the underlying buffer.
756
+ *
757
+ * Python equivalent: `ctypes.Structure`
758
+ *
759
+ * @example
760
+ * ```javascript
761
+ * import { Structure, c_int } from 'node-ctypes';
762
+ *
763
+ * class Point extends Structure {
764
+ * static _fields_ = [["x", c_int], ["y", c_int]];
765
+ * }
766
+ *
767
+ * const p = new Point(10, 20);
768
+ * console.log(p.x, p.y); // 10 20
769
+ *
770
+ * // Nested structs
771
+ * class Rect extends Structure {
772
+ * static _fields_ = [["topLeft", Point], ["bottomRight", Point]];
773
+ * }
774
+ * ```
775
+ *
776
+ * @category Structures
283
777
  */
284
778
  export class Structure<F extends Record<string, FieldSpec> = Record<string, any>> {
779
+ /**
780
+ * Create a new struct instance.
781
+ * @param args - Positional values matching field order, or a single object with field names as keys
782
+ */
285
783
  constructor(...args: any[]);
784
+
785
+ /**
786
+ * Field definitions. Array of `[name, type]` tuples, or `[name, type, bits]` for bit fields.
787
+ *
788
+ * @example
789
+ * ```javascript
790
+ * static _fields_ = [
791
+ * ["x", c_int],
792
+ * ["y", c_int],
793
+ * ["flags", c_uint32, 3], // 3-bit bit field
794
+ * ];
795
+ * ```
796
+ */
286
797
  static _fields_?: Array<[string, FieldSpec]> | Record<string, FieldSpec>;
798
+
799
+ /** If `true`, disables padding between fields. */
287
800
  static _pack_?: boolean;
801
+
802
+ /**
803
+ * List of field names whose members are promoted to the parent struct.
804
+ *
805
+ * Python equivalent: `_anonymous_`
806
+ */
288
807
  static _anonymous_?: string[];
808
+
809
+ /** Create instance with optional initial values. */
289
810
  static create<ThisT extends Structure<F>>(this: new (...args: any[]) => ThisT, values?: Partial<FieldsToInstance<F>> | Buffer): ThisT;
811
+
812
+ /** Convert a buffer or instance to a plain object. */
290
813
  static toObject<ThisT extends Structure<F>>(this: new (...args: any[]) => ThisT, bufOrInstance: Buffer | Structure<F>): FieldsToInstance<F>;
814
+
815
+ /** The underlying memory buffer. Passed automatically to native functions. */
291
816
  _buffer: Buffer;
817
+
818
+ /** The struct definition metadata. */
292
819
  _structDef: StructDef;
820
+
821
+ /** Read a field value by name. */
293
822
  get<K extends keyof F & string>(fieldName: K): FieldsToInstance<F>[K];
823
+
824
+ /** Write a field value by name. */
294
825
  set<K extends keyof F & string>(fieldName: K, value: FieldsToInstance<F>[K]): void;
826
+
827
+ /** Convert all fields to a plain JavaScript object (snapshot). */
295
828
  toObject(): FieldsToInstance<F>;
296
- [field: string]: any; // instance has dynamic properties for fields
829
+
830
+ /** Dynamic field access via Proxy. */
831
+ [field: string]: any;
297
832
  }
298
833
 
299
834
  /**
300
- * Python-like Union base class.
835
+ * Base class for Python-like union definitions.
836
+ *
837
+ * All fields share the same memory. The size equals the largest field.
838
+ *
839
+ * Python equivalent: `ctypes.Union`
840
+ *
841
+ * @example
842
+ * ```javascript
843
+ * import { Union, c_int, c_float } from 'node-ctypes';
844
+ *
845
+ * class IntOrFloat extends Union {
846
+ * static _fields_ = [["i", c_int], ["f", c_float]];
847
+ * }
848
+ *
849
+ * const u = new IntOrFloat();
850
+ * u.f = 3.14;
851
+ * console.log(u.i); // bit pattern of 3.14 as integer
852
+ * ```
853
+ *
854
+ * @category Structures
301
855
  */
302
856
  export class Union<F extends Record<string, FieldSpec> = Record<string, any>> extends Structure<F> {}
303
857
 
@@ -305,68 +859,548 @@ export class Union<F extends Record<string, FieldSpec> = Record<string, any>> ex
305
859
  // Functions
306
860
  // =============================================================================
307
861
 
308
- // Library loading
862
+ /**
863
+ * Load a shared library at the lowest level.
864
+ *
865
+ * For most use cases, prefer {@link CDLL} or {@link WinDLL}.
866
+ *
867
+ * @param path - Library path, or `null` for the current process
868
+ * @returns A {@link Library} handle
869
+ *
870
+ * @category Library Loading
871
+ */
309
872
  export function load(path: string | null): Library;
310
873
 
311
- // Callbacks
874
+ /**
875
+ * Create a callback that can be passed to native functions.
876
+ *
877
+ * The callback runs on the main thread. For thread-safe callbacks,
878
+ * use {@link threadSafeCallback}.
879
+ *
880
+ * **Important:** Call `.release()` when the callback is no longer needed.
881
+ *
882
+ * Python equivalent: `ctypes.CFUNCTYPE(...)(func)`
883
+ *
884
+ * @param fn - JavaScript function to wrap
885
+ * @param returnType - Return type of the callback
886
+ * @param argTypes - Argument types
887
+ * @returns A wrapper with a `.pointer` property and `.release()` method
888
+ *
889
+ * @example
890
+ * ```javascript
891
+ * import { callback, c_int32, c_void_p, readValue } from 'node-ctypes';
892
+ *
893
+ * const compare = callback(
894
+ * (a, b) => readValue(a, c_int32) - readValue(b, c_int32),
895
+ * c_int32, [c_void_p, c_void_p]
896
+ * );
897
+ * // Pass compare.pointer to qsort, etc.
898
+ * compare.release();
899
+ * ```
900
+ *
901
+ * @category Callbacks
902
+ */
312
903
  export function callback(fn: Function, returnType: AnyType, argTypes?: AnyType[]): CallbackWrapper;
904
+
905
+ /**
906
+ * Create a thread-safe callback that can be invoked from any thread.
907
+ *
908
+ * **Important:** Call `.release()` when the callback is no longer needed.
909
+ *
910
+ * @param fn - JavaScript function to wrap
911
+ * @param returnType - Return type of the callback
912
+ * @param argTypes - Argument types
913
+ *
914
+ * @category Callbacks
915
+ */
313
916
  export function threadSafeCallback(fn: Function, returnType: AnyType, argTypes?: AnyType[]): CallbackWrapper;
314
917
 
315
- // Memory management - Python-compatible
918
+ /**
919
+ * Create a function pointer type with cdecl calling convention.
920
+ *
921
+ * Returns a factory that takes a BigInt address and returns a callable function.
922
+ * Used for COM vtable dispatch and other scenarios with runtime function pointers.
923
+ *
924
+ * Python equivalent: `ctypes.CFUNCTYPE`
925
+ *
926
+ * @param restype - Return type
927
+ * @param argtypes - Argument types
928
+ * @returns A factory function: `(address: bigint) => callable`
929
+ *
930
+ * @example
931
+ * ```javascript
932
+ * import { CDLL, CFUNCTYPE, c_size_t, c_char_p } from 'node-ctypes';
933
+ *
934
+ * const libc = new CDLL('msvcrt.dll');
935
+ * const addr = libc.symbol('strlen');
936
+ * const StrlenType = CFUNCTYPE(c_size_t, c_char_p);
937
+ * const strlen = StrlenType(addr);
938
+ * console.log(strlen('Hello')); // 5n
939
+ * ```
940
+ *
941
+ * @category Callbacks
942
+ */
943
+ export function CFUNCTYPE(restype: AnyType, ...argtypes: AnyType[]): (address: bigint) => (...args: any[]) => any;
944
+
945
+ /**
946
+ * Create a function pointer type with stdcall calling convention (Windows).
947
+ *
948
+ * Same as {@link CFUNCTYPE} but uses stdcall. Used for Windows API function pointers.
949
+ *
950
+ * Python equivalent: `ctypes.WINFUNCTYPE`
951
+ *
952
+ * @param restype - Return type
953
+ * @param argtypes - Argument types
954
+ * @returns A factory function: `(address: bigint) => callable`
955
+ *
956
+ * @category Callbacks
957
+ */
958
+ export function WINFUNCTYPE(restype: AnyType, ...argtypes: AnyType[]): (address: bigint) => (...args: any[]) => any;
959
+
960
+ /**
961
+ * Create a C string buffer (null-terminated).
962
+ *
963
+ * Python equivalent: `ctypes.create_string_buffer`
964
+ *
965
+ * @param init - Size in bytes, a string to copy, or an existing Buffer
966
+ * @returns A Buffer containing the null-terminated string
967
+ *
968
+ * @example
969
+ * ```javascript
970
+ * const buf = create_string_buffer('Hello');
971
+ * console.log(string_at(buf)); // "Hello"
972
+ *
973
+ * const buf2 = create_string_buffer(256); // 256-byte zero-filled buffer
974
+ * ```
975
+ *
976
+ * @category Memory
977
+ */
316
978
  export function create_string_buffer(init: number | string | Buffer): Buffer;
979
+
980
+ /**
981
+ * Create a wide string buffer (wchar_t, null-terminated).
982
+ *
983
+ * On Windows, this is UTF-16LE. On Unix, UTF-32.
984
+ *
985
+ * Python equivalent: `ctypes.create_unicode_buffer`
986
+ *
987
+ * @param init - Size in characters, or a string to copy
988
+ *
989
+ * @category Memory
990
+ */
317
991
  export function create_unicode_buffer(init: number | string): Buffer;
992
+
993
+ /**
994
+ * Read a C string from an address or buffer.
995
+ *
996
+ * Python equivalent: `ctypes.string_at`
997
+ *
998
+ * @param address - Buffer, BigInt address, or numeric address
999
+ * @param size - Maximum bytes to read (optional, reads until null terminator)
1000
+ *
1001
+ * @category Memory
1002
+ */
318
1003
  export function string_at(address: Buffer | bigint | number, size?: number): string | null;
1004
+
1005
+ /**
1006
+ * Read a wide string from an address or buffer.
1007
+ *
1008
+ * Python equivalent: `ctypes.wstring_at`
1009
+ *
1010
+ * @param address - Buffer, BigInt address, or numeric address
1011
+ * @param size - Maximum characters to read
1012
+ *
1013
+ * @category Memory
1014
+ */
319
1015
  export function wstring_at(address: Buffer | bigint | number, size?: number): string | null;
1016
+
1017
+ /**
1018
+ * Get the address of a buffer as BigInt.
1019
+ *
1020
+ * Python equivalent: `ctypes.addressof`
1021
+ *
1022
+ * @category Memory
1023
+ */
320
1024
  export function addressof(ptr: Buffer | bigint | number): bigint;
1025
+
1026
+ /**
1027
+ * Copy memory from source to destination.
1028
+ *
1029
+ * Python equivalent: `ctypes.memmove`
1030
+ *
1031
+ * @category Memory
1032
+ */
321
1033
  export function memmove(dst: Buffer, src: Buffer | bigint, count: number): void;
1034
+
1035
+ /**
1036
+ * Fill memory with a byte value.
1037
+ *
1038
+ * Python equivalent: `ctypes.memset`
1039
+ *
1040
+ * @category Memory
1041
+ */
322
1042
  export function memset(dst: Buffer, value: number, count: number): void;
323
1043
 
324
- // Memory - utility (no direct Python equivalent)
1044
+ /**
1045
+ * Read a typed value from a buffer at a given offset.
1046
+ *
1047
+ * @param ptr - Buffer to read from
1048
+ * @param type - Type to read (e.g. `c_int32`, `c_double`)
1049
+ * @param offset - Byte offset (default 0)
1050
+ *
1051
+ * @example
1052
+ * ```javascript
1053
+ * const buf = Buffer.alloc(8);
1054
+ * buf.writeInt32LE(42, 0);
1055
+ * console.log(readValue(buf, c_int32)); // 42
1056
+ * ```
1057
+ *
1058
+ * @category Memory
1059
+ */
325
1060
  export function readValue(ptr: Buffer | bigint | number, type: AnyType, offset?: number): any;
1061
+
1062
+ /**
1063
+ * Write a typed value to a buffer at a given offset.
1064
+ *
1065
+ * @param ptr - Buffer to write to
1066
+ * @param type - Type to write (e.g. `c_int32`, `c_double`)
1067
+ * @param value - Value to write
1068
+ * @param offset - Byte offset (default 0)
1069
+ *
1070
+ * @category Memory
1071
+ */
326
1072
  export function writeValue(ptr: Buffer | bigint | number, type: AnyType, value: any, offset?: number): number | void;
1073
+
1074
+ /**
1075
+ * Get the size of a type in bytes.
1076
+ *
1077
+ * Python equivalent: `ctypes.sizeof`
1078
+ *
1079
+ * @example
1080
+ * ```javascript
1081
+ * sizeof(c_int32) // 4
1082
+ * sizeof(c_double) // 8
1083
+ * sizeof(c_void_p) // 8 (on 64-bit)
1084
+ * ```
1085
+ *
1086
+ * @category Memory
1087
+ */
327
1088
  export function sizeof(type: AnyType | StructDef | ArrayTypeDef): number;
1089
+
1090
+ /**
1091
+ * Get the alignment requirement of a type in bytes.
1092
+ *
1093
+ * Python equivalent: `ctypes.alignment`
1094
+ *
1095
+ * @example
1096
+ * ```javascript
1097
+ * alignment(c_int32) // 4
1098
+ * alignment(c_double) // 8
1099
+ * alignment(MyStruct) // max field alignment
1100
+ * ```
1101
+ *
1102
+ * @category Memory
1103
+ */
1104
+ export function alignment(type: AnyType | StructDef | ArrayTypeDef | typeof Structure | typeof Union): number;
1105
+
1106
+ /**
1107
+ * Create a Buffer view of native memory at a given address.
1108
+ *
1109
+ * **Warning:** Use with caution — the buffer is not bounds-checked.
1110
+ *
1111
+ * @param address - Native memory address
1112
+ * @param size - Number of bytes
1113
+ *
1114
+ * @category Memory
1115
+ */
328
1116
  export function ptrToBuffer(address: bigint | number, size: number): Buffer;
329
1117
 
330
- // Structures
1118
+ /**
1119
+ * Define a struct using the functional API.
1120
+ *
1121
+ * For Python-like class syntax, use {@link Structure} instead.
1122
+ *
1123
+ * @param fields - Object mapping field names to types
1124
+ * @param options - Struct options (e.g. `{ packed: true }`)
1125
+ *
1126
+ * @example
1127
+ * ```javascript
1128
+ * const PointDef = struct({ x: c_int32, y: c_int32 });
1129
+ * const buf = PointDef.create({ x: 10, y: 20 });
1130
+ * console.log(PointDef.get(buf, 'x')); // 10
1131
+ * ```
1132
+ *
1133
+ * @category Structures
1134
+ */
331
1135
  export function struct(fields: Record<string, FieldSpec>, options?: StructOptions): StructDef;
1136
+
1137
+ /**
1138
+ * Define a union using the functional API.
1139
+ *
1140
+ * For Python-like class syntax, use {@link Union} instead.
1141
+ *
1142
+ * @param fields - Object mapping field names to types
1143
+ *
1144
+ * @category Structures
1145
+ */
332
1146
  export function union(fields: Record<string, FieldSpec>): UnionDef;
333
- // Factory helpers that return a typed class extending Structure/Union
1147
+
1148
+ /**
1149
+ * Factory that returns a Structure subclass with typed fields.
1150
+ *
1151
+ * @param fields - Object mapping field names to types
1152
+ * @param options - Struct options
1153
+ *
1154
+ * @category Structures
1155
+ */
334
1156
  export function defineStruct<F extends Record<string, FieldSpec>>(fields: F, options?: StructOptions): new (...args: any[]) => Structure<F>;
1157
+
1158
+ /**
1159
+ * Factory that returns a Union subclass with typed fields.
1160
+ *
1161
+ * @param fields - Object mapping field names to types
1162
+ *
1163
+ * @category Structures
1164
+ */
335
1165
  export function defineUnion<F extends Record<string, FieldSpec>>(fields: F): new (...args: any[]) => Union<F>;
1166
+
1167
+ /**
1168
+ * Define a fixed-size array type.
1169
+ *
1170
+ * Python equivalent: `c_int * 5`
1171
+ *
1172
+ * @param elementType - Type of each element
1173
+ * @param count - Number of elements
1174
+ *
1175
+ * @example
1176
+ * ```javascript
1177
+ * const IntArray5 = array(c_int32, 5);
1178
+ * const arr = IntArray5.create([1, 2, 3, 4, 5]);
1179
+ * console.log(arr[0]); // 1
1180
+ * ```
1181
+ *
1182
+ * @category Structures
1183
+ */
336
1184
  export function array(elementType: AnyType | typeof Structure | typeof Union, count: number): ArrayTypeDef;
1185
+
1186
+ /**
1187
+ * Define a bit field for use in struct `_fields_`.
1188
+ *
1189
+ * @param baseType - The underlying integer type (e.g. `c_uint32`)
1190
+ * @param bits - Number of bits
1191
+ *
1192
+ * @example
1193
+ * ```javascript
1194
+ * class Flags extends Structure {
1195
+ * static _fields_ = [
1196
+ * ["enabled", bitfield(c_uint32, 1)],
1197
+ * ["mode", bitfield(c_uint32, 3)],
1198
+ * ];
1199
+ * }
1200
+ * ```
1201
+ *
1202
+ * @category Structures
1203
+ */
337
1204
  export function bitfield(baseType: AnyType, bits: number): BitFieldDef;
338
1205
 
339
- // Pointers
1206
+ /**
1207
+ * Pass an object by reference (returns its underlying buffer).
1208
+ *
1209
+ * Python equivalent: `ctypes.byref`
1210
+ *
1211
+ * @param obj - A Buffer, SimpleCData instance, or any object with a `_buffer` property
1212
+ *
1213
+ * @category Pointers
1214
+ */
340
1215
  export function byref(obj: Buffer | SimpleCDataInstance | { _buffer: Buffer }): Buffer;
341
- export function cast(ptr: Buffer | bigint, targetType: AnyType | StructDef): Buffer | { [key: string]: any };
1216
+
1217
+ /**
1218
+ * Interpret a pointer as a different type.
1219
+ *
1220
+ * Python equivalent: `ctypes.cast`
1221
+ *
1222
+ * When the target is a {@link PointerTypeDef}, returns a {@link PointerInstance} with
1223
+ * `.contents` and `[index]` access — equivalent to Python's
1224
+ * `cast(c_void_p(addr), POINTER(MyStruct))`.
1225
+ *
1226
+ * @param ptr - Buffer, BigInt address, number, or SimpleCData instance (e.g., `c_void_p`)
1227
+ * @param targetType - The target type (primitive, struct, or POINTER type)
1228
+ *
1229
+ * @example Cast to POINTER for typed array access
1230
+ * ```javascript
1231
+ * // Python: cast(c_void_p(addr), POINTER(MyStruct))
1232
+ * const pValues = cast(col.pADsValues, POINTER(ADSVALUE));
1233
+ * for (let i = 0; i < col.dwNumValues; i++) {
1234
+ * console.log(pValues[i].dwType);
1235
+ * }
1236
+ * ```
1237
+ *
1238
+ * @example Cast to primitive type
1239
+ * ```javascript
1240
+ * const floatVal = cast(intBuf, c_float);
1241
+ * ```
1242
+ *
1243
+ * @category Pointers
1244
+ */
1245
+ export function cast(ptr: Buffer | bigint | number | SimpleCDataInstance, targetType: PointerTypeDef): PointerInstance;
1246
+ export function cast(ptr: Buffer | bigint | number | SimpleCDataInstance, targetType: AnyType | StructDef): any;
1247
+
1248
+ /**
1249
+ * Create a pointer type for a given base type.
1250
+ *
1251
+ * Python equivalent: `ctypes.POINTER`
1252
+ *
1253
+ * @param baseType - The type to point to
1254
+ * @returns A pointer type with `create()`, `fromBuffer()`, etc.
1255
+ *
1256
+ * @example Basic pointer
1257
+ * ```javascript
1258
+ * const IntPtr = POINTER(c_int32);
1259
+ * const buf = Buffer.alloc(4);
1260
+ * buf.writeInt32LE(42, 0);
1261
+ *
1262
+ * const p = IntPtr.fromBuffer(buf);
1263
+ * console.log(p.contents); // 42
1264
+ * console.log(p[0]); // 42
1265
+ * ```
1266
+ *
1267
+ * @example Typed access to native array via c_void_p struct field
1268
+ * ```javascript
1269
+ * // Structure fields don't accept POINTER() types directly — use c_void_p,
1270
+ * // then wrap with POINTER().fromAddress() for typed indexed access:
1271
+ * class Column extends Structure {
1272
+ * static _fields_ = [["pValues", c_void_p], ["count", c_uint32]];
1273
+ * }
1274
+ * const pValues = POINTER(ValueStruct).fromAddress(col.pValues);
1275
+ * for (let i = 0; i < col.count; i++) {
1276
+ * console.log(pValues[i].field); // struct view at offset i
1277
+ * }
1278
+ * ```
1279
+ *
1280
+ * @category Pointers
1281
+ */
342
1282
  export function POINTER(baseType: AnyType | StructDef): PointerTypeDef;
343
- export function pointer(obj: SimpleCDataInstance | { _buffer: Buffer }): SimpleCDataInstance & { contents: any; [index: number]: any };
344
1283
 
345
- // Error handling
1284
+ /**
1285
+ * Create a pointer to an existing ctypes object.
1286
+ *
1287
+ * Python equivalent: `ctypes.pointer`
1288
+ *
1289
+ * @param obj - A SimpleCData instance or object with `_buffer`
1290
+ * @returns A pointer with `.contents` property and index access
1291
+ *
1292
+ * @example
1293
+ * ```javascript
1294
+ * const x = new c_int32(42);
1295
+ * const p = pointer(x);
1296
+ * console.log(p.contents); // 42
1297
+ * p.contents = 100;
1298
+ * console.log(x.value); // 100
1299
+ * ```
1300
+ *
1301
+ * @category Pointers
1302
+ */
1303
+ export function pointer(obj: SimpleCDataInstance | { _buffer: Buffer }): PointerInstance;
1304
+
1305
+ /**
1306
+ * Get the C library errno value.
1307
+ *
1308
+ * Python equivalent: `ctypes.get_errno()`
1309
+ *
1310
+ * @category Error Handling
1311
+ */
346
1312
  export function get_errno(): number;
1313
+
1314
+ /**
1315
+ * Set the C library errno value.
1316
+ *
1317
+ * Python equivalent: `ctypes.set_errno()`
1318
+ *
1319
+ * @category Error Handling
1320
+ */
347
1321
  export function set_errno(value: number): void;
1322
+
1323
+ /**
1324
+ * Get the last Windows error code (Windows only).
1325
+ *
1326
+ * Python equivalent: `ctypes.GetLastError()`
1327
+ *
1328
+ * @category Error Handling
1329
+ */
348
1330
  export function GetLastError(): number;
1331
+
1332
+ /**
1333
+ * Set the last Windows error code (Windows only).
1334
+ *
1335
+ * @category Error Handling
1336
+ */
349
1337
  export function SetLastError(code: number): void;
1338
+
1339
+ /**
1340
+ * Format a Windows error code into a human-readable message (Windows only).
1341
+ *
1342
+ * Python equivalent: `ctypes.FormatError()`
1343
+ *
1344
+ * @param code - Error code (defaults to `GetLastError()`)
1345
+ *
1346
+ * @category Error Handling
1347
+ */
350
1348
  export function FormatError(code?: number): string;
1349
+
1350
+ /**
1351
+ * Create an Error object from a Windows error code (Windows only).
1352
+ *
1353
+ * Python equivalent: `ctypes.WinError()`
1354
+ *
1355
+ * @param code - Error code (defaults to `GetLastError()`)
1356
+ * @returns Error with `.winerror` property
1357
+ *
1358
+ * @category Error Handling
1359
+ */
351
1360
  export function WinError(code?: number): Error & { winerror: number };
352
1361
 
353
1362
  // =============================================================================
354
1363
  // SimpleCData - Base class for simple C data types
355
1364
  // =============================================================================
356
1365
 
357
- /** Base class for simple C data types (int, float, pointer, etc.) */
1366
+ /**
1367
+ * Constructor interface for simple C data types (`c_int`, `c_float`, etc.).
1368
+ *
1369
+ * Each type class has a static `_size` (byte size) and `_type` (CType identifier),
1370
+ * and instances hold a value in a buffer.
1371
+ *
1372
+ * @category Types
1373
+ */
358
1374
  export interface SimpleCDataConstructor {
359
1375
  new (value?: any): SimpleCDataInstance;
1376
+ /** Size of this type in bytes. */
360
1377
  readonly _size: number;
361
- readonly _type: number; // CType numeric value from native module
1378
+ /** CType numeric identifier. */
1379
+ readonly _type: number;
1380
+ /** @internal */
362
1381
  readonly _isSimpleCData: true;
1382
+ /** @internal */
363
1383
  _reader(buf: Buffer, offset: number): any;
1384
+ /** @internal */
364
1385
  _writer(buf: Buffer, offset: number, value: any): void;
365
1386
  }
366
1387
 
367
- /** Instance of SimpleCData */
1388
+ /**
1389
+ * Instance of a simple C data type.
1390
+ *
1391
+ * @example
1392
+ * ```javascript
1393
+ * const x = new c_int32(42);
1394
+ * console.log(x.value); // 42
1395
+ * console.log(x._buffer); // <Buffer 2a 00 00 00>
1396
+ * ```
1397
+ *
1398
+ * @category Types
1399
+ */
368
1400
  export interface SimpleCDataInstance {
1401
+ /** The current value. */
369
1402
  value: any;
1403
+ /** The underlying memory buffer. */
370
1404
  _buffer: Buffer;
371
1405
  toString(): string;
372
1406
  toJSON(): any;
@@ -377,49 +1411,97 @@ export interface SimpleCDataInstance {
377
1411
  // Type Aliases (Python-compatible)
378
1412
  // =============================================================================
379
1413
 
1414
+ /**
1415
+ * Void type — use as return type for functions that return nothing.
1416
+ *
1417
+ * Python equivalent: `None` (as restype)
1418
+ *
1419
+ * @category Types
1420
+ */
380
1421
  export const c_void: SimpleCDataConstructor;
1422
+
1423
+ /** 32-bit signed integer. Python: `ctypes.c_int` @category Types */
381
1424
  export const c_int: SimpleCDataConstructor;
1425
+ /** 32-bit unsigned integer. Python: `ctypes.c_uint` @category Types */
382
1426
  export const c_uint: SimpleCDataConstructor;
1427
+ /** 8-bit signed integer. Python: `ctypes.c_int8` @category Types */
383
1428
  export const c_int8: SimpleCDataConstructor;
1429
+ /** 8-bit unsigned integer. Python: `ctypes.c_uint8` @category Types */
384
1430
  export const c_uint8: SimpleCDataConstructor;
1431
+ /** 16-bit signed integer. Python: `ctypes.c_int16` @category Types */
385
1432
  export const c_int16: SimpleCDataConstructor;
1433
+ /** 16-bit unsigned integer. Python: `ctypes.c_uint16` @category Types */
386
1434
  export const c_uint16: SimpleCDataConstructor;
1435
+ /** 32-bit signed integer. Python: `ctypes.c_int32` @category Types */
387
1436
  export const c_int32: SimpleCDataConstructor;
1437
+ /** 32-bit unsigned integer. Python: `ctypes.c_uint32` @category Types */
388
1438
  export const c_uint32: SimpleCDataConstructor;
1439
+ /** 64-bit signed integer (BigInt). Python: `ctypes.c_int64` @category Types */
389
1440
  export const c_int64: SimpleCDataConstructor;
1441
+ /** 64-bit unsigned integer (BigInt). Python: `ctypes.c_uint64` @category Types */
390
1442
  export const c_uint64: SimpleCDataConstructor;
1443
+ /** 32-bit float. Python: `ctypes.c_float` @category Types */
391
1444
  export const c_float: SimpleCDataConstructor;
1445
+ /** 64-bit double. Python: `ctypes.c_double` @category Types */
392
1446
  export const c_double: SimpleCDataConstructor;
1447
+ /** 8-bit character. Python: `ctypes.c_char` @category Types */
393
1448
  export const c_char: SimpleCDataConstructor;
1449
+ /** Pointer to null-terminated string. Python: `ctypes.c_char_p` @category Types */
394
1450
  export const c_char_p: SimpleCDataConstructor;
1451
+ /** Wide character. Python: `ctypes.c_wchar` @category Types */
395
1452
  export const c_wchar: SimpleCDataConstructor;
1453
+ /** Pointer to null-terminated wide string. Python: `ctypes.c_wchar_p` @category Types */
396
1454
  export const c_wchar_p: SimpleCDataConstructor;
1455
+ /** Void pointer. Python: `ctypes.c_void_p` @category Types */
397
1456
  export const c_void_p: SimpleCDataConstructor;
1457
+ /** Boolean (1 byte). Python: `ctypes.c_bool` @category Types */
398
1458
  export const c_bool: SimpleCDataConstructor;
1459
+ /** Platform-dependent unsigned size type (BigInt). Python: `ctypes.c_size_t` @category Types */
399
1460
  export const c_size_t: SimpleCDataConstructor;
1461
+ /** Platform-dependent signed size type (BigInt). Python: `ctypes.c_ssize_t` @category Types */
1462
+ export const c_ssize_t: SimpleCDataConstructor;
1463
+ /** Platform-dependent signed long. Python: `ctypes.c_long` @category Types */
400
1464
  export const c_long: SimpleCDataConstructor;
1465
+ /** Platform-dependent unsigned long. Python: `ctypes.c_ulong` @category Types */
401
1466
  export const c_ulong: SimpleCDataConstructor;
402
1467
 
403
- // Python-compatible aliases
1468
+ /** Alias for `c_int8`. Python: `ctypes.c_byte` @category Types */
404
1469
  export const c_byte: SimpleCDataConstructor;
1470
+ /** Alias for `c_uint8`. Python: `ctypes.c_ubyte` @category Types */
405
1471
  export const c_ubyte: SimpleCDataConstructor;
1472
+ /** Alias for `c_int16`. Python: `ctypes.c_short` @category Types */
406
1473
  export const c_short: SimpleCDataConstructor;
1474
+ /** Alias for `c_uint16`. Python: `ctypes.c_ushort` @category Types */
407
1475
  export const c_ushort: SimpleCDataConstructor;
1476
+ /** Alias for `c_int64`. Python: `ctypes.c_longlong` @category Types */
408
1477
  export const c_longlong: SimpleCDataConstructor;
1478
+ /** Alias for `c_uint64`. Python: `ctypes.c_ulonglong` @category Types */
409
1479
  export const c_ulonglong: SimpleCDataConstructor;
410
1480
 
411
- /** SimpleCData base class for creating custom simple types */
1481
+ /**
1482
+ * Base class for creating custom simple C data types.
1483
+ * @category Types
1484
+ */
412
1485
  export const SimpleCData: SimpleCDataConstructor;
413
1486
 
414
1487
  // =============================================================================
415
1488
  // Constants
416
1489
  // =============================================================================
417
1490
 
418
- /** Size of a pointer in bytes (4 on 32-bit, 8 on 64-bit) */
1491
+ /**
1492
+ * Size of a pointer in bytes (4 on 32-bit, 8 on 64-bit).
1493
+ * @category Constants
1494
+ */
419
1495
  export const POINTER_SIZE: number;
420
1496
 
421
- /** Size of wchar_t in bytes (2 on Windows, 4 on Unix) */
1497
+ /**
1498
+ * Size of `wchar_t` in bytes (2 on Windows, 4 on Unix).
1499
+ * @category Constants
1500
+ */
422
1501
  export const WCHAR_SIZE: number;
423
1502
 
424
- /** Null pointer constant */
1503
+ /**
1504
+ * Null pointer constant.
1505
+ * @category Constants
1506
+ */
425
1507
  export const NULL: null;