node-ctypes 2.3.0 → 2.5.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.
@@ -26,8 +26,8 @@
26
26
  * ```
27
27
  */
28
28
 
29
- import { keepAlive, writePointerField } from "../structures/helpers/common.js";
30
- import { flush, mirrorAddress, mirrorsActive } from "./mirror.js";
29
+ import { byrefOwners, isScalarSubclass, keepAlive, keepFunctionOwner, keptObject, kPointerMemory, memoryDomain, pointerOwner, scalarView, shareMemory, writePointerField } from "../structures/helpers/common.js";
30
+ import { flush, mirrorAddress, mirrorsActive, refresh } from "./mirror.js";
31
31
 
32
32
  /**
33
33
  * Gets the memory address of a buffer or pointer.
@@ -67,7 +67,7 @@ export function addressOf(ptr, alloc, native) {
67
67
  if (ptr && typeof ptr === "object" && Buffer.isBuffer(ptr._buffer)) {
68
68
  ptr = ptr._buffer;
69
69
  }
70
- if (Buffer.isBuffer(ptr)) {
70
+ if (Buffer.isBuffer(ptr) || ArrayBuffer.isView(ptr) || ptr instanceof ArrayBuffer) {
71
71
  // Native API returns the buffer's memory address in one V8 boundary
72
72
  // crossing. The fallback path below (tempBuf + writeValue) is kept
73
73
  // for backward compatibility with older .node binaries that do not
@@ -85,7 +85,8 @@ export function addressOf(ptr, alloc, native) {
85
85
  /**
86
86
  * Passes an object by reference (Python byref equivalent).
87
87
  *
88
- * Returns the underlying buffer of a SimpleCData instance or the buffer itself.
88
+ * Returns an owner-retaining byte view, or an address carrier for offsets
89
+ * outside the known buffer range.
89
90
  * This is useful for passing arguments to C functions that expect pointers.
90
91
  *
91
92
  * **Python ctypes Compatibility**:
@@ -96,7 +97,8 @@ export function addressOf(ptr, alloc, native) {
96
97
  * - SimpleCData instance (has _buffer property)
97
98
  * - Buffer object
98
99
  * - Structure instance (has _buffer property)
99
- * @returns {Buffer} The underlying buffer
100
+ * @param {number} [offset=0] - Signed byte offset
101
+ * @returns {Buffer|Object} Buffer view or owner-retaining address carrier
100
102
  * @throws {TypeError} If obj is not a ctypes instance or Buffer
101
103
  *
102
104
  * @example Pass SimpleCData by reference
@@ -126,21 +128,24 @@ export function addressOf(ptr, alloc, native) {
126
128
  * move_point(byref(point), 5, 10);
127
129
  * ```
128
130
  */
