node-ctypes 2.13.0 → 2.13.1

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 CHANGED
@@ -7,6 +7,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [2.13.1] - 2026-11-14
11
+
12
+ ### Fixed
13
+
14
+ - **Argument conversion order through the JavaScript wrapper**: as in CPython,
15
+ where each argument goes through `from_param` and its C conversion before
16
+ the next one, a scalar argument the native call rejects now fails before the
17
+ converters of the later arguments run. Before, with `errcheck`,
18
+ `use_last_error`/`use_errno`, `callAsync` or an object among the
19
+ arguments, the `_as_parameter_` getters and `from_param` of the later
20
+ arguments ran first and the error came afterwards. Values the call accepts
21
+ as they are (integral numbers, booleans and BigInts for integers, numbers for
22
+ floats, one-character strings for `c_char`/`c_wchar`) are checked in
23
+ JavaScript; the others, objects included, by the native conversion itself
24
+ (`checkArgument`), so the rules and the `ArgumentError` messages are those
25
+ of the call. The native check returns the converted C value, so the
26
+ conversion is complete before the next argument: a later adapter changing a
27
+ Buffer passed to `c_char` no longer changes what C receives (as a
28
+ `bytearray` in CPython); memory passed by address (a Buffer for a 64-bit
29
+ integer) stays the original value. Scalar `CType` numbers as argtypes follow
30
+ the same order. Too few arguments are still reported first, as
31
+ "Expected N arguments, got M". The extra cost is a few nanoseconds per scalar
32
+ argument converted by the JavaScript wrapper, and the native check for
33
+ unusual values; calls through the native entry function are unchanged.
34
+ - **Flaky Executor test** (cancellation after an inline completion): on a
35
+ loaded machine the inline waits expired, the per-function backoff then
36
+ skipped the next ones and the 1 ms of inline completions per event loop turn
37
+ was spent by the warm-up calls, so no attempt produced the inline completion
38
+ the test needs. Each attempt now uses a new executor with the longest wait,
39
+ started with another function, and a fresh event loop turn; when the machine
40
+ is so loaded that the executor thread never runs within 10 ms, the test is
41
+ skipped with that reason instead of failing. The attempts stop after 3 s, so
42
+ the skip comes within the 5 s test timeout; a second test checks that the
43
+ attempts without inline completions (`spin: 0`) stop at their time budget.
44
+
10
45
  ## [2.13.0] - 2026-11-14
11
46
 
12
47
  ### Changed
