node-ctypes 2.1.1 → 2.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.
Files changed (36) hide show
  1. package/README.md +98 -13
  2. package/build/Release/ctypes-darwin-arm64.node +0 -0
  3. package/build/Release/ctypes-darwin-x64.node +0 -0
  4. package/build/Release/ctypes-linux-arm64-musl.node +0 -0
  5. package/build/Release/ctypes-linux-arm64.node +0 -0
  6. package/build/Release/ctypes-linux-x64-musl.node +0 -0
  7. package/build/Release/ctypes-linux-x64.node +0 -0
  8. package/build/Release/ctypes-win32-arm64.node +0 -0
  9. package/build/Release/ctypes-win32-x64.node +0 -0
  10. package/lib/core/Library.js +86 -76
  11. package/lib/core/callback.js +10 -17
  12. package/lib/core/conversions.js +193 -3
  13. package/lib/core/funcptr.js +67 -52
  14. package/lib/core/lazy-loader.js +10 -4
  15. package/lib/core/types.js +196 -98
  16. package/lib/index.d.ts +193 -47
  17. package/lib/index.js +51 -28
  18. package/lib/memory/buffer.js +66 -19
  19. package/lib/memory/mirror.js +283 -0
  20. package/lib/memory/operations.js +59 -42
  21. package/lib/memory/pointer.js +180 -61
  22. package/lib/platform/constants.js +0 -1
  23. package/lib/platform/errors.js +48 -4
  24. package/lib/platform/find_library.js +78 -19
  25. package/lib/structures/Structure.js +260 -172
  26. package/lib/structures/Union.js +10 -21
  27. package/lib/structures/helpers/array.js +235 -92
  28. package/lib/structures/helpers/bitfield.js +66 -150
  29. package/lib/structures/helpers/common.js +115 -15
  30. package/lib/structures/helpers/layout.js +131 -0
  31. package/lib/structures/helpers/struct.js +144 -400
  32. package/lib/structures/helpers/union.js +132 -142
  33. package/lib/structures/helpers/wrap.js +29 -17
  34. package/lib/types/SimpleCData.js +14 -7
  35. package/lib/types/primitives.js +130 -101
  36. package/package.json +2 -1
