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.

@@ -13,11 +13,11 @@
13
13
  *
14
14
  * @example Creating custom type
15
15
  * ```javascript
16
- * import { SimpleCData } from 'node-ctypes';
16
+ * import { SimpleCData, CType } from 'node-ctypes';
17
17
  *
18
18
  * class c_int32 extends SimpleCData {
19
19
  * static _size = 4;
20
- * static _type = 'int32';
20
+ * static _type = CType.INT32; // Numeric enum value from native module
21
21
  * static _reader = (buf, off) => buf.readInt32LE(off);
22
22
  * static _writer = (buf, off, val) => buf.writeInt32LE(val, off);
23
23
  * }
@@ -59,7 +59,7 @@ export function createSimpleCDataClass(alloc) {
59
59
  *
60
60
  * Subclasses must define:
61
61
  * - `static _size`: Size in bytes
62
- * - `static _type`: Type name string
62
+ * - `static _type`: CType numeric value (from native.CType)
63
63
  * - `static _reader(buffer, offset)`: Function to read value from buffer
64
64
  * - `static _writer(buffer, offset, value)`: Function to write value to buffer
65
65
  *
@@ -76,7 +76,7 @@ export function createSimpleCDataClass(alloc) {
76
76
  * ```javascript
77
77
  * class c_uint8 extends SimpleCData {
78
78
  * static _size = 1;
79
- * static _type = 'uint8';
79
+ * static _type = CType.UINT8; // Numeric enum from native module
80
80
  * static _reader = (buf, off) => buf.readUInt8(off);
81
81
  * static _writer = (buf, off, val) => buf.writeUInt8(val, off);
82
82
  * }
