node-ctypes 1.1.0 → 1.3.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.

Potentially problematic release.


This version of node-ctypes might be problematic. Click here for more details.

@@ -87,8 +87,8 @@ import { _toNativeType, _toNativeTypes } from "./types.js";
87
87
  *
88
88
  * @param {Function} fn - JavaScript function to wrap
89
89
  * The function will receive converted arguments based on argTypes
90
- * @param {Function|Object} returnType - Return type (SimpleCData class or CType)
91
- * @param {Array<Function|Object>} argTypes - Argument types array
90
+ * @param {Function|Object|number} returnType - Return type (SimpleCData class, CType, or CType value)
91
+ * @param {Array<Function|Object|number>} argTypes - Argument types array
92
92
  * @param {Object} native - Native module reference (internal use)
93
93
  * @returns {Object} Callback wrapper with properties:
94
94
  * - `pointer` (BigInt): Function pointer to pass to C
@@ -197,8 +197,8 @@ export function callback(fn, returnType, argTypes = [], native) {
197
197
  * @param {Function} fn - JavaScript function to wrap
198
198
  * The function will be executed on Node.js's main thread regardless of
199
199
  * which thread invokes the callback
200
- * @param {Function|Object} returnType - Return type (SimpleCData class or CType)
201
- * @param {Array<Function|Object>} argTypes - Argument types array
200
+ * @param {Function|Object|number} returnType - Return type (SimpleCData class, CType, or CType value)
201
+ * @param {Array<Function|Object|number>} argTypes - Argument types array
202
202
  * @param {Object} native - Native module reference (internal use)
203
203
  * @returns {Object} Callback wrapper with properties:
204
204
  * - `pointer` (BigInt): Function pointer to pass to C
package/lib/core/types.js CHANGED
@@ -11,14 +11,14 @@
11
11
  * @example
12
12
  * ```javascript
13
13
  * import { _toNativeType, _toNativeTypes } from './core/types.js';
14
- * import { c_int32, c_char_p } from './types/integers.js';
14
+ * import { c_int32, c_char_p, CType } from 'node-ctypes';
15
15
  *
16
- * // Convert single type
17
- * const nativeType = _toNativeType(c_int32); // Returns "int32"
16
+ * // Convert single type - returns numeric CType value
17
+ * const nativeType = _toNativeType(c_int32); // Returns CType.INT32 (5)
18
18
  *
19
19
  * // Convert array of types
20
20
  * const nativeTypes = _toNativeTypes([c_int32, c_char_p]);
21
- * // Returns ["int32", <native CType for c_char_p>]
21
+ * // Returns [CType.INT32, CType.STRING]
22
22
  * ```
23
23
  */
24
24
 
@@ -28,34 +28,36 @@
28
28
  *
29
29
  * **Type Conversion Rules**:
30
30
  * 1. **SimpleCData classes** (e.g., `c_int32`, `c_uint64`):
31
- * - Most types Convert to string type name (e.g., `"int32"`, `"uint64"`)
32
- * - `c_char_p` and `c_wchar_p` → Convert to native CType for automatic string marshalling
31
+ * - Returns the numeric CType value (e.g., `CType.INT32`, `CType.STRING`)
33
32
  *
34
33
  * 2. **Structure/Union classes**: Pass through unchanged for struct/union handling
35
34
  *
36
- * 3. **Native CType objects**: Pass through unchanged (already in native format)
35
+ * 3. **Numbers (CType values)**: Pass through unchanged (already in native format)
36
+ *
37
+ * 4. **Native CType objects**: Pass through unchanged (already in native format)
37
38
  *
38
39
  * **Python ctypes Compatibility**:
39
40
  * This function mimics Python's ctypes internal type normalization, ensuring that
40
41
  * type specifications work consistently whether passed as class constructors or
41
42
  * type objects.
42
43
  *
43
- * @param {Function|Object} type - Type specification to normalize
44
+ * @param {Function|Object|number} type - Type specification to normalize
44
45
  * - SimpleCData class (e.g., `c_int32`)
45
46
  * - Structure or Union class
47
+ * - CType numeric value
46
48
  * - Native CType object
47
- * @returns {string|Object} Normalized type for native FFI:
48
- * - String type name for most SimpleCData types
49
- * - Native CType object for string pointer types (c_char_p, c_wchar_p)
49
+ * @param {Object} native - Native module reference (unused but kept for API compatibility)
50
+ * @returns {number|Object} Normalized type for native FFI:
51
+ * - Numeric CType value for SimpleCData types
50
52
  * - Original value for Structure/Union/CType objects
51
53
  *
52
54
  * @example Basic type conversion
53
55
  * ```javascript
54
- * import { c_int32, c_float, c_char_p } from 'node-ctypes';
56
+ * import { c_int32, c_float, c_char_p, CType } from 'node-ctypes';
55
57
  *
56
- * _toNativeType(c_int32); // Returns "int32"
57
- * _toNativeType(c_float); // Returns "float"
58
- * _toNativeType(c_char_p); // Returns native.types.c_char_p (CType object)
58
+ * _toNativeType(c_int32); // Returns CType.INT32 (5)
59
+ * _toNativeType(c_float); // Returns CType.FLOAT (9)
60
+ * _toNativeType(c_char_p); // Returns CType.STRING (12)
59
61
  * ```
60
62
  *
61
63
  * @example Structure type pass-through
@@ -69,21 +71,30 @@
69
71
  * _toNativeType(Point); // Returns Point class unchanged
70
72
  * ```
71
73
  *
74
+ * @example Direct CType usage
75
+ * ```javascript
76
+ * import { CType } from 'node-ctypes';
77
+ *
78
+ * _toNativeType(CType.INT32); // Returns 5 (pass-through)
79
+ * ```
80
+ *
72
81
  * @private
73
82
  * @internal
74
83
  */
75
84
  export function _toNativeType(type, native) {
76
- // SimpleCData classes: convert to string type names or native CTypes
85
+ // SimpleCData classes: return the numeric CType value directly
77
86
  if (typeof type === "function" && type._isSimpleCData) {
78
- // String pointer types need native CType for automatic conversion
79
- // This enables automatic marshalling between C strings and JS strings
80
- if (type._type === "char_p") return native.types.c_char_p;
81
- if (type._type === "wchar_p") return native.types.c_wchar_p;
82
-
83
- // All other SimpleCData types: use string type name
87
+ // _type is now a numeric CType value from native.CType
84
88
  return type._type;
85
89
  }
86
90
 
91
+ // POINTER types: return CType.POINTER (11)
92
+ // POINTER(baseType) returns an object with _pointerTo property
93
+ if (type && typeof type === "object" && type._pointerTo !== undefined) {
94
+ // All pointer types are passed as generic pointers to FFI
95
+ return native.CType.POINTER;
96
+ }
97
+
87
98
  // Structure/Union classes: pass through unchanged
88
99
  // These are handled specially by the FFI layer
89
100
  if (typeof type === "function" && type.prototype) {
@@ -92,6 +103,11 @@ export function _toNativeType(type, native) {
92
103
  return type;
93
104
  }
94
105
 
106
+ // Numbers (direct CType values): pass through unchanged
107
+ if (typeof type === "number") {
108
+ return type;
109
+ }
110
+
95
111
  // Native CType objects: pass through unchanged (already in correct format)
96
112
  return type;
97
113
  }
@@ -108,16 +124,16 @@ export function _toNativeType(type, native) {
108
124
  *
109
125
  * @param {Array|*} types - Array of type specifications, or non-array value
110
126
  * @param {Object} native - Native module reference for CType lookups
111
- * @returns {Array|*} Array of normalized types, or original value if not an array
127
+ * @returns {Array|*} Array of normalized types (CType values), or original value if not an array
112
128
  *
113
129
  * @example Function signature normalization
114
130
  * ```javascript
115
- * import { c_int32, c_char_p, c_void_p } from 'node-ctypes';
131
+ * import { c_int32, c_char_p, c_void_p, CType } from 'node-ctypes';
116
132
  *
117
133
  * // Normalize function argument types
118
134
  * const argTypes = [c_int32, c_char_p, c_void_p];
119
135
  * const normalized = _toNativeTypes(argTypes);
120
- * // Returns ["int32", <native c_char_p CType>, "void_p"]
136
+ * // Returns [CType.INT32, CType.STRING, CType.POINTER]
121
137
  * ```
122
138
  *
123
139
  * @example Non-array pass-through
package/lib/index.d.ts CHANGED
@@ -9,14 +9,42 @@
9
9
  // Basic Types
10
10
  // =============================================================================
11
11
 
12
- /** Native CType object (internal C++ type representation) */
13
- export interface CType {
14
- readonly size: number;
15
- readonly name: string;
12
+ /**
13
+ * CType - Object containing numeric type identifiers
14
+ * Single source of truth for type values
15
+ */
16
+ export interface CTypeValues {
17
+ readonly VOID: number;
18
+ readonly INT8: number;
19
+ readonly UINT8: number;
20
+ readonly INT16: number;
21
+ readonly UINT16: number;
22
+ readonly INT32: number;
23
+ readonly UINT32: number;
24
+ readonly INT64: number;
25
+ readonly UINT64: number;
26
+ readonly FLOAT: number;
27
+ readonly DOUBLE: number;
28
+ readonly POINTER: number;
29
+ readonly STRING: number;
30
+ readonly WSTRING: number;
31
+ readonly WCHAR: number;
32
+ readonly BOOL: number;
33
+ readonly SIZE_T: number;
34
+ readonly SSIZE_T: number;
35
+ readonly LONG: number;
36
+ readonly ULONG: number;
37
+ readonly STRUCT: number;
38
+ readonly UNION: number;
39
+ readonly ARRAY: number;
40
+ readonly COUNT: number;
16
41
  }
17
42
 
18
- /** Any accepted type specification (CType object or SimpleCData class) */
19
- export type AnyType = CType | SimpleCDataConstructor;
43
+ /** CType object with numeric type identifiers */
44
+ export const CType: CTypeValues;
45
+
46
+ /** Any accepted type specification (SimpleCData class, CType value, or struct/union) */
47
+ export type AnyType = SimpleCDataConstructor | number | StructDef | UnionDef;
20
48
 
21
49
  // =============================================================================
22
50
  // Library & Function
@@ -64,6 +92,16 @@ export class CDLL {
64
92
  close(): void;
65
93
  readonly path: string;
66
94
  readonly loaded: boolean;
95
+ /** Python-like function access: lib.FuncName returns a FunctionWrapper */
96
+ [funcName: string]: FunctionWrapper | any;
97
+ }
98
+
99
+ /** FunctionWrapper for Python-like argtypes/restype syntax */
100
+ export interface FunctionWrapper {
101
+ (...args: any[]): any;
102
+ argtypes: AnyType[];
103
+ restype: AnyType;
104
+ errcheck: ErrcheckCallback | null;
67
105
  }
68
106
 
69
107
  /** WinDLL - stdcall calling convention library (Windows) */
@@ -298,6 +336,7 @@ export function bitfield(baseType: AnyType, bits: number): BitFieldDef;
298
336
  export function byref(obj: Buffer | SimpleCDataInstance | { _buffer: Buffer }): Buffer;
299
337
  export function cast(ptr: Buffer | bigint, targetType: AnyType | StructDef): Buffer | { [key: string]: any };
300
338
  export function POINTER(baseType: AnyType | StructDef): PointerTypeDef;
339
+ export function pointer(obj: SimpleCDataInstance | { _buffer: Buffer }): SimpleCDataInstance & { contents: any; [index: number]: any };
301
340
 
302
341
  // Error handling
303
342
  export function get_errno(): number;
@@ -315,7 +354,7 @@ export function WinError(code?: number): Error & { winerror: number };
315
354
  export interface SimpleCDataConstructor {
316
355
  new (value?: any): SimpleCDataInstance;
317
356
  readonly _size: number;
318
- readonly _type: string;
357
+ readonly _type: number; // CType numeric value from native module
319
358
  readonly _isSimpleCData: true;
320
359
  _reader(buf: Buffer, offset: number): any;
321
360
  _writer(buf: Buffer, offset: number, value: any): void;
@@ -357,6 +396,17 @@ export const c_size_t: SimpleCDataConstructor;
357
396
  export const c_long: SimpleCDataConstructor;
358
397
  export const c_ulong: SimpleCDataConstructor;
359
398
 
399
+ // Python-compatible aliases
400
+ export const c_byte: SimpleCDataConstructor;
401
+ export const c_ubyte: SimpleCDataConstructor;
402
+ export const c_short: SimpleCDataConstructor;
403
+ export const c_ushort: SimpleCDataConstructor;
404
+ export const c_longlong: SimpleCDataConstructor;
405
+ export const c_ulonglong: SimpleCDataConstructor;
406
+
407
+ /** SimpleCData base class for creating custom simple types */
408
+ export const SimpleCData: SimpleCDataConstructor;
409
+
360
410
  // =============================================================================
361
411
  // Constants
362
412
  // =============================================================================
package/lib/index.js CHANGED
@@ -35,39 +35,12 @@ import {
35
35
  memmove as _memmove,
36
36
  memset as _memset,
37
37
  } from "./memory/buffer.js";
38
- import {
39
- readValue as _readValue,
40
- writeValue as _writeValue,
41
- sizeof as _sizeof,
42
- } from "./memory/operations.js";
43
- import {
44
- addressOf as _addressOf,
45
- byref as _byref,
46
- cast as _cast,
47
- ptrToBuffer as _ptrToBuffer,
48
- POINTER as _POINTER,
49
- } from "./memory/pointer.js";
50
- import {
51
- get_errno as _get_errno,
52
- set_errno as _set_errno,
53
- GetLastError as _GetLastError,
54
- SetLastError as _SetLastError,
55
- FormatError as _FormatError,
56
- WinError as _WinError,
57
- } from "./platform/errors.js";
58
- import {
59
- bitfield as _bitfield,
60
- _isStruct,
61
- _isArrayType,
62
- _isBitField,
63
- } from "./structures/helpers/common.js";
38
+ import { readValue as _readValue, writeValue as _writeValue, sizeof as _sizeof } from "./memory/operations.js";
39
+ import { addressOf as _addressOf, byref as _byref, cast as _cast, ptrToBuffer as _ptrToBuffer, POINTER as _POINTER, pointer as _pointer } from "./memory/pointer.js";
40
+ import { get_errno as _get_errno, set_errno as _set_errno, GetLastError as _GetLastError, SetLastError as _SetLastError, FormatError as _FormatError, WinError as _WinError } from "./platform/errors.js";
41
+ import { bitfield as _bitfield, _isStruct, _isArrayType, _isBitField } from "./structures/helpers/common.js";
64
42
  import { array as _array } from "./structures/helpers/array.js";
65
- import {
66
- _readBitField as _readBitFieldHelper,
67
- _writeBitField as _writeBitFieldHelper,
68
- _readUintFromBuffer,
69
- _writeUintToBuffer,
70
- } from "./structures/helpers/bitfield.js";
43
+ import { _readBitField as _readBitFieldHelper, _writeBitField as _writeBitFieldHelper, _readUintFromBuffer, _writeUintToBuffer } from "./structures/helpers/bitfield.js";
71
44
  import { union as _union } from "./structures/helpers/union.js";
72
45
  import { struct as _struct } from "./structures/helpers/struct.js";
73
46
  import { createStructureClass } from "./structures/Structure.js";
@@ -140,7 +113,7 @@ const native = require(nativeModulePath);
140
113
  _initConstants(native);
141
114
 
142
115
  // Re-esporta le classi native
143
- const { Version, Library, FFIFunction, Callback, ThreadSafeCallback, CType, StructType, ArrayType } = native;
116
+ const { Version, Library, FFIFunction, Callback, ThreadSafeCallback, StructType, ArrayType, CType } = native;
144
117
 
145
118
  // ============================================================================
146
119
  // Type Normalization Helpers
@@ -163,13 +136,7 @@ function load(libPath) {
163
136
  * Wrapper per funzioni con sintassi Python-like ctypes (argtypes/restype)
164
137
  */
165
138
  // Create Library classes (FunctionWrapper, CDLL, WinDLL)
166
- const { FunctionWrapper, CDLL, WinDLL } = createLibraryClasses(
167
- Library,
168
- LRUCache,
169
- _toNativeType,
170
- _toNativeTypes,
171
- native
172
- );
139
+ const { FunctionWrapper, CDLL, WinDLL } = createLibraryClasses(Library, LRUCache, _toNativeType, _toNativeTypes, native);
173
140
 
174
141
  // ============================================================================
175
142
  // Callback Functions
@@ -260,6 +227,26 @@ function POINTER(baseType) {
260
227
  return _POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p, native);
261
228
  }
262
229
 
230
+ /**
231
+ * Creates a pointer to an existing ctypes object.
232
+ * Python ctypes compatible: pointer(obj) returns a pointer to obj.
233
+ *
234
+ * @param {Object} obj - Object to create pointer to (must have ._buffer or be a Buffer)
235
+ * @returns {Object} Pointer instance pointing to obj
236
+ *
237
+ * @example
238
+ * ```javascript
239
+ * const x = new c_int32(42);
240
+ * const p = pointer(x);
241
+ * console.log(p.contents); // 42
242
+ * p.contents = 100;
243
+ * console.log(x.value); // 100
244
+ * ```
245
+ */
246
+ function pointer(obj) {
247
+ return _pointer(obj, _POINTER, sizeof, alloc, readValue, writeValue, c_void_p, native);
248
+ }
249
+
263
250
  /**
264
251
  * Helper per definire union (tutti i campi condividono offset 0)
265
252
  * Supporta nested structs e bitfields come struct()
@@ -304,9 +291,8 @@ function defineUnion(fields) {
304
291
  // --------------------------------------------------------------------------
305
292
 
306
293
  // Create Structure and Union classes with injected dependencies
307
- const Structure = createStructureClass(alloc, struct);
308
- const Union = createUnionClass(Structure, union);
309
-
294
+ const Structure = createStructureClass(alloc, struct, bitfield);
295
+ const Union = createUnionClass(Structure, union, bitfield);
310
296
 
311
297
  /**
312
298
  * Helper per creare array a dimensione fissa (come c_int * 5 in Python)
@@ -412,28 +398,38 @@ function struct(fields, options = {}) {
412
398
  const SimpleCData = createSimpleCDataClass(alloc);
413
399
 
414
400
  // Create all primitive type classes
415
- const primitiveTypes = createPrimitiveTypes(
416
- SimpleCData,
417
- native,
418
- addressOf,
419
- cstring,
420
- wstring,
421
- readCString,
422
- readWString
423
- );
401
+ const primitiveTypes = createPrimitiveTypes(SimpleCData, native, addressOf, cstring, wstring, readCString, readWString);
424
402
 
425
403
  // Destructure all types for easy access
426
404
  const {
427
405
  c_void,
428
- c_int8, c_int16, c_int32, c_int64,
429
- c_uint8, c_uint16, c_uint32, c_uint64,
430
- c_float, c_double,
406
+ c_int8,
407
+ c_int16,
408
+ c_int32,
409
+ c_int64,
410
+ c_uint8,
411
+ c_uint16,
412
+ c_uint32,
413
+ c_uint64,
414
+ c_float,
415
+ c_double,
431
416
  c_bool,
432
- c_char, c_wchar,
433
- c_void_p, c_char_p, c_wchar_p,
434
- c_size_t, c_long, c_ulong,
435
- c_byte, c_ubyte, c_short, c_ushort,
436
- c_int, c_uint, c_longlong, c_ulonglong,
417
+ c_char,
418
+ c_wchar,
419
+ c_void_p,
420
+ c_char_p,
421
+ c_wchar_p,
422
+ c_size_t,
423
+ c_long,
424
+ c_ulong,
425
+ c_byte,
426
+ c_ubyte,
427
+ c_short,
428
+ c_ushort,
429
+ c_int,
430
+ c_uint,
431
+ c_longlong,
432
+ c_ulonglong,
437
433
  } = primitiveTypes;
438
434
 
439
435
  // ============================================================================
@@ -523,6 +519,7 @@ export {
523
519
  byref,
524
520
  cast,
525
521
  POINTER,
522
+ pointer,
526
523
 
527
524
  // Error handling
528
525
  get_errno,