koffi 2.14.0-beta.3 → 2.14.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +18 -1
  2. package/README.md +1 -1
  3. package/build/koffi/darwin_arm64/koffi.node +0 -0
  4. package/build/koffi/darwin_x64/koffi.node +0 -0
  5. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  6. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  7. package/build/koffi/freebsd_x64/koffi.node +0 -0
  8. package/build/koffi/linux_arm64/koffi.node +0 -0
  9. package/build/koffi/linux_armhf/koffi.node +0 -0
  10. package/build/koffi/linux_ia32/koffi.node +0 -0
  11. package/build/koffi/linux_loong64/koffi.node +0 -0
  12. package/build/koffi/linux_riscv64d/koffi.node +0 -0
  13. package/build/koffi/linux_x64/koffi.node +0 -0
  14. package/build/koffi/musl_arm64/koffi.node +0 -0
  15. package/build/koffi/musl_x64/koffi.node +0 -0
  16. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  17. package/build/koffi/openbsd_x64/koffi.node +0 -0
  18. package/build/koffi/win32_arm64/koffi.node +0 -0
  19. package/build/koffi/win32_ia32/koffi.node +0 -0
  20. package/build/koffi/win32_x64/koffi.node +0 -0
  21. package/doc/pages/index.md +43 -4
  22. package/doc/pages/input.md +66 -0
  23. package/doc/pages/platforms.md +8 -19
  24. package/doc/pages.ini +0 -7
  25. package/doc/static/perf_windows.png +0 -0
  26. package/index.d.ts +0 -1
  27. package/index.js +10 -9
  28. package/indirect.js +10 -9
  29. package/package.json +3 -2
  30. package/src/cnoke/src/builder.js +1 -2
  31. package/src/core/base/base.cc +815 -554
  32. package/src/core/base/base.hh +578 -450
  33. package/src/core/base/crc.inc +1 -1
  34. package/src/core/base/crc_gen.py +1 -1
  35. package/src/core/base/unicode.inc +1 -1
  36. package/src/core/base/unicode_gen.py +1 -1
  37. package/src/koffi/src/abi_arm32.cc +24 -24
  38. package/src/koffi/src/abi_arm64.cc +29 -29
  39. package/src/koffi/src/abi_riscv64.cc +27 -27
  40. package/src/koffi/src/abi_x64_sysv.cc +29 -29
  41. package/src/koffi/src/abi_x64_win.cc +20 -20
  42. package/src/koffi/src/abi_x86.cc +26 -26
  43. package/src/koffi/src/call.cc +83 -237
  44. package/src/koffi/src/call.hh +8 -8
  45. package/src/koffi/src/ffi.cc +71 -67
  46. package/src/koffi/src/ffi.hh +9 -9
  47. package/src/koffi/src/parser.cc +2 -2
  48. package/src/koffi/src/parser.hh +1 -1
  49. package/src/koffi/src/trampolines/prototypes.inc +1 -1
  50. package/src/koffi/src/util.cc +43 -43
  51. package/src/koffi/src/util.hh +5 -5
  52. package/src/koffi/src/win32.cc +5 -5
  53. package/src/koffi/src/win32.hh +1 -1
package/CHANGELOG.md CHANGED
@@ -5,6 +5,24 @@
5
5
 
6
6
  ## Koffi 2
7
7
 