@@ -91,7 +91,7 @@ export function createSimpleCDataClass(alloc) {
91
91
  * ```javascript
92
92
  * class c_float extends SimpleCData {
93
93
  * static _size = 4;
94
- * static _type = 'float';
94
+ * static _type = CType.FLOAT;
95
95
  * static _reader = (buf, off) => buf.readFloatLE(off);
96
96
  * static _writer = (buf, off, val) => buf.writeFloatLE(val, off);
97
97
  * }
@@ -4,7 +4,12 @@
4
4
  * @description Primitive C type definitions (integers, floats, chars, pointers).
5
5
  *
6
6
  * This module provides all primitive C types that extend SimpleCData. Each type
7
- * defines its size, type name, and read/write functions for buffer operations.
7
+ * defines its size, CType value, and read/write functions for buffer operations.
8
+ *
9
+ * **Type System**:
10
+ * All types use numeric CType values from the native module as their `_type`
11
+ * property. This enables efficient type passing to the native FFI layer without
12
+ * string lookups.
8
13
  *
9
14
  * **Python ctypes Compatibility**:
10
15
  * All types are compatible with Python's ctypes module equivalents.
@@ -58,8 +63,14 @@
58
63
  /**
59
64
  * Creates all primitive type classes with required dependencies injected.
60
65
  *
66
+ * Each type class has:
67
+ * - `_size`: Size in bytes
68
+ * - `_type`: CType numeric value (from native.CType)
69
+ * - `_reader`: Function to read value from buffer
70
+ * - `_writer`: Function to write value to buffer
71
+ *
61
72
  * @param {Class} SimpleCData - SimpleCData base class
62
- * @param {Object} native - Native module with POINTER_SIZE
73
+ * @param {Object} native - Native module with POINTER_SIZE and CType
63
74
  * @param {Function} addressOf - addressOf function for pointers
64
75
  * @param {Function} cstring - cstring function for c_char_p
65
76
  * @param {Function} wstring - wstring function for c_wchar_p
@@ -69,13 +80,16 @@
69
80
  * @private
70
81
  */
71
82
  export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, wstring, readCString, readWString) {
83
+ // Get CType from native module - single source of truth
84
+ const CType = native.CType;
85
+
72
86
  // ============================================================================
73
87
  // Void type
74
88
  // ============================================================================
75
89
 
76
90
  class c_void extends SimpleCData {
77
91
  static _size = 0;
78
- static _type = "void";
92
+ static _type = CType.VOID;
79
93
  static _reader = (buf, off) => undefined;
80
94
  static _writer = (buf, off, val) => {};
81
95
  }
@@ -86,21 +100,21 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
86
100
 
87
101
  class c_int8 extends SimpleCData {
88
102
  static _size = 1;
89
- static _type = "int8";
103
+ static _type = CType.INT8;
90
104
  static _reader = (buf, off) => buf.readInt8(off);
91
105
  static _writer = (buf, off, val) => buf.writeInt8(val, off);
92
106
  }
93
107
 
94
108
  class c_int16 extends SimpleCData {
95
109
  static _size = 2;
96
- static _type = "int16";
110
+ static _type = CType.INT16;
97
111
  static _reader = (buf, off) => buf.readInt16LE(off);
98
112
  static _writer = (buf, off, val) => buf.writeInt16LE(val, off);
99
113
  }
100
114
 
101
115
  class c_int32 extends SimpleCData {
102
116
  static _size = 4;
103
- static _type = "int32";
117
+ static _type = CType.INT32;
104
118
  static _reader = (buf, off) => buf.readInt32LE(off);
105
119
  static _writer = (buf, off, val) => {
106
120
  const v = Number(val) | 0; // coerce to signed 32-bit
@@ -110,7 +124,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
110
124
 
111
125
  class c_int64 extends SimpleCData {
112
126
  static _size = 8;
113
- static _type = "int64";
127
+ static _type = CType.INT64;
114
128
  static _reader = (buf, off) => buf.readBigInt64LE(off);
115
129
  static _writer = (buf, off, val) => buf.writeBigInt64LE(BigInt(val), off);
116
130
  }
@@ -121,21 +135,21 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
121
135
 
122
136
  class c_uint8 extends SimpleCData {
123
137
  static _size = 1;
124
- static _type = "uint8";
138
+ static _type = CType.UINT8;
125
139
  static _reader = (buf, off) => buf.readUInt8(off);
126
140
  static _writer = (buf, off, val) => buf.writeUInt8(val, off);
127
141
  }
128
142
 
129
143
  class c_uint16 extends SimpleCData {
130
144
  static _size = 2;
131
- static _type = "uint16";
145
+ static _type = CType.UINT16;
132
146
  static _reader = (buf, off) => buf.readUInt16LE(off);
133
147
  static _writer = (buf, off, val) => buf.writeUInt16LE(val, off);
134
148
  }
135
149
 
136
150
  class c_uint32 extends SimpleCData {
137
151
  static _size = 4;
138
- static _type = "uint32";
152
+ static _type = CType.UINT32;
139
153
  static _reader = (buf, off) => buf.readUInt32LE(off);
140
154
  static _writer = (buf, off, val) => {
141
155
  const v = Number(val) >>> 0; // coerce to unsigned 32-bit
@@ -145,7 +159,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
145
159
 
146
160
  class c_uint64 extends SimpleCData {
147
161
  static _size = 8;
148
- static _type = "uint64";
162
+ static _type = CType.UINT64;
149
163
  static _reader = (buf, off) => buf.readBigUInt64LE(off);
150
164
  static _writer = (buf, off, val) => buf.writeBigUInt64LE(BigInt(val), off);
151
165
  }
@@ -156,14 +170,14 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
156
170
 
157
171
  class c_float extends SimpleCData {
158
172
  static _size = 4;
159
- static _type = "float";
173
+ static _type = CType.FLOAT;
160
174
  static _reader = (buf, off) => buf.readFloatLE(off);
161
175
  static _writer = (buf, off, val) => buf.writeFloatLE(val, off);
162
176
  }
163
177
 
164
178
  class c_double extends SimpleCData {
165
179
  static _size = 8;
166
- static _type = "double";
180
+ static _type = CType.DOUBLE;
167
181
  static _reader = (buf, off) => buf.readDoubleLE(off);
168
182
  static _writer = (buf, off, val) => buf.writeDoubleLE(val, off);
169
183
  }
@@ -174,7 +188,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
174
188
 
175
189
  class c_bool extends SimpleCData {
176
190
  static _size = 1;
177
- static _type = "bool";
191
+ static _type = CType.BOOL;
178
192
  static _reader = (buf, off) => buf.readUInt8(off) !== 0;
179
193
  static _writer = (buf, off, val) => buf.writeUInt8(val ? 1 : 0, off);
180
194
  }
@@ -185,7 +199,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
185
199
 
186
200
  class c_char extends SimpleCData {
187
201
  static _size = 1;
188
- static _type = "char";
202
+ static _type = CType.INT8; // char is int8 in C
189
203
  static _reader = (buf, off) => buf.readInt8(off);
190
204
  static _writer = (buf, off, val) => {
191
205
  if (typeof val === "string") val = val.charCodeAt(0);
@@ -195,7 +209,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
195
209
 
196
210
  class c_wchar extends SimpleCData {
197
211
  static _size = native.WCHAR_SIZE;
198
- static _type = "wchar";
212
+ static _type = CType.WCHAR;
199
213
  static _reader = (buf, off) => {
200
214
  if (native.WCHAR_SIZE === 2) {
201
215
  return String.fromCharCode(buf.readUInt16LE(off));
@@ -218,7 +232,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
218
232
 
219
233
  class c_void_p extends SimpleCData {
220
234
  static _size = native.POINTER_SIZE;
221
- static _type = "pointer";
235
+ static _type = CType.POINTER;
222
236
  static _reader = (buf, off) => {
223
237
  const ptr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : BigInt(buf.readUInt32LE(off));
224
238
  if (!ptr) return null;
@@ -257,7 +271,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
257
271
 
258
272
  class c_size_t extends SimpleCData {
259
273
  static _size = native.POINTER_SIZE;
260
- static _type = "size_t";
274
+ static _type = CType.SIZE_T;
261
275
  static _reader = (buf, off) => {
262
276
  return native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : buf.readUInt32LE(off);
263
277
  };
@@ -271,18 +285,18 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
271
285
  }
272
286
 
273
287
  // Get actual long size from native module (4 on Windows, 4 or 8 on Unix depending on arch)
274
- const _longSize = native.sizeof ? native.sizeof("long") : 4;
288
+ const _longSize = native.sizeof ? native.sizeof(CType.LONG) : 4;
275
289
 
276
290
  class c_long extends SimpleCData {
277
291
  static _size = _longSize;
278
- static _type = "long";
292
+ static _type = CType.LONG;
279
293
  static _reader = _longSize === 8 ? (buf, off) => buf.readBigInt64LE(off) : (buf, off) => buf.readInt32LE(off);
280
294
  static _writer = _longSize === 8 ? (buf, off, val) => buf.writeBigInt64LE(BigInt(val), off) : (buf, off, val) => buf.writeInt32LE(val, off);
281
295
  }
282
296
 
283
297
  class c_ulong extends SimpleCData {
284
298
  static _size = _longSize;
285
- static _type = "ulong";
299
+ static _type = CType.ULONG;
286
300
  static _reader = _longSize === 8 ? (buf, off) => buf.readBigUInt64LE(off) : (buf, off) => buf.readUInt32LE(off);
287
301
  static _writer = _longSize === 8 ? (buf, off, val) => buf.writeBigUInt64LE(BigInt(val), off) : (buf, off, val) => buf.writeUInt32LE(val, off);
288
302
  }
@@ -293,7 +307,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
293
307
 
294
308
  class c_char_p extends SimpleCData {
295
309
  static _size = native.POINTER_SIZE;
296
- static _type = "char_p";
310
+ static _type = CType.STRING;
297
311
  static _reader = (buf, off) => {
298
312
  const ptr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : BigInt(buf.readUInt32LE(off));
299
313
  if (!ptr) return null;
@@ -324,7 +338,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
324
338
 
325
339
  class c_wchar_p extends SimpleCData {
326
340
  static _size = native.POINTER_SIZE;
327
- static _type = "wchar_p";
341
+ static _type = CType.WSTRING;
328
342
  static _reader = (buf, off) => {
329
343
  const ptr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : BigInt(buf.readUInt32LE(off));
330
344
  if (!ptr) return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ctypes",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Python ctypes-like FFI for Node.js using libffi",
5
5
  "author": "Damiano Mazzella",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "generate-ffi-headers": "node third-party/libffi_cmake/generate-headers.js"
33
33
  },
34
34
  "devDependencies": {
35
- "cmake-js": "^7.4.0",
35
+ "cmake-js": "^8.0.0",
36
36
  "node-addon-api": "^8.5.0"
37
37
  },
38
38
  "engines": {