node-ctypes 2.3.0 → 2.5.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.
@@ -28,6 +28,26 @@
28
28
  */
29
29
 
30
30
  import { mirrorAddress } from "./mirror.js";
31
+ import { resolveParameter } from "../structures/helpers/common.js";
32
+ import { ArgumentError } from "../platform/errors.js";
33
+
34
+ // Fixed backing stores do not move; resizable/growable stores are never cached.
35
+ const memoryAddresses = new WeakMap();
36
+
37
+ function destinationAddress(memory, native) {
38
+ if (memory === null) return 0n;
39
+ if (typeof memory === "bigint") return memory;
40
+ const buffer = memory.buffer;
41
+ const cached = memoryAddresses.get(buffer);
42
+ if (cached !== undefined && cached.length === buffer.byteLength) {
43
+ return memory.byteOffset === 0 ? cached.address : cached.address + BigInt(memory.byteOffset);
44
+ }
45
+ const address = native.addressOf(memory);
46
+ if (buffer.byteLength !== 0 && !buffer.resizable && !buffer.growable) {
47
+ memoryAddresses.set(buffer, { address: address - BigInt(memory.byteOffset), length: buffer.byteLength });
48
+ }
49
+ return address;
50
+ }
31
51
 
32
52
  /**
33
53
  * Allocates a zero-initialized buffer of the specified size.
@@ -392,7 +412,19 @@ export function create_unicode_buffer(init, size, native) {
392
412
  * ```
393
413
  */
394
414
  export function string_at(address, size, native) {
395
- return native.readCString(stringSource(address, native), size);
415
+ const source = stringSource(address, native);
416
+ if (typeof size === "number" && Number.isFinite(size) && size >= 0) {
417
+ const length = Math.trunc(size);
418
+ if (length === 0) return "";
419
+ if (source === null || source === undefined) return null;
420
+ // Sized reads are binary: Latin1 preserves every byte as one JS code unit.
421
+ if (Buffer.isBuffer(source)) {
422
+ if (length > source.length) throw new RangeError("size exceeds the buffer length");
423
+ return source.toString("latin1", 0, length);
424
+ }
425
+ return native.ptrToBuffer(source, length)?.toString("latin1") ?? null;
426
+ }
427
+ return native.readCString(source, size);
396
428
  }
397
429
 
398
430
  /**
@@ -430,10 +462,15 @@ export function wstring_at(address, size, native) {
430
462
  */
431
463
  function stringSource(value, native) {
432
464
  if (value === null || value === undefined || typeof value === "bigint" || typeof value === "number") return value;
433
- // Array proxies pass Buffer.isBuffer but are resolved through _buffer.
434
- if (Buffer.isBuffer(value) && value._buffer === undefined) return mirrorAddress(value) ?? value;
435
- const memory = memoryOf(value, native, "address", true);
436
- return memory instanceof Uint8Array && !Buffer.isBuffer(memory) ? Buffer.from(memory.buffer, memory.byteOffset, memory.byteLength) : memory;
465
+ try {
466
+ // Array proxies pass Buffer.isBuffer but are resolved through _buffer.
467
+ if (Buffer.isBuffer(value) && value._buffer === undefined) return mirrorAddress(value) ?? value;
468
+ const memory = memoryOf(value, native, "address", true);
469
+ return memory instanceof Uint8Array && !Buffer.isBuffer(memory) ? Buffer.from(memory.buffer, memory.byteOffset, memory.byteLength) : memory;
470
+ } catch (error) {
471
+ if (error instanceof ArgumentError) throw error;
472
+ throw new ArgumentError(`argument 1: ${error?.name ?? "Error"}: ${error?.message ?? String(error)}`, { cause: error });
473
+ }
437
474
  }
438
475
 
439
476
  /**
@@ -459,16 +496,21 @@ function stringSource(value, native) {
459
496
  * ```
460
497
  */
461
498
  export function memmove(dst, src, count, native) {
462
- const to = memoryOf(dst, native, "dst");
463
- const from = memoryOf(src, native, "src");
499
+ if (!Number.isSafeInteger(count) || count < 0) throw new RangeError("memmove: count must be a non-negative safe integer");
500
+ const to = memoryOf(dst, native, "dst", count === 0);
501
+ const from = memoryOf(src, native, "src", count === 0);
502
+ if (count === 0) return destinationAddress(to, native);
464
503
  // Come Buffer.copy, la copia si ferma alla fine dei Buffer coinvolti.
465
504
  let n = count;
466
505
  if (typeof to !== "bigint") n = Math.min(n, to.length);
467
506
  if (typeof from !== "bigint") n = Math.min(n, from.length);
468
- if (n <= 0) return;
507
+ if (n <= 0) return destinationAddress(to, native);
469
508
  // TypedArray.set gestisce anche la sovrapposizione (stesso ArrayBuffer).
470
- if (typeof to !== "bigint" && typeof from !== "bigint") to.set(n === from.length ? from : from.subarray(0, n));
471
- else native.memmove(to, from, n);
509
+ if (typeof to !== "bigint" && typeof from !== "bigint") {
510
+ to.set(n === from.length ? from : from.subarray(0, n));
511
+ return destinationAddress(to, native);
512
+ }
513
+ return native.memmove(to, from, n);
472
514
  }