129
- export function byref(obj) {
131
+ export function byref(obj, offset = 0, native) {
132
+ if (!Number.isSafeInteger(offset)) throw new TypeError("byref() offset must be a safe integer");
133
+ let buffer;
130
134
  // Puntatore: la memoria del puntatore (T**), così una funzione C può
131
135
  // scriverci un indirizzo e ri-puntarlo (Python byref(p)).
132
136
  if (obj && typeof obj === "object" && obj._pointerType !== undefined) {
133
- return obj._pointerSlot;
137
+ buffer = obj._pointerSlot;
134
138
  }
135
139
  // SimpleCData instance or Structure instance - return its buffer
136
- if (obj && obj._buffer && Buffer.isBuffer(obj._buffer)) {
137
- return obj._buffer;
138
- }
140
+ else if (obj && obj._buffer && Buffer.isBuffer(obj._buffer)) buffer = obj._buffer;
139
141
  // Buffer - return as-is
140
- if (Buffer.isBuffer(obj)) {
141
- return obj;
142
- }
143
- throw new TypeError("byref() argument must be a ctypes instance or Buffer");
142
+ else if (Buffer.isBuffer(obj)) buffer = obj;
143
+ if (buffer === undefined) throw new TypeError("byref() argument must be a ctypes instance or Buffer");
144
+ const type = obj._pointerType ?? obj._arrayType ?? (obj.constructor?._isSimpleCData || typeof obj.constructor?._buildStruct === "function" ? obj.constructor : obj._structDef);
145
+ const view = offset >= 0 && offset <= buffer.length ? buffer.subarray(offset) : { address: (mirrorAddress(buffer) ?? native.addressOf(buffer)) + BigInt(offset) };
146
+ if (!Buffer.isBuffer(view)) Object.defineProperty(view, "_as_parameter_", { value: view.address });
147
+ byrefOwners.set(view, { owner: obj, type, offset });
148
+ return view;
144
149
  }
145
150
 
146
151
  /**
@@ -187,18 +192,21 @@ export function byref(obj) {
187
192
  */
188
193
  export function cast(ptr, targetType, readValue, sizeof, ptrToBuffer, native) {
189
194
  // Validate inputs
190
- if (!ptr) {
195
+ if (!ptr && !(targetType?._pointerTo !== undefined || targetType?._isPointerValued === true)) {
191
196
  throw new TypeError("cast() requires a non-null pointer");
192
197
  }
193
198
  if (!targetType) {
194
199
  throw new TypeError("cast() requires a target type");
195
200
  }
201
+ if (ArrayBuffer.isView(ptr) && !Buffer.isBuffer(ptr)) ptr = Buffer.from(ptr.buffer, ptr.byteOffset, ptr.byteLength);
202
+ else if (ptr instanceof ArrayBuffer) ptr = Buffer.from(ptr);
196
203
 
197
204
  // PointerTypeDef — return a PointerInstance via fromAddress() or fromBuffer()
198
205
  // Python equivalent: cast(c_void_p(addr), POINTER(c_int32)) — read stored address
199
206
  // Python equivalent: cast(arr, POINTER(c_int32)) — pointer to arr's memory
200
207
  // Python equivalent: cast(p, POINTER(c_double)) — pointer to same memory as p
201
208
  if (typeof targetType === "object" && targetType._pointerTo !== undefined) {
209
+ if (!ptr) return targetType.fromAddress(0n);
202
210
  // PointerInstance — use its address/buffer (Python: cast(p, POINTER(OtherType)))
203
211
  if (ptr && typeof ptr === "object" && typeof ptr.address === "bigint") {
204
212
  if (ptr._buffer) {
@@ -243,23 +251,19 @@ export function cast(ptr, targetType, readValue, sizeof, ptrToBuffer, native) {
243
251
  return targetType.fromBuffer(ptr);
244
252
  }
245
253
 
246
- let addr;
247
- if (typeof ptr === "bigint") {
248
- addr = ptr;
249
- } else if (typeof ptr === "number") {
250
- addr = BigInt(ptr);
251
- } else {
252
- throw new TypeError("cast(): ptr must be a BigInt, number, Buffer, ctypes instance, or PointerInstance");
253
- }
254
- return targetType.fromAddress(addr);
254
+ return targetType.fromAddress(castSourceAddress(ptr, native), ptr);
255
+ }
256
+
257
+ if (typeof targetType === "function" && targetType._isFuncPtr === true) {
258
+ return keepFunctionOwner(targetType(castSourceAddress(ptr, native)), ptr);
255
259
  }
256
260
 
257
261
  // c_void_p / c_char_p / c_wchar_p: an instance pointing to the source,
258
262
  // which it keeps alive (Python: cast(buf, c_void_p).value == addressof(buf)).
259
263
  if (typeof targetType === "function" && targetType._isSimpleCData === true && isPointerValued(targetType, native)) {
260
264
  const result = new targetType();
261
- writePointerField(result._buffer, 0, castSourceAddress(ptr, native), native.POINTER_SIZE);
262
- if (typeof ptr === "object") keepAlive(result._buffer, 0, ptr);
265
+ writePointerField(result._buffer, 0, ptr == null || ptr === 0 || ptr === 0n ? 0n : castSourceAddress(ptr, native), native.POINTER_SIZE);
266
+ if (typeof ptr === "object" || typeof ptr === "function") keepAlive(result._buffer, 0, ptr);
263
267
  return result;
264
268
  }
265
269
 
@@ -300,7 +304,7 @@ function isPointerValued(type, native) {
300
304
  function castSourceAddress(ptr, native) {
301
305
  if (typeof ptr === "bigint") return ptr;
302
306
  if (typeof ptr === "number") return BigInt(ptr);
303
- if (ptr !== null && typeof ptr === "object") {
307
+ if (ptr !== null && (typeof ptr === "object" || typeof ptr === "function")) {
304
308
  if (ptr._pointerType !== undefined) return ptr.address;
305
309
  const T = ptr.constructor;
306
310
  if (T?._isSimpleCData === true && isPointerValued(T, native)) {
@@ -308,7 +312,8 @@ function castSourceAddress(ptr, native) {
308
312
  }
309
313
  if (Buffer.isBuffer(ptr._buffer)) return addressOf(ptr._buffer, null, native);
310
314
  if (Buffer.isBuffer(ptr)) return addressOf(ptr, null, native);
311
- if (ptr._as_parameter_ !== undefined) return castSourceAddress(ptr._as_parameter_, native);
315
+ const parameter = ptr._as_parameter_;
316
+ if (parameter !== undefined) return castSourceAddress(parameter, native);
312
317
  }
313
318
  throw new TypeError("cast(): ptr must be a BigInt, number, Buffer, ctypes instance, or PointerInstance");
314
319
  }
@@ -411,26 +416,23 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
411
416
  !baseType._isArrayType &&
412
417
  !baseType._isFuncPtr;
413
418
 
414
- // Se non è thunk, `baseType` rimane il valore passato e `baseSize` è
415
- // calcolabile subito. Se è thunk, entrambi vengono rimpiazzati al primo
416
- // uso, senza mutare le chiusure dei trap.
419
+ const isAggregate = (type) => typeof type === "function" && (typeof type._buildStruct === "function" || typeof type._buildUnion === "function");
420
+ // A pointer does not materialize or finalize its incomplete pointee.
417
421
  let baseSize;
422
+ let scalarSubclass = isScalarSubclass(baseType);
418
423
  let _lazyResolved = false;
419
- function resolveIfLazy() {
424
+ function resolveType() {
420
425
  if (!isLazyThunk || _lazyResolved) return;
421
- const resolved = baseType();
422
- // NB: impostare `_lazyResolved = true` e `baseType = resolved` PRIMA di
423
- // chiamare sizeof(). sizeof può innescare `resolved._buildStruct()` che
424
- // a sua volta riattraversa i fields, tra cui questo stesso POINTER —
425
- // senza il flag risolto, si entrerebbe in ricorsione infinita.
426
- // La size del POINTER (pointer word) è comunque costante (POINTER_SIZE);
427
- // `baseSize` serve solo per pointer arithmetic che avviene dopo la
428
- // costruzione della struct, quando `resolved` è completamente definito.
429
- baseType = resolved;
426
+ baseType = baseType();
427
+ scalarSubclass = isScalarSubclass(baseType);
430
428
  _lazyResolved = true;
431
- baseSize = typeof resolved === "object" ? resolved.size : sizeof(resolved);
429
+ if (!isAggregate(baseType)) baseSize = typeof baseType === "object" ? baseType.size : sizeof(baseType);
430
+ }
431
+ function resolveSize() {
432
+ resolveType();
433
+ baseSize ??= typeof baseType === "object" ? baseType.size : sizeof(baseType);
432
434
  }
433
- if (!isLazyThunk) {
435
+ if (!isLazyThunk && !isAggregate(baseType)) {
434
436
  baseSize = typeof baseType === "object" ? baseType.size : sizeof(baseType);
435
437
  }
436
438
 
@@ -453,36 +455,50 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
453
455
  // Array (Python POINTER(c_int * 3)): contents e p[i] sono istanze dell'array.
454
456
  const isArrayBase = () => baseType !== null && (typeof baseType === "function" || typeof baseType === "object") && baseType._isArrayType === true;
455
457
  const hasInstances = () => isArrayBase() || (typeof baseType === "function" && (baseType._isSimpleCData === true || typeof baseType._buildStruct === "function" || typeof baseType._buildUnion === "function"));
456
- const isStructBase = () => isArrayBase() || (typeof baseType === "function" && (typeof baseType._buildStruct === "function" || typeof baseType._buildUnion === "function"));
458
+ const isStructBase = () => scalarSubclass || isArrayBase() || (typeof baseType === "function" && (typeof baseType._buildStruct === "function" || typeof baseType._buildUnion === "function"));
457
459
 
458
460
  // POINTER(POINTER(T)) / POINTER(CFUNCTYPE): elementi puntatore, letti come
459
461
  // oggetto puntatore / funzione (NULL → null) e scritti come indirizzo.
460
462
  const isPointerBase = () => (typeof baseType === "object" && baseType !== null && baseType._pointerTo !== undefined) || (typeof baseType === "function" && baseType._isFuncPtr === true);
461
463
 
462
- function readPointerAt(buffer, address, offset) {
464
+ function readPointerAt(buffer, address, offset, owner) {
463
465
  const inBuffer = buffer && offset + baseSize <= buffer.length;
464
466
  const src = inBuffer ? buffer : native.ptrToBuffer(address + BigInt(offset), baseSize);
465
467
  const off = inBuffer ? offset : 0;
468
+ if (!inBuffer) shareMemory(src, owner, address + BigInt(offset) - owner.origin);
469
+ if (mirrorsActive) refresh(src, off, baseSize);
466
470
  const target = baseSize === 8 ? src.readBigUInt64LE(off) : BigInt(src.readUInt32LE(off));
471
+ const keep = pointerOwner(src, off, target);
467
472
  if (target === 0n) return null;
468
- return typeof baseType === "function" ? baseType(target) : baseType.fromAddress(target);
473
+ if (typeof baseType !== "function") return baseType.fromAddress(target, keep);
474
+ return keepFunctionOwner(baseType(target), keep);
469
475
  }
470
476
 
471
- function writePointerAt(buffer, address, offset, value) {
477
+ function writePointerAt(buffer, address, offset, value, owner) {
472
478
  if (buffer && offset + baseSize <= buffer.length) {
473
479
  writePointerField(buffer, offset, value, baseSize);
480
+ if (mirrorsActive) flush(buffer, offset, baseSize);
474
481
  return;
475
482
  }
476
483
  const target = native.ptrToBuffer(address + BigInt(offset), baseSize);
484
+ shareMemory(target, owner, address + BigInt(offset) - owner.origin);
477
485
  writePointerField(target, 0, value, baseSize);
478
486
  if (mirrorsActive) flush(target);
479
487
  }
480
488
 
481
- function instanceAt(buffer, address, offset) {
489
+ function instanceAt(buffer, address, offset, owner) {
490
+ if (scalarSubclass) {
491
+ if (buffer && offset + baseSize <= buffer.length) return scalarView(baseType, buffer.subarray(offset, offset + baseSize));
492
+ const view = native.ptrToBuffer(address + BigInt(offset), baseSize);
493
+ shareMemory(view, owner, address + BigInt(offset) - owner.origin);
494
+ return scalarView(baseType, view);
495
+ }
482
496
  if (buffer && offset + baseSize <= buffer.length) {
483
497
  return baseType.from_buffer(buffer, offset);
484
498
  }
485
- return baseType.from_address(address + BigInt(offset));
499
+ const result = baseType.from_address(address + BigInt(offset));
500
+ shareMemory(result._buffer, owner, address + BigInt(offset) - owner.origin);
501
+ return result;
486
502
  }
487
503
 
488
504
  // Scrittura a un indirizzo senza Buffer associato: per le primitive il
@@ -490,24 +506,26 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
490
506
  // lo copia all'indirizzo, invece di creare un Buffer esterno (~400 ns,
491
507
  // vietato in Electron).
492
508
  let scratch = null;
493
- function writeAtAddress(addr, value) {
494
- if (typeof baseType === "function" && baseType._isSimpleCData) {
509
+ function writeAtAddress(addr, value, owner) {
510
+ if (typeof baseType === "function" && baseType._isSimpleCData && !baseType._isPointerValued) {
495
511
  scratch ??= alloc(baseSize);
496
512
  baseType._writer(scratch, 0, value);
497
513
  native.writeMemory(scratch, 0, baseSize, addr);
498
514
  return;
499
515
  }
500
516
  const buf = native.ptrToBuffer(addr, baseSize);
517
+ shareMemory(buf, owner, addr - owner.origin);
501
518
  writeValue(buf, baseType, value, 0);
502
519
  }
503
520
 
504
521
  // Lettura a un indirizzo senza Buffer associato: per le primitive
505
522
  // readValue(indirizzo) evita il Buffer esterno di ptrToBuffer.
506
- function readAtAddress(addr) {
523
+ function readAtAddress(addr, owner) {
507
524
  if (typeof baseType === "function" && baseType._isSimpleCData) {
508
525
  return readValue(addr, baseType, 0);
509
526
  }
510
527
  const buf = native.ptrToBuffer(addr, baseSize);
528
+ shareMemory(buf, owner, addr - owner.origin);
511
529
  return readValue(buf, baseType, 0);
512
530
  }
513
531
 
@@ -515,16 +533,17 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
515
533
  * Creates a pointer instance with Python ctypes-compatible API.
516
534
  * Supports .contents property and [index] access for pointer arithmetic.
517
535
  */
518
- function createPointerInstance(addressOrBuffer, keep) {
519
- // Risolvi il thunk lazy alla prima creazione di istanza: a questo
520
- // punto la classe target è sicuramente inizializzata.
521
- resolveIfLazy();
536
+ function createPointerInstance(addressOrBuffer, keep, memory) {
537
+ resolveType();
522
538
 
523
539
  let _address = 0n;
524
540
  let _buffer = null;
525
541
  // Python `_objects`: ciò che deve restare vivo finché il puntatore punta
526
542
  // lì (la sorgente di cast(), il puntatore da cui deriva add()).
527
543
  let _objects = keep;
544
+ let _memory = memory;
545
+ if (keep?._pointerType !== undefined) _objects = keep._buffer ?? keep._objects;
546
+ if (_memory === undefined && _objects?.[kPointerMemory]) _memory = _objects;
528
547
  // Memoria del puntatore (Python: un puntatore è un oggetto ctypes di
529
548
  // POINTER_SIZE byte), creata alla prima richiesta di byref(p) /
530
549
  // addressof(p). Da allora è lei a dare l'indirizzo: una funzione C che ci
@@ -538,6 +557,8 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
538
557
  _address = addr;
539
558
  _buffer = null;
540
559
  _objects = undefined;
560
+ _memory = undefined;
561
+ keepAlive(_slot, 0, null);
541
562
  }
542
563
  }
543
564
 
@@ -545,6 +566,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
545
566
  if (_slot === null) return;
546
567
  if (native.POINTER_SIZE === 8) _slot.writeBigUInt64LE(BigInt.asUintN(64, _address), 0);
547
568
  else _slot.writeUInt32LE(Number(BigInt.asUintN(32, _address)), 0);
569
+ keepAlive(_slot, 0, _memory ?? _buffer ?? _objects);
548
570
  }
549
571
 
550
572
  if (addressOrBuffer === undefined || addressOrBuffer === null) {
@@ -564,11 +586,15 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
564
586
  // vale il suo indirizzo originale, come un puntatore creato da indirizzo.
565
587
  function pointTo(buf) {
566
588
  _objects = undefined;
589
+ _memory = undefined;
567
590
  const mirrored = mirrorAddress(buf);
568
591
  if (mirrored !== undefined) {
569
592
  _buffer = null;
570
593
  _address = mirrored;
571
594
  _objects = buf;
595
+ const domain = memoryDomain(buf);
596
+ _memory = { owner: buf, root: domain.root, origin: mirrored - domain.offset - BigInt(buf.byteOffset) };
597
+ _memory[kPointerMemory] = true;
572
598
  } else {
573
599
  _buffer = buf;
574
600
  _address = getBufferAddress(buf);
@@ -576,10 +602,40 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
576
602
  store();
577
603
  }
578
604
 
605
+ function getMemory() {
606
+ if (_memory === undefined) {
607
+ let buffer = _buffer;
608
+ let address = _address;
609
+ if (buffer === null && _objects !== null && typeof _objects === "object") {
610
+ const known = _objects.constructor?._isPointerValued ? keptObject(_objects._buffer, 0) : _objects;
611
+ const candidate = Buffer.isBuffer(known) ? known : known?._buffer;
612
+ if (Buffer.isBuffer(candidate)) {
613
+ const base = getBufferAddress(candidate);
614
+ if (_address >= base && _address < base + BigInt(candidate.length)) {
615
+ buffer = candidate;
616
+ address = base;
617
+ }
618
+ }
619
+ }
620
+ const domain = buffer === null ? { root: {}, offset: 0n } : memoryDomain(buffer);
621
+ _memory = {
622
+ owner: buffer ?? _objects,
623
+ root: domain.root,
624
+ origin: buffer === null ? 0n : address - domain.offset - BigInt(buffer.byteOffset),
625
+ };
626
+ _memory[kPointerMemory] = true;
627
+ if (_slot !== null) keepAlive(_slot, 0, _memory);
628
+ }
629
+ return _memory;
630
+ }
631
+
579
632
  const instance = {
580
633
  _pointerType: PointerType,
581
634
  _baseType: baseType,
582
- _baseSize: baseSize,
635
+ get _baseSize() {
636
+ resolveSize();
637
+ return baseSize;
638
+ },
583
639
 
584
640
  /**
585
641
  * Get the raw address as BigInt
@@ -603,7 +659,8 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
603
659
 
604
660
  /** Oggetti tenuti vivi dal puntatore (Python `_objects`). */
605
661
  get _objects() {
606
- return _objects;
662
+ sync();
663
+ return getMemory();
607
664
  },
608
665
 
609
666
  /**
@@ -617,23 +674,25 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
617
674
  if (_address === 0n && !_buffer) {
618
675
  throw new Error("NULL pointer access");
619
676
  }
677
+ resolveSize();
620
678
  if (isPointerBase()) {
621
- return readPointerAt(_buffer, _address, 0);
679
+ return readPointerAt(_buffer, _address, 0, getMemory());
622
680
  }
623
681
  if (hasInstances()) {
624
- return instanceAt(_buffer, _address, 0);
682
+ return instanceAt(_buffer, _address, 0, getMemory());
625
683
  }
626
684
  if (_buffer) {
627
685
  // We have the actual buffer, read from it
628
686
  return readValue(_buffer, baseType, 0);
629
687
  }
630
- return readAtAddress(_address);
688
+ return readAtAddress(_address, getMemory());
631
689
  },
632
690
 
633
691
  set contents(value) {
634
692
  sync();
635
693
  // Python: assegnare un'istanza ri-punta il puntatore a quell'istanza.
636
694
  if (value !== null && typeof value === "object" && (value._isSimpleCDataInstance === true || value._structDef !== undefined || value._arrayType !== undefined) && Buffer.isBuffer(value._buffer)) {
695
+ resolveSize();
637
696
  // Python accetta anche un'istanza di un altro tipo; una più piccola
638
697
  // del tipo puntato farebbe leggere e scrivere oltre la sua memoria.
639
698
  if (value._buffer.length < baseSize) {
@@ -645,12 +704,13 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
645
704
  if (_address === 0n && !_buffer) {
646
705
  throw new Error("NULL pointer access");
647
706
  }
707
+ resolveSize();
648
708
  if (isPointerBase()) {
649
- writePointerAt(_buffer, _address, 0, value);
709
+ writePointerAt(_buffer, _address, 0, value, getMemory());
650
710
  } else if (_buffer) {
651
711
  writeValue(_buffer, baseType, value, 0);
652
712
  } else {
653
- writeAtAddress(_address, value);
713
+ writeAtAddress(_address, value, getMemory());
654
714
  }
655
715
  },
656
716
 
@@ -679,6 +739,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
679
739
  _address = BigInt(value);
680
740
  _buffer = null;
681
741
  _objects = undefined;
742
+ _memory = undefined;
682
743
  store();
683
744
  }
684
745
  },
@@ -695,8 +756,10 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
695
756
  * For compatibility - get pointer as buffer (8 bytes containing address)
696
757
  */
697
758
  toBuffer() {
759
+ sync();
698
760
  const buf = alloc(native.POINTER_SIZE);
699
761
  writeValue(buf, c_void_p, _address);
762
+ keepAlive(buf, 0, _memory ?? _buffer ?? _objects);
700
763
  return buf;
701
764
  },
702
765
 
@@ -711,9 +774,10 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
711
774
  */
712
775
  add(n) {
713
776
  sync();
777
+ resolveSize();
714
778
  const offsetBytes = BigInt(n) * BigInt(baseSize);
715
779
  // Il nuovo puntatore tiene viva la memoria di questo (Python _objects).
716
- return createPointerInstance(_address + offsetBytes, _buffer ?? _objects);
780
+ return createPointerInstance(_address + offsetBytes, _buffer ?? _objects, getMemory());
717
781
  },
718
782
 
719
783
  /**
@@ -739,12 +803,13 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
739
803
  if (target.isNull) {
740
804
  throw new Error("NULL pointer access");
741
805
  }
806
+ resolveSize();
742
807
  const offset = index * baseSize;
743
808
  if (isPointerBase()) {
744
- return readPointerAt(_buffer, _address, offset);
809
+ return readPointerAt(_buffer, _address, offset, getMemory());
745
810
  }
746
811
  if (isStructBase()) {
747
- return instanceAt(_buffer, _address, offset);
812
+ return instanceAt(_buffer, _address, offset, getMemory());
748
813
  }
749
814
  if (_buffer && offset + baseSize <= _buffer.length) {
750
815
  if (typeof baseType === "function" && baseType._isSimpleCData) {
@@ -752,7 +817,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
752
817
  }
753
818
  return readValue(_buffer, baseType, offset);
754
819
  }
755
- return readAtAddress(_address + BigInt(offset));
820
+ return readAtAddress(_address + BigInt(offset), getMemory());
756
821
  }
757
822
  return Reflect.get(target, prop, receiver);
758
823
  },
@@ -764,16 +829,17 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
764
829
  if (target.isNull) {
765
830
  throw new Error("NULL pointer access");
766
831
  }
832
+ resolveSize();
767
833
  const offset = index * baseSize;
768
834
  if (isPointerBase()) {
769
- writePointerAt(_buffer, _address, offset, value);
835
+ writePointerAt(_buffer, _address, offset, value, getMemory());
770
836
  return true;
771
837
  }
772
838
  if (_buffer && offset + baseSize <= _buffer.length) {
773
839
  writeValue(_buffer, baseType, value, offset);
774
840
  return true;
775
841
  }
776
- writeAtAddress(_address + BigInt(offset), value);
842
+ writeAtAddress(_address + BigInt(offset), value, getMemory());
777
843
  return true;
778
844
  }
779
845
  return Reflect.set(target, prop, value, receiver);
@@ -789,11 +855,15 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
789
855
  // accesso — così l'uso come `POINTER(() => Node)` in field type resta
790
856
  // Python-compatibile senza forzare l'utente a invalidare manualmente.
791
857
  get _pointerTo() {
792
- resolveIfLazy();
858
+ resolveType();
859
+ return baseType;
860
+ },
861
+ get _type_() {
862
+ resolveType();
793
863
  return baseType;
794
864
  },
795
865
  get _baseSize() {
796
- resolveIfLazy();
866
+ resolveSize();
797
867
  return baseSize;
798
868
  },
799
869
  size: native.POINTER_SIZE,
@@ -887,9 +957,12 @@ export function pointer(obj, POINTER_fn, sizeof, alloc, readValue, writeValue, c
887
957
  let targetBuffer;
888
958
  let baseType;
889
959
 
890
- // Le istanze di array sono Proxy su un Buffer (Buffer.isBuffer è vero): la
891
- // loro memoria è `_buffer`, controllato per primo.
892
- if (obj !== null && typeof obj === "object" && Buffer.isBuffer(obj._buffer)) {
960
+ if (obj !== null && typeof obj === "object" && obj._pointerType !== undefined) {
961
+ targetBuffer = obj._pointerSlot;
962
+ baseType = obj._pointerType;
963
+ }
964
+ // Array proxies expose their actual memory through _buffer.
965
+ else if (obj !== null && typeof obj === "object" && Buffer.isBuffer(obj._buffer)) {
893
966
  // Structure/Union instance, array or SimpleCData with _buffer
894
967
  targetBuffer = obj._buffer;
895
968
  // Get the base type for POINTER creation
@@ -1,3 +1,5 @@
1
+ import { booleanValue, registerSimpleType } from "../structures/helpers/common.js";
2
+
1
3
  /**
2
4
  * @file win_types.js
3
5
  * @module platform/win_types
@@ -43,14 +45,17 @@ export function createWinTypes(prims, { Structure, POINTER, array }) {
43
45
 
44
46
  // VARIANT_BOOL: 16-bit, VARIANT_TRUE = -1. Reads as a boolean, as Python.
45
47
  class VARIANT_BOOL extends c_short {
48
+ static _isBoolean = true;
49
+ static _isVariantBool = true;
46
50
  static _reader = (buf, off) => buf.readInt16LE(off) !== 0;
47
- static _writer = (buf, off, val) => buf.writeInt16LE(val ? -1 : 0, off);
51
+ static _writer = (buf, off, val) => buf.writeInt16LE(booleanValue(val) ? -1 : 0, off);
48
52
  static _readerBE = (buf, off) => buf.readInt16BE(off) !== 0;
49
- static _writerBE = (buf, off, val) => buf.writeInt16BE(val ? -1 : 0, off);
53
+ static _writerBE = (buf, off, val) => buf.writeInt16BE(booleanValue(val) ? -1 : 0, off);
50
54
  static from_param(val) {
51
- return val ? -1 : 0;
55
+ return booleanValue(val) ? -1 : 0;
52
56
  }
53
57
  }
58
+ registerSimpleType(VARIANT_BOOL);
54
59
 
55
60
  // String types
56
61
  const LPCOLESTR = c_wchar_p;