@@ -1396,7 +1431,8 @@ It contains breaking changes: see [Migrating from 1.x](README.md#migrating-from-
1396
1431
 
1397
1432
  Initial releases.
1398
1433
 
1399
- [Unreleased]: https://github.com/dmazzella/node-ctypes/compare/v2.13.0...HEAD
1434
+ [Unreleased]: https://github.com/dmazzella/node-ctypes/compare/v2.13.1...HEAD
1435
+ [2.13.1]: https://github.com/dmazzella/node-ctypes/compare/v2.13.0...v2.13.1
1400
1436
  [2.13.0]: https://github.com/dmazzella/node-ctypes/compare/v2.12.2...v2.13.0
1401
1437
  [2.12.2]: https://github.com/dmazzella/node-ctypes/compare/v2.12.1...v2.12.2
1402
1438
  [2.12.1]: https://github.com/dmazzella/node-ctypes/compare/v2.12.0...v2.12.1
Binary file
Binary file
@@ -333,8 +333,12 @@ export function argumentConverters(argTypes, native) {
333
333
  const kind = typeof t === "function" ? t._type : t;
334
334
  if (kind === native.CType.STRING || kind === native.CType.WSTRING) return stringArgument(t, kind, index, native);
335
335
  if (kind === native.CType.POINTER) return voidPointerArgument(index, native);
336
- if (typeof t !== "function" || t._isSimpleCData !== true) return coerceObject;
337
- return scalarArgument(t, kind, index);
336
+ if (typeof t !== "function" || t._isSimpleCData !== true) {
337
+ // A scalar CType number as argtype: the same conversion order.
338
+ const check = typeof kind === "number" ? scalarCheck(kind, index, native) : undefined;
339
+ return check === undefined ? coerceObject : (a, owners) => check(coerceObject(a, owners));
340
+ }
341
+ return scalarArgument(t, kind, index, native);
338
342
  };
339
343
  const converters = argTypes.map((t, index) => {
340
344
  const base = baseConverter(t, index);
@@ -408,8 +412,73 @@ function stringArgument(T, kind, index, native) {
408
412
  return convert;
409
413
  }
410
414
 
411
- function scalarArgument(T, kind, index) {
415
+ // The largest BigInt magnitude a float or double argument accepts here: the
416
+ // native conversion raises RangeError from 2 ** 1024.
417
+ const maxFloatBigInt = 2n ** 1023n;
418
+
419
+ /**
420
+ * Conversion of a scalar argument as the native call does it, before the next
421
+ * argument is converted (CPython: from_param, then the C conversion, argument
422
+ * by argument). Values the call accepts as they are pass unchanged: integral
423
+ * numbers, booleans and BigInts for the integer kinds (any integral Number is
424
+ * reduced to the type's bits), numbers, booleans and BigInts up to 2 ** 1023
425
+ * for float and double, a character below 256 or an integer in range(256) for
426
+ * c_char, one UTF-16 unit (not a surrogate) for c_wchar, anything for bool.
427
+ * Other values go to the native conversion itself (`checkArgument`): its
428
+ * rules and ArgumentError messages are those of the call, and it returns the
429
+ * converted C value, so code running afterwards (a later adapter changing a
430
+ * Buffer passed to c_char) cannot change it; memory passed by address stays
431
+ * the original value.
432
+ *
433
+ * @returns {Function|undefined} `(value) => value`, undefined for other kinds
434
+ */
435
+ function scalarCheck(kind, index, native) {
436
+ const C = native.CType;
437
+ let plain;
438
+ switch (kind) {
439
+ case C.BOOL:
440
+ // ToBoolean, which runs no JavaScript and cannot change later.
441
+ return (value) => value;
442
+ case C.FLOAT:
443
+ case C.DOUBLE:
444
+ plain = (value) => typeof value === "number" || typeof value === "boolean" || (typeof value === "bigint" && value >= -maxFloatBigInt && value <= maxFloatBigInt);
445
+ break;
446
+ case C.INT8:
447
+ case C.UINT8:
448
+ case C.INT16:
449
+ case C.UINT16:
450
+ case C.INT32:
451
+ case C.UINT32:
452
+ case C.INT64:
453
+ case C.UINT64:
454
+ case C.LONG:
455
+ case C.ULONG:
456
+ case C.SIZE_T:
457
+ case C.SSIZE_T:
458
+ plain = (value) => (typeof value === "number" && Number.isInteger(value)) || typeof value === "boolean" || typeof value === "bigint";
459
+ break;
460
+ case C.CHAR:
461
+ plain = (value) => (typeof value === "string" && value.length === 1 && value.charCodeAt(0) < 256) || (typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 255) || typeof value === "boolean";
462
+ break;
463
+ case C.WCHAR:
464
+ plain = (value) => {
465
+ if (typeof value !== "string" || value.length !== 1) return false;
466
+ const unit = value.charCodeAt(0);
467
+ return unit < 0xd800 || unit > 0xdfff;
468
+ };
469
+ break;
470
+ default:
471
+ return undefined;
472
+ }
473
+ return (value) => (plain(value) ? value : native.checkArgument(index, kind, value));
474
+ }
475
+
476
+ function scalarArgument(T, kind, index, native) {
412
477
  const boolean = T._isBoolean === true;
478
+ // As CPython (from_param, then the C conversion, argument by argument), a
479
+ // value the call would reject fails here, before the converters of the later
480
+ // arguments can run user code (adapters, getters).
481
+ const checked = scalarCheck(kind, index, native) ?? ((value) => value);
413
482
  const stored = (value, info, owners, adapted) => {
414
483
  retainArgument(owners, value);
415
484
  if (!boolean && info.type !== T && !(value instanceof T) && !(adapted && info.kind === kind)) {
@@ -423,7 +492,7 @@ function scalarArgument(T, kind, index) {
423
492
  if (info !== undefined) return stored(value, info, owners, adapted);
424
493
  value = resolveArgument(value, owners, index);
425
494
  }
426
- if (value === null || typeof value !== "object") return value;
495
+ if (value === null || typeof value !== "object") return checked(value);
427
496
  const info = scalarInstanceInfo(value);
428
497
  if (info !== undefined) return stored(value, info, owners, adapted);
429
498
  retainArgument(owners, value);
@@ -433,11 +502,11 @@ function scalarArgument(T, kind, index) {
433
502
  }
434
503
  return scalarArgumentValue(value);
435
504
  }
436
- return objectMemory(value, owners);
505
+ return checked(objectMemory(value, owners));
437
506
  };
438
507
  const convert = make(false);
439
508
  Object.defineProperty(convert, "fromParam", { value: make(true) });
440
- // A primitive value (not an object or a function) is returned unchanged.
509
+ // A valid primitive value (not an object or a function) is returned unchanged.
441
510
  Object.defineProperty(convert, "passesPrimitives", { value: true });
442
511
  return convert;
443
512
  }
@@ -576,6 +645,9 @@ function arrayArgument(T, index) {
576
645
  export function convertArguments(args, converters, owners) {
577
646
  const n = args.length;
578
647
  const declared = converters.length;
648
+ // The arity first, as the native call (and CPython) report it before any
649
+ // argument conversion error.
650
+ if (n < declared) throw new TypeError(`Expected ${declared} arguments, got ${n}`);
579
651
  const out = new Array(n);
580
652
  for (let i = 0; i < n; i++) {
581
653
  out[i] = i < declared ? converters[i](args[i], owners) : coerceVariadic(args[i], owners);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ctypes",
3
- "version": "2.13.0",
3
+ "version": "2.13.1",
4
4
  "description": "Python ctypes-like FFI for Node.js using libffi",
5
5
  "author": "Damiano Mazzella",
6
6
  "license": "MIT",