node-ctypes 2.13.0 → 2.13.2

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.
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.2",
4
4
  "description": "Python ctypes-like FFI for Node.js using libffi",
5
5
  "author": "Damiano Mazzella",
6
6
  "license": "MIT",
@@ -34,6 +34,7 @@
34
34
  "test:types": "tsc -p tests/types/tsconfig.json",
35
35
  "bench:koffi": "node tests/benchmarks/benchmark_koffi.js",
36
36
  "bench:ffi": "node --experimental-ffi tests/benchmarks/benchmark_node_ffi.js",
37
+ "bench:charts": "node tests/benchmarks/charts.js",
37
38
  "docs": "typedoc",
38
39
  "docs:serve": "typedoc && npx serve docs"
39
40
  },