node-ctypes 1.5.0 → 1.6.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/README.md +139 -779
- package/build/Release/ctypes-darwin-arm64.node +0 -0
- package/build/Release/ctypes-darwin-x64.node +0 -0
- package/build/Release/ctypes-linux-arm64.node +0 -0
- package/build/Release/ctypes-linux-x64.node +0 -0
- package/build/Release/ctypes-win32-arm64.node +0 -0
- package/build/Release/ctypes-win32-x64.node +0 -0
- package/lib/index.d.ts +940 -52
- package/package.json +7 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* node-ctypes - Python ctypes-like FFI for Node.js
|
|
3
|
-
*
|
|
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
|
-
*
|
|
14
|
-
*
|
|
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
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* CType enum with numeric type identifiers.
|
|
99
|
+
* @category Types
|
|
100
|
+
*/
|
|
44
101
|
export const CType: CTypeValues;
|
|
45
102
|
|
|
46
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
|
|
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
|
-
/**
|
|
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
|
-
|
|
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
|
-
/**
|
|
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
|
-
|
|
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
|
-
/**
|
|
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,27 +328,49 @@ export class WinDLL extends CDLL {
|
|
|
117
328
|
// Callbacks
|
|
118
329
|
// =============================================================================
|
|
119
330
|
|
|
120
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
376
|
type: AnyType | StructDef | ArrayTypeDef | BitFieldDef;
|
|
@@ -153,22 +386,39 @@ export interface FieldDef {
|
|
|
153
386
|
baseSize?: number;
|
|
154
387
|
}
|
|
155
388
|
|
|
156
|
-
/**
|
|
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
|
-
|
|
421
|
+
/** @category Structures */
|
|
172
422
|
export type FieldSpec = AnyType | StructDef | ArrayTypeDef | BitFieldDef | AnonymousField;
|
|
173
423
|
|
|
174
424
|
type JsFromCType<T> = T extends "int64" | "uint64" | "size_t"
|
|
@@ -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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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,15 +544,33 @@ export interface AnonymousField {
|
|
|
240
544
|
// Pointers
|
|
241
545
|
// =============================================================================
|
|
242
546
|
|
|
243
|
-
/**
|
|
547
|
+
/**
|
|
548
|
+
* A pointer type created by {@link POINTER}.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```javascript
|
|
552
|
+
* const IntPtr = POINTER(c_int32);
|
|
553
|
+
* const p = IntPtr.fromBuffer(buf);
|
|
554
|
+
* console.log(p.contents); // dereferenced value
|
|
555
|
+
* ```
|
|
556
|
+
*
|
|
557
|
+
* @category Pointers
|
|
558
|
+
*/
|
|
244
559
|
export interface PointerTypeDef {
|
|
560
|
+
/** The type this pointer points to. */
|
|
245
561
|
readonly _pointerTo: AnyType | StructDef;
|
|
562
|
+
/** Size of the pointed-to type. */
|
|
246
563
|
readonly _baseSize: number;
|
|
564
|
+
/** Pointer size (always 8 on 64-bit). */
|
|
247
565
|
readonly size: number;
|
|
248
566
|
|
|
567
|
+
/** Create a NULL pointer. */
|
|
249
568
|
create(): Buffer;
|
|
569
|
+
/** Create a pointer to an existing buffer. */
|
|
250
570
|
fromBuffer(targetBuf: Buffer): Buffer;
|
|
571
|
+
/** Dereference the pointer, returning the target address or null. */
|
|
251
572
|
deref(ptrBuf: Buffer): bigint | null;
|
|
573
|
+
/** Update the pointer target. */
|
|
252
574
|
set(ptrBuf: Buffer, value: Buffer | bigint): void;
|
|
253
575
|
toString(): string;
|
|
254
576
|
}
|
|
@@ -257,7 +579,10 @@ export interface PointerTypeDef {
|
|
|
257
579
|
// Native Struct/Array Types
|
|
258
580
|
// =============================================================================
|
|
259
581
|
|
|
260
|
-
/**
|
|
582
|
+
/**
|
|
583
|
+
* Low-level native struct type. Prefer {@link Structure} for most use cases.
|
|
584
|
+
* @category Structures
|
|
585
|
+
*/
|
|
261
586
|
export class StructType {
|
|
262
587
|
constructor(isUnion?: boolean);
|
|
263
588
|
addField(name: string, type: AnyType | StructType | ArrayType): this;
|
|
@@ -267,7 +592,10 @@ export class StructType {
|
|
|
267
592
|
read(buffer: Buffer): Record<string, any>;
|
|
268
593
|
}
|
|
269
594
|
|
|
270
|
-
/**
|
|
595
|
+
/**
|
|
596
|
+
* Low-level native array type. Prefer {@link array} for most use cases.
|
|
597
|
+
* @category Structures
|
|
598
|
+
*/
|
|
271
599
|
export class ArrayType {
|
|
272
600
|
constructor(elementType: AnyType, count: number);
|
|
273
601
|
getSize(): number;
|
|
@@ -277,27 +605,109 @@ export class ArrayType {
|
|
|
277
605
|
}
|
|
278
606
|
|
|
279
607
|
/**
|
|
280
|
-
* Python-like
|
|
281
|
-
*
|
|
282
|
-
*
|
|
608
|
+
* Base class for Python-like struct definitions.
|
|
609
|
+
*
|
|
610
|
+
* Subclasses define fields via `static _fields_`. Field access is transparent —
|
|
611
|
+
* `point.x` reads directly from the underlying buffer.
|
|
612
|
+
*
|
|
613
|
+
* Python equivalent: `ctypes.Structure`
|
|
614
|
+
*
|
|
615
|
+
* @example
|
|
616
|
+
* ```javascript
|
|
617
|
+
* import { Structure, c_int } from 'node-ctypes';
|
|
618
|
+
*
|
|
619
|
+
* class Point extends Structure {
|
|
620
|
+
* static _fields_ = [["x", c_int], ["y", c_int]];
|
|
621
|
+
* }
|
|
622
|
+
*
|
|
623
|
+
* const p = new Point(10, 20);
|
|
624
|
+
* console.log(p.x, p.y); // 10 20
|
|
625
|
+
*
|
|
626
|
+
* // Nested structs
|
|
627
|
+
* class Rect extends Structure {
|
|
628
|
+
* static _fields_ = [["topLeft", Point], ["bottomRight", Point]];
|
|
629
|
+
* }
|
|
630
|
+
* ```
|
|
631
|
+
*
|
|
632
|
+
* @category Structures
|
|
283
633
|
*/
|
|
284
634
|
export class Structure<F extends Record<string, FieldSpec> = Record<string, any>> {
|
|
635
|
+
/**
|
|
636
|
+
* Create a new struct instance.
|
|
637
|
+
* @param args - Positional values matching field order, or a single object with field names as keys
|
|
638
|
+
*/
|
|
285
639
|
constructor(...args: any[]);
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Field definitions. Array of `[name, type]` tuples, or `[name, type, bits]` for bit fields.
|
|
643
|
+
*
|
|
644
|
+
* @example
|
|
645
|
+
* ```javascript
|
|
646
|
+
* static _fields_ = [
|
|
647
|
+
* ["x", c_int],
|
|
648
|
+
* ["y", c_int],
|
|
649
|
+
* ["flags", c_uint32, 3], // 3-bit bit field
|
|
650
|
+
* ];
|
|
651
|
+
* ```
|
|
652
|
+
*/
|
|
286
653
|
static _fields_?: Array<[string, FieldSpec]> | Record<string, FieldSpec>;
|
|
654
|
+
|
|
655
|
+
/** If `true`, disables padding between fields. */
|
|
287
656
|
static _pack_?: boolean;
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* List of field names whose members are promoted to the parent struct.
|
|
660
|
+
*
|
|
661
|
+
* Python equivalent: `_anonymous_`
|
|
662
|
+
*/
|
|
288
663
|
static _anonymous_?: string[];
|
|
664
|
+
|
|
665
|
+
/** Create instance with optional initial values. */
|
|
289
666
|
static create<ThisT extends Structure<F>>(this: new (...args: any[]) => ThisT, values?: Partial<FieldsToInstance<F>> | Buffer): ThisT;
|
|
667
|
+
|
|
668
|
+
/** Convert a buffer or instance to a plain object. */
|
|
290
669
|
static toObject<ThisT extends Structure<F>>(this: new (...args: any[]) => ThisT, bufOrInstance: Buffer | Structure<F>): FieldsToInstance<F>;
|
|
670
|
+
|
|
671
|
+
/** The underlying memory buffer. Passed automatically to native functions. */
|
|
291
672
|
_buffer: Buffer;
|
|
673
|
+
|
|
674
|
+
/** The struct definition metadata. */
|
|
292
675
|
_structDef: StructDef;
|
|
676
|
+
|
|
677
|
+
/** Read a field value by name. */
|
|
293
678
|
get<K extends keyof F & string>(fieldName: K): FieldsToInstance<F>[K];
|
|
679
|
+
|
|
680
|
+
/** Write a field value by name. */
|
|
294
681
|
set<K extends keyof F & string>(fieldName: K, value: FieldsToInstance<F>[K]): void;
|
|
682
|
+
|
|
683
|
+
/** Convert all fields to a plain JavaScript object (snapshot). */
|
|
295
684
|
toObject(): FieldsToInstance<F>;
|
|
296
|
-
|
|
685
|
+
|
|
686
|
+
/** Dynamic field access via Proxy. */
|
|
687
|
+
[field: string]: any;
|
|
297
688
|
}
|
|
298
689
|
|
|
299
690
|
/**
|
|
300
|
-
* Python-like
|
|
691
|
+
* Base class for Python-like union definitions.
|
|
692
|
+
*
|
|
693
|
+
* All fields share the same memory. The size equals the largest field.
|
|
694
|
+
*
|
|
695
|
+
* Python equivalent: `ctypes.Union`
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
698
|
+
* ```javascript
|
|
699
|
+
* import { Union, c_int, c_float } from 'node-ctypes';
|
|
700
|
+
*
|
|
701
|
+
* class IntOrFloat extends Union {
|
|
702
|
+
* static _fields_ = [["i", c_int], ["f", c_float]];
|
|
703
|
+
* }
|
|
704
|
+
*
|
|
705
|
+
* const u = new IntOrFloat();
|
|
706
|
+
* u.f = 3.14;
|
|
707
|
+
* console.log(u.i); // bit pattern of 3.14 as integer
|
|
708
|
+
* ```
|
|
709
|
+
*
|
|
710
|
+
* @category Structures
|
|
301
711
|
*/
|
|
302
712
|
export class Union<F extends Record<string, FieldSpec> = Record<string, any>> extends Structure<F> {}
|
|
303
713
|
|
|
@@ -305,68 +715,500 @@ export class Union<F extends Record<string, FieldSpec> = Record<string, any>> ex
|
|
|
305
715
|
// Functions
|
|
306
716
|
// =============================================================================
|
|
307
717
|
|
|
308
|
-
|
|
718
|
+
/**
|
|
719
|
+
* Load a shared library at the lowest level.
|
|
720
|
+
*
|
|
721
|
+
* For most use cases, prefer {@link CDLL} or {@link WinDLL}.
|
|
722
|
+
*
|
|
723
|
+
* @param path - Library path, or `null` for the current process
|
|
724
|
+
* @returns A {@link Library} handle
|
|
725
|
+
*
|
|
726
|
+
* @category Library Loading
|
|
727
|
+
*/
|
|
309
728
|
export function load(path: string | null): Library;
|
|
310
729
|
|
|
311
|
-
|
|
730
|
+
/**
|
|
731
|
+
* Create a callback that can be passed to native functions.
|
|
732
|
+
*
|
|
733
|
+
* The callback runs on the main thread. For thread-safe callbacks,
|
|
734
|
+
* use {@link threadSafeCallback}.
|
|
735
|
+
*
|
|
736
|
+
* **Important:** Call `.release()` when the callback is no longer needed.
|
|
737
|
+
*
|
|
738
|
+
* Python equivalent: `ctypes.CFUNCTYPE(...)(func)`
|
|
739
|
+
*
|
|
740
|
+
* @param fn - JavaScript function to wrap
|
|
741
|
+
* @param returnType - Return type of the callback
|
|
742
|
+
* @param argTypes - Argument types
|
|
743
|
+
* @returns A wrapper with a `.pointer` property and `.release()` method
|
|
744
|
+
*
|
|
745
|
+
* @example
|
|
746
|
+
* ```javascript
|
|
747
|
+
* import { callback, c_int32, c_void_p, readValue } from 'node-ctypes';
|
|
748
|
+
*
|
|
749
|
+
* const compare = callback(
|
|
750
|
+
* (a, b) => readValue(a, c_int32) - readValue(b, c_int32),
|
|
751
|
+
* c_int32, [c_void_p, c_void_p]
|
|
752
|
+
* );
|
|
753
|
+
* // Pass compare.pointer to qsort, etc.
|
|
754
|
+
* compare.release();
|
|
755
|
+
* ```
|
|
756
|
+
*
|
|
757
|
+
* @category Callbacks
|
|
758
|
+
*/
|
|
312
759
|
export function callback(fn: Function, returnType: AnyType, argTypes?: AnyType[]): CallbackWrapper;
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* Create a thread-safe callback that can be invoked from any thread.
|
|
763
|
+
*
|
|
764
|
+
* **Important:** Call `.release()` when the callback is no longer needed.
|
|
765
|
+
*
|
|
766
|
+
* @param fn - JavaScript function to wrap
|
|
767
|
+
* @param returnType - Return type of the callback
|
|
768
|
+
* @param argTypes - Argument types
|
|
769
|
+
*
|
|
770
|
+
* @category Callbacks
|
|
771
|
+
*/
|
|
313
772
|
export function threadSafeCallback(fn: Function, returnType: AnyType, argTypes?: AnyType[]): CallbackWrapper;
|
|
314
773
|
|
|
315
|
-
|
|
774
|
+
/**
|
|
775
|
+
* Create a function pointer type with cdecl calling convention.
|
|
776
|
+
*
|
|
777
|
+
* Returns a factory that takes a BigInt address and returns a callable function.
|
|
778
|
+
* Used for COM vtable dispatch and other scenarios with runtime function pointers.
|
|
779
|
+
*
|
|
780
|
+
* Python equivalent: `ctypes.CFUNCTYPE`
|
|
781
|
+
*
|
|
782
|
+
* @param restype - Return type
|
|
783
|
+
* @param argtypes - Argument types
|
|
784
|
+
* @returns A factory function: `(address: bigint) => callable`
|
|
785
|
+
*
|
|
786
|
+
* @example
|
|
787
|
+
* ```javascript
|
|
788
|
+
* import { CDLL, CFUNCTYPE, c_size_t, c_char_p } from 'node-ctypes';
|
|
789
|
+
*
|
|
790
|
+
* const libc = new CDLL('msvcrt.dll');
|
|
791
|
+
* const addr = libc.symbol('strlen');
|
|
792
|
+
* const StrlenType = CFUNCTYPE(c_size_t, c_char_p);
|
|
793
|
+
* const strlen = StrlenType(addr);
|
|
794
|
+
* console.log(strlen('Hello')); // 5n
|
|
795
|
+
* ```
|
|
796
|
+
*
|
|
797
|
+
* @category Callbacks
|
|
798
|
+
*/
|
|
799
|
+
export function CFUNCTYPE(restype: AnyType, ...argtypes: AnyType[]): (address: bigint) => (...args: any[]) => any;
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Create a function pointer type with stdcall calling convention (Windows).
|
|
803
|
+
*
|
|
804
|
+
* Same as {@link CFUNCTYPE} but uses stdcall. Used for Windows API function pointers.
|
|
805
|
+
*
|
|
806
|
+
* Python equivalent: `ctypes.WINFUNCTYPE`
|
|
807
|
+
*
|
|
808
|
+
* @param restype - Return type
|
|
809
|
+
* @param argtypes - Argument types
|
|
810
|
+
* @returns A factory function: `(address: bigint) => callable`
|
|
811
|
+
*
|
|
812
|
+
* @category Callbacks
|
|
813
|
+
*/
|
|
814
|
+
export function WINFUNCTYPE(restype: AnyType, ...argtypes: AnyType[]): (address: bigint) => (...args: any[]) => any;
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Create a C string buffer (null-terminated).
|
|
818
|
+
*
|
|
819
|
+
* Python equivalent: `ctypes.create_string_buffer`
|
|
820
|
+
*
|
|
821
|
+
* @param init - Size in bytes, a string to copy, or an existing Buffer
|
|
822
|
+
* @returns A Buffer containing the null-terminated string
|
|
823
|
+
*
|
|
824
|
+
* @example
|
|
825
|
+
* ```javascript
|
|
826
|
+
* const buf = create_string_buffer('Hello');
|
|
827
|
+
* console.log(string_at(buf)); // "Hello"
|
|
828
|
+
*
|
|
829
|
+
* const buf2 = create_string_buffer(256); // 256-byte zero-filled buffer
|
|
830
|
+
* ```
|
|
831
|
+
*
|
|
832
|
+
* @category Memory
|
|
833
|
+
*/
|
|
316
834
|
export function create_string_buffer(init: number | string | Buffer): Buffer;
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* Create a wide string buffer (wchar_t, null-terminated).
|
|
838
|
+
*
|
|
839
|
+
* On Windows, this is UTF-16LE. On Unix, UTF-32.
|
|
840
|
+
*
|
|
841
|
+
* Python equivalent: `ctypes.create_unicode_buffer`
|
|
842
|
+
*
|
|
843
|
+
* @param init - Size in characters, or a string to copy
|
|
844
|
+
*
|
|
845
|
+
* @category Memory
|
|
846
|
+
*/
|
|
317
847
|
export function create_unicode_buffer(init: number | string): Buffer;
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Read a C string from an address or buffer.
|
|
851
|
+
*
|
|
852
|
+
* Python equivalent: `ctypes.string_at`
|
|
853
|
+
*
|
|
854
|
+
* @param address - Buffer, BigInt address, or numeric address
|
|
855
|
+
* @param size - Maximum bytes to read (optional, reads until null terminator)
|
|
856
|
+
*
|
|
857
|
+
* @category Memory
|
|
858
|
+
*/
|
|
318
859
|
export function string_at(address: Buffer | bigint | number, size?: number): string | null;
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* Read a wide string from an address or buffer.
|
|
863
|
+
*
|
|
864
|
+
* Python equivalent: `ctypes.wstring_at`
|
|
865
|
+
*
|
|
866
|
+
* @param address - Buffer, BigInt address, or numeric address
|
|
867
|
+
* @param size - Maximum characters to read
|
|
868
|
+
*
|
|
869
|
+
* @category Memory
|
|
870
|
+
*/
|
|
319
871
|
export function wstring_at(address: Buffer | bigint | number, size?: number): string | null;
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Get the address of a buffer as BigInt.
|
|
875
|
+
*
|
|
876
|
+
* Python equivalent: `ctypes.addressof`
|
|
877
|
+
*
|
|
878
|
+
* @category Memory
|
|
879
|
+
*/
|
|
320
880
|
export function addressof(ptr: Buffer | bigint | number): bigint;
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Copy memory from source to destination.
|
|
884
|
+
*
|
|
885
|
+
* Python equivalent: `ctypes.memmove`
|
|
886
|
+
*
|
|
887
|
+
* @category Memory
|
|
888
|
+
*/
|
|
321
889
|
export function memmove(dst: Buffer, src: Buffer | bigint, count: number): void;
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Fill memory with a byte value.
|
|
893
|
+
*
|
|
894
|
+
* Python equivalent: `ctypes.memset`
|
|
895
|
+
*
|
|
896
|
+
* @category Memory
|
|
897
|
+
*/
|
|
322
898
|
export function memset(dst: Buffer, value: number, count: number): void;
|
|
323
899
|
|
|
324
|
-
|
|
900
|
+
/**
|
|
901
|
+
* Read a typed value from a buffer at a given offset.
|
|
902
|
+
*
|
|
903
|
+
* @param ptr - Buffer to read from
|
|
904
|
+
* @param type - Type to read (e.g. `c_int32`, `c_double`)
|
|
905
|
+
* @param offset - Byte offset (default 0)
|
|
906
|
+
*
|
|
907
|
+
* @example
|
|
908
|
+
* ```javascript
|
|
909
|
+
* const buf = Buffer.alloc(8);
|
|
910
|
+
* buf.writeInt32LE(42, 0);
|
|
911
|
+
* console.log(readValue(buf, c_int32)); // 42
|
|
912
|
+
* ```
|
|
913
|
+
*
|
|
914
|
+
* @category Memory
|
|
915
|
+
*/
|
|
325
916
|
export function readValue(ptr: Buffer | bigint | number, type: AnyType, offset?: number): any;
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Write a typed value to a buffer at a given offset.
|
|
920
|
+
*
|
|
921
|
+
* @param ptr - Buffer to write to
|
|
922
|
+
* @param type - Type to write (e.g. `c_int32`, `c_double`)
|
|
923
|
+
* @param value - Value to write
|
|
924
|
+
* @param offset - Byte offset (default 0)
|
|
925
|
+
*
|
|
926
|
+
* @category Memory
|
|
927
|
+
*/
|
|
326
928
|
export function writeValue(ptr: Buffer | bigint | number, type: AnyType, value: any, offset?: number): number | void;
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* Get the size of a type in bytes.
|
|
932
|
+
*
|
|
933
|
+
* Python equivalent: `ctypes.sizeof`
|
|
934
|
+
*
|
|
935
|
+
* @example
|
|
936
|
+
* ```javascript
|
|
937
|
+
* sizeof(c_int32) // 4
|
|
938
|
+
* sizeof(c_double) // 8
|
|
939
|
+
* sizeof(c_void_p) // 8 (on 64-bit)
|
|
940
|
+
* ```
|
|
941
|
+
*
|
|
942
|
+
* @category Memory
|
|
943
|
+
*/
|
|
327
944
|
export function sizeof(type: AnyType | StructDef | ArrayTypeDef): number;
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Create a Buffer view of native memory at a given address.
|
|
948
|
+
*
|
|
949
|
+
* **Warning:** Use with caution — the buffer is not bounds-checked.
|
|
950
|
+
*
|
|
951
|
+
* @param address - Native memory address
|
|
952
|
+
* @param size - Number of bytes
|
|
953
|
+
*
|
|
954
|
+
* @category Memory
|
|
955
|
+
*/
|
|
328
956
|
export function ptrToBuffer(address: bigint | number, size: number): Buffer;
|
|
329
957
|
|
|
330
|
-
|
|
958
|
+
/**
|
|
959
|
+
* Define a struct using the functional API.
|
|
960
|
+
*
|
|
961
|
+
* For Python-like class syntax, use {@link Structure} instead.
|
|
962
|
+
*
|
|
963
|
+
* @param fields - Object mapping field names to types
|
|
964
|
+
* @param options - Struct options (e.g. `{ packed: true }`)
|
|
965
|
+
*
|
|
966
|
+
* @example
|
|
967
|
+
* ```javascript
|
|
968
|
+
* const PointDef = struct({ x: c_int32, y: c_int32 });
|
|
969
|
+
* const buf = PointDef.create({ x: 10, y: 20 });
|
|
970
|
+
* console.log(PointDef.get(buf, 'x')); // 10
|
|
971
|
+
* ```
|
|
972
|
+
*
|
|
973
|
+
* @category Structures
|
|
974
|
+
*/
|
|
331
975
|
export function struct(fields: Record<string, FieldSpec>, options?: StructOptions): StructDef;
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* Define a union using the functional API.
|
|
979
|
+
*
|
|
980
|
+
* For Python-like class syntax, use {@link Union} instead.
|
|
981
|
+
*
|
|
982
|
+
* @param fields - Object mapping field names to types
|
|
983
|
+
*
|
|
984
|
+
* @category Structures
|
|
985
|
+
*/
|
|
332
986
|
export function union(fields: Record<string, FieldSpec>): UnionDef;
|
|
333
|
-
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* Factory that returns a Structure subclass with typed fields.
|
|
990
|
+
*
|
|
991
|
+
* @param fields - Object mapping field names to types
|
|
992
|
+
* @param options - Struct options
|
|
993
|
+
*
|
|
994
|
+
* @category Structures
|
|
995
|
+
*/
|
|
334
996
|
export function defineStruct<F extends Record<string, FieldSpec>>(fields: F, options?: StructOptions): new (...args: any[]) => Structure<F>;
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Factory that returns a Union subclass with typed fields.
|
|
1000
|
+
*
|
|
1001
|
+
* @param fields - Object mapping field names to types
|
|
1002
|
+
*
|
|
1003
|
+
* @category Structures
|
|
1004
|
+
*/
|
|
335
1005
|
export function defineUnion<F extends Record<string, FieldSpec>>(fields: F): new (...args: any[]) => Union<F>;
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Define a fixed-size array type.
|
|
1009
|
+
*
|
|
1010
|
+
* Python equivalent: `c_int * 5`
|
|
1011
|
+
*
|
|
1012
|
+
* @param elementType - Type of each element
|
|
1013
|
+
* @param count - Number of elements
|
|
1014
|
+
*
|
|
1015
|
+
* @example
|
|
1016
|
+
* ```javascript
|
|
1017
|
+
* const IntArray5 = array(c_int32, 5);
|
|
1018
|
+
* const arr = IntArray5.create([1, 2, 3, 4, 5]);
|
|
1019
|
+
* console.log(arr[0]); // 1
|
|
1020
|
+
* ```
|
|
1021
|
+
*
|
|
1022
|
+
* @category Structures
|
|
1023
|
+
*/
|
|
336
1024
|
export function array(elementType: AnyType | typeof Structure | typeof Union, count: number): ArrayTypeDef;
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* Define a bit field for use in struct `_fields_`.
|
|
1028
|
+
*
|
|
1029
|
+
* @param baseType - The underlying integer type (e.g. `c_uint32`)
|
|
1030
|
+
* @param bits - Number of bits
|
|
1031
|
+
*
|
|
1032
|
+
* @example
|
|
1033
|
+
* ```javascript
|
|
1034
|
+
* class Flags extends Structure {
|
|
1035
|
+
* static _fields_ = [
|
|
1036
|
+
* ["enabled", bitfield(c_uint32, 1)],
|
|
1037
|
+
* ["mode", bitfield(c_uint32, 3)],
|
|
1038
|
+
* ];
|
|
1039
|
+
* }
|
|
1040
|
+
* ```
|
|
1041
|
+
*
|
|
1042
|
+
* @category Structures
|
|
1043
|
+
*/
|
|
337
1044
|
export function bitfield(baseType: AnyType, bits: number): BitFieldDef;
|
|
338
1045
|
|
|
339
|
-
|
|
1046
|
+
/**
|
|
1047
|
+
* Pass an object by reference (returns its underlying buffer).
|
|
1048
|
+
*
|
|
1049
|
+
* Python equivalent: `ctypes.byref`
|
|
1050
|
+
*
|
|
1051
|
+
* @param obj - A Buffer, SimpleCData instance, or any object with a `_buffer` property
|
|
1052
|
+
*
|
|
1053
|
+
* @category Pointers
|
|
1054
|
+
*/
|
|
340
1055
|
export function byref(obj: Buffer | SimpleCDataInstance | { _buffer: Buffer }): Buffer;
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Interpret a pointer as a different type.
|
|
1059
|
+
*
|
|
1060
|
+
* Python equivalent: `ctypes.cast`
|
|
1061
|
+
*
|
|
1062
|
+
* @param ptr - Buffer or BigInt address to cast
|
|
1063
|
+
* @param targetType - The target type
|
|
1064
|
+
*
|
|
1065
|
+
* @category Pointers
|
|
1066
|
+
*/
|
|
341
1067
|
export function cast(ptr: Buffer | bigint, targetType: AnyType | StructDef): Buffer | { [key: string]: any };
|
|
1068
|
+
|
|
1069
|
+
/**
|
|
1070
|
+
* Create a pointer type for a given base type.
|
|
1071
|
+
*
|
|
1072
|
+
* Python equivalent: `ctypes.POINTER`
|
|
1073
|
+
*
|
|
1074
|
+
* @param baseType - The type to point to
|
|
1075
|
+
* @returns A pointer type with `create()`, `fromBuffer()`, etc.
|
|
1076
|
+
*
|
|
1077
|
+
* @example
|
|
1078
|
+
* ```javascript
|
|
1079
|
+
* const IntPtr = POINTER(c_int32);
|
|
1080
|
+
* const buf = Buffer.alloc(4);
|
|
1081
|
+
* buf.writeInt32LE(42, 0);
|
|
1082
|
+
*
|
|
1083
|
+
* const p = IntPtr.fromBuffer(buf);
|
|
1084
|
+
* console.log(p.contents); // 42
|
|
1085
|
+
* console.log(p[0]); // 42
|
|
1086
|
+
* ```
|
|
1087
|
+
*
|
|
1088
|
+
* @category Pointers
|
|
1089
|
+
*/
|
|
342
1090
|
export function POINTER(baseType: AnyType | StructDef): PointerTypeDef;
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* Create a pointer to an existing ctypes object.
|
|
1094
|
+
*
|
|
1095
|
+
* Python equivalent: `ctypes.pointer`
|
|
1096
|
+
*
|
|
1097
|
+
* @param obj - A SimpleCData instance or object with `_buffer`
|
|
1098
|
+
* @returns A pointer with `.contents` property and index access
|
|
1099
|
+
*
|
|
1100
|
+
* @example
|
|
1101
|
+
* ```javascript
|
|
1102
|
+
* const x = new c_int32(42);
|
|
1103
|
+
* const p = pointer(x);
|
|
1104
|
+
* console.log(p.contents); // 42
|
|
1105
|
+
* p.contents = 100;
|
|
1106
|
+
* console.log(x.value); // 100
|
|
1107
|
+
* ```
|
|
1108
|
+
*
|
|
1109
|
+
* @category Pointers
|
|
1110
|
+
*/
|
|
343
1111
|
export function pointer(obj: SimpleCDataInstance | { _buffer: Buffer }): SimpleCDataInstance & { contents: any; [index: number]: any };
|
|
344
1112
|
|
|
345
|
-
|
|
1113
|
+
/**
|
|
1114
|
+
* Get the C library errno value.
|
|
1115
|
+
*
|
|
1116
|
+
* Python equivalent: `ctypes.get_errno()`
|
|
1117
|
+
*
|
|
1118
|
+
* @category Error Handling
|
|
1119
|
+
*/
|
|
346
1120
|
export function get_errno(): number;
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* Set the C library errno value.
|
|
1124
|
+
*
|
|
1125
|
+
* Python equivalent: `ctypes.set_errno()`
|
|
1126
|
+
*
|
|
1127
|
+
* @category Error Handling
|
|
1128
|
+
*/
|
|
347
1129
|
export function set_errno(value: number): void;
|
|
1130
|
+
|
|
1131
|
+
/**
|
|
1132
|
+
* Get the last Windows error code (Windows only).
|
|
1133
|
+
*
|
|
1134
|
+
* Python equivalent: `ctypes.GetLastError()`
|
|
1135
|
+
*
|
|
1136
|
+
* @category Error Handling
|
|
1137
|
+
*/
|
|
348
1138
|
export function GetLastError(): number;
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* Set the last Windows error code (Windows only).
|
|
1142
|
+
*
|
|
1143
|
+
* @category Error Handling
|
|
1144
|
+
*/
|
|
349
1145
|
export function SetLastError(code: number): void;
|
|
1146
|
+
|
|
1147
|
+
/**
|
|
1148
|
+
* Format a Windows error code into a human-readable message (Windows only).
|
|
1149
|
+
*
|
|
1150
|
+
* Python equivalent: `ctypes.FormatError()`
|
|
1151
|
+
*
|
|
1152
|
+
* @param code - Error code (defaults to `GetLastError()`)
|
|
1153
|
+
*
|
|
1154
|
+
* @category Error Handling
|
|
1155
|
+
*/
|
|
350
1156
|
export function FormatError(code?: number): string;
|
|
1157
|
+
|
|
1158
|
+
/**
|
|
1159
|
+
* Create an Error object from a Windows error code (Windows only).
|
|
1160
|
+
*
|
|
1161
|
+
* Python equivalent: `ctypes.WinError()`
|
|
1162
|
+
*
|
|
1163
|
+
* @param code - Error code (defaults to `GetLastError()`)
|
|
1164
|
+
* @returns Error with `.winerror` property
|
|
1165
|
+
*
|
|
1166
|
+
* @category Error Handling
|
|
1167
|
+
*/
|
|
351
1168
|
export function WinError(code?: number): Error & { winerror: number };
|
|
352
1169
|
|
|
353
1170
|
// =============================================================================
|
|
354
1171
|
// SimpleCData - Base class for simple C data types
|
|
355
1172
|
// =============================================================================
|
|
356
1173
|
|
|
357
|
-
/**
|
|
1174
|
+
/**
|
|
1175
|
+
* Constructor interface for simple C data types (`c_int`, `c_float`, etc.).
|
|
1176
|
+
*
|
|
1177
|
+
* Each type class has a static `_size` (byte size) and `_type` (CType identifier),
|
|
1178
|
+
* and instances hold a value in a buffer.
|
|
1179
|
+
*
|
|
1180
|
+
* @category Types
|
|
1181
|
+
*/
|
|
358
1182
|
export interface SimpleCDataConstructor {
|
|
359
1183
|
new (value?: any): SimpleCDataInstance;
|
|
1184
|
+
/** Size of this type in bytes. */
|
|
360
1185
|
readonly _size: number;
|
|
361
|
-
|
|
1186
|
+
/** CType numeric identifier. */
|
|
1187
|
+
readonly _type: number;
|
|
1188
|
+
/** @internal */
|
|
362
1189
|
readonly _isSimpleCData: true;
|
|
1190
|
+
/** @internal */
|
|
363
1191
|
_reader(buf: Buffer, offset: number): any;
|
|
1192
|
+
/** @internal */
|
|
364
1193
|
_writer(buf: Buffer, offset: number, value: any): void;
|
|
365
1194
|
}
|
|
366
1195
|
|
|
367
|
-
/**
|
|
1196
|
+
/**
|
|
1197
|
+
* Instance of a simple C data type.
|
|
1198
|
+
*
|
|
1199
|
+
* @example
|
|
1200
|
+
* ```javascript
|
|
1201
|
+
* const x = new c_int32(42);
|
|
1202
|
+
* console.log(x.value); // 42
|
|
1203
|
+
* console.log(x._buffer); // <Buffer 2a 00 00 00>
|
|
1204
|
+
* ```
|
|
1205
|
+
*
|
|
1206
|
+
* @category Types
|
|
1207
|
+
*/
|
|
368
1208
|
export interface SimpleCDataInstance {
|
|
1209
|
+
/** The current value. */
|
|
369
1210
|
value: any;
|
|
1211
|
+
/** The underlying memory buffer. */
|
|
370
1212
|
_buffer: Buffer;
|
|
371
1213
|
toString(): string;
|
|
372
1214
|
toJSON(): any;
|
|
@@ -377,49 +1219,95 @@ export interface SimpleCDataInstance {
|
|
|
377
1219
|
// Type Aliases (Python-compatible)
|
|
378
1220
|
// =============================================================================
|
|
379
1221
|
|
|
1222
|
+
/**
|
|
1223
|
+
* Void type — use as return type for functions that return nothing.
|
|
1224
|
+
*
|
|
1225
|
+
* Python equivalent: `None` (as restype)
|
|
1226
|
+
*
|
|
1227
|
+
* @category Types
|
|
1228
|
+
*/
|
|
380
1229
|
export const c_void: SimpleCDataConstructor;
|
|
1230
|
+
|
|
1231
|
+
/** 32-bit signed integer. Python: `ctypes.c_int` @category Types */
|
|
381
1232
|
export const c_int: SimpleCDataConstructor;
|
|
1233
|
+
/** 32-bit unsigned integer. Python: `ctypes.c_uint` @category Types */
|
|
382
1234
|
export const c_uint: SimpleCDataConstructor;
|
|
1235
|
+
/** 8-bit signed integer. Python: `ctypes.c_int8` @category Types */
|
|
383
1236
|
export const c_int8: SimpleCDataConstructor;
|
|
1237
|
+
/** 8-bit unsigned integer. Python: `ctypes.c_uint8` @category Types */
|
|
384
1238
|
export const c_uint8: SimpleCDataConstructor;
|
|
1239
|
+
/** 16-bit signed integer. Python: `ctypes.c_int16` @category Types */
|
|
385
1240
|
export const c_int16: SimpleCDataConstructor;
|
|
1241
|
+
/** 16-bit unsigned integer. Python: `ctypes.c_uint16` @category Types */
|
|
386
1242
|
export const c_uint16: SimpleCDataConstructor;
|
|
1243
|
+
/** 32-bit signed integer. Python: `ctypes.c_int32` @category Types */
|
|
387
1244
|
export const c_int32: SimpleCDataConstructor;
|
|
1245
|
+
/** 32-bit unsigned integer. Python: `ctypes.c_uint32` @category Types */
|
|
388
1246
|
export const c_uint32: SimpleCDataConstructor;
|
|
1247
|
+
/** 64-bit signed integer (BigInt). Python: `ctypes.c_int64` @category Types */
|
|
389
1248
|
export const c_int64: SimpleCDataConstructor;
|
|
1249
|
+
/** 64-bit unsigned integer (BigInt). Python: `ctypes.c_uint64` @category Types */
|
|
390
1250
|
export const c_uint64: SimpleCDataConstructor;
|
|
1251
|
+
/** 32-bit float. Python: `ctypes.c_float` @category Types */
|
|
391
1252
|
export const c_float: SimpleCDataConstructor;
|
|
1253
|
+
/** 64-bit double. Python: `ctypes.c_double` @category Types */
|
|
392
1254
|
export const c_double: SimpleCDataConstructor;
|
|
1255
|
+
/** 8-bit character. Python: `ctypes.c_char` @category Types */
|
|
393
1256
|
export const c_char: SimpleCDataConstructor;
|
|
1257
|
+
/** Pointer to null-terminated string. Python: `ctypes.c_char_p` @category Types */
|
|
394
1258
|
export const c_char_p: SimpleCDataConstructor;
|
|
1259
|
+
/** Wide character. Python: `ctypes.c_wchar` @category Types */
|
|
395
1260
|
export const c_wchar: SimpleCDataConstructor;
|
|
1261
|
+
/** Pointer to null-terminated wide string. Python: `ctypes.c_wchar_p` @category Types */
|
|
396
1262
|
export const c_wchar_p: SimpleCDataConstructor;
|
|
1263
|
+
/** Void pointer. Python: `ctypes.c_void_p` @category Types */
|
|
397
1264
|
export const c_void_p: SimpleCDataConstructor;
|
|
1265
|
+
/** Boolean (1 byte). Python: `ctypes.c_bool` @category Types */
|
|
398
1266
|
export const c_bool: SimpleCDataConstructor;
|
|
1267
|
+
/** Platform-dependent size type (BigInt). Python: `ctypes.c_size_t` @category Types */
|
|
399
1268
|
export const c_size_t: SimpleCDataConstructor;
|
|
1269
|
+
/** Platform-dependent signed long. Python: `ctypes.c_long` @category Types */
|
|
400
1270
|
export const c_long: SimpleCDataConstructor;
|
|
1271
|
+
/** Platform-dependent unsigned long. Python: `ctypes.c_ulong` @category Types */
|
|
401
1272
|
export const c_ulong: SimpleCDataConstructor;
|
|
402
1273
|
|
|
403
|
-
|
|
1274
|
+
/** Alias for `c_int8`. Python: `ctypes.c_byte` @category Types */
|
|
404
1275
|
export const c_byte: SimpleCDataConstructor;
|
|
1276
|
+
/** Alias for `c_uint8`. Python: `ctypes.c_ubyte` @category Types */
|
|
405
1277
|
export const c_ubyte: SimpleCDataConstructor;
|
|
1278
|
+
/** Alias for `c_int16`. Python: `ctypes.c_short` @category Types */
|
|
406
1279
|
export const c_short: SimpleCDataConstructor;
|
|
1280
|
+
/** Alias for `c_uint16`. Python: `ctypes.c_ushort` @category Types */
|
|
407
1281
|
export const c_ushort: SimpleCDataConstructor;
|
|
1282
|
+
/** Alias for `c_int64`. Python: `ctypes.c_longlong` @category Types */
|
|
408
1283
|
export const c_longlong: SimpleCDataConstructor;
|
|
1284
|
+
/** Alias for `c_uint64`. Python: `ctypes.c_ulonglong` @category Types */
|
|
409
1285
|
export const c_ulonglong: SimpleCDataConstructor;
|
|
410
1286
|
|
|
411
|
-
/**
|
|
1287
|
+
/**
|
|
1288
|
+
* Base class for creating custom simple C data types.
|
|
1289
|
+
* @category Types
|
|
1290
|
+
*/
|
|
412
1291
|
export const SimpleCData: SimpleCDataConstructor;
|
|
413
1292
|
|
|
414
1293
|
// =============================================================================
|
|
415
1294
|
// Constants
|
|
416
1295
|
// =============================================================================
|
|
417
1296
|
|
|
418
|
-
/**
|
|
1297
|
+
/**
|
|
1298
|
+
* Size of a pointer in bytes (4 on 32-bit, 8 on 64-bit).
|
|
1299
|
+
* @category Constants
|
|
1300
|
+
*/
|
|
419
1301
|
export const POINTER_SIZE: number;
|
|
420
1302
|
|
|
421
|
-
/**
|
|
1303
|
+
/**
|
|
1304
|
+
* Size of `wchar_t` in bytes (2 on Windows, 4 on Unix).
|
|
1305
|
+
* @category Constants
|
|
1306
|
+
*/
|
|
422
1307
|
export const WCHAR_SIZE: number;
|
|
423
1308
|
|
|
424
|
-
/**
|
|
1309
|
+
/**
|
|
1310
|
+
* Null pointer constant.
|
|
1311
|
+
* @category Constants
|
|
1312
|
+
*/
|
|
425
1313
|
export const NULL: null;
|