node-ctypes 2.13.2 → 2.15.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 +71 -1
- 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 +69 -51
- package/lib/core/callback-conversions.js +123 -0
- package/lib/core/callback.js +68 -4
- package/lib/core/conversions.js +74 -412
- package/lib/core/funcptr.js +31 -287
- package/lib/core/invocation.js +170 -0
- package/lib/core/native-aggregate.js +193 -0
- package/lib/core/parameters.js +45 -0
- package/lib/core/paramflags.js +183 -0
- package/lib/core/results.js +86 -0
- package/lib/core/types.js +14 -277
- package/lib/index.d.ts +3 -0
- package/lib/index.js +2 -1
- package/lib/memory/buffer.js +1 -1
- package/lib/memory/mirror.js +2 -2
- package/lib/memory/operations.js +2 -1
- package/lib/memory/ownership.js +218 -0
- package/lib/memory/pointer.js +6 -1
- package/lib/platform/win_types.js +1 -1
- package/lib/structures/Structure.js +50 -37
- package/lib/structures/helpers/array.js +27 -13
- package/lib/structures/helpers/bitfield.js +1 -1
- package/lib/structures/helpers/common.js +3 -368
- package/lib/structures/helpers/fields.js +111 -0
- package/lib/structures/helpers/layout.js +44 -57
- package/lib/structures/helpers/struct.js +19 -108
- package/lib/structures/helpers/union.js +13 -114
- package/lib/structures/helpers/wrap.js +1 -1
- package/lib/structures/protocol.js +8 -0
- package/lib/types/SimpleCData.js +2 -1
- package/lib/types/primitives.js +2 -1
- package/lib/types/scalar-protocol.js +120 -0
- package/package.json +1 -1
|
@@ -34,11 +34,17 @@
|
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
36
|
import { bitFieldAccessors } from "./bitfield.js";
|
|
37
|
-
import {
|
|
37
|
+
import { readArrayField } from "./array.js";
|
|
38
|
+
import { declareFields } from "./fields.js";
|
|
39
|
+
import { normalizePack } from "./layout.js";
|
|
38
40
|
import { buildWrapper } from "./wrap.js";
|
|
39
41
|
import { isMirror, syncDefinition } from "../../memory/mirror.js";
|
|
40
42
|
import { allocTransient } from "../../memory/buffer.js";
|
|
41
|
-
import {
|
|
43
|
+
import { kFromBytes } from "../Structure.js";
|
|
44
|
+
import { copyInstance, pointerOwner } from "../../memory/ownership.js";
|
|
45
|
+
import { fieldsOf, simpleFieldWriter, writePointerField } from "./common.js";
|
|
46
|
+
import { structArgumentFactories, structGetters, structResultReaders } from "../protocol.js";
|
|
47
|
+
import { isScalarSubclass, scalarReader } from "../../types/scalar-protocol.js";
|
|
42
48
|
|
|
43
49
|
/**
|
|
44
50
|
* Creates a struct type definition with proper alignment and padding.
|
|
@@ -107,104 +113,7 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
107
113
|
// lays out bitfields from the big end. Used by BigEndianStructure /
|
|
108
114
|
// LittleEndianStructure (on LE host, swapped=true means "use BE").
|
|
109
115
|
const swapped = options.swapped === true;
|
|
110
|
-
const
|
|
111
|
-
// Python: una sottoclasse con _fields_ estende il layout della base, i cui
|
|
112
|
-
// campi restano agli stessi offset.
|
|
113
|
-
const base = options.base ?? null;
|
|
114
|
-
const fieldDefs = base ? base.fields.map((f) => ({ ...f })) : [];
|
|
115
|
-
const fieldNames = new Set(fieldDefs.map((f) => f.name));
|
|
116
|
-
|
|
117
|
-
// Tipo, dimensione e allineamento naturale di ogni campo; gli offset sono
|
|
118
|
-
// calcolati dopo, tutti insieme, da computeLayout (come Python).
|
|
119
|
-
const ownDefs = [];
|
|
120
|
-
for (const [name, type] of fieldEntries(fields)) {
|
|
121
|
-
// Validate field name
|
|
122
|
-
if (typeof name !== "string" || (!Array.isArray(fields) && !(fields instanceof Map) && name.trim().length === 0)) {
|
|
123
|
-
throw new TypeError("Field name must be a non-empty string");
|
|
124
|
-
}
|
|
125
|
-
if (fieldNames.has(name)) {
|
|
126
|
-
throw new TypeError(`Duplicate field name: ${name}`);
|
|
127
|
-
}
|
|
128
|
-
fieldNames.add(name);
|
|
129
|
-
ownDefs.push(describeField(name, type));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Campo di _anonymous_: una struct/union (classe o definizione), i cui
|
|
133
|
-
// campi sono raggiungibili dalla struct esterna.
|
|
134
|
-
function anonymousField(name, { type, cls }) {
|
|
135
|
-
let def = type;
|
|
136
|
-
if (typeof type === "function" && typeof type._buildStruct === "function") {
|
|
137
|
-
cls = type;
|
|
138
|
-
def = type._finalizeFields();
|
|
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
|
-
return { name, type: def, cls, size: def.size, alignment: def.alignment, isNested: true, isAnonymous: true };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function describeField(name, type) {
|
|
147
|
-
// Anonymous field - { type: SomeStruct, anonymous: true }
|
|
148
|
-
if (typeof type === "object" && type !== null && type.anonymous === true && type.type) {
|
|
149
|
-
return anonymousField(name, type);
|
|
150
|
-
}
|
|
151
|
-
// Bit field
|
|
152
|
-
if (_isBitField(type)) {
|
|
153
|
-
const { baseType, bits, baseSize } = type;
|
|
154
|
-
return { name, type: baseType, size: 0, alignment: Math.min(baseSize, native.POINTER_SIZE), isBitField: true, bitSize: bits, baseSize };
|
|
155
|
-
}
|
|
156
|
-
// Structure / Union class (Union estende Structure)
|
|
157
|
-
if (typeof type === "function" && type.prototype instanceof Structure) {
|
|
158
|
-
const nested = type._finalizeFields();
|
|
159
|
-
// `cls`: Python restituisce un'istanza della classe annidata, sulla
|
|
160
|
-
// stessa memoria.
|
|
161
|
-
return { name, type: nested, cls: type, size: nested.size, alignment: nested.alignment, isNested: true };
|
|
162
|
-
}
|
|
163
|
-
// struct() / union() definition
|
|
164
|
-
if (_isStruct(type)) {
|
|
165
|
-
return { name, type, size: type.size, alignment: type.alignment, isNested: true };
|
|
166
|
-
}
|
|
167
|
-
// Array type: alignment == element alignment (C standard).
|
|
168
|
-
if (_isArrayType(type)) {
|
|
169
|
-
if (swapped) type = type._swappedType ?? type;
|
|
170
|
-
const elemSize = sizeof(type.elementType);
|
|
171
|
-
const alignment = typeof type.getAlignment === "function" ? type.getAlignment() : Math.min(elemSize, native.POINTER_SIZE);
|
|
172
|
-
return { name, type, size: type.getSize(), alignment, isArray: true };
|
|
173
|
-
}
|
|
174
|
-
// POINTER() / CFUNCTYPE — pointer-sized field with typed access
|
|
175
|
-
if ((typeof type === "object" && type !== null && type._pointerTo !== undefined) || (typeof type === "function" && type._isFuncPtr === true)) {
|
|
176
|
-
// CFUNCTYPE: campo puntatore letto come funzione chiamabile (Python parity)
|
|
177
|
-
return { name, type: typeof type === "function" ? funcPtrFieldType(type) : type, size: native.POINTER_SIZE, alignment: native.POINTER_SIZE, isPointer: true };
|
|
178
|
-
}
|
|
179
|
-
// Tipo base (SimpleCData)
|
|
180
|
-
if (!(typeof type === "function" && type._isSimpleCData)) {
|
|
181
|
-
throw new TypeError(`struct field "${name}": type must be a SimpleCData class, struct, union, or array`);
|
|
182
|
-
}
|
|
183
|
-
return { name, type, size: type._size, alignment: Math.min(type._size, native.POINTER_SIZE) };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const layout = computeLayout(
|
|
187
|
-
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 })),
|
|
188
|
-
{ isStruct: true, gccLayout, pack: packValue, align: options.align ?? 1, base, bigEndian: swapped },
|
|
189
|
-
);
|
|
190
|
-
ownDefs.forEach((f, i) => {
|
|
191
|
-
f.offset = layout.offsets[i].offset;
|
|
192
|
-
if (f.isBitField) f.bitOffset = layout.offsets[i].bitOffset;
|
|
193
|
-
// Allineamento del tipo in C, prima di _pack_ (usato per le struct native
|
|
194
|
-
// passate per valore, lib/core/types.js).
|
|
195
|
-
f.naturalAlignment = f.alignment;
|
|
196
|
-
if (packValue) f.alignment = Math.min(f.alignment, packValue);
|
|
197
|
-
f.swapped = swapped;
|
|
198
|
-
fieldDefs.push(f);
|
|
199
|
-
});
|
|
200
|
-
const totalSize = layout.size;
|
|
201
|
-
const maxAlignment = layout.alignment;
|
|
202
|
-
|
|
203
|
-
// Crea Map per lookup O(1) invece di find() O(n)
|
|
204
|
-
const fieldMap = new Map();
|
|
205
|
-
for (const field of fieldDefs) {
|
|
206
|
-
fieldMap.set(field.name, field);
|
|
207
|
-
}
|
|
116
|
+
const { base, fieldDefs, fieldMap, size: totalSize, alignment: maxAlignment } = declareFields(fields, options, { isStruct: true, pack: packValue, swapped, sizeof, _isStruct, _isArrayType, _isBitField, Structure, native });
|
|
208
117
|
|
|
209
118
|
// Precalcola la lookup plana di tutti i nomi accessibili dal Proxy,
|
|
210
119
|
// inclusi i sotto-campi dei campi _anonymous_. Il Proxy get/set/has
|
|
@@ -351,7 +260,7 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
351
260
|
* @param {string} fieldName - Nome del campo (supporta 'outer.inner' per nested)
|
|
352
261
|
* @returns {*} Valore del campo
|
|
353
262
|
*/
|
|
354
|
-
get(bufOrObj, fieldName) {
|
|
263
|
+
get(bufOrObj, fieldName, owner) {
|
|
355
264
|
// supporta sia buffer che object wrapper o plain object
|
|
356
265
|
let buf;
|
|
357
266
|
if (Buffer.isBuffer(bufOrObj)) {
|
|
@@ -400,7 +309,7 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
400
309
|
if (field.isNested) {
|
|
401
310
|
const nestedBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
402
311
|
// Classe Structure/Union: un'istanza sulla stessa memoria (Python).
|
|
403
|
-
if (field.cls !== undefined) return
|
|
312
|
+
if (field.cls !== undefined) return field.cls[kFromBytes](nestedBuf, owner);
|
|
404
313
|
return field.type.create
|
|
405
314
|
? new Proxy(
|
|
406
315
|
nestedBuf,
|
|
@@ -422,8 +331,7 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
422
331
|
// Array field (Python: c_char * n / c_wchar * n si leggono come stringa)
|
|
423
332
|
if (field.isArray) {
|
|
424
333
|
const arrayBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
425
|
-
|
|
426
|
-
return field.type.wrap(arrayBuf);
|
|
334
|
+
return readArrayField(field.type, arrayBuf, owner);
|
|
427
335
|
}
|
|
428
336
|
|
|
429
337
|
// Tipo base: stesso accessor del fast path (rispetta l'endianness)
|
|
@@ -443,7 +351,7 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
443
351
|
const hasField = promotes(f, firstPart);
|
|
444
352
|
if (hasField) {
|
|
445
353
|
const nestedBuf = buf.subarray(f.offset, f.offset + f.size);
|
|
446
|
-
return f.type.get(nestedBuf, fieldName);
|
|
354
|
+
return f.type.get(nestedBuf, fieldName, owner);
|
|
447
355
|
}
|
|
448
356
|
}
|
|
449
357
|
}
|
|
@@ -453,7 +361,7 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
453
361
|
// Nested struct con dot notation
|
|
454
362
|
if (field.isNested && parts.length > 1) {
|
|
455
363
|
const nestedBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
456
|
-
return field.type.get(nestedBuf, parts.slice(1).join("."));
|
|
364
|
+
return field.type.get(nestedBuf, parts.slice(1).join("."), owner);
|
|
457
365
|
}
|
|
458
366
|
|
|
459
367
|
throw new Error(`Unknown field: ${fieldName}`);
|
|
@@ -709,7 +617,7 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
709
617
|
}
|
|
710
618
|
return buf;
|
|
711
619
|
};
|
|
712
|
-
const
|
|
620
|
+
const scalarWriters = writers.every(([, write]) => typeof write === "function");
|
|
713
621
|
// The Buffer built here is fresh and passed as is; with `convert` the
|
|
714
622
|
// result of a create override goes through it as any other argument.
|
|
715
623
|
structArgumentFactories.set(structDef, (values, convert, owners) => {
|
|
@@ -719,7 +627,10 @@ export function struct(fields, options = {}, sizeof, alloc, readValue, writeValu
|
|
|
719
627
|
const value = current.call(structDef, values);
|
|
720
628
|
return convert === undefined ? value : convert(value, owners);
|
|
721
629
|
}
|
|
722
|
-
|
|
630
|
+
if (structDef.set === set && scalarWriters) return createScalar(values);
|
|
631
|
+
// A custom setter receives the buffer and may publish it to a later adapter.
|
|
632
|
+
const bytes = createBuffer(values);
|
|
633
|
+
return convert === undefined ? bytes : convert(bytes, owners);
|
|
723
634
|
});
|
|
724
635
|
const toObject = structDef.toObject;
|
|
725
636
|
const readers = fieldDefs.map((field) => [field.name, fieldReaders[field.name]]);
|
|
@@ -35,10 +35,16 @@
|
|
|
35
35
|
*/
|
|
36
36
|
|
|
37
37
|
import { bitFieldAccessors } from "./bitfield.js";
|
|
38
|
-
import {
|
|
38
|
+
import { readArrayField } from "./array.js";
|
|
39
|
+
import { declareFields } from "./fields.js";
|
|
40
|
+
import { normalizePack } from "./layout.js";
|
|
39
41
|
import { buildWrapper } from "./wrap.js";
|
|
40
42
|
import { syncDefinition } from "../../memory/mirror.js";
|
|
41
|
-
import {
|
|
43
|
+
import { kFromBytes } from "../Structure.js";
|
|
44
|
+
import { clearKeepAlives, copyInstance, pointerOwner } from "../../memory/ownership.js";
|
|
45
|
+
import { fieldsOf, simpleFieldWriter, writePointerField } from "./common.js";
|
|
46
|
+
import { structGetters } from "../protocol.js";
|
|
47
|
+
import { scalarReader } from "../../types/scalar-protocol.js";
|
|
42
48
|
|
|
43
49
|
/**
|
|
44
50
|
* Creates a union type definition where all fields share offset 0.
|
|
@@ -115,113 +121,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
115
121
|
const swapped = options.swapped === true;
|
|
116
122
|
// Python _pack_: `pack` da solo, o `packed: true` (= pack 1).
|
|
117
123
|
const packValue = normalizePack(options.pack ?? (options.packed ? 1 : 0));
|
|
118
|
-
const
|
|
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);
|
|
129
|
-
let size, alignment;
|
|
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._finalizeFields();
|
|
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
|
-
}
|
|
149
|
-
// Nested struct (object form or class form)
|
|
150
|
-
else if ((typeof type === "function" && type.prototype instanceof Structure) || _isStruct(type)) {
|
|
151
|
-
// Normalize type: if it's a Structure/Union class, get its _structDef
|
|
152
|
-
let nestedStruct = type;
|
|
153
|
-
if (typeof type === "function" && typeof type._buildStruct === "function") {
|
|
154
|
-
nestedStruct = type._finalizeFields();
|
|
155
|
-
fieldDef.type = nestedStruct;
|
|
156
|
-
// Python restituisce un'istanza della classe annidata.
|
|
157
|
-
fieldDef.cls = type;
|
|
158
|
-
}
|
|
159
|
-
size = nestedStruct.size;
|
|
160
|
-
alignment = nestedStruct.alignment;
|
|
161
|
-
fieldDef.isNested = true;
|
|
162
|
-
}
|
|
163
|
-
// Array type: alignment == element alignment (C standard)
|
|
164
|
-
else if (_isArrayType(type)) {
|
|
165
|
-
if (swapped) fieldDef.type = type._swappedType ?? type;
|
|
166
|
-
size = type.getSize();
|
|
167
|
-
alignment = typeof type.getAlignment === "function" ? type.getAlignment() : Math.min(sizeof(type.elementType), native.POINTER_SIZE);
|
|
168
|
-
fieldDef.isArray = true;
|
|
169
|
-
}
|
|
170
|
-
// Bit field
|
|
171
|
-
else if (_isBitField(type)) {
|
|
172
|
-
size = type.baseSize;
|
|
173
|
-
alignment = Math.min(size, native.POINTER_SIZE);
|
|
174
|
-
fieldDef.isBitField = true;
|
|
175
|
-
fieldDef.bitSize = type.bits;
|
|
176
|
-
fieldDef.baseSize = type.baseSize;
|
|
177
|
-
fieldDef.type = type.baseType; // Usa il tipo base per lettura/scrittura
|
|
178
|
-
}
|
|
179
|
-
// POINTER() type
|
|
180
|
-
else if ((typeof type === "object" && type !== null && type._pointerTo !== undefined) || (typeof type === "function" && type._isFuncPtr === true)) {
|
|
181
|
-
size = native.POINTER_SIZE;
|
|
182
|
-
alignment = native.POINTER_SIZE;
|
|
183
|
-
// CFUNCTYPE: campo puntatore letto come funzione chiamabile (Python parity)
|
|
184
|
-
fieldDef.type = typeof type === "function" ? funcPtrFieldType(type) : type;
|
|
185
|
-
fieldDef.isPointer = true;
|
|
186
|
-
}
|
|
187
|
-
// Tipo base (SimpleCData)
|
|
188
|
-
else {
|
|
189
|
-
// Validate type is a SimpleCData class
|
|
190
|
-
if (!(typeof type === "function" && type._isSimpleCData)) {
|
|
191
|
-
throw new TypeError(`union field "${name}": type must be a SimpleCData class, struct, union, or array`);
|
|
192
|
-
}
|
|
193
|
-
size = type._size;
|
|
194
|
-
alignment = Math.min(size, native.POINTER_SIZE);
|
|
195
|
-
fieldDef.type = type;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
fieldDef.size = size;
|
|
199
|
-
fieldDef.alignment = alignment;
|
|
200
|
-
ownDefs.push(fieldDef);
|
|
201
|
-
}
|
|
202
|
-
|
|
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;
|
|
219
|
-
|
|
220
|
-
// Crea Map per lookup O(1)
|
|
221
|
-
const fieldMap = new Map();
|
|
222
|
-
for (const field of fieldDefs) {
|
|
223
|
-
fieldMap.set(field.name, field);
|
|
224
|
-
}
|
|
124
|
+
const { base, fieldDefs, fieldMap, size: maxSize, alignment: maxAlignment } = declareFields(fields, options, { isStruct: false, pack: packValue, swapped, sizeof, _isStruct, _isArrayType, _isBitField, Structure, native });
|
|
225
125
|
|
|
226
126
|
// Pre-compile reader/writer per campi primitivi e POINTER — evita il
|
|
227
127
|
// dispatch completo in unionDef.get/set a ogni accesso. Per i tipi
|
|
@@ -341,7 +241,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
341
241
|
);
|
|
342
242
|
},
|
|
343
243
|
|
|
344
|
-
get(bufOrObj, fieldName) {
|
|
244
|
+
get(bufOrObj, fieldName, owner) {
|
|
345
245
|
// NUOVO: supporta sia buffer che object wrapper
|
|
346
246
|
let buf;
|
|
347
247
|
if (Buffer.isBuffer(bufOrObj)) {
|
|
@@ -358,7 +258,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
358
258
|
if (!field) {
|
|
359
259
|
const view = anonymousView(buf, fieldName);
|
|
360
260
|
if (view === undefined) throw new Error(`Unknown field: ${fieldName}`);
|
|
361
|
-
return view.def.get(view.buf, fieldName);
|
|
261
|
+
return view.def.get(view.buf, fieldName, owner);
|
|
362
262
|
}
|
|
363
263
|
|
|
364
264
|
// Bit field
|
|
@@ -370,7 +270,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
370
270
|
if (field.isNested) {
|
|
371
271
|
const nestedBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
372
272
|
// Classe Structure/Union: un'istanza sulla stessa memoria (Python).
|
|
373
|
-
if (field.cls !== undefined) return
|
|
273
|
+
if (field.cls !== undefined) return field.cls[kFromBytes](nestedBuf, owner);
|
|
374
274
|
// Proxy-based nested instance
|
|
375
275
|
return new Proxy(nestedBuf, {
|
|
376
276
|
get(target, prop, receiver) {
|
|
@@ -434,8 +334,7 @@ export function union(fields, options, sizeof, alloc, readValue, writeValue, _is
|
|
|
434
334
|
// Array (Python: c_char * n / c_wchar * n si leggono come stringa)
|
|
435
335
|
if (field.isArray) {
|
|
436
336
|
const arrayBuf = buf.subarray(field.offset, field.offset + field.size);
|
|
437
|
-
|
|
438
|
-
return field.type.wrap(arrayBuf);
|
|
337
|
+
return readArrayField(field.type, arrayBuf, owner);
|
|
439
338
|
}
|
|
440
339
|
|
|
441
340
|
// POINTER() field — return PointerInstance
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
import { mirroredAccessors } from "../../memory/mirror.js";
|
|
22
|
-
import { structGetters } from "
|
|
22
|
+
import { structGetters } from "../protocol.js";
|
|
23
23
|
|
|
24
24
|
// Proprietà meta del Buffer che NON vanno mai interpretate come field name.
|
|
25
25
|
// Per i campi Proxy passano direttamente attraverso al target buffer.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Internal aggregate identity and operations, shared without modifying user declarations. */
|
|
2
|
+
|
|
3
|
+
export const structArgumentFactories = new WeakMap();
|
|
4
|
+
export const structResultReaders = new WeakMap();
|
|
5
|
+
export const structGetters = new WeakMap();
|
|
6
|
+
|
|
7
|
+
// A field may shadow the public constructor property.
|
|
8
|
+
export const structClass = Symbol("constructor");
|
package/lib/types/SimpleCData.js
CHANGED
|
@@ -46,7 +46,8 @@
|
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
48
|
|
|
49
|
-
import { copyInstance, inDllBuffer,
|
|
49
|
+
import { copyInstance, inDllBuffer, sourceBytes } from "../memory/ownership.js";
|
|
50
|
+
import { registerScalarAccessor, scalarOutparam, scalarTypeInfo, scalarView } from "./scalar-protocol.js";
|
|
50
51
|
|
|
51
52
|
/**
|
|
52
53
|
* Creates the SimpleCData base class with required dependencies injected.
|
package/lib/types/primitives.js
CHANGED
|
@@ -60,7 +60,8 @@
|
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
62
|
|
|
63
|
-
import {
|
|
63
|
+
import { keepAlive } from "../memory/ownership.js";
|
|
64
|
+
import { booleanValue, notIntegral, registerSimpleType } from "./scalar-protocol.js";
|
|
64
65
|
|
|
65
66
|
/**
|
|
66
67
|
* Creates all primitive type classes with required dependencies injected.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/** Scalar conversion and views must distinguish built-in ctypes from application subclasses. */
|
|
2
|
+
import { flush, kMirrorBase, mirrorBase, mirroredPrototype, mirrorsActive, refresh } from "../memory/mirror.js";
|
|
3
|
+
|
|
4
|
+
const simpleTypes = new WeakSet();
|
|
5
|
+
const scalarInstances = new WeakMap();
|
|
6
|
+
const scalarTypeInfos = new WeakMap();
|
|
7
|
+
const scalarAccessors = [];
|
|
8
|
+
const scalarArgumentKinds = new Set();
|
|
9
|
+
const scalarArgumentTypes = new Map();
|
|
10
|
+
const applyScalarReader = Reflect.apply;
|
|
11
|
+
|
|
12
|
+
export function registerSimpleType(type) {
|
|
13
|
+
simpleTypes.add(type);
|
|
14
|
+
scalarArgumentKinds.add(type._type);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function scalarTypeInfo(type, swapped = false) {
|
|
18
|
+
let variants = scalarTypeInfos.get(type);
|
|
19
|
+
if (variants === undefined) {
|
|
20
|
+
const kind = type._type;
|
|
21
|
+
variants = [{ type, kind, swapped: false, pointer: type._isPointerValued === true }];
|
|
22
|
+
scalarTypeInfos.set(type, variants);
|
|
23
|
+
}
|
|
24
|
+
const index = swapped ? 1 : 0;
|
|
25
|
+
if (variants[index] === undefined) variants[index] = { ...variants[0], swapped: true };
|
|
26
|
+
return variants[index];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function registerScalarAccessor(accessor) {
|
|
30
|
+
scalarAccessors.push(accessor);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function registerScalarInstance(value, type, swapped = false) {
|
|
34
|
+
scalarInstances.set(value, scalarTypeInfo(type, swapped));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function scalarInstanceInfo(value) {
|
|
38
|
+
if (value === null || (typeof value !== "object" && typeof value !== "function")) return undefined;
|
|
39
|
+
for (const accessor of scalarAccessors) {
|
|
40
|
+
const info = accessor(value);
|
|
41
|
+
if (info !== undefined) return info;
|
|
42
|
+
}
|
|
43
|
+
return scalarInstances.get(value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function scalarArgumentKind(value) {
|
|
47
|
+
return scalarInstanceInfo(value)?.kind;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function scalarArgumentValue(value, info = scalarInstanceInfo(value)) {
|
|
51
|
+
if (info === undefined) return value.value;
|
|
52
|
+
const buffer = value._buffer;
|
|
53
|
+
if (mirrorsActive) refresh(buffer);
|
|
54
|
+
const reader = info.swapped ? info.type._readerBE : info.type._reader;
|
|
55
|
+
return applyScalarReader(reader, info.type, [buffer, 0]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function scalarArgumentSnapshot(value, info = scalarInstanceInfo(value)) {
|
|
59
|
+
if (info === undefined || info.pointer || info.kind === 0 || !scalarArgumentKinds.has(info.kind)) return value;
|
|
60
|
+
let constructor = scalarArgumentTypes.get(info.kind);
|
|
61
|
+
if (constructor === undefined) {
|
|
62
|
+
constructor = function ScalarArgument() {};
|
|
63
|
+
Object.defineProperties(constructor, {
|
|
64
|
+
_isSimpleCData: { value: true },
|
|
65
|
+
_type: { value: info.kind },
|
|
66
|
+
});
|
|
67
|
+
scalarArgumentTypes.set(info.kind, constructor);
|
|
68
|
+
}
|
|
69
|
+
// Keep the inferred type, but snapshot the value before the next adapter.
|
|
70
|
+
return { constructor, value: scalarArgumentValue(value, info) };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function isScalarSubclass(type) {
|
|
74
|
+
return typeof type === "function" && type._isSimpleCData === true && !simpleTypes.has(type);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function scalarView(type, buffer, swapped = false) {
|
|
78
|
+
// C-owned representations must bypass application constructors and writers.
|
|
79
|
+
const base = mirrorsActive ? mirrorBase(buffer) : undefined;
|
|
80
|
+
const value = Object.create(base === undefined ? type.prototype : mirroredPrototype(type));
|
|
81
|
+
value._buffer = buffer;
|
|
82
|
+
if (base !== undefined) value[kMirrorBase] = base;
|
|
83
|
+
const useSwapped = !!(swapped && type._readerBE && type._writerBE);
|
|
84
|
+
registerScalarInstance(value, type, useSwapped);
|
|
85
|
+
if (useSwapped) {
|
|
86
|
+
Object.defineProperty(value, "value", {
|
|
87
|
+
get() {
|
|
88
|
+
if (base !== undefined) refresh(buffer);
|
|
89
|
+
return type._readerBE(buffer, 0);
|
|
90
|
+
},
|
|
91
|
+
set(v) {
|
|
92
|
+
type._writerBE(buffer, 0, v);
|
|
93
|
+
if (base !== undefined) flush(buffer);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function scalarReader(type, reader, swapped = false) {
|
|
101
|
+
return isScalarSubclass(type) ? (buffer, offset) => scalarView(type, buffer.subarray(offset, offset + type._size), swapped) : reader;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function scalarOutparam() {
|
|
105
|
+
return isScalarSubclass(this.constructor) ? this : this.value;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Python: a float is not an integer ("'float' object cannot be interpreted as
|
|
110
|
+
* an integer"); in JS, numbers that are not integral (or not finite).
|
|
111
|
+
*
|
|
112
|
+
* @throws {TypeError}
|
|
113
|
+
*/
|
|
114
|
+
export function notIntegral() {
|
|
115
|
+
throw new TypeError("'float' object cannot be interpreted as an integer");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function booleanValue(value) {
|
|
119
|
+
return !!(value !== null && typeof value === "object" && value._isSimpleCDataInstance === true ? value.value : value);
|
|
120
|
+
}
|