node-ctypes 2.1.1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -13
- 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 +86 -76
- package/lib/core/callback.js +10 -17
- package/lib/core/conversions.js +193 -3
- package/lib/core/funcptr.js +67 -52
- package/lib/core/lazy-loader.js +10 -4
- package/lib/core/types.js +196 -98
- package/lib/index.d.ts +193 -47
- package/lib/index.js +51 -28
- package/lib/memory/buffer.js +66 -19
- package/lib/memory/mirror.js +283 -0
- package/lib/memory/operations.js +59 -42
- package/lib/memory/pointer.js +180 -61
- package/lib/platform/constants.js +0 -1
- package/lib/platform/errors.js +48 -4
- package/lib/platform/find_library.js +78 -19
- package/lib/structures/Structure.js +260 -172
- package/lib/structures/Union.js +10 -21
- package/lib/structures/helpers/array.js +235 -92
- package/lib/structures/helpers/bitfield.js +66 -150
- package/lib/structures/helpers/common.js +115 -15
- package/lib/structures/helpers/layout.js +131 -0
- package/lib/structures/helpers/struct.js +144 -400
- package/lib/structures/helpers/union.js +132 -142
- package/lib/structures/helpers/wrap.js +29 -17
- package/lib/types/SimpleCData.js +14 -7
- package/lib/types/primitives.js +130 -101
- package/package.json +2 -1
|
@@ -34,9 +34,11 @@
|
|
|
34
34
|
* ```
|
|
35
35
|
*/
|
|
36
36
|
|
|
37
|
-
import {
|
|
37
|
+
import { bitFieldAccessors } from "./bitfield.js";
|
|
38
|
+
import { computeLayout, isGccLayout } from "./layout.js";
|
|
38
39
|
import { buildWrapper } from "./wrap.js";
|
|
39
|
-
import {
|
|
40
|
+
import { syncDefinition } from "../../memory/mirror.js";
|
|
41
|
+
import { copyInstance, fieldEntries, fieldsOf, funcPtrFieldType, simpleFieldWriter, writePointerField } from "./common.js";
|
|
40
42
|
|
|
41
43
|
/**
|
|
42
44
|
* Creates a union type definition where all fields share offset 0.
|
|
@@ -108,39 +110,64 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
108
110
|
}
|
|
109
111
|
options = options || {};
|
|
110
112
|
const swapped = options.swapped === true;
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
// Python _pack_: `pack` da solo, o `packed: true` (= pack 1).
|
|
114
|
+
const packValue = typeof options.pack === "number" ? options.pack : options.packed ? 1 : 0;
|
|
115
|
+
const gccLayout = isGccLayout(options.layout, packValue);
|
|
116
|
+
const base = options.base ?? null;
|
|
117
|
+
const fieldDefs = base ? base.fields.map((f) => ({ ...f })) : [];
|
|
118
|
+
const ownDefs = [];
|
|
119
|
+
|
|
120
|
+
const fieldNames = new Set(fieldDefs.map((f) => f.name));
|
|
121
|
+
for (const [name, type] of fieldEntries(fields)) {
|
|
122
|
+
if (fieldNames.has(name)) {
|
|
123
|
+
throw new TypeError(`Duplicate field name: ${name}`);
|
|
124
|
+
}
|
|
125
|
+
fieldNames.add(name);
|
|
116
126
|
let size, alignment;
|
|
117
|
-
let fieldDef = { name, type
|
|
118
|
-
|
|
127
|
+
let fieldDef = { name, type };
|
|
128
|
+
|
|
129
|
+
// Campo di _anonymous_: i suoi campi sono raggiungibili dalla union.
|
|
130
|
+
if (typeof type === "object" && type !== null && type.anonymous === true && type.type) {
|
|
131
|
+
let def = type.type;
|
|
132
|
+
fieldDef.cls = type.cls;
|
|
133
|
+
if (typeof def === "function" && typeof def._buildStruct === "function") {
|
|
134
|
+
fieldDef.cls = def;
|
|
135
|
+
def = def._structDef || def._buildStruct();
|
|
136
|
+
}
|
|
137
|
+
if (def === null || typeof def !== "object" || !Array.isArray(def.fields)) {
|
|
138
|
+
throw new TypeError(`anonymous field "${name}" must be a structure or union`);
|
|
139
|
+
}
|
|
140
|
+
fieldDef.type = def;
|
|
141
|
+
fieldDef.isNested = true;
|
|
142
|
+
fieldDef.isAnonymous = true;
|
|
143
|
+
size = def.size;
|
|
144
|
+
alignment = def.alignment;
|
|
145
|
+
}
|
|
119
146
|
// Nested struct (object form or class form)
|
|
120
|
-
if (_isStruct(type)) {
|
|
147
|
+
else if (_isStruct(type)) {
|
|
121
148
|
// Normalize type: if it's a Structure/Union class, get its _structDef
|
|
122
149
|
let nestedStruct = type;
|
|
123
150
|
if (typeof type === "function" && typeof type._buildStruct === "function") {
|
|
124
151
|
nestedStruct = type._structDef || type._buildStruct();
|
|
125
152
|
fieldDef.type = nestedStruct;
|
|
153
|
+
// Python restituisce un'istanza della classe annidata.
|
|
154
|
+
fieldDef.cls = type;
|
|
126
155
|
}
|
|
127
156
|
size = nestedStruct.size;
|
|
128
157
|
alignment = nestedStruct.alignment;
|
|
129
158
|
fieldDef.isNested = true;
|
|
130
159
|
}
|
|
131
|
-
// Array type
|
|
160
|
+
// Array type: alignment == element alignment (C standard)
|
|
132
161
|
else if (_isArrayType(type)) {
|
|
133
162
|
size = type.getSize();
|
|
134
|
-
|
|
135
|
-
alignment = Math.min(elemSize, native.POINTER_SIZE);
|
|
163
|
+
alignment = typeof type.getAlignment === "function" ? type.getAlignment() : Math.min(sizeof(type.elementType), native.POINTER_SIZE);
|
|
136
164
|
fieldDef.isArray = true;
|
|
137
165
|
}
|
|
138
|
-
// Bit field
|
|
166
|
+
// Bit field
|
|
139
167
|
else if (_isBitField(type)) {
|
|
140
168
|
size = type.baseSize;
|
|
141
169
|
alignment = Math.min(size, native.POINTER_SIZE);
|
|
142
170
|
fieldDef.isBitField = true;
|
|
143
|
-
fieldDef.bitOffset = 0;
|
|
144
171
|
fieldDef.bitSize = type.bits;
|
|
145
172
|
fieldDef.baseSize = type.baseSize;
|
|
146
173
|
fieldDef.type = type.baseType; // Usa il tipo base per lettura/scrittura
|
|
@@ -148,7 +175,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
148
175
|
// POINTER() type
|
|
149
176
|
else if ((typeof type === "object" && type !== null && type._pointerTo !== undefined) || (typeof type === "function" && type._isFuncPtr === true)) {
|
|
150
177
|
size = native.POINTER_SIZE;
|
|
151
|
-
alignment =
|
|
178
|
+
alignment = native.POINTER_SIZE;
|
|
152
179
|
// CFUNCTYPE: campo puntatore letto come funzione chiamabile (Python parity)
|
|
153
180
|
fieldDef.type = typeof type === "function" ? funcPtrFieldType(type) : type;
|
|
154
181
|
fieldDef.isPointer = true;
|
|
@@ -166,20 +193,25 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
166
193
|
|
|
167
194
|
fieldDef.size = size;
|
|
168
195
|
fieldDef.alignment = alignment;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (size > maxSize) {
|
|
172
|
-
maxSize = size;
|
|
173
|
-
}
|
|
174
|
-
if (alignment > maxAlignment) {
|
|
175
|
-
maxAlignment = alignment;
|
|
176
|
-
}
|
|
196
|
+
ownDefs.push(fieldDef);
|
|
177
197
|
}
|
|
178
198
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
199
|
+
const layout = computeLayout(
|
|
200
|
+
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 })),
|
|
201
|
+
{ isStruct: false, gccLayout, pack: packValue, align: options.align ?? 1, base, bigEndian: swapped },
|
|
202
|
+
);
|
|
203
|
+
ownDefs.forEach((f, i) => {
|
|
204
|
+
f.offset = layout.offsets[i].offset;
|
|
205
|
+
if (f.isBitField) f.bitOffset = layout.offsets[i].bitOffset;
|
|
206
|
+
// Allineamento del tipo in C, prima di _pack_ (usato per le struct native
|
|
207
|
+
// passate per valore, lib/core/types.js).
|
|
208
|
+
f.naturalAlignment = f.alignment;
|
|
209
|
+
if (packValue) f.alignment = Math.min(f.alignment, packValue);
|
|
210
|
+
f.swapped = swapped;
|
|
211
|
+
fieldDefs.push(f);
|
|
212
|
+
});
|
|
213
|
+
const maxSize = layout.size;
|
|
214
|
+
const maxAlignment = layout.alignment;
|
|
183
215
|
|
|
184
216
|
// Crea Map per lookup O(1)
|
|
185
217
|
const fieldMap = new Map();
|
|
@@ -202,85 +234,18 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
202
234
|
|
|
203
235
|
// Bitfield precompilato (parity con struct.js).
|
|
204
236
|
if (field.isBitField) {
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const extendMask = ~mask;
|
|
218
|
-
fieldReaders[name] = (buf) => {
|
|
219
|
-
const raw = (buf.readUInt8(fieldOffset) >> bitOffset) & mask;
|
|
220
|
-
return (raw & signBit) ? (raw | extendMask) : raw;
|
|
221
|
-
};
|
|
222
|
-
} else {
|
|
223
|
-
fieldReaders[name] = (buf) => (buf.readUInt8(fieldOffset) >> bitOffset) & mask;
|
|
224
|
-
}
|
|
225
|
-
fieldWriters[name] = (buf, val) => {
|
|
226
|
-
const v = buf.readUInt8(fieldOffset);
|
|
227
|
-
buf.writeUInt8(((v & clearMask) | ((val & mask) << bitOffset)) & 0xff, fieldOffset);
|
|
228
|
-
};
|
|
229
|
-
} else if (baseSize === 2) {
|
|
230
|
-
const mask = (1 << bitSize) - 1;
|
|
231
|
-
const clearMask = ~(mask << bitOffset);
|
|
232
|
-
if (isSigned) {
|
|
233
|
-
const signBit = 1 << (bitSize - 1);
|
|
234
|
-
const extendMask = ~mask;
|
|
235
|
-
fieldReaders[name] = (buf) => {
|
|
236
|
-
const raw = (buf.readUInt16LE(fieldOffset) >> bitOffset) & mask;
|
|
237
|
-
return (raw & signBit) ? (raw | extendMask) : raw;
|
|
238
|
-
};
|
|
239
|
-
} else {
|
|
240
|
-
fieldReaders[name] = (buf) => (buf.readUInt16LE(fieldOffset) >> bitOffset) & mask;
|
|
241
|
-
}
|
|
242
|
-
fieldWriters[name] = (buf, val) => {
|
|
243
|
-
const v = buf.readUInt16LE(fieldOffset);
|
|
244
|
-
buf.writeUInt16LE(((v & clearMask) | ((val & mask) << bitOffset)) & 0xffff, fieldOffset);
|
|
245
|
-
};
|
|
246
|
-
} else if (baseSize === 4) {
|
|
247
|
-
const mask = (1 << bitSize) - 1;
|
|
248
|
-
const clearMask = ~(mask << bitOffset);
|
|
249
|
-
if (isSigned) {
|
|
250
|
-
const signBit = 1 << (bitSize - 1);
|
|
251
|
-
const extendMask = ~mask;
|
|
252
|
-
fieldReaders[name] = (buf) => {
|
|
253
|
-
const raw = (buf.readUInt32LE(fieldOffset) >>> bitOffset) & mask;
|
|
254
|
-
return (raw & signBit) ? (raw | extendMask) : raw;
|
|
255
|
-
};
|
|
256
|
-
} else {
|
|
257
|
-
fieldReaders[name] = (buf) => (buf.readUInt32LE(fieldOffset) >>> bitOffset) & mask;
|
|
258
|
-
}
|
|
259
|
-
fieldWriters[name] = (buf, val) => {
|
|
260
|
-
const v = buf.readUInt32LE(fieldOffset);
|
|
261
|
-
buf.writeUInt32LE(((v & clearMask) | ((val & mask) << bitOffset)) >>> 0, fieldOffset);
|
|
262
|
-
};
|
|
263
|
-
} else if (baseSize === 8) {
|
|
264
|
-
const bMask = (1n << BigInt(bitSize)) - 1n;
|
|
265
|
-
const bBitOffset = BigInt(bitOffset);
|
|
266
|
-
const bClearMask = ~(bMask << bBitOffset);
|
|
267
|
-
if (isSigned) {
|
|
268
|
-
const bSignBit = 1n << BigInt(bitSize - 1);
|
|
269
|
-
const bExtend = ~bMask;
|
|
270
|
-
fieldReaders[name] = (buf) => {
|
|
271
|
-
const raw = (buf.readBigUInt64LE(fieldOffset) >> bBitOffset) & bMask;
|
|
272
|
-
const signed = (raw & bSignBit) ? (raw | bExtend) : raw;
|
|
273
|
-
const asNum = Number(signed);
|
|
274
|
-
return (asNum >= Number.MIN_SAFE_INTEGER && asNum <= Number.MAX_SAFE_INTEGER) ? asNum : signed;
|
|
275
|
-
};
|
|
276
|
-
} else {
|
|
277
|
-
fieldReaders[name] = (buf) => Number((buf.readBigUInt64LE(fieldOffset) >> bBitOffset) & bMask);
|
|
278
|
-
}
|
|
279
|
-
fieldWriters[name] = (buf, val) => {
|
|
280
|
-
const v = buf.readBigUInt64LE(fieldOffset);
|
|
281
|
-
buf.writeBigUInt64LE((v & bClearMask) | ((BigInt(val) & bMask) << bBitOffset), fieldOffset);
|
|
282
|
-
};
|
|
283
|
-
}
|
|
237
|
+
const { read, write } = bitFieldAccessors({
|
|
238
|
+
offset,
|
|
239
|
+
baseSize: field.baseSize,
|
|
240
|
+
bitOffset: field.bitOffset,
|
|
241
|
+
bitSize: field.bitSize,
|
|
242
|
+
signed: field.type?._signed === true,
|
|
243
|
+
bigEndian: field.swapped === true,
|
|
244
|
+
});
|
|
245
|
+
field.readBits = read;
|
|
246
|
+
field.writeBits = write;
|
|
247
|
+
fieldReaders[name] = read;
|
|
248
|
+
fieldWriters[name] = write;
|
|
284
249
|
continue;
|
|
285
250
|
}
|
|
286
251
|
if (field.isPointer) {
|
|
@@ -301,15 +266,34 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
301
266
|
|
|
302
267
|
const fieldType = field.type;
|
|
303
268
|
if (fieldType && fieldType._isSimpleCData) {
|
|
304
|
-
const reader = swapped && fieldType._readerBE ? fieldType._readerBE : fieldType._reader;
|
|
305
|
-
const writer = swapped && fieldType._writerBE ? fieldType._writerBE : fieldType._writer;
|
|
269
|
+
const reader = field.swapped && fieldType._readerBE ? fieldType._readerBE : fieldType._reader;
|
|
270
|
+
const writer = field.swapped && fieldType._writerBE ? fieldType._writerBE : fieldType._writer;
|
|
306
271
|
if (reader && writer) {
|
|
307
272
|
fieldReaders[name] = (buf) => reader(buf, offset);
|
|
308
|
-
fieldWriters[name] = (
|
|
273
|
+
fieldWriters[name] = simpleFieldWriter(fieldType, writer, offset);
|
|
309
274
|
}
|
|
310
275
|
}
|
|
311
276
|
}
|
|
312
277
|
|
|
278
|
+
// Campi raggiungibili attraverso i campi di _anonymous_ (anche annidati).
|
|
279
|
+
const anonymousFieldNames = Object.create(null);
|
|
280
|
+
const anonymousOwner = new Map();
|
|
281
|
+
for (const field of fieldDefs) {
|
|
282
|
+
if (!field.isAnonymous) continue;
|
|
283
|
+
for (const sub of field.type.fields) {
|
|
284
|
+
anonymousFieldNames[sub.name] = true;
|
|
285
|
+
anonymousOwner.set(sub.name, field);
|
|
286
|
+
}
|
|
287
|
+
for (const name of Object.keys(field.type.anonymousFieldNames ?? {})) {
|
|
288
|
+
anonymousFieldNames[name] = true;
|
|
289
|
+
anonymousOwner.set(name, field);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const anonymousView = (buf, fieldName) => {
|
|
293
|
+
const owner = anonymousOwner.get(fieldName);
|
|
294
|
+
return owner === undefined ? undefined : { def: owner.type, buf: buf.subarray(owner.offset, owner.offset + owner.size) };
|
|
295
|
+
};
|
|
296
|
+
|
|
313
297
|
const unionDef = {
|
|
314
298
|
size: maxSize,
|
|
315
299
|
alignment: maxAlignment,
|
|
@@ -317,7 +301,10 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
317
301
|
fieldMap,
|
|
318
302
|
fieldReaders,
|
|
319
303
|
fieldWriters,
|
|
304
|
+
anonymousFieldNames,
|
|
320
305
|
isUnion: true,
|
|
306
|
+
swapped,
|
|
307
|
+
base, // definizione della union base (i suoi campi sono i primi di `fields`)
|
|
321
308
|
_isStructType: true, // Per compatibilità con _isStruct()
|
|
322
309
|
|
|
323
310
|
create(values = {}) {
|
|
@@ -331,12 +318,14 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
331
318
|
}
|
|
332
319
|
}
|
|
333
320
|
|
|
334
|
-
//
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
321
|
+
// Come Python, i campi di _anonymous_ sono raggiungibili dall'istanza.
|
|
322
|
+
return new Proxy(
|
|
323
|
+
buf,
|
|
324
|
+
buildWrapper(unionDef, {
|
|
325
|
+
instanceTag: "UnionInstance",
|
|
326
|
+
exposeAnonymous: true,
|
|
327
|
+
}),
|
|
328
|
+
);
|
|
340
329
|
},
|
|
341
330
|
|
|
342
331
|
get(bufOrObj, fieldName) {
|
|
@@ -353,16 +342,22 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
353
342
|
|
|
354
343
|
// O(1) lookup con Map
|
|
355
344
|
const field = fieldMap.get(fieldName);
|
|
356
|
-
if (!field)
|
|
345
|
+
if (!field) {
|
|
346
|
+
const view = anonymousView(buf, fieldName);
|
|
347
|
+
if (view === undefined) throw new Error(`Unknown field: ${fieldName}`);
|
|
348
|
+
return view.def.get(view.buf, fieldName);
|
|
349
|
+
}
|
|
357
350
|
|
|
358
351
|
// Bit field
|
|
359
352
|
if (field.isBitField) {
|
|
360
|
-
return
|
|
353
|
+
return field.readBits(buf);
|
|
361
354
|
}
|
|
362
355
|
|
|
363
356
|
// Nested struct
|
|
364
357
|
if (field.isNested) {
|
|
365
358
|
const nestedBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
359
|
+
// Classe Structure/Union: un'istanza sulla stessa memoria (Python).
|
|
360
|
+
if (field.cls !== undefined) return new field.cls(nestedBuf);
|
|
366
361
|
// Proxy-based nested instance
|
|
367
362
|
return new Proxy(nestedBuf, {
|
|
368
363
|
get(target, prop, receiver) {
|
|
@@ -436,11 +431,8 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
436
431
|
return pointerOrNull(field.type, addr);
|
|
437
432
|
}
|
|
438
433
|
|
|
439
|
-
// Tipo base
|
|
440
|
-
|
|
441
|
-
return field.type._readerBE(buf, field.offset);
|
|
442
|
-
}
|
|
443
|
-
return readValue(buf, field.type, field.offset);
|
|
434
|
+
// Tipo base: stesso accessor del fast path (rispetta l'endianness)
|
|
435
|
+
return fieldReaders[field.name](buf);
|
|
444
436
|
},
|
|
445
437
|
|
|
446
438
|
set(bufOrObj, fieldName, value) {
|
|
@@ -458,11 +450,16 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
458
450
|
|
|
459
451
|
// Lookup with Map
|
|
460
452
|
const field = fieldMap.get(fieldName);
|
|
461
|
-
if (!field)
|
|
453
|
+
if (!field) {
|
|
454
|
+
const view = anonymousView(buf, fieldName);
|
|
455
|
+
if (view === undefined) throw new Error(`Unknown field: ${fieldName}`);
|
|
456
|
+
view.def.set(view.buf, fieldName, value);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
462
459
|
|
|
463
460
|
// Bit field
|
|
464
461
|
if (field.isBitField) {
|
|
465
|
-
|
|
462
|
+
field.writeBits(buf, value);
|
|
466
463
|
return;
|
|
467
464
|
}
|
|
468
465
|
|
|
@@ -484,7 +481,8 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
484
481
|
|
|
485
482
|
// Array
|
|
486
483
|
if (field.isArray) {
|
|
487
|
-
|
|
484
|
+
// Python: bytes / str con il controllo di lunghezza ("bytes too long").
|
|
485
|
+
if (field.type._charKind !== undefined && (typeof value === "string" || (field.type._charKind === "char" && Buffer.isBuffer(value) && value._buffer === undefined))) {
|
|
488
486
|
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})`);
|
|
489
487
|
} else if (Buffer.isBuffer(value)) {
|
|
490
488
|
value.copy(buf, field.offset, 0, field.size);
|
|
@@ -506,12 +504,8 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
506
504
|
return;
|
|
507
505
|
}
|
|
508
506
|
|
|
509
|
-
// Tipo base
|
|
510
|
-
|
|
511
|
-
field.type._writerBE(buf, field.offset, value);
|
|
512
|
-
return;
|
|
513
|
-
}
|
|
514
|
-
writeValue(buf, field.type, value, field.offset);
|
|
507
|
+
// Tipo base: stesso accessor del fast path (rispetta l'endianness)
|
|
508
|
+
fieldWriters[field.name](buf, value);
|
|
515
509
|
},
|
|
516
510
|
|
|
517
511
|
toObject(bufOrObj) {
|
|
@@ -551,7 +545,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
551
545
|
Object.defineProperty(obj, field.name, {
|
|
552
546
|
get() {
|
|
553
547
|
if (field.isBitField) {
|
|
554
|
-
return
|
|
548
|
+
return field.readBits(buf);
|
|
555
549
|
} else if (field.isNested) {
|
|
556
550
|
// Cache nested objects per evitare di ricrearli ogni volta
|
|
557
551
|
if (!nestedCache.has(field.name)) {
|
|
@@ -567,21 +561,17 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
567
561
|
} else if (field.isPointer) {
|
|
568
562
|
const addr = native.POINTER_SIZE === 8 ? buf.readBigUInt64LE(0) : BigInt(buf.readUInt32LE(0));
|
|
569
563
|
return pointerOrNull(field.type, addr);
|
|
570
|
-
} else if (swapped && typeof field.type === "function" && field.type._isSimpleCData && field.type._readerBE) {
|
|
571
|
-
return field.type._readerBE(buf, 0);
|
|
572
564
|
} else {
|
|
573
|
-
return
|
|
565
|
+
return fieldReaders[field.name](buf);
|
|
574
566
|
}
|
|
575
567
|
},
|
|
576
568
|
set(value) {
|
|
577
569
|
if (field.isBitField) {
|
|
578
|
-
|
|
570
|
+
field.writeBits(buf, value);
|
|
579
571
|
} else if (field.isPointer) {
|
|
580
572
|
writePointerField(buf, 0, value, native.POINTER_SIZE);
|
|
581
|
-
} else if (swapped && typeof field.type === "function" && field.type._isSimpleCData && field.type._writerBE) {
|
|
582
|
-
field.type._writerBE(buf, 0, value);
|
|
583
573
|
} else {
|
|
584
|
-
|
|
574
|
+
fieldWriters[field.name](buf, value);
|
|
585
575
|
}
|
|
586
576
|
},
|
|
587
577
|
enumerable: true,
|
|
@@ -594,7 +584,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
594
584
|
|
|
595
585
|
// fromObject: write plain object values into buffer
|
|
596
586
|
fromObject: function (buf, obj) {
|
|
597
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
587
|
+
for (const [key, value] of Object.entries(fieldsOf(obj))) {
|
|
598
588
|
if (this.fields.some((f) => f.name === key)) {
|
|
599
589
|
this.set(buf, key, value);
|
|
600
590
|
}
|
|
@@ -602,5 +592,5 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
602
592
|
},
|
|
603
593
|
};
|
|
604
594
|
|
|
605
|
-
return unionDef;
|
|
595
|
+
return syncDefinition(unionDef);
|
|
606
596
|
}
|
|
@@ -18,15 +18,15 @@
|
|
|
18
18
|
* `!f.isAnonymous`) gestite inline in `union.js`.
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
+
import { mirroredAccessors } from "../../memory/mirror.js";
|
|
22
|
+
|
|
21
23
|
// Proprietà meta del Buffer che NON vanno mai interpretate come field name.
|
|
22
24
|
// Per i campi Proxy passano direttamente attraverso al target buffer.
|
|
23
|
-
const BUFFER_META_PROPS = new Set([
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"BYTES_PER_ELEMENT",
|
|
29
|
-
]);
|
|
25
|
+
const BUFFER_META_PROPS = new Set(["length", "byteLength", "byteOffset", "buffer", "BYTES_PER_ELEMENT"]);
|
|
26
|
+
|
|
27
|
+
// L'handler dipende solo da def e opzioni (il Buffer è il target): uno per
|
|
28
|
+
// combinazione, invece di ricrearlo a ogni accesso a un campo annidato.
|
|
29
|
+
const wrapperCache = new WeakMap();
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* Costruisce un Proxy handler per un'istanza Structure/Union.
|
|
@@ -45,10 +45,28 @@ const BUFFER_META_PROPS = new Set([
|
|
|
45
45
|
* @param {string} opts.instanceTag — valore restituito per Symbol.toStringTag
|
|
46
46
|
* @param {boolean} opts.exposeAnonymous — true per struct (sotto-campi `_anonymous_`
|
|
47
47
|
* visibili come top-level), false per union.
|
|
48
|
+
* @param {boolean} [opts.mirrored] — Buffer copia di memoria nativa (Electron):
|
|
49
|
+
* lettori e scrittori che sincronizzano il campo.
|
|
48
50
|
* @returns {ProxyHandler<Buffer>}
|
|
49
51
|
*/
|
|
50
|
-
export function buildWrapper(def, { instanceTag, exposeAnonymous }) {
|
|
51
|
-
|
|
52
|
+
export function buildWrapper(def, { instanceTag, exposeAnonymous, mirrored = false }) {
|
|
53
|
+
let byOptions = wrapperCache.get(def);
|
|
54
|
+
if (byOptions === undefined) {
|
|
55
|
+
byOptions = new Map();
|
|
56
|
+
wrapperCache.set(def, byOptions);
|
|
57
|
+
}
|
|
58
|
+
const key = `${instanceTag}|${exposeAnonymous}|${mirrored}`;
|
|
59
|
+
let handler = byOptions.get(key);
|
|
60
|
+
if (handler === undefined) {
|
|
61
|
+
handler = createWrapper(def, instanceTag, exposeAnonymous, mirrored);
|
|
62
|
+
byOptions.set(key, handler);
|
|
63
|
+
}
|
|
64
|
+
return handler;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function createWrapper(def, instanceTag, exposeAnonymous, mirrored) {
|
|
68
|
+
const { fieldMap, anonymousFieldNames } = def;
|
|
69
|
+
const { fieldReaders, fieldWriters } = mirrored ? mirroredAccessors(def) : def;
|
|
52
70
|
|
|
53
71
|
// Precalcola la lista di chiavi visibili (stabile durante tutta la vita
|
|
54
72
|
// del def). Evita di ricostruirla a ogni Object.keys / {...spread} / ecc.
|
|
@@ -80,10 +98,7 @@ export function buildWrapper(def, { instanceTag, exposeAnonymous }) {
|
|
|
80
98
|
if (r !== undefined) return r(target);
|
|
81
99
|
}
|
|
82
100
|
// FALLBACK: tipi complessi (nested/array) e anonymous subfields.
|
|
83
|
-
if (
|
|
84
|
-
fieldMap?.has(prop) ||
|
|
85
|
-
(exposeAnonymous && anonymousFieldNames?.[prop] === true)
|
|
86
|
-
) {
|
|
101
|
+
if (fieldMap?.has(prop) || (exposeAnonymous && anonymousFieldNames?.[prop] === true)) {
|
|
87
102
|
return def.get(target, prop);
|
|
88
103
|
}
|
|
89
104
|
}
|
|
@@ -102,10 +117,7 @@ export function buildWrapper(def, { instanceTag, exposeAnonymous }) {
|
|
|
102
117
|
return true;
|
|
103
118
|
}
|
|
104
119
|
}
|
|
105
|
-
if (
|
|
106
|
-
fieldMap?.has(prop) ||
|
|
107
|
-
(exposeAnonymous && anonymousFieldNames?.[prop] === true)
|
|
108
|
-
) {
|
|
120
|
+
if (fieldMap?.has(prop) || (exposeAnonymous && anonymousFieldNames?.[prop] === true)) {
|
|
109
121
|
def.set(target, prop, value);
|
|
110
122
|
return true;
|
|
111
123
|
}
|
package/lib/types/SimpleCData.js
CHANGED
|
@@ -46,6 +46,8 @@
|
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
48
|
|
|
49
|
+
import { kMirrorBase, mirrorBase, mirroredPrototype, mirrorsActive } from "../memory/mirror.js";
|
|
50
|
+
|
|
49
51
|
/**
|
|
50
52
|
* Creates the SimpleCData base class with required dependencies injected.
|
|
51
53
|
*
|
|
@@ -54,6 +56,16 @@
|
|
|
54
56
|
* @private
|
|
55
57
|
*/
|
|
56
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
|
+
|
|
57
69
|
/**
|
|
58
70
|
* SimpleCData base class for primitive C types.
|
|
59
71
|
*
|
|
@@ -146,9 +158,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
146
158
|
if (!Buffer.isBuffer(buffer)) {
|
|
147
159
|
throw new TypeError("from_buffer requires a Buffer");
|
|
148
160
|
}
|
|
149
|
-
|
|
150
|
-
inst._buffer = buffer.subarray(offset, offset + this._size);
|
|
151
|
-
return inst;
|
|
161
|
+
return withBuffer(this, buffer.subarray(offset, offset + this._size));
|
|
152
162
|
}
|
|
153
163
|
|
|
154
164
|
/**
|
|
@@ -178,10 +188,7 @@ export function createSimpleCDataClass(alloc, ptrToBuffer) {
|
|
|
178
188
|
throw new Error("from_address requires ptrToBuffer injected at construction");
|
|
179
189
|
}
|
|
180
190
|
const addr = typeof address === "bigint" ? address : BigInt(address);
|
|
181
|
-
|
|
182
|
-
const inst = Object.create(this.prototype);
|
|
183
|
-
inst._buffer = buf;
|
|
184
|
-
return inst;
|
|
191
|
+
return withBuffer(this, ptrToBuffer(addr, this._size));
|
|
185
192
|
}
|
|
186
193
|
|
|
187
194
|
/**
|