@@ -60,6 +60,8 @@
60
60
  * ```
61
61
  */
62
62
 
63
+ import { keepAlive, notIntegral } from "../structures/helpers/common.js";
64
+
63
65
  /**
64
66
  * Creates all primitive type classes with required dependencies injected.
65
67
  *
@@ -98,6 +100,39 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
98
100
  // Integer types - signed
99
101
  // ============================================================================
100
102
 
103
+ // Python: un intero è troncato ai bit del tipo (c_uint8(256) → 0,
104
+ // c_int64(2**64 + 7) → 7). Signed e unsigned hanno gli stessi bit, quindi i
105
+ // writer (tranne c_int32, più veloce con writeInt32) scrivono la
106
+ // rappresentazione senza segno. Number via ToInt32/ToUint32 (esatti per ogni
107
+ // double), BigInt via asIntN/asUintN.
108
+ // Il caso number è testato per primo: è il percorso caldo dei campi.
109
+ // Un number non intero è un float, che Python rifiuta per i tipi interi.
110
+ const u32 = (val) => (typeof val === "number" ? (Number.isInteger(val) ? val >>> 0 : notIntegral()) : typeof val === "bigint" ? Number(BigInt.asUintN(32, val)) : val >>> 0);
111
+ const i32 = (val) => (typeof val === "number" ? (Number.isInteger(val) ? val | 0 : notIntegral()) : typeof val === "bigint" ? Number(BigInt.asIntN(32, val)) : val | 0);
112
+ const u64 = (val) => BigInt.asUintN(64, typeof val === "bigint" ? val : typeof val === "number" && !Number.isInteger(val) ? notIntegral() : BigInt(val));
113
+ const writeU8 = (buf, off, val) => {
114
+ buf[off] = u32(val);
115
+ };
116
+ // Interi a 64 bit: un number intero sicuro (il caso comune) è scritto come
117
+ // due metà a 32 bit, senza passare da BigInt (~10x più lento).
118
+ const TWO_32 = 4294967296;
119
+ const writeU64LE = (buf, off, val) => {
120
+ if (typeof val === "number" && Number.isSafeInteger(val) && off + 8 <= buf.length) {
121
+ buf.writeUInt32LE(val >>> 0, off);
122
+ buf.writeUInt32LE(Math.floor(val / TWO_32) >>> 0, off + 4);
123
+ } else {
124
+ buf.writeBigUInt64LE(u64(val), off);
125
+ }
126
+ };
127
+ const writeU64BE = (buf, off, val) => {
128
+ if (typeof val === "number" && Number.isSafeInteger(val) && off + 8 <= buf.length) {
129
+ buf.writeUInt32BE(Math.floor(val / TWO_32) >>> 0, off);
130
+ buf.writeUInt32BE(val >>> 0, off + 4);
131
+ } else {
132
+ buf.writeBigUInt64BE(u64(val), off);
133
+ }
134
+ };
135
+
101
136
  // Each numeric primitive carries both a default LE _reader/_writer AND BE
102
137
  // variants (_readerBE/_writerBE). BigEndianStructure / LittleEndianStructure
103
138
  // opt-in via the struct's _swapped_ flag; native-order code is unaffected.
@@ -108,7 +143,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
108
143
  static _type = CType.INT8;
109
144
  static _signed = true;
110
145
  static _reader = (buf, off) => buf.readInt8(off);
111
- static _writer = (buf, off, val) => buf.writeInt8(val, off);
146
+ static _writer = writeU8;
112
147
  static _readerBE = c_int8._reader;
113
148
  static _writerBE = c_int8._writer;
114
149
  }
@@ -118,9 +153,9 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
118
153
  static _type = CType.INT16;
119
154
  static _signed = true;
120
155
  static _reader = (buf, off) => buf.readInt16LE(off);
121
- static _writer = (buf, off, val) => buf.writeInt16LE(val, off);
156
+ static _writer = (buf, off, val) => buf.writeUInt16LE(u32(val) & 0xffff, off);
122
157
  static _readerBE = (buf, off) => buf.readInt16BE(off);
123
- static _writerBE = (buf, off, val) => buf.writeInt16BE(val, off);
158
+ static _writerBE = (buf, off, val) => buf.writeUInt16BE(u32(val) & 0xffff, off);
124
159
  }
125
160
 
126
161
  class c_int32 extends SimpleCData {
@@ -128,12 +163,9 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
128
163
  static _type = CType.INT32;
129
164
  static _signed = true;
130
165
  static _reader = (buf, off) => buf.readInt32LE(off);
131
- static _writer = (buf, off, val) => {
132
- const v = Number(val) | 0; // coerce to signed 32-bit
133
- return buf.writeInt32LE(v, off);
134
- };
166
+ static _writer = (buf, off, val) => buf.writeInt32LE(i32(val), off);
135
167
  static _readerBE = (buf, off) => buf.readInt32BE(off);
136
- static _writerBE = (buf, off, val) => buf.writeInt32BE(Number(val) | 0, off);
168
+ static _writerBE = (buf, off, val) => buf.writeInt32BE(i32(val), off);
137
169
  }
138
170
 
139
171
  class c_int64 extends SimpleCData {
@@ -141,9 +173,9 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
141
173
  static _type = CType.INT64;
142
174
  static _signed = true;
143
175
  static _reader = (buf, off) => buf.readBigInt64LE(off);
144
- static _writer = (buf, off, val) => buf.writeBigInt64LE(BigInt(val), off);
176
+ static _writer = writeU64LE;
145
177
  static _readerBE = (buf, off) => buf.readBigInt64BE(off);
146
- static _writerBE = (buf, off, val) => buf.writeBigInt64BE(BigInt(val), off);
178
+ static _writerBE = writeU64BE;
147
179
  }
148
180
 
149
181
  // ============================================================================
@@ -154,7 +186,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
154
186
  static _size = 1;
155
187
  static _type = CType.UINT8;
156
188
  static _reader = (buf, off) => buf.readUInt8(off);
157
- static _writer = (buf, off, val) => buf.writeUInt8(val, off);
189
+ static _writer = writeU8;
158
190
  static _readerBE = c_uint8._reader;
159
191
  static _writerBE = c_uint8._writer;
160
192
  }
@@ -163,30 +195,27 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
163
195
  static _size = 2;
164
196
  static _type = CType.UINT16;
165
197
  static _reader = (buf, off) => buf.readUInt16LE(off);
166
- static _writer = (buf, off, val) => buf.writeUInt16LE(val, off);
198
+ static _writer = (buf, off, val) => buf.writeUInt16LE(u32(val) & 0xffff, off);
167
199
  static _readerBE = (buf, off) => buf.readUInt16BE(off);
168
- static _writerBE = (buf, off, val) => buf.writeUInt16BE(val, off);
200
+ static _writerBE = (buf, off, val) => buf.writeUInt16BE(u32(val) & 0xffff, off);
169
201
  }
170
202
 
171
203
  class c_uint32 extends SimpleCData {
172
204
  static _size = 4;
173
205
  static _type = CType.UINT32;
174
206
  static _reader = (buf, off) => buf.readUInt32LE(off);
175
- static _writer = (buf, off, val) => {
176
- const v = Number(val) >>> 0; // coerce to unsigned 32-bit
177
- return buf.writeUInt32LE(v, off);
178
- };
207
+ static _writer = (buf, off, val) => buf.writeUInt32LE(u32(val), off);
179
208
  static _readerBE = (buf, off) => buf.readUInt32BE(off);
180
- static _writerBE = (buf, off, val) => buf.writeUInt32BE(Number(val) >>> 0, off);
209
+ static _writerBE = (buf, off, val) => buf.writeUInt32BE(u32(val), off);
181
210
  }
182
211
 
183
212
  class c_uint64 extends SimpleCData {
184
213
  static _size = 8;
185
214
  static _type = CType.UINT64;
186
215
  static _reader = (buf, off) => buf.readBigUInt64LE(off);
187
- static _writer = (buf, off, val) => buf.writeBigUInt64LE(BigInt(val), off);
216
+ static _writer = writeU64LE;
188
217
  static _readerBE = (buf, off) => buf.readBigUInt64BE(off);
189
- static _writerBE = (buf, off, val) => buf.writeBigUInt64BE(BigInt(val), off);
218
+ static _writerBE = writeU64BE;
190
219
  }
191
220
 
192
221
  // ============================================================================
@@ -231,27 +260,53 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
231
260
  // Python c_char: il valore è un bytes di 1 byte; in JS una stringa di 1
232
261
  // carattere con codice 0-255. In scrittura accetta anche un numero o un Buffer.
233
262
  // Python ctypes accetta solo un singolo carattere (o un intero 0..255 per c_char).
263
+ const typeName = (val) => {
264
+ if (val === null) return "null";
265
+ if (Buffer.isBuffer(val)) return "Buffer";
266
+ if (typeof val === "object") return val.constructor?.name || "object";
267
+ return typeof val;
268
+ };
269
+
270
+ const CHAR_EXPECTED = "one character bytes, bytearray, or an integer in range(256) expected";
234
271
  const charCode = (val) => {
235
272
  if (typeof val === "string") {
236
- if (val.length === 1 && val.charCodeAt(0) <= 0xff) return val.charCodeAt(0);
237
- } else if (Buffer.isBuffer(val) || val instanceof Uint8Array) {
238
- if (val.length === 1) return val[0];
239
- } else if (typeof val === "number" && Number.isInteger(val) && val >= 0 && val <= 0xff) {
273
+ if (val.length !== 1) throw new TypeError(`${CHAR_EXPECTED}, not a string of length ${val.length}`);
274
+ const code = val.charCodeAt(0);
275
+ if (code > 0xff) throw new TypeError("character not in range(256)");
276
+ return code;
277
+ }
278
+ if (val instanceof Uint8Array) {
279
+ if (val.length !== 1) throw new TypeError(`${CHAR_EXPECTED}, not ${typeName(val)} of length ${val.length}`);
280
+ return val[0];
281
+ }
282
+ if (typeof val === "number" && Number.isInteger(val)) {
283
+ if (val < 0 || val > 0xff) throw new TypeError("integer not in range(256)");
240
284
  return val;
241
285
  }
242
- throw new TypeError("one character bytes, bytearray or integer expected");
286
+ if (typeof val === "boolean") return val ? 1 : 0;
287
+ throw new TypeError(`${CHAR_EXPECTED}, not ${typeName(val)}`);
243
288
  };
244
289
 
290
+ // Python c_wchar: un solo carattere. Dove wchar_t è a 16 bit un carattere
291
+ // fuori dal BMP occupa due wchar_t e non è accettato.
292
+ const WCHAR_LIMIT = native.WCHAR_SIZE === 2 ? 0x10000 : 0x110000;
245
293
  const wcharCode = (val) => {
246
294
  if (typeof val === "string") {
247
295
  const code = val.codePointAt(0);
248
- if (code !== undefined && val.length === (code > 0xffff ? 2 : 1)) return code;
249
- throw new TypeError("one character unicode string expected");
296
+ let length = val.length;
297
+ if (length === 2 && code > 0xffff && code < WCHAR_LIMIT) length = 1;
298
+ if (length !== 1) throw new TypeError(`a unicode character expected, not a string of length ${length}`);
299
+ return code;
250
300
  }
251
- if (typeof val === "number" && Number.isInteger(val) && val >= 0) return val;
252
- throw new TypeError(`unicode string expected instead of ${val === null ? "null" : typeof val}`);
301
+ if (typeof val === "number") {
302
+ if (Number.isInteger(val) && val >= 0 && val < WCHAR_LIMIT) return val;
303
+ throw new TypeError(`wchar_t code point not in range(0x${WCHAR_LIMIT.toString(16)})`);
304
+ }
305
+ throw new TypeError(`a unicode character expected, not instance of ${typeName(val)}`);
253
306
  };
254
307
 
308
+ const wcharString = (code) => String.fromCodePoint(code > 0x10ffff ? 0xfffd : code);
309
+
255
310
  class c_char extends SimpleCData {
256
311
  static _size = 1;
257
312
  static _type = CType.CHAR;
@@ -272,7 +327,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
272
327
  if (native.WCHAR_SIZE === 2) {
273
328
  return String.fromCharCode(buf.readUInt16LE(off));
274
329
  }
275
- return String.fromCodePoint(buf.readUInt32LE(off));
330
+ return wcharString(buf.readUInt32LE(off));
276
331
  };
277
332
  static _writer = (buf, off, val) => {
278
333
  const code = wcharCode(val);
@@ -286,7 +341,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
286
341
  if (native.WCHAR_SIZE === 2) {
287
342
  return String.fromCharCode(buf.readUInt16BE(off));
288
343
  }
289
- return String.fromCodePoint(buf.readUInt32BE(off));
344
+ return wcharString(buf.readUInt32BE(off));
290
345
  };
291
346
  static _writerBE = (buf, off, val) => {
292
347
  const code = wcharCode(val);
@@ -305,60 +360,58 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
305
360
  class c_void_p extends SimpleCData {
306
361
  static _size = native.POINTER_SIZE;
307
362
  static _type = CType.POINTER;
363
+ static _isPointerValued = true;
364
+ // Campi c_void_p: anche istanze c_char_p / c_wchar_p (il loro indirizzo).
365
+ static _acceptsPointerValues = true;
308
366
  static _reader = (buf, off) => {
309
367
  const ptr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : BigInt(buf.readUInt32LE(off));
310
368
  if (!ptr) return null;
311
369
  return ptr;
312
370
  };
313
371
  static _writer = (buf, off, val) => {
314
- // Accept Buffers (pointer to memory) and struct proxies with _buffer
315
- if (Buffer.isBuffer(val)) {
316
- const addr = addressOf(val);
317
- const v = typeof addr === "bigint" ? addr : BigInt(addr || 0);
318
- if (native.POINTER_SIZE === 8) buf.writeBigUInt64LE(v, off);
319
- else buf.writeUInt32LE(Number(v), off);
320
- return;
321
- }
322
- if (val && typeof val === "object" && val._buffer && Buffer.isBuffer(val._buffer)) {
323
- const addr = addressOf(val._buffer);
324
- const v = typeof addr === "bigint" ? addr : BigInt(addr || 0);
325
- if (native.POINTER_SIZE === 8) buf.writeBigUInt64LE(v, off);
326
- else buf.writeUInt32LE(Number(v), off);
327
- return;
328
- }
372
+ const address = voidPointerAddress(val);
373
+ if (native.POINTER_SIZE === 8) buf.writeBigUInt64LE(address, off);
374
+ else buf.writeUInt32LE(Number(address), off);
375
+ // La memoria assegnata resta viva con il buffer (Python `_objects`).
376
+ keepAlive(buf, off, val !== null && typeof val === "object" ? val : null);
377
+ };
378
+ }
329
379
 
330
- // Fallback: accept BigInt or number
331
- const v = typeof val === "bigint" ? val : BigInt(val || 0);
332
- if (native.POINTER_SIZE === 8) {
333
- buf.writeBigUInt64LE(v, off);
334
- } else {
335
- buf.writeUInt32LE(Number(v), off);
380
+ // Indirizzo assegnato a un c_void_p: memoria di Buffer e istanze, valore di
381
+ // puntatori, c_void_p / c_char_p / c_wchar_p e `_as_parameter_`.
382
+ function voidPointerAddress(val) {
383
+ if (typeof val === "bigint") return val;
384
+ if (val === null || val === undefined) return 0n;
385
+ if (typeof val === "object") {
386
+ if (val._pointerType !== undefined) return val.address;
387
+ const T = val.constructor;
388
+ if (T?._isSimpleCData === true && (T._type === CType.POINTER || T._type === CType.STRING || T._type === CType.WSTRING)) {
389
+ return native.POINTER_SIZE === 8 ? val._buffer.readBigUInt64LE(0) : BigInt(val._buffer.readUInt32LE(0));
336
390
  }
337
- };
391
+ if (Buffer.isBuffer(val) || Buffer.isBuffer(val._buffer)) return BigInt(addressOf(val));
392
+ if (val._as_parameter_ !== undefined) return voidPointerAddress(val._as_parameter_);
393
+ throw new TypeError("c_void_p: cannot be converted to pointer");
394
+ }
395
+ return BigInt(val);
338
396
  }
339
397
 
340
398
  // ============================================================================
341
399
  // Platform-specific types
342
400
  // ============================================================================
343
401
 
402
+ // Interi pointer-sized e long: stessi writer troncanti degli interi a 32/64 bit.
403
+ const writeWord = (size) => (size === 8 ? writeU64LE : (buf, off, val) => buf.writeUInt32LE(u32(val), off));
404
+ const writeWordBE = (size) => (size === 8 ? writeU64BE : (buf, off, val) => buf.writeUInt32BE(u32(val), off));
405
+
344
406
  class c_size_t extends SimpleCData {
345
407
  static _size = native.POINTER_SIZE;
346
408
  static _type = CType.SIZE_T;
347
409
  static _reader = (buf, off) => {
348
410
  return native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : buf.readUInt32LE(off);
349
411
  };
350
- static _writer = (buf, off, val) => {
351
- if (native.POINTER_SIZE === 8) {
352
- buf.writeBigUInt64LE(BigInt(val), off);
353
- } else {
354
- buf.writeUInt32LE(Number(val), off);
355
- }
356
- };
412
+ static _writer = writeWord(native.POINTER_SIZE);
357
413
  static _readerBE = (buf, off) => (native.POINTER_SIZE === 8 ? buf.readBigUInt64BE(off) : buf.readUInt32BE(off));
358
- static _writerBE = (buf, off, val) => {
359
- if (native.POINTER_SIZE === 8) buf.writeBigUInt64BE(BigInt(val), off);
360
- else buf.writeUInt32BE(Number(val), off);
361
- };
414
+ static _writerBE = writeWordBE(native.POINTER_SIZE);
362
415
  }
363
416
 
364
417
  class c_ssize_t extends SimpleCData {
@@ -367,18 +420,9 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
367
420
  static _reader = (buf, off) => {
368
421
  return native.POINTER_SIZE === 8 ? buf.readBigInt64LE(off) : buf.readInt32LE(off);
369
422
  };
370
- static _writer = (buf, off, val) => {
371
- if (native.POINTER_SIZE === 8) {
372
- buf.writeBigInt64LE(BigInt(val), off);
373
- } else {
374
- buf.writeInt32LE(Number(val), off);
375
- }
376
- };
423
+ static _writer = writeWord(native.POINTER_SIZE);
377
424
  static _readerBE = (buf, off) => (native.POINTER_SIZE === 8 ? buf.readBigInt64BE(off) : buf.readInt32BE(off));
378
- static _writerBE = (buf, off, val) => {
379
- if (native.POINTER_SIZE === 8) buf.writeBigInt64BE(BigInt(val), off);
380
- else buf.writeInt32BE(Number(val), off);
381
- };
425
+ static _writerBE = writeWordBE(native.POINTER_SIZE);
382
426
  }
383
427
 
384
428
  // Get actual long size from native module (4 on Windows, 4 or 8 on Unix depending on arch)
@@ -387,50 +431,33 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
387
431
  // c_long / c_ulong valgono BigInt su ogni piattaforma, anche dove `long` è a
388
432
  // 32 bit (Windows): stesso tipo JS per ritorni, campi e `.value` ovunque.
389
433
  // In scrittura accettano number e BigInt (troncato ai bit del tipo, come Python).
390
- const asInt32 = (val) => (typeof val === "bigint" ? Number(BigInt.asIntN(32, val)) : val);
391
- const asUint32 = (val) => (typeof val === "bigint" ? Number(BigInt.asUintN(32, val)) : val);
392
-
393
434
  class c_long extends SimpleCData {
394
435
  static _size = _longSize;
395
436
  static _type = CType.LONG;
396
437
  static _reader = _longSize === 8 ? (buf, off) => buf.readBigInt64LE(off) : (buf, off) => BigInt(buf.readInt32LE(off));
397
- static _writer = _longSize === 8 ? (buf, off, val) => buf.writeBigInt64LE(BigInt(val), off) : (buf, off, val) => buf.writeInt32LE(asInt32(val), off);
438
+ static _writer = writeWord(_longSize);
398
439
  static _readerBE = _longSize === 8 ? (buf, off) => buf.readBigInt64BE(off) : (buf, off) => BigInt(buf.readInt32BE(off));
399
- static _writerBE = _longSize === 8 ? (buf, off, val) => buf.writeBigInt64BE(BigInt(val), off) : (buf, off, val) => buf.writeInt32BE(asInt32(val), off);
440
+ static _writerBE = writeWordBE(_longSize);
400
441
  }
401
442
 
402
443
  class c_ulong extends SimpleCData {
403
444
  static _size = _longSize;
404
445
  static _type = CType.ULONG;
405
446
  static _reader = _longSize === 8 ? (buf, off) => buf.readBigUInt64LE(off) : (buf, off) => BigInt(buf.readUInt32LE(off));
406
- static _writer = _longSize === 8 ? (buf, off, val) => buf.writeBigUInt64LE(BigInt(val), off) : (buf, off, val) => buf.writeUInt32LE(asUint32(val), off);
447
+ static _writer = writeWord(_longSize);
407
448
  static _readerBE = _longSize === 8 ? (buf, off) => buf.readBigUInt64BE(off) : (buf, off) => BigInt(buf.readUInt32BE(off));
408
- static _writerBE = _longSize === 8 ? (buf, off, val) => buf.writeBigUInt64BE(BigInt(val), off) : (buf, off, val) => buf.writeUInt32BE(asUint32(val), off);
449
+ static _writerBE = writeWordBE(_longSize);
409
450
  }
410
451
 
411
452
  // ============================================================================
412
453
  // String pointer types (special handling)
413
454
  // ============================================================================
414
455
 
415
- // Keepalive helper: when assigning a JS string (or an arbitrary Buffer) to a
416
- // c_char_p / c_wchar_p field of a Structure, we convert/reference the backing
417
- // Buffer and attach it to the struct's underlying Buffer so it outlives the
418
- // write. Without this the converted Buffer would be GC-eligible immediately
419
- // after _writer returns, leaving a dangling pointer in the field.
420
- // This mirrors Python ctypes' `_objects` keepalive mechanism.
421
- const _stringKeepalive = (targetBuf, off, val) => {
422
- let keep = targetBuf.__ctypesKeepalive;
423
- if (!keep) {
424
- keep = Object.create(null);
425
- Object.defineProperty(targetBuf, "__ctypesKeepalive", {
426
- value: keep,
427
- enumerable: false,
428
- configurable: false,
429
- writable: false,
430
- });
431
- }
432
- keep[off] = val;
433
- };
456
+ // Keepalive: when assigning a JS string (or an arbitrary Buffer) to a
457
+ // c_char_p / c_wchar_p field, the converted Buffer is kept alive with the
458
+ // memory of the structure (see keepAlive), otherwise the field would point
459
+ // to collected memory. This mirrors Python ctypes' `_objects`.
460
+ const _stringKeepalive = keepAlive;
434
461
 
435
462
  const _writePointerField = (buf, off, val) => {
436
463
  const addr = typeof val === "bigint" ? val : BigInt(val || 0);
@@ -444,6 +471,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
444
471
  class c_char_p extends SimpleCData {
445
472
  static _size = native.POINTER_SIZE;
446
473
  static _type = CType.STRING;
474
+ static _isPointerValued = true;
447
475
  static _reader = (buf, off) => {
448
476
  const ptr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : BigInt(buf.readUInt32LE(off));
449
477
  if (!ptr) return null;
@@ -469,6 +497,7 @@ export function createPrimitiveTypes(SimpleCData, native, addressOf, cstring, ws
469
497
  class c_wchar_p extends SimpleCData {
470
498
  static _size = native.POINTER_SIZE;
471
499
  static _type = CType.WSTRING;
500
+ static _isPointerValued = true;
472
501
  static _reader = (buf, off) => {
473
502
  const ptr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(off) : BigInt(buf.readUInt32LE(off));
474
503
  if (!ptr) return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ctypes",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "description": "Python ctypes-like FFI for Node.js using libffi",
5
5
  "author": "Damiano Mazzella",
6
6
  "license": "MIT",
@@ -27,6 +27,7 @@
27
27
  "scripts": {
28
28
  "build": "cmake-js compile --target ctypes",
29
29
  "build:debug": "cmake-js compile --target ctypes --debug",
30
+ "build:test-fixture": "cmake-js compile --target ctypes_test_fixture",
30
31
  "rebuild": "cmake-js rebuild --target ctypes",
31
32
  "clean": "cmake-js clean",
32
33
  "generate-ffi-headers": "node third-party/libffi_cmake/generate-headers.js",