8
+ ### Koffi 2.14
9
+
10
+ #### Koffi 2.14.1
11
+
12
+ *Released on 2025-09-13*
13
+
14
+ - Recompile Linux (musl) ARM64 prebuild with Alpine 3.20 (instead of Alpine edge)
15
+ - Clean up redundant code paths for handling string arguments
16
+
17
+ #### Koffi 2.14.0
18
+
19
+ *Released on 2025-08-17*
20
+
21
+ - Improve support for structs with [flexible array member](input#flexible-arrays)
22
+ - Automatically encode/decode dynamic arrays pointers when length is known though struct member
23
+ - Fix parser crash when direction qualifier is followed by unknown type
24
+ - Add missing TypeScript types and arguments
25
+
8
26
  ### Koffi 2.13
9
27
 
10
28
  #### Koffi 2.13.0
@@ -969,7 +987,6 @@ This entry documents changes since version 1.1.0.
969
987
 
970
988
  The following features and improvements are planned, not necessarily in that order:
971
989
 
972
- - Port Koffi to Loong64 ISA and ABI
973
990
  - Port Koffi to PowerPC (POWER9+) ISA and ABI
974
991
  - Optimize passing of structs and arrays (with auto-generated JS)
975
992
  - Automate Windows/AArch64 (qemu) and macOS/AArch64 (how? ... thanks Apple) tests
package/README.md CHANGED
@@ -4,7 +4,7 @@ Koffi is a fast and easy-to-use C FFI module for Node.js, featuring:
4
4
 
5
5
  * Low-overhead and fast performance (see [benchmarks](https://koffi.dev/benchmarks))
6
6
  * Support for primitive and aggregate data types (structs and fixed-size arrays), both by reference (pointer) and by value
7
- * Javascript functions can be used as C callbacks (since 1.2.0)
7
+ * Javascript functions can be used as C callbacks
8
8
  * Well-tested code base for popular OS/architecture combinations
9
9
 
10
10
  The following combinations of OS and architectures __are officially supported and tested__ at the moment:
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,16 +1,51 @@
1
- # Koffi
1
+ # Overview
2
2
 
3
3
  Koffi is a **fast and easy-to-use C FFI module for Node.js**, featuring:
4
4
 
5
5
  * Low-overhead and fast performance (see [benchmarks](benchmarks))
6
6
  * Support for primitive and aggregate data types (structs and fixed-size arrays), both by reference (pointer) and by value
7
- * Javascript functions can be used as C callbacks (since 1.2.0)
8
- * Well-tested code base for [popular OS/architecture combinations](platforms)
7
+ * Javascript functions can be used as C callbacks
8
+ * Well-tested code base for popular OS/architecture combinations
9
9
 
10
- Koffi requires a recent [Node.js](https://nodejs.org/) version with N-API version 8 support, see [this page](platforms) for more information.
10
+ I work on this project on my spare time, if you like this project, consider supporting me:
11
+
12
+ <p style="display: flex; gap: 2em; justify-content: center;">
13
+ <a href="https://buymeacoffee.com/koromix" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174" style="border-radius: 12px;"></a>
14
+ </p>
15
+
16
+ Koffi requires [Node.js](https://nodejs.org/) version 16 or later. Use [NVM](https://github.com/nvm-sh/nvm) to install more recent Node versions on older Linux distributions.
17
+
18
+ The following combinations of OS and architectures __are officially supported and tested__ at the moment:
19
+
20
+ ISA / OS | Windows | Linux (glibc) | Linux (musl) | macOS | FreeBSD | OpenBSD
21
+ ------------------ | ------- | ------------- | ------------ | ----- | ------- | -------
22
+ x86 (IA32) [^2] | ✅ | ✅ | 🟨 | ⬜️ | ✅ | ✅
23
+ x86_64 (AMD64) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅
24
+ ARM32 LE [^3] | ⬜️ | ✅ | 🟨 | ⬜️ | 🟨 | 🟨
25
+ ARM64 (AArch64) LE | ✅ | ✅ | ✅ | ✅ | ✅ | 🟨
26
+ RISC-V 64 [^4] | ⬜️ | ✅ | 🟨 | ⬜️ | 🟨 | 🟨
27
+ LoongArch64 | ⬜️ | ✅ | 🟨 | ⬜️ | 🟨 | 🟨
28
+
29
+ <div class="legend">✅ Yes | 🟨 Probably | ⬜️ Not applicable</div>
30
+
31
+ [^2]: The following call conventions are supported for forward calls: cdecl, stdcall, MS fastcall, thiscall. Only cdecl and stdcall can be used for C to JS callbacks.
32
+ [^3]: The prebuilt binary uses the hard float ABI and expects a VFP coprocessor. Build from source to use Koffi with a different ABI (softfp, soft).
33
+ [^4]: The prebuilt binary uses the LP64D (double-precision float) ABI. The LP64 ABI is supported in theory if you build Koffi from source (untested), the LP64F ABI is not supported.
34
+
35
+ For all fully supported platforms (green check marks), a prebuilt binary is included in the NPM package which means you can install Koffi without a C++ compiler.
36
+
37
+ # Source code
11
38
 
12
39
  The source code is available here: https://github.com/Koromix/rygel/ (in the *src/koffi* subdirectory).
13
40
 
41
+ > [!NOTE]
42
+ > Most of my projects live in a single repository (or monorepo), which have two killer features for me:
43
+ >
44
+ > - Cross-project refactoring
45
+ > - Simplified dependency management
46
+ >
47
+ > You can find a more detailed rationale here: https://danluu.com/monorepo/
48
+
14
49
  New releases are frequent, look at the [changelog](changelog) for more information.
15
50
 
16
51
  # License
@@ -18,3 +53,7 @@ New releases are frequent, look at the [changelog](changelog) for more informati
18
53
  This program is free software: you can redistribute it and/or modify it under the terms of the **MIT License**.
19
54
 
20
55
  Find more information here: https://choosealicense.com/licenses/mit/
56
+
57
+ <style>
58
+ table td:not(:first-child) { text-align: center; }
59
+ </style>
@@ -396,6 +396,72 @@ Koffi can also convert JS strings to fixed-sized arrays in the following cases:
396
396
 
397
397
  The reverse case is also true, Koffi can convert a C fixed-size buffer to a JS string. This happens by default for char, char16_t and char32_t arrays, but you can also explicitly ask for this with the `String` array hint (e.g. `koffi.array('char', 8, 'String')`).
398
398
 
399
+ ## Flexible arrays
400
+
401
+ *Added in Koffi 2.14.0*
402
+
403
+ C structs ending with a flexible array member are often used for variable-sized structs, and are generally paired with dynamic memory allocation. In many cases, the number of elements is described by another struct member.
404
+
405
+ Use `koffi.array(type, countedBy, maxLen)` to make a flexible array type, for which the array length is determined by the struct member indicated by the `countedBy` parameter. Flexible array types can only be used as the last member of a struct, as shown below:
406
+
407
+ ```js
408
+ const FlexibleArray = koffi.struct('FlexibleArray', {
409
+ count: 'size_t',
410
+ numbers: koffi.array('int', 'count', 128)
411
+ });
412
+ ````
413
+
414
+ For various reasons, Koffi requires you to specify an upper bound (`maxLen`) for the number of elements in the flexible array member.
415
+
416
+ > [!WARNING]
417
+ > Also, unlike C flexible arrays, the struct size will expand to accomodate the maximum size of the flexible array.
418
+
419
+ The following example illustrates how to use a flexible array API in C from Koffi.
420
+
421
+ ```c
422
+ // Build with: clang -fPIC -o flexible.so -shared flexible.c -Wall -O2
423
+
424
+ #include <stddef.h>
425
+
426
+ struct FlexibleArray {
427
+ size_t count;
428
+ int numbers[];
429
+ };
430
+
431
+ void AppendValues(struct FlexibleArray *arr, size_t count, int start, int step)
432
+ {
433
+ for (size_t i = 0; i < count; i++) {
434
+ arr->numbers[arr->count + i] = start + i * step;
435
+ }
436
+ arr->count += count;
437
+ }
438
+ ```
439
+
440
+ ```js
441
+ // ES6 syntax: import koffi from 'koffi';
442
+ const koffi = require('koffi');
443
+
444
+ const lib = koffi.load('./flexible.so');
445
+
446
+ const FlexibleArray = koffi.struct('FlexibleArray', {
447
+ count: 'size_t',
448
+ numbers: koffi.array('int', 'count', 256, 'Array')
449
+ });
450
+
451
+ const AppendValues = lib.func('void AppendValues(_Inout_ FlexibleArray *arr, int count, int start, int step)');
452
+
453
+ let array = { count: 0, numbers: [] };
454
+
455
+ AppendValues(array, 5, 1, 1);
456
+ console.log(array); // Prints { count: 5, numbers: [1, 2, 3, 4, 5] }
457
+
458
+ AppendValues(array, 3, 10, 2);
459
+ console.log(array); // Prints { count: 8, numbers: [1, 2, 3, 4, 5, 10, 12, 14] }
460
+ ```
461
+
462
+ > [!NOTE]
463
+ > This is frequently used in the Win32 API, an exemple of this is [AllocateAndInitializeSid()](https://learn.microsoft.com/windows/win32/api/securitybaseapi/nf-securitybaseapi-allocateandinitializesid).
464
+
399
465
  ## Dynamic arrays (pointers)
400
466
 
401
467
  In C, dynamically-sized arrays are usually passed around as pointers. Read more about [array pointers](pointers#dynamic-arrays) in the relevant section.
@@ -14,25 +14,14 @@ Use [NVM](https://github.com/nvm-sh/nvm) to install more recent Node versions on
14
14
 
15
15
  The following combinations of OS and architectures __are officially supported and tested__ at the moment:
16
16
 
17
- ISA / OS | Windows | Linux (glibc) | Linux (musl)
18
- ------------------ | ------- | ------------- | ------------
19
- x86 (IA32) [^1] | ✅ | ✅ | 🟨
20
- x86_64 (AMD64) | ✅ | ✅ | ✅
21
- ARM32 LE [^2] | ⬜️ | ✅ | 🟨
22
- ARM64 (AArch64) LE | ✅ | ✅ | ✅
23
- RISC-V 64 [^3] | ⬜️ | ✅ | 🟨
24
- LoongArch64 | ⬜️ | ✅ | 🟨
25
-
26
- <div class="legend">✅ Yes | 🟨 Probably | ⬜️ Not applicable</div>
27
-
28
- ISA / OS | macOS | FreeBSD | OpenBSD
29
- ------------------ | ----- | ----------- | --------
30
- x86 (IA32) [^1] | ⬜️ | ✅ | ✅
31
- x86_64 (AMD64) | ✅ | ✅ | ✅
32
- ARM32 LE [^2] | ⬜️ | 🟨 | 🟨
33
- ARM64 (AArch64) LE | ✅ | ✅ | 🟨
34
- RISC-V 64 [^3] | ⬜️ | 🟨 | 🟨
35
- LoongArch64 | ⬜️ | 🟨 | 🟨
17
+ ISA / OS | Windows | Linux (glibc) | Linux (musl) | macOS | FreeBSD | OpenBSD
18
+ ------------------ | ----------- | ------------- | ------------ | ----------- | ----------- | --------
19
+ x86 (IA32) [^1] | ✅ Yes | ✅ Yes | 🟨 Probably | ⬜️ *N/A* | ✅ Yes | ✅ Yes
20
+ x86_64 (AMD64) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes
21
+ ARM32 LE [^2] | ⬜️ *N/A* | ✅ Yes | 🟨 Probably | ⬜️ *N/A* | 🟨 Probably | 🟨 Probably
22
+ ARM64 (AArch64) LE | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | 🟨 Probably
23
+ RISC-V 64 [^3] | ⬜️ *N/A* | ✅ Yes | 🟨 Probably | ⬜️ *N/A* | 🟨 Probably | 🟨 Probably
24
+ LoongArch64 | ⬜️ *N/A* | ✅ Yes | 🟨 Probably | ⬜️ *N/A* | 🟨 Probably | 🟨 Probably
36
25
 
37
26
  <div class="legend">✅ Yes | 🟨 Probably | ⬜️ Not applicable</div>
38
27
 
package/doc/pages.ini CHANGED
@@ -2,13 +2,6 @@
2
2
  Title = Koffi
3
3
  Menu = Overview
4
4
  Description = Koffi presentation and features
5
- ToC = Off
6
- Template = templates/page.html
7
-
8
- [pages/platforms.md]
9
- Title = Requirements | Koffi
10
- Menu = Documentation / Requirements
11
- Description = Supported systemd and architectures
12
5
  Template = templates/page.html
13
6
 
14
7
  [pages/start.md]
Binary file
package/index.d.ts CHANGED
@@ -105,7 +105,6 @@ export class Union {
105
105
  }
106
106
 
107
107
  export function array(ref: TypeSpec, len: number, hint?: ArrayHint | null): IKoffiCType;
108
- export function array(ref: TypeSpec, countedBy: string, hint?: ArrayHint | null): IKoffiCType;
109
108
  export function array(ref: TypeSpec, countedBy: string, maxLen: number, hint?: ArrayHint | null): IKoffiCType;
110
109
 
111
110
  export function opaque(name: string | null | undefined): IKoffiCType;
package/index.js CHANGED
@@ -4,9 +4,9 @@ var __commonJS = (cb, mod3) => function __require() {
4
4
  return mod3 || (0, cb[__getOwnPropNames(cb)[0]])((mod3 = { exports: {} }).exports, mod3), mod3.exports;
5
5
  };
6
6
 
7
- // bin/Koffi/package/src/cnoke/src/tools.js
7
+ // ../../../bin/Koffi/package/src/cnoke/src/tools.js
8
8
  var require_tools = __commonJS({
9
- "bin/Koffi/package/src/cnoke/src/tools.js"(exports2, module2) {
9
+ "../../../bin/Koffi/package/src/cnoke/src/tools.js"(exports2, module2) {
10
10
  "use strict";
11
11
  var crypto = require("crypto");
12
12
  var fs2 = require("fs");
@@ -397,12 +397,12 @@ var require_tools = __commonJS({
397
397
  }
398
398
  });
399
399
 
400
- // bin/Koffi/package/src/koffi/package.json
400
+ // ../../../bin/Koffi/package/src/koffi/package.json
401
401
  var require_package = __commonJS({
402
- "bin/Koffi/package/src/koffi/package.json"(exports2, module2) {
402
+ "../../../bin/Koffi/package/src/koffi/package.json"(exports2, module2) {
403
403
  module2.exports = {
404
404
  name: "koffi",
405
- version: "2.14.0-beta.3",
405
+ version: "2.14.1",
406
406
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
407
407
  keywords: [
408
408
  "foreign",
@@ -438,14 +438,15 @@ var require_package = __commonJS({
438
438
  node: 16,
439
439
  napi: 8,
440
440
  require: "./index.js"
441
- }
441
+ },
442
+ funding: "https://buymeacoffee.com/koromix"
442
443
  };
443
444
  }
444
445
  });
445
446
 
446
- // bin/Koffi/package/src/koffi/src/init.js
447
+ // ../../../bin/Koffi/package/src/koffi/src/init.js
447
448
  var require_init = __commonJS({
448
- "bin/Koffi/package/src/koffi/src/init.js"(exports, module) {
449
+ "../../../bin/Koffi/package/src/koffi/src/init.js"(exports, module) {
449
450
  var fs = require("fs");
450
451
  var path = require("path");
451
452
  var util = require("util");
@@ -528,7 +529,7 @@ var require_init = __commonJS({
528
529
  }
529
530
  });
530
531
 
531
- // bin/Koffi/package/src/koffi/index.js
532
+ // ../../../bin/Koffi/package/src/koffi/index.js
532
533
  var { detect: detect2, init: init2 } = require_init();
533
534
  var triplet2 = detect2();
534
535
  var native2 = null;
package/indirect.js CHANGED
@@ -4,9 +4,9 @@ var __commonJS = (cb, mod3) => function __require() {
4
4
  return mod3 || (0, cb[__getOwnPropNames(cb)[0]])((mod3 = { exports: {} }).exports, mod3), mod3.exports;
5
5
  };
6
6
 
7
- // bin/Koffi/package/src/cnoke/src/tools.js
7
+ // ../../../bin/Koffi/package/src/cnoke/src/tools.js
8
8
  var require_tools = __commonJS({
9
- "bin/Koffi/package/src/cnoke/src/tools.js"(exports2, module2) {
9
+ "../../../bin/Koffi/package/src/cnoke/src/tools.js"(exports2, module2) {
10
10
  "use strict";
11
11
  var crypto = require("crypto");
12
12
  var fs2 = require("fs");
@@ -397,12 +397,12 @@ var require_tools = __commonJS({
397
397
  }
398
398
  });
399
399
 
400
- // bin/Koffi/package/src/koffi/package.json
400
+ // ../../../bin/Koffi/package/src/koffi/package.json
401
401
  var require_package = __commonJS({
402
- "bin/Koffi/package/src/koffi/package.json"(exports2, module2) {
402
+ "../../../bin/Koffi/package/src/koffi/package.json"(exports2, module2) {
403
403
  module2.exports = {
404
404
  name: "koffi",
405
- version: "2.14.0-beta.3",
405
+ version: "2.14.1",
406
406
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
407
407
  keywords: [
408
408
  "foreign",
@@ -438,14 +438,15 @@ var require_package = __commonJS({
438
438
  node: 16,
439
439
  napi: 8,
440
440
  require: "./index.js"
441
- }
441
+ },
442
+ funding: "https://buymeacoffee.com/koromix"
442
443
  };
443
444
  }
444
445
  });
445
446
 
446
- // bin/Koffi/package/src/koffi/src/init.js
447
+ // ../../../bin/Koffi/package/src/koffi/src/init.js
447
448
  var require_init = __commonJS({
448
- "bin/Koffi/package/src/koffi/src/init.js"(exports, module) {
449
+ "../../../bin/Koffi/package/src/koffi/src/init.js"(exports, module) {
449
450
  var fs = require("fs");
450
451
  var path = require("path");
451
452
  var util = require("util");
@@ -528,7 +529,7 @@ var require_init = __commonJS({
528
529
  }
529
530
  });
530
531
 
531
- // bin/Koffi/package/src/koffi/indirect.js
532
+ // ../../../bin/Koffi/package/src/koffi/indirect.js
532
533
  var { detect: detect2, init: init2 } = require_init();
533
534
  var triplet2 = detect2();
534
535
  var mod2 = init2(triplet2, null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.14.0-beta.3",
3
+ "version": "2.14.1",
4
4
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
5
5
  "keywords": [
6
6
  "foreign",
@@ -33,5 +33,6 @@
33
33
  "node": 16,
34
34
  "napi": 8,
35
35
  "require": "./index.js"
36
- }
36
+ },
37
+ "funding": "https://buymeacoffee.com/koromix"
37
38
  }
@@ -309,8 +309,7 @@ function Builder(config = {}) {
309
309
  tools.unlink_recursive(build_dir);
310
310
  };
311
311
 
312
- function find_parent_directory(dirname, basename)
313
- {
312
+ function find_parent_directory(dirname, basename) {
314
313
  if (process.platform == 'win32')
315
314
  dirname = dirname.replace(/\\/g, '/');
316
315