node-ctypes 2.2.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 +504 -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 +136 -109
- package/lib/core/callback.js +17 -20
- package/lib/core/conversions.js +376 -16
- package/lib/core/funcptr.js +148 -73
- package/lib/core/lazy-loader.js +10 -4
- package/lib/core/types.js +201 -98
- package/lib/index.d.ts +276 -83
- package/lib/index.js +44 -32
- package/lib/memory/buffer.js +82 -22
- package/lib/memory/operations.js +55 -45
- package/lib/memory/pointer.js +233 -57
- package/lib/platform/constants.js +0 -1
- package/lib/platform/errors.js +48 -4
- package/lib/platform/find_library.js +78 -19
- package/lib/platform/win_types.js +8 -3
- package/lib/structures/Structure.js +252 -203
- package/lib/structures/Union.js +10 -21
- package/lib/structures/helpers/array.js +102 -16
- package/lib/structures/helpers/bitfield.js +75 -150
- package/lib/structures/helpers/common.js +310 -21
- package/lib/structures/helpers/layout.js +145 -0
- package/lib/structures/helpers/struct.js +160 -419
- package/lib/structures/helpers/union.js +154 -155
- package/lib/structures/helpers/wrap.js +3 -15
- package/lib/types/SimpleCData.js +11 -32
- package/lib/types/primitives.js +160 -113
- package/lib/utils/cache.js +34 -2
- package/package.json +4 -1
|
@@ -34,10 +34,11 @@
|
|
|
34
34
|
* ```
|
|
35
35
|
*/
|
|
36
36
|
|
|
37
|
-
import {
|
|
37
|
+
import { bitFieldAccessors } from "./bitfield.js";
|
|
38
|
+
import { computeLayout, isGccLayout, normalizePack } from "./layout.js";
|
|
38
39
|
import { buildWrapper } from "./wrap.js";
|
|
39
40
|
import { syncDefinition } from "../../memory/mirror.js";
|
|
40
|
-
import { copyInstance, funcPtrFieldType, writePointerField } from "./common.js";
|
|
41
|
+
import { clearKeepAlives, copyInstance, fieldEntries, fieldsOf, funcPtrFieldType, pointerOwner, scalarReader, simpleFieldWriter, writePointerField } from "./common.js";
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
44
|
* Creates a union type definition where all fields share offset 0.
|
|
@@ -89,7 +90,10 @@ import { copyInstance, funcPtrFieldType, writePointerField } from "./common.js";
|
|
|
89
90
|
* ```
|
|
90
91
|
*/
|
|
91
92
|
// Python: un campo POINTER(T) NULL è un puntatore falsy; in JS `null`.
|
|
92
|
-
const pointerOrNull = (ptrType, address) =>
|
|
93
|
+
const pointerOrNull = (ptrType, address, buf, offset) => {
|
|
94
|
+
const owner = pointerOwner(buf, offset, address);
|
|
95
|
+
return address === 0n ? null : ptrType.fromAddress(address, owner);
|
|
96
|
+
};
|
|
93
97
|
|
|
94
98
|
export function union(fields, options, sizeof, alloc, readValue, writeValue, _isStruct, _isArrayType, _isBitField, Structure, Union, native) {
|
|
95
99
|
// Backward-compat: legacy positional signature (fields, sizeof, alloc, ...)
|
|
@@ -109,39 +113,65 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
109
113
|
}
|
|
110
114
|
options = options || {};
|
|
111
115
|
const swapped = options.swapped === true;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
// Python _pack_: `pack` da solo, o `packed: true` (= pack 1).
|
|
117
|
+
const packValue = normalizePack(options.pack ?? (options.packed ? 1 : 0));
|
|
118
|
+
const gccLayout = isGccLayout(options.layout, packValue, options.pack != null || options.packed === true);
|
|
119
|
+
const base = options.base ?? null;
|
|
120
|
+
const fieldDefs = base ? base.fields.map((f) => ({ ...f })) : [];
|
|
121
|
+
const ownDefs = [];
|
|
122
|
+
|
|
123
|
+
const fieldNames = new Set(fieldDefs.map((f) => f.name));
|
|
124
|
+
for (const [name, type] of fieldEntries(fields)) {
|
|
125
|
+
if (fieldNames.has(name)) {
|
|
126
|
+
throw new TypeError(`Duplicate field name: ${name}`);
|
|
127
|
+
}
|
|
128
|
+
fieldNames.add(name);
|
|
117
129
|
let size, alignment;
|
|
118
|
-
let fieldDef = { name, type
|
|
119
|
-
|
|
130
|
+
let fieldDef = { name, type };
|
|
131
|
+
|
|
132
|
+
// Campo di _anonymous_: i suoi campi sono raggiungibili dalla union.
|
|
133
|
+
if (typeof type === "object" && type !== null && type.anonymous === true && type.type) {
|
|
134
|
+
let def = type.type;
|
|
135
|
+
fieldDef.cls = type.cls;
|
|
136
|
+
if (typeof def === "function" && typeof def._buildStruct === "function") {
|
|
137
|
+
fieldDef.cls = def;
|
|
138
|
+
def = def._structDef || def._buildStruct();
|
|
139
|
+
}
|
|
140
|
+
if (def === null || typeof def !== "object" || !Array.isArray(def.fields)) {
|
|
141
|
+
throw new TypeError(`anonymous field "${name}" must be a structure or union`);
|
|
142
|
+
}
|
|
143
|
+
fieldDef.type = def;
|
|
144
|
+
fieldDef.isNested = true;
|
|
145
|
+
fieldDef.isAnonymous = true;
|
|
146
|
+
size = def.size;
|
|
147
|
+
alignment = def.alignment;
|
|
148
|
+
}
|
|
120
149
|
// Nested struct (object form or class form)
|
|
121
|
-
if (_isStruct(type)) {
|
|
150
|
+
else if (_isStruct(type)) {
|
|
122
151
|
// Normalize type: if it's a Structure/Union class, get its _structDef
|
|
123
152
|
let nestedStruct = type;
|
|
124
153
|
if (typeof type === "function" && typeof type._buildStruct === "function") {
|
|
125
154
|
nestedStruct = type._structDef || type._buildStruct();
|
|
126
155
|
fieldDef.type = nestedStruct;
|
|
156
|
+
// Python restituisce un'istanza della classe annidata.
|
|
157
|
+
fieldDef.cls = type;
|
|
127
158
|
}
|
|
128
159
|
size = nestedStruct.size;
|
|
129
160
|
alignment = nestedStruct.alignment;
|
|
130
161
|
fieldDef.isNested = true;
|
|
131
162
|
}
|
|
132
|
-
// Array type
|
|
163
|
+
// Array type: alignment == element alignment (C standard)
|
|
133
164
|
else if (_isArrayType(type)) {
|
|
165
|
+
if (swapped) fieldDef.type = type._swappedType ?? type;
|
|
134
166
|
size = type.getSize();
|
|
135
|
-
|
|
136
|
-
alignment = Math.min(elemSize, native.POINTER_SIZE);
|
|
167
|
+
alignment = typeof type.getAlignment === "function" ? type.getAlignment() : Math.min(sizeof(type.elementType), native.POINTER_SIZE);
|
|
137
168
|
fieldDef.isArray = true;
|
|
138
169
|
}
|
|
139
|
-
// Bit field
|
|
170
|
+
// Bit field
|
|
140
171
|
else if (_isBitField(type)) {
|
|
141
172
|
size = type.baseSize;
|
|
142
173
|
alignment = Math.min(size, native.POINTER_SIZE);
|
|
143
174
|
fieldDef.isBitField = true;
|
|
144
|
-
fieldDef.bitOffset = 0;
|
|
145
175
|
fieldDef.bitSize = type.bits;
|
|
146
176
|
fieldDef.baseSize = type.baseSize;
|
|
147
177
|
fieldDef.type = type.baseType; // Usa il tipo base per lettura/scrittura
|
|
@@ -149,7 +179,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
149
179
|
// POINTER() type
|
|
150
180
|
else if ((typeof type === "object" && type !== null && type._pointerTo !== undefined) || (typeof type === "function" && type._isFuncPtr === true)) {
|
|
151
181
|
size = native.POINTER_SIZE;
|
|
152
|
-
alignment =
|
|
182
|
+
alignment = native.POINTER_SIZE;
|
|
153
183
|
// CFUNCTYPE: campo puntatore letto come funzione chiamabile (Python parity)
|
|
154
184
|
fieldDef.type = typeof type === "function" ? funcPtrFieldType(type) : type;
|
|
155
185
|
fieldDef.isPointer = true;
|
|
@@ -167,20 +197,25 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
167
197
|
|
|
168
198
|
fieldDef.size = size;
|
|
169
199
|
fieldDef.alignment = alignment;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (size > maxSize) {
|
|
173
|
-
maxSize = size;
|
|
174
|
-
}
|
|
175
|
-
if (alignment > maxAlignment) {
|
|
176
|
-
maxAlignment = alignment;
|
|
177
|
-
}
|
|
200
|
+
ownDefs.push(fieldDef);
|
|
178
201
|
}
|
|
179
202
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
203
|
+
const layout = computeLayout(
|
|
204
|
+
ownDefs.map((f) => (f.isBitField ? { name: f.name, size: f.baseSize, align: f.alignment, bitSize: f.bitSize } : { name: f.name, size: f.size, align: f.alignment })),
|
|
205
|
+
{ isStruct: false, gccLayout, pack: packValue, align: options.align ?? 1, base, bigEndian: swapped },
|
|
206
|
+
);
|
|
207
|
+
ownDefs.forEach((f, i) => {
|
|
208
|
+
f.offset = layout.offsets[i].offset;
|
|
209
|
+
if (f.isBitField) f.bitOffset = layout.offsets[i].bitOffset;
|
|
210
|
+
// Allineamento del tipo in C, prima di _pack_ (usato per le struct native
|
|
211
|
+
// passate per valore, lib/core/types.js).
|
|
212
|
+
f.naturalAlignment = f.alignment;
|
|
213
|
+
if (packValue) f.alignment = Math.min(f.alignment, packValue);
|
|
214
|
+
f.swapped = swapped;
|
|
215
|
+
fieldDefs.push(f);
|
|
216
|
+
});
|
|
217
|
+
const maxSize = layout.size;
|
|
218
|
+
const maxAlignment = layout.alignment;
|
|
184
219
|
|
|
185
220
|
// Crea Map per lookup O(1)
|
|
186
221
|
const fieldMap = new Map();
|
|
@@ -194,6 +229,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
194
229
|
// generico.
|
|
195
230
|
const fieldReaders = Object.create(null);
|
|
196
231
|
const fieldWriters = Object.create(null);
|
|
232
|
+
const mayKeepObjects = fieldDefs.some((field) => field.isPointer || field.type?._isPointerValued || field.isNested || field.isArray);
|
|
197
233
|
// Risolvi POINTER_SIZE una sola volta (costante di processo).
|
|
198
234
|
const is64 = native.POINTER_SIZE === 8;
|
|
199
235
|
for (const field of fieldDefs) {
|
|
@@ -203,96 +239,30 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
203
239
|
|
|
204
240
|
// Bitfield precompilato (parity con struct.js).
|
|
205
241
|
if (field.isBitField) {
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
fieldReaders[name] = (buf) => {
|
|
220
|
-
const raw = (buf.readUInt8(fieldOffset) >> bitOffset) & mask;
|
|
221
|
-
return (raw & signBit) ? (raw | extendMask) : raw;
|
|
222
|
-
};
|
|
223
|
-
} else {
|
|
224
|
-
fieldReaders[name] = (buf) => (buf.readUInt8(fieldOffset) >> bitOffset) & mask;
|
|
225
|
-
}
|
|
226
|
-
fieldWriters[name] = (buf, val) => {
|
|
227
|
-
const v = buf.readUInt8(fieldOffset);
|
|
228
|
-
buf.writeUInt8(((v & clearMask) | ((val & mask) << bitOffset)) & 0xff, fieldOffset);
|
|
229
|
-
};
|
|
230
|
-
} else if (baseSize === 2) {
|
|
231
|
-
const mask = (1 << bitSize) - 1;
|
|
232
|
-
const clearMask = ~(mask << bitOffset);
|
|
233
|
-
if (isSigned) {
|
|
234
|
-
const signBit = 1 << (bitSize - 1);
|
|
235
|
-
const extendMask = ~mask;
|
|
236
|
-
fieldReaders[name] = (buf) => {
|
|
237
|
-
const raw = (buf.readUInt16LE(fieldOffset) >> bitOffset) & mask;
|
|
238
|
-
return (raw & signBit) ? (raw | extendMask) : raw;
|
|
239
|
-
};
|
|
240
|
-
} else {
|
|
241
|
-
fieldReaders[name] = (buf) => (buf.readUInt16LE(fieldOffset) >> bitOffset) & mask;
|
|
242
|
-
}
|
|
243
|
-
fieldWriters[name] = (buf, val) => {
|
|
244
|
-
const v = buf.readUInt16LE(fieldOffset);
|
|
245
|
-
buf.writeUInt16LE(((v & clearMask) | ((val & mask) << bitOffset)) & 0xffff, fieldOffset);
|
|
246
|
-
};
|
|
247
|
-
} else if (baseSize === 4) {
|
|
248
|
-
const mask = (1 << bitSize) - 1;
|
|
249
|
-
const clearMask = ~(mask << bitOffset);
|
|
250
|
-
if (isSigned) {
|
|
251
|
-
const signBit = 1 << (bitSize - 1);
|
|
252
|
-
const extendMask = ~mask;
|
|
253
|
-
fieldReaders[name] = (buf) => {
|
|
254
|
-
const raw = (buf.readUInt32LE(fieldOffset) >>> bitOffset) & mask;
|
|
255
|
-
return (raw & signBit) ? (raw | extendMask) : raw;
|
|
256
|
-
};
|
|
257
|
-
} else {
|
|
258
|
-
fieldReaders[name] = (buf) => (buf.readUInt32LE(fieldOffset) >>> bitOffset) & mask;
|
|
259
|
-
}
|
|
260
|
-
fieldWriters[name] = (buf, val) => {
|
|
261
|
-
const v = buf.readUInt32LE(fieldOffset);
|
|
262
|
-
buf.writeUInt32LE(((v & clearMask) | ((val & mask) << bitOffset)) >>> 0, fieldOffset);
|
|
263
|
-
};
|
|
264
|
-
} else if (baseSize === 8) {
|
|
265
|
-
const bMask = (1n << BigInt(bitSize)) - 1n;
|
|
266
|
-
const bBitOffset = BigInt(bitOffset);
|
|
267
|
-
const bClearMask = ~(bMask << bBitOffset);
|
|
268
|
-
if (isSigned) {
|
|
269
|
-
const bSignBit = 1n << BigInt(bitSize - 1);
|
|
270
|
-
const bExtend = ~bMask;
|
|
271
|
-
fieldReaders[name] = (buf) => {
|
|
272
|
-
const raw = (buf.readBigUInt64LE(fieldOffset) >> bBitOffset) & bMask;
|
|
273
|
-
const signed = (raw & bSignBit) ? (raw | bExtend) : raw;
|
|
274
|
-
const asNum = Number(signed);
|
|
275
|
-
return (asNum >= Number.MIN_SAFE_INTEGER && asNum <= Number.MAX_SAFE_INTEGER) ? asNum : signed;
|
|
276
|
-
};
|
|
277
|
-
} else {
|
|
278
|
-
fieldReaders[name] = (buf) => Number((buf.readBigUInt64LE(fieldOffset) >> bBitOffset) & bMask);
|
|
279
|
-
}
|
|
280
|
-
fieldWriters[name] = (buf, val) => {
|
|
281
|
-
const v = buf.readBigUInt64LE(fieldOffset);
|
|
282
|
-
buf.writeBigUInt64LE((v & bClearMask) | ((BigInt(val) & bMask) << bBitOffset), fieldOffset);
|
|
283
|
-
};
|
|
284
|
-
}
|
|
242
|
+
const { read, write } = bitFieldAccessors({
|
|
243
|
+
offset,
|
|
244
|
+
baseSize: field.baseSize,
|
|
245
|
+
bitOffset: field.bitOffset,
|
|
246
|
+
bitSize: field.bitSize,
|
|
247
|
+
signed: field.type?._signed === true,
|
|
248
|
+
bigEndian: field.swapped === true,
|
|
249
|
+
boolean: field.type?._isBoolean === true,
|
|
250
|
+
});
|
|
251
|
+
field.readBits = read;
|
|
252
|
+
field.writeBits = simpleFieldWriter(field.type, (buf, offset, value) => write(buf, value), 0);
|
|
253
|
+
fieldReaders[name] = read;
|
|
254
|
+
fieldWriters[name] = field.writeBits;
|
|
285
255
|
continue;
|
|
286
256
|
}
|
|
287
257
|
if (field.isPointer) {
|
|
288
258
|
const ptrType = field.type;
|
|
289
259
|
if (is64) {
|
|
290
|
-
fieldReaders[name] = (buf) => pointerOrNull(ptrType, buf.readBigUInt64LE(offset));
|
|
260
|
+
fieldReaders[name] = (buf) => pointerOrNull(ptrType, buf.readBigUInt64LE(offset), buf, offset);
|
|
291
261
|
fieldWriters[name] = (buf, val) => {
|
|
292
262
|
writePointerField(buf, offset, val, 8);
|
|
293
263
|
};
|
|
294
264
|
} else {
|
|
295
|
-
fieldReaders[name] = (buf) => pointerOrNull(ptrType, BigInt(buf.readUInt32LE(offset)));
|
|
265
|
+
fieldReaders[name] = (buf) => pointerOrNull(ptrType, BigInt(buf.readUInt32LE(offset)), buf, offset);
|
|
296
266
|
fieldWriters[name] = (buf, val) => {
|
|
297
267
|
writePointerField(buf, offset, val, 4);
|
|
298
268
|
};
|
|
@@ -302,15 +272,41 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
302
272
|
|
|
303
273
|
const fieldType = field.type;
|
|
304
274
|
if (fieldType && fieldType._isSimpleCData) {
|
|
305
|
-
const reader = swapped && fieldType._readerBE ? fieldType._readerBE : fieldType._reader;
|
|
306
|
-
const writer = swapped && fieldType._writerBE ? fieldType._writerBE : fieldType._writer;
|
|
275
|
+
const reader = scalarReader(fieldType, field.swapped && fieldType._readerBE ? fieldType._readerBE : fieldType._reader, field.swapped);
|
|
276
|
+
const writer = field.swapped && fieldType._writerBE ? fieldType._writerBE : fieldType._writer;
|
|
307
277
|
if (reader && writer) {
|
|
308
278
|
fieldReaders[name] = (buf) => reader(buf, offset);
|
|
309
|
-
|
|
279
|
+
const write = simpleFieldWriter(fieldType, writer, offset);
|
|
280
|
+
fieldWriters[name] =
|
|
281
|
+
mayKeepObjects && !fieldType._isPointerValued
|
|
282
|
+
? (buf, value) => {
|
|
283
|
+
write(buf, value);
|
|
284
|
+
clearKeepAlives(buf, offset, fieldType._size);
|
|
285
|
+
}
|
|
286
|
+
: write;
|
|
310
287
|
}
|
|
311
288
|
}
|
|
312
289
|
}
|
|
313
290
|
|
|
291
|
+
// Campi raggiungibili attraverso i campi di _anonymous_ (anche annidati).
|
|
292
|
+
const anonymousFieldNames = Object.create(null);
|
|
293
|
+
const anonymousOwner = new Map();
|
|
294
|
+
for (const field of fieldDefs) {
|
|
295
|
+
if (!field.isAnonymous) continue;
|
|
296
|
+
for (const sub of field.type.fields) {
|
|
297
|
+
anonymousFieldNames[sub.name] = true;
|
|
298
|
+
anonymousOwner.set(sub.name, field);
|
|
299
|
+
}
|
|
300
|
+
for (const name of Object.keys(field.type.anonymousFieldNames ?? {})) {
|
|
301
|
+
anonymousFieldNames[name] = true;
|
|
302
|
+
anonymousOwner.set(name, field);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const anonymousView = (buf, fieldName) => {
|
|
306
|
+
const owner = anonymousOwner.get(fieldName);
|
|
307
|
+
return owner === undefined ? undefined : { def: owner.type, buf: buf.subarray(owner.offset, owner.offset + owner.size) };
|
|
308
|
+
};
|
|
309
|
+
|
|
314
310
|
const unionDef = {
|
|
315
311
|
size: maxSize,
|
|
316
312
|
alignment: maxAlignment,
|
|
@@ -318,7 +314,10 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
318
314
|
fieldMap,
|
|
319
315
|
fieldReaders,
|
|
320
316
|
fieldWriters,
|
|
317
|
+
anonymousFieldNames,
|
|
321
318
|
isUnion: true,
|
|
319
|
+
swapped,
|
|
320
|
+
base, // definizione della union base (i suoi campi sono i primi di `fields`)
|
|
322
321
|
_isStructType: true, // Per compatibilità con _isStruct()
|
|
323
322
|
|
|
324
323
|
create(values = {}) {
|
|
@@ -332,12 +331,14 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
332
331
|
}
|
|
333
332
|
}
|
|
334
333
|
|
|
335
|
-
//
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
334
|
+
// Come Python, i campi di _anonymous_ sono raggiungibili dall'istanza.
|
|
335
|
+
return new Proxy(
|
|
336
|
+
buf,
|
|
337
|
+
buildWrapper(unionDef, {
|
|
338
|
+
instanceTag: "UnionInstance",
|
|
339
|
+
exposeAnonymous: true,
|
|
340
|
+
}),
|
|
341
|
+
);
|
|
341
342
|
},
|
|
342
343
|
|
|
343
344
|
get(bufOrObj, fieldName) {
|
|
@@ -354,16 +355,22 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
354
355
|
|
|
355
356
|
// O(1) lookup con Map
|
|
356
357
|
const field = fieldMap.get(fieldName);
|
|
357
|
-
if (!field)
|
|
358
|
+
if (!field) {
|
|
359
|
+
const view = anonymousView(buf, fieldName);
|
|
360
|
+
if (view === undefined) throw new Error(`Unknown field: ${fieldName}`);
|
|
361
|
+
return view.def.get(view.buf, fieldName);
|
|
362
|
+
}
|
|
358
363
|
|
|
359
364
|
// Bit field
|
|
360
365
|
if (field.isBitField) {
|
|
361
|
-
return
|
|
366
|
+
return field.readBits(buf);
|
|
362
367
|
}
|
|
363
368
|
|
|
364
369
|
// Nested struct
|
|
365
370
|
if (field.isNested) {
|
|
366
371
|
const nestedBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
372
|
+
// Classe Structure/Union: un'istanza sulla stessa memoria (Python).
|
|
373
|
+
if (field.cls !== undefined) return new field.cls(nestedBuf);
|
|
367
374
|
// Proxy-based nested instance
|
|
368
375
|
return new Proxy(nestedBuf, {
|
|
369
376
|
get(target, prop, receiver) {
|
|
@@ -434,14 +441,11 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
434
441
|
// POINTER() field — return PointerInstance
|
|
435
442
|
if (field.isPointer) {
|
|
436
443
|
const addr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(field.offset) : BigInt(buf.readUInt32LE(field.offset));
|
|
437
|
-
return pointerOrNull(field.type, addr);
|
|
444
|
+
return pointerOrNull(field.type, addr, buf, field.offset);
|
|
438
445
|
}
|
|
439
446
|
|
|
440
|
-
// Tipo base
|
|
441
|
-
|
|
442
|
-
return field.type._readerBE(buf, field.offset);
|
|
443
|
-
}
|
|
444
|
-
return readValue(buf, field.type, field.offset);
|
|
447
|
+
// Tipo base: stesso accessor del fast path (rispetta l'endianness)
|
|
448
|
+
return fieldReaders[field.name](buf);
|
|
445
449
|
},
|
|
446
450
|
|
|
447
451
|
set(bufOrObj, fieldName, value) {
|
|
@@ -459,21 +463,24 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
459
463
|
|
|
460
464
|
// Lookup with Map
|
|
461
465
|
const field = fieldMap.get(fieldName);
|
|
462
|
-
if (!field)
|
|
466
|
+
if (!field) {
|
|
467
|
+
const view = anonymousView(buf, fieldName);
|
|
468
|
+
if (view === undefined) throw new Error(`Unknown field: ${fieldName}`);
|
|
469
|
+
view.def.set(view.buf, fieldName, value);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
463
472
|
|
|
464
473
|
// Bit field
|
|
465
474
|
if (field.isBitField) {
|
|
466
|
-
|
|
475
|
+
field.writeBits(buf, value);
|
|
467
476
|
return;
|
|
468
477
|
}
|
|
469
478
|
|
|
470
479
|
// Nested struct
|
|
471
480
|
if (field.isNested) {
|
|
472
|
-
if (Buffer.isBuffer(value)) {
|
|
473
|
-
value.copy(buf, field.offset, 0, field.size);
|
|
474
|
-
} else if (value !== null && typeof value === "object" && Buffer.isBuffer(value._buffer)) {
|
|
481
|
+
if (Buffer.isBuffer(value) || (value !== null && typeof value === "object" && Buffer.isBuffer(value._buffer))) {
|
|
475
482
|
// Istanza struct/union: copia dei byte (Python), non campo per campo
|
|
476
|
-
copyInstance(buf, field.offset, field.size, value);
|
|
483
|
+
copyInstance(buf, field.offset, field.size, value, 0, field.type);
|
|
477
484
|
} else if (typeof value === "object") {
|
|
478
485
|
const nestedBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
479
486
|
for (const [k, v] of Object.entries(value)) {
|
|
@@ -485,18 +492,16 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
485
492
|
|
|
486
493
|
// Array
|
|
487
494
|
if (field.isArray) {
|
|
488
|
-
|
|
495
|
+
// Python: bytes / str con il controllo di lunghezza ("bytes too long").
|
|
496
|
+
if (field.type._charKind !== undefined && (typeof value === "string" || (field.type._charKind === "char" && Buffer.isBuffer(value) && value._buffer === undefined))) {
|
|
489
497
|
field.type.setValue(buf.subarray(field.offset, field.offset + field.size), value, (len, max) => `${field.type._charKind === "char" ? "bytes" : "string"} too long (${len}, maximum length ${max})`);
|
|
490
|
-
} else if (Buffer.isBuffer(value)) {
|
|
491
|
-
|
|
498
|
+
} else if (Buffer.isBuffer(value) || Buffer.isBuffer(value?._buffer)) {
|
|
499
|
+
copyInstance(buf, field.offset, field.size, value, 0, field.type);
|
|
492
500
|
} else if (Array.isArray(value)) {
|
|
493
501
|
const wrapped = field.type.wrap(buf.subarray(field.offset, field.offset + field.size));
|
|
494
502
|
for (let i = 0; i < Math.min(value.length, field.type.length); i++) {
|
|
495
503
|
wrapped[i] = value[i];
|
|
496
504
|
}
|
|
497
|
-
} else if (value && value._buffer && Buffer.isBuffer(value._buffer)) {
|
|
498
|
-
// Handle array proxy instances
|
|
499
|
-
value._buffer.copy(buf, field.offset, 0, field.size);
|
|
500
505
|
}
|
|
501
506
|
return;
|
|
502
507
|
}
|
|
@@ -507,12 +512,8 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
507
512
|
return;
|
|
508
513
|
}
|
|
509
514
|
|
|
510
|
-
// Tipo base
|
|
511
|
-
|
|
512
|
-
field.type._writerBE(buf, field.offset, value);
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
writeValue(buf, field.type, value, field.offset);
|
|
515
|
+
// Tipo base: stesso accessor del fast path (rispetta l'endianness)
|
|
516
|
+
fieldWriters[field.name](buf, value);
|
|
516
517
|
},
|
|
517
518
|
|
|
518
519
|
toObject(bufOrObj) {
|
|
@@ -552,7 +553,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
552
553
|
Object.defineProperty(obj, field.name, {
|
|
553
554
|
get() {
|
|
554
555
|
if (field.isBitField) {
|
|
555
|
-
return
|
|
556
|
+
return field.readBits(buf);
|
|
556
557
|
} else if (field.isNested) {
|
|
557
558
|
// Cache nested objects per evitare di ricrearli ogni volta
|
|
558
559
|
if (!nestedCache.has(field.name)) {
|
|
@@ -567,22 +568,20 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
567
568
|
return [...wrapped];
|
|
568
569
|
} else if (field.isPointer) {
|
|
569
570
|
const addr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(0) : BigInt(buf.readUInt32LE(0));
|
|
570
|
-
return pointerOrNull(field.type, addr);
|
|
571
|
-
} else if (swapped && typeof field.type === "function" && field.type._isSimpleCData && field.type._readerBE) {
|
|
572
|
-
return field.type._readerBE(buf, 0);
|
|
571
|
+
return pointerOrNull(field.type, addr, buf, 0);
|
|
573
572
|
} else {
|
|
574
|
-
return
|
|
573
|
+
return fieldReaders[field.name](buf);
|
|
575
574
|
}
|
|
576
575
|
},
|
|
577
576
|
set(value) {
|
|
578
577
|
if (field.isBitField) {
|
|
579
|
-
|
|
578
|
+
field.writeBits(buf, value);
|
|
579
|
+
} else if (field.isArray) {
|
|
580
|
+
unionDef.set(buf, field.name, value);
|
|
580
581
|
} else if (field.isPointer) {
|
|
581
582
|
writePointerField(buf, 0, value, native.POINTER_SIZE);
|
|
582
|
-
} else if (swapped && typeof field.type === "function" && field.type._isSimpleCData && field.type._writerBE) {
|
|
583
|
-
field.type._writerBE(buf, 0, value);
|
|
584
583
|
} else {
|
|
585
|
-
|
|
584
|
+
fieldWriters[field.name](buf, value);
|
|
586
585
|
}
|
|
587
586
|
},
|
|
588
587
|
enumerable: true,
|
|
@@ -595,7 +594,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
595
594
|
|
|
596
595
|
// fromObject: write plain object values into buffer
|
|
597
596
|
fromObject: function (buf, obj) {
|
|
598
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
597
|
+
for (const [key, value] of Object.entries(fieldsOf(obj))) {
|
|
599
598
|
if (this.fields.some((f) => f.name === key)) {
|
|
600
599
|
this.set(buf, key, value);
|
|
601
600
|
}
|
|
@@ -22,13 +22,7 @@ import { mirroredAccessors } from "../../memory/mirror.js";
|
|
|
22
22
|
|
|
23
23
|
// Proprietà meta del Buffer che NON vanno mai interpretate come field name.
|
|
24
24
|
// Per i campi Proxy passano direttamente attraverso al target buffer.
|
|
25
|
-
const BUFFER_META_PROPS = new Set([
|
|
26
|
-
"length",
|
|
27
|
-
"byteLength",
|
|
28
|
-
"byteOffset",
|
|
29
|
-
"buffer",
|
|
30
|
-
"BYTES_PER_ELEMENT",
|
|
31
|
-
]);
|
|
25
|
+
const BUFFER_META_PROPS = new Set(["length", "byteLength", "byteOffset", "buffer", "BYTES_PER_ELEMENT"]);
|
|
32
26
|
|
|
33
27
|
// L'handler dipende solo da def e opzioni (il Buffer è il target): uno per
|
|
34
28
|
// combinazione, invece di ricrearlo a ogni accesso a un campo annidato.
|
|
@@ -104,10 +98,7 @@ function createWrapper(def, instanceTag, exposeAnonymous, mirrored) {
|
|
|
104
98
|
if (r !== undefined) return r(target);
|
|
105
99
|
}
|
|
106
100
|
// FALLBACK: tipi complessi (nested/array) e anonymous subfields.
|
|
107
|
-
if (
|
|
108
|
-
fieldMap?.has(prop) ||
|
|
109
|
-
(exposeAnonymous && anonymousFieldNames?.[prop] === true)
|
|
110
|
-
) {
|
|
101
|
+
if (fieldMap?.has(prop) || (exposeAnonymous && anonymousFieldNames?.[prop] === true)) {
|
|
111
102
|
return def.get(target, prop);
|
|
112
103
|
}
|
|
113
104
|
}
|
|
@@ -126,10 +117,7 @@ function createWrapper(def, instanceTag, exposeAnonymous, mirrored) {
|
|
|
126
117
|
return true;
|
|
127
118
|
}
|
|
128
119
|
}
|
|
129
|
-
if (
|
|
130
|
-
fieldMap?.has(prop) ||
|
|
131
|
-
(exposeAnonymous && anonymousFieldNames?.[prop] === true)
|
|
132
|
-
) {
|
|
120
|
+
if (fieldMap?.has(prop) || (exposeAnonymous && anonymousFieldNames?.[prop] === true)) {
|
|
133
121
|
def.set(target, prop, value);
|
|
134
122
|
return true;
|
|
135
123
|
}
|
package/lib/types/SimpleCData.js
CHANGED
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
48
|
|
|
49
|
-
import {
|
|
49
|
+
import { copyInstance, inDllBuffer, scalarOutparam, scalarView, sourceBytes } from "../structures/helpers/common.js";
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* Creates the SimpleCData base class with required dependencies injected.
|
|
@@ -56,16 +56,6 @@ import { kMirrorBase, mirrorBase, mirroredPrototype, mirrorsActive } from "../me
|
|
|
56
56
|
* @private
|
|
57
57
|
*/
|
|
58
58
|
export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
59
|
-
// Istanza su un Buffer esistente. Su una copia di memoria nativa (Electron,
|
|
60
|
-
// memory/mirror.js) `value` resta sincronizzato con la memoria.
|
|
61
|
-
function withBuffer(Ctor, buf) {
|
|
62
|
-
const base = mirrorsActive ? mirrorBase(buf) : undefined;
|
|
63
|
-
const inst = Object.create(base === undefined ? Ctor.prototype : mirroredPrototype(Ctor));
|
|
64
|
-
inst._buffer = buf;
|
|
65
|
-
if (base !== undefined) inst[kMirrorBase] = base;
|
|
66
|
-
return inst;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
59
|
/**
|
|
70
60
|
* SimpleCData base class for primitive C types.
|
|
71
61
|
*
|
|
@@ -119,7 +109,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
119
109
|
constructor(value) {
|
|
120
110
|
const Ctor = this.constructor;
|
|
121
111
|
this._buffer = alloc(Ctor._size);
|
|
122
|
-
if (value !== undefined
|
|
112
|
+
if (value !== undefined) {
|
|
123
113
|
this.value = value;
|
|
124
114
|
}
|
|
125
115
|
}
|
|
@@ -155,10 +145,8 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
155
145
|
* @returns {SimpleCData} New instance wrapping the buffer slice
|
|
156
146
|
*/
|
|
157
147
|
static from_buffer(buffer, offset = 0) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
return withBuffer(this, buffer.subarray(offset, offset + this._size));
|
|
148
|
+
const bytes = sourceBytes(buffer, offset, this._size, "from_buffer");
|
|
149
|
+
return scalarView(this, Buffer.isBuffer(bytes) ? bytes.subarray(offset, offset + this._size) : Buffer.from(bytes.buffer, bytes.byteOffset + offset, this._size));
|
|
162
150
|
}
|
|
163
151
|
|
|
164
152
|
/**
|
|
@@ -168,12 +156,10 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
168
156
|
* @returns {SimpleCData} New instance with copied data
|
|
169
157
|
*/
|
|
170
158
|
static from_buffer_copy(buffer, offset = 0) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
buffer.copy(inst._buffer, 0, offset, offset + this._size);
|
|
176
|
-
return inst;
|
|
159
|
+
const bytes = sourceBytes(buffer, offset, this._size, "from_buffer_copy");
|
|
160
|
+
const copy = alloc(this._size);
|
|
161
|
+
copyInstance(copy, 0, this._size, bytes, offset, this);
|
|
162
|
+
return scalarView(this, copy);
|
|
177
163
|
}
|
|
178
164
|
|
|
179
165
|
/**
|
|
@@ -188,7 +174,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
188
174
|
throw new Error("from_address requires ptrToBuffer injected at construction");
|
|
189
175
|
}
|
|
190
176
|
const addr = typeof address === "bigint" ? address : BigInt(address);
|
|
191
|
-
return
|
|
177
|
+
return scalarView(this, ptrToBuffer(addr, this._size));
|
|
192
178
|
}
|
|
193
179
|
|
|
194
180
|
/**
|
|
@@ -206,15 +192,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
206
192
|
* @returns {SimpleCData} Instance whose buffer aliases the symbol's memory
|
|
207
193
|
*/
|
|
208
194
|
static in_dll(library, name) {
|
|
209
|
-
|
|
210
|
-
if (!nativeLib || typeof nativeLib.symbol !== "function") {
|
|
211
|
-
throw new TypeError("in_dll: first arg must be a Library (CDLL/WinDLL)");
|
|
212
|
-
}
|
|
213
|
-
const addr = nativeLib.symbol(name);
|
|
214
|
-
if (!addr) {
|
|
215
|
-
throw new Error(`in_dll: symbol '${name}' not found`);
|
|
216
|
-
}
|
|
217
|
-
return this.from_address(addr);
|
|
195
|
+
return scalarView(this, inDllBuffer(library, name, this._size, ptrToBuffer));
|
|
218
196
|
}
|
|
219
197
|
|
|
220
198
|
/**
|
|
@@ -239,6 +217,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
239
217
|
}
|
|
240
218
|
|
|
241
219
|
SimpleCData._isSimpleCData = true;
|
|
220
|
+
Object.defineProperty(SimpleCData.prototype, "__ctypes_from_outparam__", { value: scalarOutparam, writable: true, configurable: true });
|
|
242
221
|
|
|
243
222
|
// Marcatore per riconoscere le istanze (es. negli argomenti di funzione) con
|
|
244
223
|
// un semplice accesso a proprietà, senza instanceof né `constructor`.
|