473
515
 
474
516
  // Memoria di un argomento di memmove/memset, come Python: Buffer, ogni
@@ -477,6 +519,10 @@ export function memmove(dst, src, count, native) {
477
519
  // Gli indirizzi (anche quello originale di una copia, memory/mirror.js) sono
478
520
  // letti e scritti dal nativo, senza creare un Buffer esterno.
479
521
  function memoryOf(value, native, name, allowNull = false) {
522
+ value = resolveParameter(value);
523
+ if (ArrayBuffer.isView(value) && value._buffer === undefined) {
524
+ return mirrorAddress(value) ?? (value instanceof Uint8Array ? value : new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
525
+ }
480
526
  if (value !== null && typeof value === "object") {
481
527
  // c_void_p / c_char_p / c_wchar_p: the memory they point to, as in Python.
482
528
  const T = value.constructor;
@@ -495,7 +541,7 @@ function memoryOf(value, native, name, allowNull = false) {
495
541
  if (value._as_parameter_ !== undefined) return memoryOf(value._as_parameter_, native, name, allowNull);
496
542
  }
497
543
  if (typeof value === "bigint" || typeof value === "number") return addressOrNull(value, name, allowNull);
498
- if (allowNull && value === null) return null;
544
+ if (value === null) return addressOrNull(0n, name, allowNull);
499
545
  throw new TypeError(`${name} must be a Buffer, a ctypes instance, a pointer or an address`);
500
546
  }
501
547
 
@@ -528,11 +574,14 @@ function addressOrNull(value, name, allowNull = false) {
528
574
  * ```
529
575
  */
530
576
  export function memset(dst, value, count, native) {
531
- const to = memoryOf(dst, native, "dst");
577
+ if (!Number.isSafeInteger(count) || count < 0) throw new RangeError("memset: count must be a non-negative safe integer");
578
+ const to = memoryOf(dst, native, "dst", count === 0);
579
+ if (count === 0) return destinationAddress(to, native);
532
580
  if (typeof to !== "bigint") {
533
581
  // Come memmove, si ferma alla fine della memoria JS.
534
582
  to.fill(value, 0, Math.max(0, Math.min(count, to.length)));
583
+ return destinationAddress(to, native);
535
584
  } else if (count > 0) {
536
- native.memmove(to, Buffer.alloc(count, value), count);
585
+ return native.memmove(to, Buffer.alloc(count, value), count);
537
586
  }
538
587
  }
@@ -28,7 +28,7 @@
28
28
  */
29
29
 
30
30
  import { flush, mirrorsActive, refresh } from "./mirror.js";
31
- import { copyInstance } from "../structures/helpers/common.js";
31
+ import { copyInstance, isScalarSubclass, scalarView } from "../structures/helpers/common.js";
32
32
 
33
33
  /**
34
34
  * Factory che cattura le dipendenze in closure e restituisce un bundle di
@@ -151,6 +151,7 @@ export function createMemoryOps(deps) {
151
151
  ptr = memoryAt(ptr, offset, () => size, "readValue");
152
152
  checkBounds(ptr, offset, size, "Read");
153
153
  if (mirrorsActive) refresh(ptr, offset, size);
154
+ if (isScalarSubclass(type)) return scalarView(type, ptr.subarray(offset, offset + size));
154
155
  return type._reader(ptr, offset);
155
156
  }
156
157
 
@@ -192,12 +193,9 @@ export function createMemoryOps(deps) {
192
193
  const writeComposite = (def) => {
193
194
  checkBounds(ptr, offset, def.size, "Write");
194
195
  const tempBuf = ptr.subarray(offset, offset + def.size);
195
- if (Buffer.isBuffer(value)) {
196
- value.copy(tempBuf, 0, 0, def.size);
197
- if (mirrorsActive) flush(tempBuf);
198
- } else if (value !== null && typeof value === "object" && Buffer.isBuffer(value._buffer)) {
196
+ if (Buffer.isBuffer(value) || (value !== null && typeof value === "object" && Buffer.isBuffer(value._buffer))) {
199
197
  // Istanza struct/union: copia dei byte (Python), non campo per campo.
200
- copyInstance(tempBuf, 0, def.size, value);
198
+ copyInstance(tempBuf, 0, def.size, value, 0, def);
201
199
  if (mirrorsActive) flush(tempBuf);
202
200
  } else if (typeof value === "object" && value !== null) {
203
201
  for (const [k, v] of Object.entries(value)) {