node-ctypes 2.3.0 → 2.4.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/CHANGELOG.md +932 -0
- package/README.md +446 -99
- package/build/Release/ctypes-darwin-arm64.node +0 -0
- package/build/Release/ctypes-darwin-x64.node +0 -0
- package/build/Release/ctypes-linux-arm64-musl.node +0 -0
- package/build/Release/ctypes-linux-arm64.node +0 -0
- package/build/Release/ctypes-linux-x64-musl.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/core/Library.js +79 -62
- package/lib/core/callback.js +7 -3
- package/lib/core/conversions.js +226 -34
- package/lib/core/funcptr.js +115 -57
- package/lib/core/types.js +7 -2
- package/lib/index.d.ts +119 -54
- package/lib/index.js +10 -8
- package/lib/memory/buffer.js +39 -8
- package/lib/memory/operations.js +4 -6
- package/lib/memory/pointer.js +105 -38
- package/lib/platform/win_types.js +8 -3
- package/lib/structures/Structure.js +8 -39
- package/lib/structures/helpers/array.js +31 -12
- package/lib/structures/helpers/bitfield.js +17 -8
- package/lib/structures/helpers/common.js +218 -29
- package/lib/structures/helpers/layout.js +17 -3
- package/lib/structures/helpers/struct.js +25 -26
- package/lib/structures/helpers/union.js +32 -22
- package/lib/types/SimpleCData.js +11 -32
- package/lib/types/primitives.js +34 -16
- package/lib/utils/cache.js +34 -2
- package/package.json +3 -1
package/lib/memory/operations.js
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
30
|
import { flush, mirrorsActive, refresh } from "./mirror.js";
|
|
31
|
-
import { copyInstance } from "../structures/helpers/common.js";
|
|
31
|
+
import { copyInstance, isScalarSubclass, scalarView } from "../structures/helpers/common.js";
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Factory che cattura le dipendenze in closure e restituisce un bundle di
|
|
@@ -151,6 +151,7 @@ export function createMemoryOps(deps) {
|
|
|
151
151
|
ptr = memoryAt(ptr, offset, () => size, "readValue");
|
|
152
152
|
checkBounds(ptr, offset, size, "Read");
|
|
153
153
|
if (mirrorsActive) refresh(ptr, offset, size);
|
|
154
|
+
if (isScalarSubclass(type)) return scalarView(type, ptr.subarray(offset, offset + size));
|
|
154
155
|
return type._reader(ptr, offset);
|
|
155
156
|
}
|
|
156
157
|
|
|
@@ -192,12 +193,9 @@ export function createMemoryOps(deps) {
|
|
|
192
193
|
const writeComposite = (def) => {
|
|
193
194
|
checkBounds(ptr, offset, def.size, "Write");
|
|
194
195
|
const tempBuf = ptr.subarray(offset, offset + def.size);
|
|
195
|
-
if (Buffer.isBuffer(value)) {
|
|
196
|
-
value.copy(tempBuf, 0, 0, def.size);
|
|
197
|
-
if (mirrorsActive) flush(tempBuf);
|
|
198
|
-
} else if (value !== null && typeof value === "object" && Buffer.isBuffer(value._buffer)) {
|
|
196
|
+
if (Buffer.isBuffer(value) || (value !== null && typeof value === "object" && Buffer.isBuffer(value._buffer))) {
|
|
199
197
|
// Istanza struct/union: copia dei byte (Python), non campo per campo.
|
|
200
|
-
copyInstance(tempBuf, 0, def.size, value);
|
|
198
|
+
copyInstance(tempBuf, 0, def.size, value, 0, def);
|
|
201
199
|
if (mirrorsActive) flush(tempBuf);
|
|
202
200
|
} else if (typeof value === "object" && value !== null) {
|
|
203
201
|
for (const [k, v] of Object.entries(value)) {
|
package/lib/memory/pointer.js
CHANGED
|
@@ -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
|
|
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
|
-
* @
|
|
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
|
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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) {
|
|
@@ -258,7 +266,7 @@ export function cast(ptr, targetType, readValue, sizeof, ptrToBuffer, native) {
|
|
|
258
266
|
// which it keeps alive (Python: cast(buf, c_void_p).value == addressof(buf)).
|
|
259
267
|
if (typeof targetType === "function" && targetType._isSimpleCData === true && isPointerValued(targetType, native)) {
|
|
260
268
|
const result = new targetType();
|
|
261
|
-
writePointerField(result._buffer, 0, castSourceAddress(ptr, native), native.POINTER_SIZE);
|
|
269
|
+
writePointerField(result._buffer, 0, ptr == null || ptr === 0 || ptr === 0n ? 0n : castSourceAddress(ptr, native), native.POINTER_SIZE);
|
|
262
270
|
if (typeof ptr === "object") keepAlive(result._buffer, 0, ptr);
|
|
263
271
|
return result;
|
|
264
272
|
}
|
|
@@ -415,6 +423,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
415
423
|
// calcolabile subito. Se è thunk, entrambi vengono rimpiazzati al primo
|
|
416
424
|
// uso, senza mutare le chiusure dei trap.
|
|
417
425
|
let baseSize;
|
|
426
|
+
let scalarSubclass = isScalarSubclass(baseType);
|
|
418
427
|
let _lazyResolved = false;
|
|
419
428
|
function resolveIfLazy() {
|
|
420
429
|
if (!isLazyThunk || _lazyResolved) return;
|
|
@@ -427,6 +436,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
427
436
|
// `baseSize` serve solo per pointer arithmetic che avviene dopo la
|
|
428
437
|
// costruzione della struct, quando `resolved` è completamente definito.
|
|
429
438
|
baseType = resolved;
|
|
439
|
+
scalarSubclass = isScalarSubclass(baseType);
|
|
430
440
|
_lazyResolved = true;
|
|
431
441
|
baseSize = typeof resolved === "object" ? resolved.size : sizeof(resolved);
|
|
432
442
|
}
|
|
@@ -453,36 +463,50 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
453
463
|
// Array (Python POINTER(c_int * 3)): contents e p[i] sono istanze dell'array.
|
|
454
464
|
const isArrayBase = () => baseType !== null && (typeof baseType === "function" || typeof baseType === "object") && baseType._isArrayType === true;
|
|
455
465
|
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"));
|
|
466
|
+
const isStructBase = () => scalarSubclass || isArrayBase() || (typeof baseType === "function" && (typeof baseType._buildStruct === "function" || typeof baseType._buildUnion === "function"));
|
|
457
467
|
|
|
458
468
|
// POINTER(POINTER(T)) / POINTER(CFUNCTYPE): elementi puntatore, letti come
|
|
459
469
|
// oggetto puntatore / funzione (NULL → null) e scritti come indirizzo.
|
|
460
470
|
const isPointerBase = () => (typeof baseType === "object" && baseType !== null && baseType._pointerTo !== undefined) || (typeof baseType === "function" && baseType._isFuncPtr === true);
|
|
461
471
|
|
|
462
|
-
function readPointerAt(buffer, address, offset) {
|
|
472
|
+
function readPointerAt(buffer, address, offset, owner) {
|
|
463
473
|
const inBuffer = buffer && offset + baseSize <= buffer.length;
|
|
464
474
|
const src = inBuffer ? buffer : native.ptrToBuffer(address + BigInt(offset), baseSize);
|
|
465
475
|
const off = inBuffer ? offset : 0;
|
|
476
|
+
if (!inBuffer) shareMemory(src, owner, address + BigInt(offset) - owner.origin);
|
|
477
|
+
if (mirrorsActive) refresh(src, off, baseSize);
|
|
466
478
|
const target = baseSize === 8 ? src.readBigUInt64LE(off) : BigInt(src.readUInt32LE(off));
|
|
479
|
+
const keep = pointerOwner(src, off, target);
|
|
467
480
|
if (target === 0n) return null;
|
|
468
|
-
|
|
481
|
+
if (typeof baseType !== "function") return baseType.fromAddress(target, keep);
|
|
482
|
+
return keepFunctionOwner(baseType(target), keep);
|
|
469
483
|
}
|
|
470
484
|
|
|
471
|
-
function writePointerAt(buffer, address, offset, value) {
|
|
485
|
+
function writePointerAt(buffer, address, offset, value, owner) {
|
|
472
486
|
if (buffer && offset + baseSize <= buffer.length) {
|
|
473
487
|
writePointerField(buffer, offset, value, baseSize);
|
|
488
|
+
if (mirrorsActive) flush(buffer, offset, baseSize);
|
|
474
489
|
return;
|
|
475
490
|
}
|
|
476
491
|
const target = native.ptrToBuffer(address + BigInt(offset), baseSize);
|
|
492
|
+
shareMemory(target, owner, address + BigInt(offset) - owner.origin);
|
|
477
493
|
writePointerField(target, 0, value, baseSize);
|
|
478
494
|
if (mirrorsActive) flush(target);
|
|
479
495
|
}
|
|
480
496
|
|
|
481
|
-
function instanceAt(buffer, address, offset) {
|
|
497
|
+
function instanceAt(buffer, address, offset, owner) {
|
|
498
|
+
if (scalarSubclass) {
|
|
499
|
+
if (buffer && offset + baseSize <= buffer.length) return scalarView(baseType, buffer.subarray(offset, offset + baseSize));
|
|
500
|
+
const view = native.ptrToBuffer(address + BigInt(offset), baseSize);
|
|
501
|
+
shareMemory(view, owner, address + BigInt(offset) - owner.origin);
|
|
502
|
+
return scalarView(baseType, view);
|
|
503
|
+
}
|
|
482
504
|
if (buffer && offset + baseSize <= buffer.length) {
|
|
483
505
|
return baseType.from_buffer(buffer, offset);
|
|
484
506
|
}
|
|
485
|
-
|
|
507
|
+
const result = baseType.from_address(address + BigInt(offset));
|
|
508
|
+
shareMemory(result._buffer, owner, address + BigInt(offset) - owner.origin);
|
|
509
|
+
return result;
|
|
486
510
|
}
|
|
487
511
|
|
|
488
512
|
// Scrittura a un indirizzo senza Buffer associato: per le primitive il
|
|
@@ -490,24 +514,26 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
490
514
|
// lo copia all'indirizzo, invece di creare un Buffer esterno (~400 ns,
|
|
491
515
|
// vietato in Electron).
|
|
492
516
|
let scratch = null;
|
|
493
|
-
function writeAtAddress(addr, value) {
|
|
494
|
-
if (typeof baseType === "function" && baseType._isSimpleCData) {
|
|
517
|
+
function writeAtAddress(addr, value, owner) {
|
|
518
|
+
if (typeof baseType === "function" && baseType._isSimpleCData && !baseType._isPointerValued) {
|
|
495
519
|
scratch ??= alloc(baseSize);
|
|
496
520
|
baseType._writer(scratch, 0, value);
|
|
497
521
|
native.writeMemory(scratch, 0, baseSize, addr);
|
|
498
522
|
return;
|
|
499
523
|
}
|
|
500
524
|
const buf = native.ptrToBuffer(addr, baseSize);
|
|
525
|
+
shareMemory(buf, owner, addr - owner.origin);
|
|
501
526
|
writeValue(buf, baseType, value, 0);
|
|
502
527
|
}
|
|
503
528
|
|
|
504
529
|
// Lettura a un indirizzo senza Buffer associato: per le primitive
|
|
505
530
|
// readValue(indirizzo) evita il Buffer esterno di ptrToBuffer.
|
|
506
|
-
function readAtAddress(addr) {
|
|
531
|
+
function readAtAddress(addr, owner) {
|
|
507
532
|
if (typeof baseType === "function" && baseType._isSimpleCData) {
|
|
508
533
|
return readValue(addr, baseType, 0);
|
|
509
534
|
}
|
|
510
535
|
const buf = native.ptrToBuffer(addr, baseSize);
|
|
536
|
+
shareMemory(buf, owner, addr - owner.origin);
|
|
511
537
|
return readValue(buf, baseType, 0);
|
|
512
538
|
}
|
|
513
539
|
|
|
@@ -515,7 +541,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
515
541
|
* Creates a pointer instance with Python ctypes-compatible API.
|
|
516
542
|
* Supports .contents property and [index] access for pointer arithmetic.
|
|
517
543
|
*/
|
|
518
|
-
function createPointerInstance(addressOrBuffer, keep) {
|
|
544
|
+
function createPointerInstance(addressOrBuffer, keep, memory) {
|
|
519
545
|
// Risolvi il thunk lazy alla prima creazione di istanza: a questo
|
|
520
546
|
// punto la classe target è sicuramente inizializzata.
|
|
521
547
|
resolveIfLazy();
|
|
@@ -525,6 +551,9 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
525
551
|
// Python `_objects`: ciò che deve restare vivo finché il puntatore punta
|
|
526
552
|
// lì (la sorgente di cast(), il puntatore da cui deriva add()).
|
|
527
553
|
let _objects = keep;
|
|
554
|
+
let _memory = memory;
|
|
555
|
+
if (keep?._pointerType !== undefined) _objects = keep._buffer ?? keep._objects;
|
|
556
|
+
if (_memory === undefined && _objects?.[kPointerMemory]) _memory = _objects;
|
|
528
557
|
// Memoria del puntatore (Python: un puntatore è un oggetto ctypes di
|
|
529
558
|
// POINTER_SIZE byte), creata alla prima richiesta di byref(p) /
|
|
530
559
|
// addressof(p). Da allora è lei a dare l'indirizzo: una funzione C che ci
|
|
@@ -538,6 +567,8 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
538
567
|
_address = addr;
|
|
539
568
|
_buffer = null;
|
|
540
569
|
_objects = undefined;
|
|
570
|
+
_memory = undefined;
|
|
571
|
+
keepAlive(_slot, 0, null);
|
|
541
572
|
}
|
|
542
573
|
}
|
|
543
574
|
|
|
@@ -545,6 +576,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
545
576
|
if (_slot === null) return;
|
|
546
577
|
if (native.POINTER_SIZE === 8) _slot.writeBigUInt64LE(BigInt.asUintN(64, _address), 0);
|
|
547
578
|
else _slot.writeUInt32LE(Number(BigInt.asUintN(32, _address)), 0);
|
|
579
|
+
keepAlive(_slot, 0, _memory ?? _buffer ?? _objects);
|
|
548
580
|
}
|
|
549
581
|
|
|
550
582
|
if (addressOrBuffer === undefined || addressOrBuffer === null) {
|
|
@@ -564,11 +596,15 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
564
596
|
// vale il suo indirizzo originale, come un puntatore creato da indirizzo.
|
|
565
597
|
function pointTo(buf) {
|
|
566
598
|
_objects = undefined;
|
|
599
|
+
_memory = undefined;
|
|
567
600
|
const mirrored = mirrorAddress(buf);
|
|
568
601
|
if (mirrored !== undefined) {
|
|
569
602
|
_buffer = null;
|
|
570
603
|
_address = mirrored;
|
|
571
604
|
_objects = buf;
|
|
605
|
+
const domain = memoryDomain(buf);
|
|
606
|
+
_memory = { owner: buf, root: domain.root, origin: mirrored - domain.offset - BigInt(buf.byteOffset) };
|
|
607
|
+
_memory[kPointerMemory] = true;
|
|
572
608
|
} else {
|
|
573
609
|
_buffer = buf;
|
|
574
610
|
_address = getBufferAddress(buf);
|
|
@@ -576,6 +612,33 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
576
612
|
store();
|
|
577
613
|
}
|
|
578
614
|
|
|
615
|
+
function getMemory() {
|
|
616
|
+
if (_memory === undefined) {
|
|
617
|
+
let buffer = _buffer;
|
|
618
|
+
let address = _address;
|
|
619
|
+
if (buffer === null && _objects !== null && typeof _objects === "object") {
|
|
620
|
+
const known = _objects.constructor?._isPointerValued ? keptObject(_objects._buffer, 0) : _objects;
|
|
621
|
+
const candidate = Buffer.isBuffer(known) ? known : known?._buffer;
|
|
622
|
+
if (Buffer.isBuffer(candidate)) {
|
|
623
|
+
const base = getBufferAddress(candidate);
|
|
624
|
+
if (_address >= base && _address < base + BigInt(candidate.length)) {
|
|
625
|
+
buffer = candidate;
|
|
626
|
+
address = base;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
const domain = buffer === null ? { root: {}, offset: 0n } : memoryDomain(buffer);
|
|
631
|
+
_memory = {
|
|
632
|
+
owner: buffer ?? _objects,
|
|
633
|
+
root: domain.root,
|
|
634
|
+
origin: buffer === null ? 0n : address - domain.offset - BigInt(buffer.byteOffset),
|
|
635
|
+
};
|
|
636
|
+
_memory[kPointerMemory] = true;
|
|
637
|
+
if (_slot !== null) keepAlive(_slot, 0, _memory);
|
|
638
|
+
}
|
|
639
|
+
return _memory;
|
|
640
|
+
}
|
|
641
|
+
|
|
579
642
|
const instance = {
|
|
580
643
|
_pointerType: PointerType,
|
|
581
644
|
_baseType: baseType,
|
|
@@ -603,7 +666,8 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
603
666
|
|
|
604
667
|
/** Oggetti tenuti vivi dal puntatore (Python `_objects`). */
|
|
605
668
|
get _objects() {
|
|
606
|
-
|
|
669
|
+
sync();
|
|
670
|
+
return getMemory();
|
|
607
671
|
},
|
|
608
672
|
|
|
609
673
|
/**
|
|
@@ -618,16 +682,16 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
618
682
|
throw new Error("NULL pointer access");
|
|
619
683
|
}
|
|
620
684
|
if (isPointerBase()) {
|
|
621
|
-
return readPointerAt(_buffer, _address, 0);
|
|
685
|
+
return readPointerAt(_buffer, _address, 0, getMemory());
|
|
622
686
|
}
|
|
623
687
|
if (hasInstances()) {
|
|
624
|
-
return instanceAt(_buffer, _address, 0);
|
|
688
|
+
return instanceAt(_buffer, _address, 0, getMemory());
|
|
625
689
|
}
|
|
626
690
|
if (_buffer) {
|
|
627
691
|
// We have the actual buffer, read from it
|
|
628
692
|
return readValue(_buffer, baseType, 0);
|
|
629
693
|
}
|
|
630
|
-
return readAtAddress(_address);
|
|
694
|
+
return readAtAddress(_address, getMemory());
|
|
631
695
|
},
|
|
632
696
|
|
|
633
697
|
set contents(value) {
|
|
@@ -646,11 +710,11 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
646
710
|
throw new Error("NULL pointer access");
|
|
647
711
|
}
|
|
648
712
|
if (isPointerBase()) {
|
|
649
|
-
writePointerAt(_buffer, _address, 0, value);
|
|
713
|
+
writePointerAt(_buffer, _address, 0, value, getMemory());
|
|
650
714
|
} else if (_buffer) {
|
|
651
715
|
writeValue(_buffer, baseType, value, 0);
|
|
652
716
|
} else {
|
|
653
|
-
writeAtAddress(_address, value);
|
|
717
|
+
writeAtAddress(_address, value, getMemory());
|
|
654
718
|
}
|
|
655
719
|
},
|
|
656
720
|
|
|
@@ -679,6 +743,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
679
743
|
_address = BigInt(value);
|
|
680
744
|
_buffer = null;
|
|
681
745
|
_objects = undefined;
|
|
746
|
+
_memory = undefined;
|
|
682
747
|
store();
|
|
683
748
|
}
|
|
684
749
|
},
|
|
@@ -695,8 +760,10 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
695
760
|
* For compatibility - get pointer as buffer (8 bytes containing address)
|
|
696
761
|
*/
|
|
697
762
|
toBuffer() {
|
|
763
|
+
sync();
|
|
698
764
|
const buf = alloc(native.POINTER_SIZE);
|
|
699
765
|
writeValue(buf, c_void_p, _address);
|
|
766
|
+
keepAlive(buf, 0, _memory ?? _buffer ?? _objects);
|
|
700
767
|
return buf;
|
|
701
768
|
},
|
|
702
769
|
|
|
@@ -713,7 +780,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
713
780
|
sync();
|
|
714
781
|
const offsetBytes = BigInt(n) * BigInt(baseSize);
|
|
715
782
|
// Il nuovo puntatore tiene viva la memoria di questo (Python _objects).
|
|
716
|
-
return createPointerInstance(_address + offsetBytes, _buffer ?? _objects);
|
|
783
|
+
return createPointerInstance(_address + offsetBytes, _buffer ?? _objects, getMemory());
|
|
717
784
|
},
|
|
718
785
|
|
|
719
786
|
/**
|
|
@@ -741,10 +808,10 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
741
808
|
}
|
|
742
809
|
const offset = index * baseSize;
|
|
743
810
|
if (isPointerBase()) {
|
|
744
|
-
return readPointerAt(_buffer, _address, offset);
|
|
811
|
+
return readPointerAt(_buffer, _address, offset, getMemory());
|
|
745
812
|
}
|
|
746
813
|
if (isStructBase()) {
|
|
747
|
-
return instanceAt(_buffer, _address, offset);
|
|
814
|
+
return instanceAt(_buffer, _address, offset, getMemory());
|
|
748
815
|
}
|
|
749
816
|
if (_buffer && offset + baseSize <= _buffer.length) {
|
|
750
817
|
if (typeof baseType === "function" && baseType._isSimpleCData) {
|
|
@@ -752,7 +819,7 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
752
819
|
}
|
|
753
820
|
return readValue(_buffer, baseType, offset);
|
|
754
821
|
}
|
|
755
|
-
return readAtAddress(_address + BigInt(offset));
|
|
822
|
+
return readAtAddress(_address + BigInt(offset), getMemory());
|
|
756
823
|
}
|
|
757
824
|
return Reflect.get(target, prop, receiver);
|
|
758
825
|
},
|
|
@@ -766,14 +833,14 @@ export function POINTER(baseType, sizeof, alloc, readValue, writeValue, c_void_p
|
|
|
766
833
|
}
|
|
767
834
|
const offset = index * baseSize;
|
|
768
835
|
if (isPointerBase()) {
|
|
769
|
-
writePointerAt(_buffer, _address, offset, value);
|
|
836
|
+
writePointerAt(_buffer, _address, offset, value, getMemory());
|
|
770
837
|
return true;
|
|
771
838
|
}
|
|
772
839
|
if (_buffer && offset + baseSize <= _buffer.length) {
|
|
773
840
|
writeValue(_buffer, baseType, value, offset);
|
|
774
841
|
return true;
|
|
775
842
|
}
|
|
776
|
-
writeAtAddress(_address + BigInt(offset), value);
|
|
843
|
+
writeAtAddress(_address + BigInt(offset), value, getMemory());
|
|
777
844
|
return true;
|
|
778
845
|
}
|
|
779
846
|
return Reflect.set(target, prop, value, receiver);
|
|
@@ -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;
|
|
@@ -65,7 +65,8 @@
|
|
|
65
65
|
*/
|
|
66
66
|
|
|
67
67
|
import { kMirrorBase, mirrorBase, mirroredAccessors, mirrorsActive } from "../memory/mirror.js";
|
|
68
|
-
import { fieldsOf } from "./helpers/common.js";
|
|
68
|
+
import { copyInstance, fieldsOf, inDllBuffer, sourceBytes } from "./helpers/common.js";
|
|
69
|
+
import { normalizePack } from "./helpers/layout.js";
|
|
69
70
|
|
|
70
71
|
/**
|
|
71
72
|
* Creates the Structure base class with required dependencies injected.
|
|
@@ -81,18 +82,6 @@ function isPlainObject(value) {
|
|
|
81
82
|
return proto === Object.prototype || proto === null;
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
// Sorgente di from_buffer / from_buffer_copy (Python: ValueError per offset
|
|
85
|
-
// negativi e sorgenti troppo piccole): Buffer, TypedArray/DataView o istanza
|
|
86
|
-
// ctypes, come per gli array.
|
|
87
|
-
function sourceBytes(source, offset, size, name) {
|
|
88
|
-
const bytes = source !== null && typeof source === "object" && !ArrayBuffer.isView(source) && Buffer.isBuffer(source._buffer) ? source._buffer : source;
|
|
89
|
-
if (!ArrayBuffer.isView(bytes)) throw new TypeError(`${name}: source must be a Buffer, a typed array or a ctypes instance`);
|
|
90
|
-
if (!Number.isInteger(offset)) throw new TypeError(`${name}: offset must be an integer`);
|
|
91
|
-
if (offset < 0) throw new RangeError(`${name}: offset cannot be negative`);
|
|
92
|
-
if (bytes.byteLength - offset < size) throw new RangeError(`${name}: Buffer size too small (${bytes.byteLength} instead of at least ${size + offset} bytes)`);
|
|
93
|
-
return bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
85
|
export function createStructureClass(alloc, struct, bitfield, ptrToBuffer) {
|
|
97
86
|
// Buffer dell'istanza, letto dall'handler condiviso. Chiave privata e non
|
|
98
87
|
// `__buffer`, così riassegnare `__buffer` non cambia i campi (come prima).
|
|
@@ -427,7 +416,7 @@ export function createStructureClass(alloc, struct, bitfield, ptrToBuffer) {
|
|
|
427
416
|
const def = this._structDef || this._buildStruct();
|
|
428
417
|
const bytes = sourceBytes(source, offset, def.size, "from_buffer_copy");
|
|
429
418
|
const copy = Buffer.alloc(def.size);
|
|
430
|
-
copy.
|
|
419
|
+
copyInstance(copy, 0, def.size, bytes, offset, def);
|
|
431
420
|
return new this(copy);
|
|
432
421
|
}
|
|
433
422
|
|
|
@@ -456,15 +445,8 @@ export function createStructureClass(alloc, struct, bitfield, ptrToBuffer) {
|
|
|
456
445
|
* Python ctypes parity: `S.in_dll(library, name)`.
|
|
457
446
|
*/
|
|
458
447
|
static in_dll(library, name) {
|
|
459
|
-
const
|
|
460
|
-
|
|
461
|
-
throw new TypeError("in_dll: first arg must be a Library (CDLL/WinDLL)");
|
|
462
|
-
}
|
|
463
|
-
const addr = nativeLib.symbol(name);
|
|
464
|
-
if (!addr) {
|
|
465
|
-
throw new Error(`in_dll: symbol '${name}' not found`);
|
|
466
|
-
}
|
|
467
|
-
return this.from_address(addr);
|
|
448
|
+
const def = this._structDef || this._buildStruct();
|
|
449
|
+
return new this(inDllBuffer(library, name, def.size, ptrToBuffer));
|
|
468
450
|
}
|
|
469
451
|
|
|
470
452
|
// Build the underlying struct definition and cache it on the constructor
|
|
@@ -535,22 +517,9 @@ export function createStructureClass(alloc, struct, bitfield, ptrToBuffer) {
|
|
|
535
517
|
// `_pack_`, `_layout_`, `_align_` e byte order.
|
|
536
518
|
static _layoutOptions() {
|
|
537
519
|
const options = {};
|
|
538
|
-
if (this._pack_ !== undefined) {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
// `true` is tolerated as an alias for `_pack_ = 1`. Anything else is
|
|
542
|
-
// rejected so we don't silently produce a mislaid struct.
|
|
543
|
-
const p = this._pack_;
|
|
544
|
-
if (p === true) {
|
|
545
|
-
options.pack = 1;
|
|
546
|
-
} else if (p === false) {
|
|
547
|
-
// explicit false = no packing, same as absent
|
|
548
|
-
} else if (typeof p === "number" && Number.isInteger(p) && p >= 1 && (p & (p - 1)) === 0) {
|
|
549
|
-
options.pack = p;
|
|
550
|
-
} else {
|
|
551
|
-
throw new TypeError(`_pack_ must be a positive power-of-two integer, got ${p}`);
|
|
552
|
-
}
|
|
553
|
-
if (options.pack !== undefined) options.packed = true;
|
|
520
|
+
if (this._pack_ !== undefined && this._pack_ !== null) {
|
|
521
|
+
options.pack = normalizePack(this._pack_);
|
|
522
|
+
options.packed = options.pack !== 0;
|
|
554
523
|
}
|
|
555
524
|
// Python ctypes: "ms" (Windows, or with _pack_) or "gcc-sysv" bitfield layout.
|
|
556
525
|
if (this._layout_ !== undefined) options.layout = this._layout_;
|