node-ctypes 1.14.0 → 2.0.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.
@@ -74,6 +74,147 @@
74
74
  * @private
75
75
  */
76
76
  export function createStructureClass(alloc, struct, bitfield, ptrToBuffer) {
77
+ // Buffer dell'istanza, letto dall'handler condiviso. Chiave privata e non
78
+ // `__buffer`, così riassegnare `__buffer` non cambia i campi (come prima).
79
+ const kBuffer = Symbol("buffer");
80
+
81
+ // Un handler Proxy per definizione, condiviso da tutte le istanze: prima
82
+ // ogni `new` ricreava l'handler (5 closure) e la mappa dei nomi dei campi.
83
+ const handlerCache = new WeakMap();
84
+
85
+ function getProxyHandler(def) {
86
+ let handler = handlerCache.get(def);
87
+ if (handler !== undefined) return handler;
88
+
89
+ // Lookup plana dei nomi (normali + sotto-campi anonymous) su oggetto
90
+ // null-proto: bracket access è inline-cached da V8 (più veloce di
91
+ // Map.has / Set.has che sono builtin megamorfici).
92
+ const allFieldNames = Object.create(null);
93
+ for (const field of def.fields) {
94
+ allFieldNames[field.name] = true;
95
+ if (field.isAnonymous && field.type && Array.isArray(field.type.fields)) {
96
+ for (const subField of field.type.fields) {
97
+ allFieldNames[subField.name] = true;
98
+ }
99
+ }
100
+ }
101
+
102
+ // Pre-compiled reader/writer maps from structDef for FAST PATH
103
+ // (avoids going through def.get/def.set branching on every access)
104
+ const readers = def.fieldReaders;
105
+ const writers = def.fieldWriters;
106
+
107
+ handler = {
108
+ get(target, prop, receiver) {
109
+ // FAST PATH: pre-compiled reader (simple scalar / pointer fields).
110
+ // `readers` può essere undefined quando questa Structure è in realtà
111
+ // una Union (unionDef non espone fieldReaders al livello esterno).
112
+ if (typeof prop === "string") {
113
+ const reader = readers !== undefined ? readers[prop] : undefined;
114
+ if (reader !== undefined) return reader(target[kBuffer]);
115
+ if (allFieldNames[prop] === true) return def.get(target[kBuffer], prop);
116
+ }
117
+
118
+ // SLOW PATH: special properties and methods
119
+ if (typeof prop === "symbol") {
120
+ if (prop === Symbol.toStringTag) return target.constructor.name || "Structure";
121
+ return undefined;
122
+ }
123
+
124
+ if (prop === "_buffer" || prop === "__buffer") return target[kBuffer];
125
+ if (prop === "_structDef" || prop === "__structDef") return def;
126
+ // La classe, non una sua copia legata (x.constructor === Point).
127
+ if (prop === "constructor") return target.constructor;
128
+
129
+ const method = target[prop];
130
+ if (typeof method === "function") {
131
+ // Bind al Proxy (receiver), NON al target raw. Un metodo custom
132
+ // come `mag2() { return this.x * this.x + ... }` dentro fa
133
+ // `this.x` che deve passare attraverso il Proxy get trap per
134
+ // leggere il field. Binding su `target` (istanza raw) salta
135
+ // il trap e ritorna undefined → NaN nei calcoli.
136
+ return method.bind(receiver);
137
+ }
138
+
139
+ return Reflect.get(target, prop, receiver);
140
+ },
141
+ set(target, prop, value, receiver) {
142
+ // FAST PATH: pre-compiled writer (null-proto obj, bracket access)
143
+ if (typeof prop === "string") {
144
+ const writer = writers !== undefined ? writers[prop] : undefined;
145
+ if (writer !== undefined) {
146
+ writer(target[kBuffer], value);
147
+ return true;
148
+ }
149
+ if (allFieldNames[prop] === true) {
150
+ def.set(target[kBuffer], prop, value);
151
+ return true;
152
+ }
153
+ }
154
+
155
+ return Reflect.set(target, prop, value, receiver);
156
+ },
157
+ has(target, prop) {
158
+ if (typeof prop === "string" && allFieldNames[prop] === true) return true;
159
+ if (prop === "_buffer" || prop === "_structDef" || prop === "toObject") return true;
160
+ return Reflect.has(target, prop);
161
+ },
162
+ ownKeys(target) {
163
+ const keys = [];
164
+ for (const f of def.fields) {
165
+ if (f.isAnonymous && f.type && Array.isArray(f.type.fields)) {
166
+ for (const sf of f.type.fields) {
167
+ keys.push(sf.name);
168
+ }
169
+ } else {
170
+ keys.push(f.name);
171
+ }
172
+ }
173
+ return keys;
174
+ },
175
+ getOwnPropertyDescriptor(target, prop) {
176
+ if (typeof prop === "string" && allFieldNames[prop] === true) {
177
+ return {
178
+ enumerable: true,
179
+ configurable: true,
180
+ writable: true,
181
+ };
182
+ }
183
+ if (prop === "_buffer" || prop === "_structDef") {
184
+ return {
185
+ enumerable: false,
186
+ configurable: true,
187
+ writable: true,
188
+ };
189
+ }
190
+ return Reflect.getOwnPropertyDescriptor(target, prop);
191
+ },
192
+ };
193
+ handlerCache.set(def, handler);
194
+ return handler;
195
+ }
196
+
197
+ // Scrittore precompilato del campo quando esiste, altrimenti def.set generico.
198
+ function setField(def, buf, name, value) {
199
+ const writer = def.fieldWriters !== undefined ? def.fieldWriters[name] : undefined;
200
+ if (writer !== undefined) {
201
+ writer(buf, value);
202
+ } else {
203
+ def.set(buf, name, value);
204
+ }
205
+ }
206
+
207
+ // Campi assegnati dagli argomenti posizionali (new Point(10, 20)).
208
+ const positionalCache = new WeakMap();
209
+
210
+ function getPositionalFields(def) {
211
+ let ordered = positionalCache.get(def);
212
+ if (ordered === undefined) {
213
+ ordered = def.fields.filter((f) => !f.isAnonymous);
214
+ positionalCache.set(def, ordered);
215
+ }
216
+ return ordered;
217
+ }
77
218
  /**
78
219
  * Structure base class for creating C-style structures.
79
220
  *
@@ -140,15 +281,19 @@ export function createStructureClass(alloc, struct, bitfield, ptrToBuffer) {
140
281
  constructor(...args) {
141
282
  const Ctor = this.constructor;
142
283
  const def = Ctor._structDef || Ctor._buildStruct();
143
- // Allocate buffer for instance
144
- let buf = alloc(def.size);
145
- buf.fill(0);
284
+ let buf;
146
285
 
147
286
  // Support positional args: new Point(10,20)
148
- if (args.length === 1 && typeof args[0] === "object" && !Array.isArray(args[0]) && !Buffer.isBuffer(args[0])) {
287
+ // Un'istanza di struct/union come unico argomento è posizionale
288
+ // (Python: RECT(point)), non un oggetto di inizializzazione per nome.
289
+ const single = args.length === 1 ? args[0] : undefined;
290
+ const isNamedInit = typeof single === "object" && single !== null && !Array.isArray(single) && !Buffer.isBuffer(single) && single._structDef === undefined;
291
+ if (isNamedInit) {
292
+ // alloc() restituisce un buffer già azzerato (Buffer.alloc)
293
+ buf = alloc(def.size);
149
294
  const initial = args[0];
150
295
  for (const [k, v] of Object.entries(initial)) {
151
- def.set(buf, k, v);
296
+ setField(def, buf, k, v);
152
297
  }
153
298
  } else if (args.length === 1 && Buffer.isBuffer(args[0])) {
154
299
  // new Point(buffer) - wrap existing buffer
@@ -157,120 +302,23 @@ export function createStructureClass(alloc, struct, bitfield, ptrToBuffer) {
157
302
  if (buf.length < def.size) {
158
303
  throw new RangeError(`Buffer size (${buf.length}) is smaller than struct size (${def.size})`);
159
304
  }
160
- } else if (args.length > 0) {
161
- // Positional mapping to non-anonymous declared fields order
162
- const ordered = def.fields.filter((f) => !f.isAnonymous);
163
- for (let i = 0; i < Math.min(args.length, ordered.length); i++) {
164
- def.set(buf, ordered[i].name, args[i]);
305
+ } else {
306
+ buf = alloc(def.size);
307
+ if (args.length > 0) {
308
+ // Positional mapping to non-anonymous declared fields order
309
+ const ordered = getPositionalFields(def);
310
+ for (let i = 0; i < Math.min(args.length, ordered.length); i++) {
311
+ setField(def, buf, ordered[i].name, args[i]);
312
+ }
165
313
  }
166
314
  }
167
315
 
168
316
  // Store internal references on this (needed for methods like toObject)
169
317
  this.__buffer = buf;
170
318
  this.__structDef = def;
319
+ this[kBuffer] = buf;
171
320
 
172
- // Lookup plana dei nomi (normali + sotto-campi anonymous) su oggetto
173
- // null-proto: bracket access è inline-cached da V8 (più veloce di
174
- // Map.has / Set.has che sono builtin megamorfici).
175
- const allFieldNames = Object.create(null);
176
- for (const field of def.fields) {
177
- allFieldNames[field.name] = true;
178
- if (field.isAnonymous && field.type && Array.isArray(field.type.fields)) {
179
- for (const subField of field.type.fields) {
180
- allFieldNames[subField.name] = true;
181
- }
182
- }
183
- }
184
-
185
- // Cache pre-compiled reader/writer maps from structDef for FAST PATH
186
- // (avoids going through def.get/def.set branching on every access)
187
- const readers = def.fieldReaders;
188
- const writers = def.fieldWriters;
189
-
190
- return new Proxy(this, {
191
- get(target, prop, receiver) {
192
- // FAST PATH: pre-compiled reader (simple scalar / pointer fields).
193
- // `readers` può essere undefined quando questa Structure è in realtà
194
- // una Union (unionDef non espone fieldReaders al livello esterno).
195
- if (typeof prop === "string") {
196
- const reader = readers !== undefined ? readers[prop] : undefined;
197
- if (reader !== undefined) return reader(buf);
198
- if (allFieldNames[prop] === true) return def.get(buf, prop);
199
- }
200
-
201
- // SLOW PATH: special properties and methods
202
- if (typeof prop === "symbol") {
203
- if (prop === Symbol.toStringTag) return Ctor.name || "Structure";
204
- return undefined;
205
- }
206
-
207
- if (prop === "_buffer" || prop === "__buffer") return buf;
208
- if (prop === "_structDef" || prop === "__structDef") return def;
209
-
210
- const method = target[prop];
211
- if (typeof method === "function") {
212
- // Bind al Proxy (receiver), NON al target raw. Un metodo custom
213
- // come `mag2() { return this.x * this.x + ... }` dentro fa
214
- // `this.x` che deve passare attraverso il Proxy get trap per
215
- // leggere il field. Binding su `target` (istanza raw) salta
216
- // il trap e ritorna undefined → NaN nei calcoli.
217
- return method.bind(receiver);
218
- }
219
-
220
- return Reflect.get(target, prop, receiver);
221
- },
222
- set(target, prop, value, receiver) {
223
- // FAST PATH: pre-compiled writer (null-proto obj, bracket access)
224
- if (typeof prop === "string") {
225
- const writer = writers !== undefined ? writers[prop] : undefined;
226
- if (writer !== undefined) {
227
- writer(buf, value);
228
- return true;
229
- }
230
- if (allFieldNames[prop] === true) {
231
- def.set(buf, prop, value);
232
- return true;
233
- }
234
- }
235
-
236
- return Reflect.set(target, prop, value, receiver);
237
- },
238
- has(target, prop) {
239
- if (typeof prop === "string" && allFieldNames[prop] === true) return true;
240
- if (prop === "_buffer" || prop === "_structDef" || prop === "toObject") return true;
241
- return Reflect.has(target, prop);
242
- },
243
- ownKeys(target) {
244
- const keys = [];
245
- for (const f of def.fields) {
246
- if (f.isAnonymous && f.type && Array.isArray(f.type.fields)) {
247
- for (const sf of f.type.fields) {
248
- keys.push(sf.name);
249
- }
250
- } else {
251
- keys.push(f.name);
252
- }
253
- }
254
- return keys;
255
- },
256
- getOwnPropertyDescriptor(target, prop) {
257
- if (typeof prop === "string" && allFieldNames[prop] === true) {
258
- return {
259
- enumerable: true,
260
- configurable: true,
261
- writable: true,
262
- };
263
- }
264
- if (prop === "_buffer" || prop === "_structDef") {
265
- return {
266
- enumerable: false,
267
- configurable: true,
268
- writable: true,
269
- };
270
- }
271
- return Reflect.getOwnPropertyDescriptor(target, prop);
272
- },
273
- });
321
+ return new Proxy(this, getProxyHandler(def));
274
322
  }
275
323
 
276
324
  // Static getters for size, alignment, and fields (enables use as nested type)
@@ -104,13 +104,42 @@
104
104
  * points[0].y = 20;
105
105
  * ```
106
106
  */
107
- export function array(elementType, count, sizeof, alloc, readValue, writeValue, Structure, Union, ArrayType) {
107
+ import { encodeWide, decodeWide } from "../../memory/buffer.js";
108
+ import { funcPtrFieldType, writePointerField } from "./common.js";
109
+
110
+ // Python: gli array di c_char / c_wchar (anche come campi struct) hanno un
111
+ // `.value` stringa fino al primo NUL; quelli di c_char anche `.raw` (bytes).
112
+ function charArrayValue(buf, charKind, wcharSize) {
113
+ if (charKind === "char") {
114
+ const end = buf.indexOf(0);
115
+ return buf.toString("utf8", 0, end === -1 ? buf.length : end);
116
+ }
117
+ return decodeWide(buf, wcharSize);
118
+ }
119
+
120
+ function setCharArrayValue(buf, charKind, wcharSize, value, message) {
121
+ const units = charKind === "char" ? (Buffer.isBuffer(value) ? value : Buffer.from(String(value), "utf8")) : encodeWide(String(value), wcharSize);
122
+ if (units.length > buf.length) {
123
+ throw new RangeError(message(units.length / (charKind === "char" ? 1 : wcharSize), buf.length / (charKind === "char" ? 1 : wcharSize)));
124
+ }
125
+ units.copy(buf);
126
+ const unit = charKind === "char" ? 1 : wcharSize;
127
+ if (units.length + unit <= buf.length) {
128
+ buf.fill(0, units.length, units.length + unit);
129
+ }
130
+ }
131
+
132
+ export function array(elementType, count, sizeof, alloc, readValue, writeValue, Structure, Union, ArrayType, voidPointerType) {
108
133
  // Validate elementType: accept SimpleCData classes, Structure/Union classes,
109
134
  // or native CType-like objects.
110
135
  const isSimple = typeof elementType === "function" && elementType._isSimpleCData;
111
136
  const isStruct = typeof elementType === "function" && (elementType.prototype instanceof Structure || elementType.prototype instanceof Union);
112
- const isNativeTypeObj = typeof elementType === "object" && elementType !== null && (typeof elementType.getSize === "function" || elementType.size !== undefined);
113
- if (!isSimple && !isStruct && !isNativeTypeObj) {
137
+ // Array di puntatori (Python: POINTER(T) * n, CFUNCTYPE * n).
138
+ const isPointerElem = (typeof elementType === "object" && elementType !== null && elementType._pointerTo !== undefined) || (typeof elementType === "function" && elementType._isFuncPtr === true);
139
+ const isNativeTypeObj = !isPointerElem && typeof elementType === "object" && elementType !== null && (typeof elementType.getSize === "function" || elementType.size !== undefined);
140
+ // Array di array (Python: (c_int * 2) * 3).
141
+ const isArrayElem = typeof elementType === "function" && elementType._isArrayType === true;
142
+ if (!isSimple && !isStruct && !isNativeTypeObj && !isArrayElem && !isPointerElem) {
114
143
  throw new TypeError("array elementType must be a SimpleCData class or a Structure/Union class");
115
144
  }
116
145
 
@@ -118,9 +147,11 @@ export function array(elementType, count, sizeof, alloc, readValue, writeValue,
118
147
  // Array alignment == element alignment (C standard), NOT element size.
119
148
  // For Structure/Union: ask the class. For SimpleCData: alignment == size
120
149
  // (each primitive is self-aligned on all platforms we target).
121
- const elementAlignment = isStruct ? (elementType.alignment ?? elementSize) : elementSize;
150
+ const elementAlignment = isStruct ? (elementType.alignment ?? elementSize) : isArrayElem ? elementType.getAlignment() : elementSize;
122
151
  let nativeArray;
123
- if (isNativeTypeObj && ArrayType) {
152
+ if (isPointerElem && ArrayType && voidPointerType) {
153
+ nativeArray = new ArrayType(voidPointerType._type, count);
154
+ } else if (isNativeTypeObj && ArrayType) {
124
155
  nativeArray = new ArrayType(elementType, count);
125
156
  } else if (isSimple && ArrayType) {
126
157
  // Array di SimpleCData: costruiamo un vero native ArrayType passando
@@ -147,10 +178,104 @@ export function array(elementType, count, sizeof, alloc, readValue, writeValue,
147
178
  };
148
179
  }
149
180
 
181
+ // Precalcolati una volta per tipo: l'handler del Proxy li ripeteva ad ogni
182
+ // accesso indicizzato (instanceof Structure/Union + dispatch di readValue).
183
+ const isStructElem = typeof elementType === "function" && (elementType.prototype instanceof Structure || elementType.prototype instanceof Union);
184
+ const elemReader = isSimple ? elementType._reader : null;
185
+ const elemWriter = isSimple ? elementType._writer : null;
186
+
187
+ // Array di caratteri (Python: c_char * n / c_wchar * n): `.value` e `.raw`.
188
+ const charKind = isSimple ? elementType._charKind : undefined;
189
+ const tooLong = charKind === "char" ? () => "byte string too long" : () => "string too long";
190
+ const getValue = charKind === undefined ? undefined : (buf) => charArrayValue(buf, charKind, elementSize);
191
+ const setValue = charKind === undefined ? undefined : (buf, value, message = tooLong) => setCharArrayValue(buf, charKind, elementSize, value, message);
192
+ const setRaw = (buf, value) => {
193
+ const bytes = Buffer.isBuffer(value) ? value : Buffer.from(String(value), "utf8");
194
+ if (bytes.length > buf.length) throw new RangeError("byte string too long");
195
+ bytes.copy(buf);
196
+ };
197
+
198
+ // Elementi puntatore: NULL → null, altrimenti oggetto puntatore / funzione.
199
+ const pointerElemType = !isPointerElem ? null : typeof elementType === "function" ? funcPtrFieldType(elementType) : elementType;
200
+ const readPointer = (target, off) => {
201
+ const address = elementSize === 8 ? target.readBigUInt64LE(off) : BigInt(target.readUInt32LE(off));
202
+ return address === 0n ? null : pointerElemType.fromAddress(address);
203
+ };
204
+ const writePointer = (target, off, value) => writePointerField(target, off, value, elementSize);
205
+
206
+ const readAt = (target, index) => {
207
+ if (index >= 0 && index < count) {
208
+ const off = index * elementSize;
209
+ if (pointerElemType !== null) {
210
+ return readPointer(target, off);
211
+ }
212
+ if (isStructElem) {
213
+ return new elementType(target.subarray(off, off + elementSize));
214
+ }
215
+ if (isArrayElem) {
216
+ return elementType.wrap(target.subarray(off, off + elementSize));
217
+ }
218
+ if (elemReader !== null && off + elementSize <= target.length) {
219
+ return elemReader(target, off);
220
+ }
221
+ return readValue(target, elementType, off);
222
+ }
223
+ // Numeric index out of bounds -> undefined (JavaScript behavior)
224
+ return undefined;
225
+ };
226
+
227
+ const writeAt = (target, index, value) => {
228
+ if (index >= 0 && index < count) {
229
+ const off = index * elementSize;
230
+ if (pointerElemType !== null) {
231
+ writePointer(target, off, value);
232
+ } else if (isArrayElem) {
233
+ const inner = target.subarray(off, off + elementSize);
234
+ const source = value !== null && typeof value === "object" && Buffer.isBuffer(value._buffer) ? value._buffer : value;
235
+ if (Buffer.isBuffer(source)) {
236
+ source.copy(inner, 0, 0, elementSize);
237
+ } else if (source !== null && typeof source === "object" && typeof source.length === "number") {
238
+ const view = elementType.wrap(inner);
239
+ for (let i = 0; i < Math.min(source.length, elementType.length); i++) view[i] = source[i];
240
+ }
241
+ } else if (elemWriter !== null && off + elementSize <= target.length) {
242
+ elemWriter(target, off, value);
243
+ } else {
244
+ writeValue(target, elementType, value, off);
245
+ }
246
+ return true;
247
+ }
248
+ // Index out of bounds - don't write anything but return false
249
+ return false;
250
+ };
251
+
252
+ // Indice da una chiave che inizia con una cifra (il caso comune arr[i]);
253
+ // NaN per tutto il resto, che segue il percorso generico.
254
+ const fastIndex = (prop) => {
255
+ if (typeof prop === "string") {
256
+ const c = prop.charCodeAt(0);
257
+ if (c >= 48 && c <= 57) {
258
+ const index = Number(prop);
259
+ if (Number.isInteger(index)) return index;
260
+ }
261
+ }
262
+ return NaN;
263
+ };
264
+
150
265
  // Wrap function that returns a Proxy for array-like indexing
151
266
  const wrap = function (buffer) {
152
267
  return new Proxy(buffer, {
153
268
  get(target, prop, receiver) {
269
+ const fast = fastIndex(prop);
270
+ if (fast === fast) {
271
+ return readAt(target, fast);
272
+ }
273
+
274
+ if (charKind !== undefined) {
275
+ if (prop === "value") return getValue(target);
276
+ if (prop === "raw" && charKind === "char") return Buffer.from(target);
277
+ }
278
+
154
279
  // Special case for _buffer to return the underlying buffer
155
280
  if (prop === "_buffer") {
156
281
  return target;
@@ -179,13 +304,7 @@ export function array(elementType, count, sizeof, alloc, readValue, writeValue,
179
304
  if (prop === Symbol.iterator) {
180
305
  return function* () {
181
306
  for (let i = 0; i < count; i++) {
182
- const off = i * elementSize;
183
- // If elementType is a struct/union class, return an instance bound to the slice
184
- if (typeof elementType === "function" && (elementType.prototype instanceof Structure || elementType.prototype instanceof Union)) {
185
- yield new elementType(target.subarray(off, off + elementSize));
186
- } else {
187
- yield readValue(target, elementType, off);
188
- }
307
+ yield readAt(target, i);
189
308
  }
190
309
  };
191
310
  }
@@ -202,15 +321,7 @@ export function array(elementType, count, sizeof, alloc, readValue, writeValue,
202
321
  // If it's a number (index) - intercept BEFORE checking target
203
322
  const index = Number(prop);
204
323
  if (Number.isInteger(index) && !isNaN(index)) {
205
- if (index >= 0 && index < count) {
206
- const off = index * elementSize;
207
- if (typeof elementType === "function" && (elementType.prototype instanceof Structure || elementType.prototype instanceof Union)) {
208
- return new elementType(target.subarray(off, off + elementSize));
209
- }
210
- return readValue(target, elementType, off);
211
- }
212
- // Numeric index out of bounds -> undefined (JavaScript behavior)
213
- return undefined;
324
+ return readAt(target, index);
214
325
  }
215
326
 
216
327
  // For everything else, use the normal buffer
@@ -222,6 +333,22 @@ export function array(elementType, count, sizeof, alloc, readValue, writeValue,
222
333
  return value;
223
334
  },
224
335
  set(target, prop, value) {
336
+ const fast = fastIndex(prop);
337
+ if (fast === fast) {
338
+ return writeAt(target, fast, value);
339
+ }
340
+
341
+ if (charKind !== undefined) {
342
+ if (prop === "value") {
343
+ setValue(target, value);
344
+ return true;
345
+ }
346
+ if (prop === "raw" && charKind === "char") {
347
+ setRaw(target, value);
348
+ return true;
349
+ }
350
+ }
351
+
225
352
  // Ignore Symbols
226
353
  if (typeof prop === "symbol") {
227
354
  return Reflect.set(target, prop, value, target);
@@ -232,53 +359,62 @@ export function array(elementType, count, sizeof, alloc, readValue, writeValue,
232
359
 
233
360
  const index = Number(prop);
234
361
  if (Number.isInteger(index) && !isNaN(index)) {
235
- if (index >= 0 && index < count) {
236
- writeValue(target, elementType, value, index * elementSize);
237
- return true;
238
- }
239
- // Index out of bounds - don't write anything but return false
240
- return false;
362
+ return writeAt(target, index, value);
241
363
  }
242
364
  return Reflect.set(target, prop, value, target);
243
365
  },
244
366
  });
245
367
  };
246
368
 
247
- // Return a wrapper object that delegates to nativeArray but overrides create()
248
- return {
369
+ // create automatically returns the proxy
370
+ const create = (values) => {
371
+ const buffer = alloc(count * elementSize);
372
+ if (Array.isArray(values)) {
373
+ for (let i = 0; i < Math.min(values.length, count); i++) {
374
+ writeAt(buffer, i, values[i]);
375
+ }
376
+ } else if (typeof values === "string") {
377
+ for (let i = 0; i < Math.min(values.length, count); i++) {
378
+ writeAt(buffer, i, values.charCodeAt(i));
379
+ }
380
+ }
381
+ return wrap(buffer);
382
+ };
383
+
384
+ // Python: il tipo array si chiama con gli elementi, (c_int * 3)(1, 2, 3)
385
+ // → array(c_int, 3)(1, 2, 3) (anche con `new`). `.create([...])` resta.
386
+ const ArrayCType = function (...values) {
387
+ return create(values);
388
+ };
389
+
390
+ // `length` (numero di elementi) è una proprietà propria non scrivibile delle
391
+ // funzioni: va ridefinita con defineProperties.
392
+ Object.defineProperties(ArrayCType, {
249
393
  // Type information
250
- elementType,
251
- length: count,
394
+ elementType: { value: elementType, enumerable: true },
395
+ length: { value: count, enumerable: true },
252
396
 
253
397
  // Delegated methods
254
- getSize: () => nativeArray.getSize(),
255
- getLength: () => nativeArray.getLength(),
256
- getAlignment: () => nativeArray.getAlignment(),
257
-
258
- // create automatically returns the proxy
259
- create: (values) => {
260
- const size = count * sizeof(elementType);
261
- const buffer = alloc(size);
262
- buffer.fill(0);
263
- if (Array.isArray(values)) {
264
- for (let i = 0; i < Math.min(values.length, count); i++) {
265
- writeValue(buffer, elementType, values[i], i * sizeof(elementType));
266
- }
267
- } else if (typeof values === "string") {
268
- for (let i = 0; i < Math.min(values.length, count); i++) {
269
- writeValue(buffer, elementType, values.charCodeAt(i), i * sizeof(elementType));
270
- }
271
- }
272
- return wrap(buffer);
273
- },
398
+ getSize: { value: () => nativeArray.getSize(), enumerable: true },
399
+ getLength: { value: () => nativeArray.getLength(), enumerable: true },
400
+ getAlignment: { value: () => nativeArray.getAlignment(), enumerable: true },
401
+
402
+ create: { value: create, enumerable: true },
274
403
 
275
404
  // Explicit wrap method
276
- wrap: wrap,
405
+ wrap: { value: wrap, enumerable: true },
406
+
407
+ // Array di caratteri: tipo e conversione da/verso stringa (campi struct).
408
+ _charKind: { value: charKind },
409
+ getValue: { value: getValue },
410
+ setValue: { value: setValue },
277
411
 
278
412
  // For compatibility, expose the native array (needed for StructType.addField)
279
- _native: nativeArray,
413
+ _native: { value: nativeArray },
280
414
 
281
415
  // Helper to check if this is an ArrayType wrapper
282
- _isArrayType: true,
283
- };
416
+ _isArrayType: { value: true },
417
+ });
418
+
419
+ return ArrayCType;
284
420
  }
@@ -157,7 +157,8 @@ export function _writeBitField(buf, offset, baseType, bitOffset, bitSize, newVal
157
157
  } else {
158
158
  const mask = (1 << bitSize) - 1;
159
159
  const clearMask = ~(mask << bitOffset);
160
- value = (value & clearMask) | ((newValue & mask) << bitOffset);
160
+ const bits = typeof newValue === "bigint" ? Number(BigInt.asUintN(32, newValue)) : newValue;
161
+ value = (value & clearMask) | ((bits & mask) << bitOffset);
161
162
  }
162
163
 
163
164
  _writeUintToBuffer(buf, offset, baseSize, value);