koffi 2.2.3 → 2.2.5

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 (102) hide show
  1. package/{ChangeLog.md → CHANGELOG.md} +19 -3
  2. package/LICENSE.txt +160 -656
  3. package/README.md +1 -1
  4. package/doc/calls.md +277 -0
  5. package/doc/changes.md +1 -1
  6. package/doc/functions.md +17 -275
  7. package/doc/index.rst +4 -2
  8. package/doc/misc.md +97 -0
  9. package/doc/pointers.md +135 -0
  10. package/doc/static/custom.css +3 -3
  11. package/doc/types.md +3 -193
  12. package/package.json +4 -4
  13. package/src/cnoke/LICENSE.txt +160 -656
  14. package/src/cnoke/assets/FindCNoke.cmake +3 -3
  15. package/src/cnoke/assets/win_delay_hook.c +3 -3
  16. package/src/cnoke/cnoke.js +3 -3
  17. package/src/cnoke/package.json +2 -2
  18. package/src/core/libcc/libcc.cc +4 -4
  19. package/src/core/libcc/libcc.hh +3 -3
  20. package/src/koffi/CMakeLists.txt +3 -3
  21. package/src/koffi/benchmark/CMakeLists.txt +3 -3
  22. package/src/koffi/benchmark/atoi_koffi.js +3 -3
  23. package/src/koffi/benchmark/atoi_napi.cc +3 -3
  24. package/src/koffi/benchmark/atoi_napi.js +3 -3
  25. package/src/koffi/benchmark/atoi_node_ffi.js +3 -3
  26. package/src/koffi/benchmark/benchmark.js +3 -3
  27. package/src/koffi/benchmark/rand_koffi.js +3 -3
  28. package/src/koffi/benchmark/rand_napi.cc +3 -3
  29. package/src/koffi/benchmark/rand_napi.js +3 -3
  30. package/src/koffi/benchmark/rand_node_ffi.js +3 -3
  31. package/src/koffi/benchmark/raylib_cc.cc +3 -3
  32. package/src/koffi/benchmark/raylib_cc.js +3 -3
  33. package/src/koffi/benchmark/raylib_koffi.js +3 -3
  34. package/src/koffi/benchmark/raylib_node_ffi.js +3 -3
  35. package/src/koffi/benchmark/raylib_node_raylib.js +3 -3
  36. package/src/koffi/build/2.2.5/koffi_darwin_arm64.tar.gz +0 -0
  37. package/src/koffi/build/2.2.5/koffi_darwin_x64.tar.gz +0 -0
  38. package/src/koffi/build/2.2.5/koffi_freebsd_arm64.tar.gz +0 -0
  39. package/src/koffi/build/2.2.5/koffi_freebsd_ia32.tar.gz +0 -0
  40. package/src/koffi/build/2.2.5/koffi_freebsd_x64.tar.gz +0 -0
  41. package/src/koffi/build/2.2.5/koffi_linux_arm32hf.tar.gz +0 -0
  42. package/src/koffi/build/2.2.5/koffi_linux_arm64.tar.gz +0 -0
  43. package/src/koffi/build/2.2.5/koffi_linux_ia32.tar.gz +0 -0
  44. package/src/koffi/build/2.2.5/koffi_linux_riscv64hf64.tar.gz +0 -0
  45. package/src/koffi/build/2.2.5/koffi_linux_x64.tar.gz +0 -0
  46. package/src/koffi/build/2.2.5/koffi_openbsd_ia32.tar.gz +0 -0
  47. package/src/koffi/build/2.2.5/koffi_openbsd_x64.tar.gz +0 -0
  48. package/src/koffi/build/2.2.5/koffi_win32_arm64.tar.gz +0 -0
  49. package/src/koffi/build/2.2.5/koffi_win32_ia32.tar.gz +0 -0
  50. package/src/koffi/build/2.2.5/koffi_win32_x64.tar.gz +0 -0
  51. package/src/koffi/src/abi_arm32.cc +3 -3
  52. package/src/koffi/src/abi_arm32_fwd.S +3 -3
  53. package/src/koffi/src/abi_arm64.cc +3 -3
  54. package/src/koffi/src/abi_arm64_fwd.S +3 -3
  55. package/src/koffi/src/abi_arm64_fwd.asm +3 -3
  56. package/src/koffi/src/abi_riscv64.cc +3 -3
  57. package/src/koffi/src/abi_riscv64_fwd.S +3 -3
  58. package/src/koffi/src/abi_trampolines.inc +3 -3
  59. package/src/koffi/src/abi_x64_sysv.cc +3 -3
  60. package/src/koffi/src/abi_x64_sysv_fwd.S +3 -3
  61. package/src/koffi/src/abi_x64_win.cc +3 -3
  62. package/src/koffi/src/abi_x64_win_fwd.asm +3 -3
  63. package/src/koffi/src/abi_x86.cc +3 -3
  64. package/src/koffi/src/abi_x86_fwd.S +3 -3
  65. package/src/koffi/src/abi_x86_fwd.asm +3 -3
  66. package/src/koffi/src/call.cc +3 -3
  67. package/src/koffi/src/call.hh +3 -3
  68. package/src/koffi/src/ffi.cc +5 -5
  69. package/src/koffi/src/ffi.hh +3 -7
  70. package/src/koffi/src/index.js +3 -3
  71. package/src/koffi/src/parser.cc +3 -3
  72. package/src/koffi/src/parser.hh +3 -3
  73. package/src/koffi/src/util.cc +3 -3
  74. package/src/koffi/src/util.hh +3 -3
  75. package/src/koffi/src/win32.hh +3 -3
  76. package/src/koffi/test/CMakeLists.txt +3 -3
  77. package/src/koffi/test/async.js +3 -3
  78. package/src/koffi/test/callbacks.js +3 -3
  79. package/src/koffi/test/misc.c +3 -3
  80. package/src/koffi/test/raylib.js +3 -3
  81. package/src/koffi/test/sqlite.js +3 -3
  82. package/src/koffi/test/sync.js +3 -3
  83. package/src/koffi/test/win32.c +3 -3
  84. package/src/koffi/test/win32.js +3 -3
  85. package/src/koffi/tools/qemu.js +4 -4
  86. package/src/koffi/tools/write_trampolines.py +3 -3
  87. package/doc/memory.md +0 -33
  88. package/src/koffi/build/2.2.3/koffi_darwin_arm64.tar.gz +0 -0
  89. package/src/koffi/build/2.2.3/koffi_darwin_x64.tar.gz +0 -0
  90. package/src/koffi/build/2.2.3/koffi_freebsd_arm64.tar.gz +0 -0
  91. package/src/koffi/build/2.2.3/koffi_freebsd_ia32.tar.gz +0 -0
  92. package/src/koffi/build/2.2.3/koffi_freebsd_x64.tar.gz +0 -0
  93. package/src/koffi/build/2.2.3/koffi_linux_arm32hf.tar.gz +0 -0
  94. package/src/koffi/build/2.2.3/koffi_linux_arm64.tar.gz +0 -0
  95. package/src/koffi/build/2.2.3/koffi_linux_ia32.tar.gz +0 -0
  96. package/src/koffi/build/2.2.3/koffi_linux_riscv64hf64.tar.gz +0 -0
  97. package/src/koffi/build/2.2.3/koffi_linux_x64.tar.gz +0 -0
  98. package/src/koffi/build/2.2.3/koffi_openbsd_ia32.tar.gz +0 -0
  99. package/src/koffi/build/2.2.3/koffi_openbsd_x64.tar.gz +0 -0
  100. package/src/koffi/build/2.2.3/koffi_win32_arm64.tar.gz +0 -0
  101. package/src/koffi/build/2.2.3/koffi_win32_ia32.tar.gz +0 -0
  102. package/src/koffi/build/2.2.3/koffi_win32_x64.tar.gz +0 -0
package/doc/misc.md ADDED
@@ -0,0 +1,97 @@
1
+ # Miscellaneous
2
+
3
+ ## Types
4
+
5
+ ### Introspection
6
+
7
+ *New in Koffi 2.0: `koffi.resolve()`, new in Koffi 2.2: `koffi.offsetof()`*
8
+
9
+ ```{note}
10
+ The value returned by `introspect()` has **changed in version 2.0 and in version 2.2**.
11
+
12
+ In Koffi 1.x, it could only be used with struct types and returned the object passed to koffi.struct() with the member names and types.
13
+
14
+ Starting in Koffi 2.2, each record member is exposed as an object containing the name, the type and the offset within the record.
15
+
16
+ Consult the [migration guide](changes.md) for more information.
17
+ ```
18
+
19
+ Use `koffi.introspect(type)` to get detailed information about a type: name, primitive, size, alignment, members (record types), reference type (array, pointer) and length (array).
20
+
21
+ ```js
22
+ const FoobarType = koffi.struct('FoobarType', {
23
+ a: 'int',
24
+ b: 'char *',
25
+ c: 'double'
26
+ });
27
+
28
+ console.log(koffi.introspect(FoobarType));
29
+
30
+ // Expected result on 64-bit machines:
31
+ // {
32
+ // name: 'FoobarType',
33
+ // primitive: 'Record',
34
+ // size: 24,
35
+ // alignment: 8,
36
+ // members: {
37
+ // a: { name: 'a', type: [External: 4b28a60], offset: 0 },
38
+ // b: { name: 'b', type: [External: 4b292e0], offset: 8 },
39
+ // c: { name: 'c', type: [External: 4b29260], offset: 16 }
40
+ // }
41
+ // }
42
+ ```
43
+
44
+ Koffi also exposes a few more utility functions to get a subset of this information:
45
+
46
+ - `koffi.sizeof(type)` to get the size of a type
47
+ - `koffi.alignof(type)` to get the alignment of a type
48
+ - `koffi.offsetof(type, member_name)` to get the offset of a record member
49
+ - `koffi.resolve(type)` to get the resolved type object from a type string
50
+
51
+ Just like before, you can refer to primitive types by their name or through `koffi.types`:
52
+
53
+ ```js
54
+ // These two lines do the same:
55
+ console.log(koffi.sizeof('long'));
56
+ console.log(koffi.sizeof(koffi.types.long));
57
+ ```
58
+
59
+ ### Aliases
60
+
61
+ *New in Koffi 2.0*
62
+
63
+ You can alias a type with `koffi.alias(name, type)`. Aliased types are completely equivalent.
64
+
65
+ ## Settings
66
+
67
+ ### Memory usage
68
+
69
+ For synchronous/normal calls, Koffi uses two preallocated memory blocks:
70
+
71
+ - One to construct the C stack and assign registers, subsequently used by the platform-specific assembly code (1 MiB by default)
72
+ - One to allocate strings and big objects/structs (2 MiB by default)
73
+
74
+ Unless very big strings or objects (at least more than one page of memory) are used, Koffi does not directly allocate any extra memory during calls or callbacks. However, please note that the JS engine (V8) might.
75
+
76
+ The size (in bytes) of these preallocated blocks can be changed. Use `koffi.config()` to get an object with the settings, and `koffi.config(obj)` to apply new settings.
77
+
78
+ ```js
79
+ let config = koffi.config();
80
+ console.log(config);
81
+ ```
82
+
83
+ The same is true for asynchronous calls. When an asynchronous call is made, Koffi will allocate new blocks unless there is an unused (resident) set of blocks still available. Once the asynchronous call is finished, these blocks are freed if there are more than `resident_async_pools` sets of blocks left around.
84
+
85
+ There cannot be more than `max_async_calls` running at the same time.
86
+
87
+ ### Default settings
88
+
89
+ Setting | Default | Description
90
+ -------------------- | ------- | -----------------------------------------------
91
+ sync_stack_size | 1 MiB | Stack size for synchronous calls
92
+ sync_heap_size | 2 MiB | Heap size for synchronous calls
93
+ async_stack_size | 256 kiB | Stack size for asynchronous calls
94
+ async_heap_size | 512 kiB | Heap size for asynchronous calls
95
+ resident_async_pools | 2 | Number of resident pools for asynchronous calls
96
+ max_async_calls | 64 | Maximum number of ongoing asynchronous calls
97
+ max_type_size | 64 MiB | Maximum size of Koffi types (for arrays and structs)
@@ -0,0 +1,135 @@
1
+ # Data pointers
2
+
3
+ ## How pointers are used
4
+
5
+ In C, pointer arguments are used for differenty purposes. It is important to distinguish these use cases because Koffi provides different ways to deal with each of them:
6
+
7
+ - **Struct pointers**: Use of struct pointers by C libraries fall in two cases: avoid (potentially) expensive copies, and to let the function change struct contents (output or input/output arguments).
8
+ - **Opaque pointers**: the library does not expose the contents of the structs, and only provides you with a pointer to it (e.g. `FILE *`). Only the functions provided by the library can do something with this pointer, in Koffi we call this an opaque type. This is usually done for ABI-stability reason, and to prevent library users from messing directly with library internals.
9
+ - **Pointers to primitive types**: This is more rare, and generally used for output or input/output arguments. The Win32 API has a lot of these.
10
+ - **Arrays**: in C, you dynamically-sized arrays are usually passed to functions with pointers, either NULL-terminated (or any other sentinel value) or with an additional length argument.
11
+
12
+ ## Pointer types
13
+
14
+ ### Struct pointers
15
+
16
+ The following Win32 example uses `GetCursorPos()` (with an output parameter) to retrieve and show the current cursor position.
17
+
18
+ ```js
19
+ const koffi = require('koffi');
20
+ const lib = koffi.load('kernel32.dll');
21
+
22
+ // Type declarations
23
+ const POINT = koffi.struct('POINT', {
24
+ x: 'long',
25
+ y: 'long'
26
+ });
27
+
28
+ // Functions declarations
29
+ const GetCursorPos = lib.func('int __stdcall GetCursorPos(_Out_ POINT *pos)');
30
+
31
+ // Get and show cursor position
32
+ let pos = {};
33
+ if (!GetCursorPos(pos))
34
+ throw new Error('Failed to get cursor position');
35
+ console.log(pos);
36
+ ```
37
+
38
+ ### Opaque pointers
39
+
40
+ *New in Koffi 2.0*
41
+
42
+ Some C libraries use handles, which behave as pointers to opaque structs. An example of this is the HANDLE type in the Win32 API. If you want to reproduce this behavior, you can define a **named pointer type** to an opaque type, like so:
43
+
44
+ ```js
45
+ const HANDLE = koffi.pointer('HANDLE', koffi.opaque());
46
+
47
+ // And now you get to use it this way:
48
+ const GetHandleInformation = lib.func('bool __stdcall GetHandleInformation(HANDLE h, _Out_ uint32_t *flags)');
49
+ const CloseHandle = lib.func('bool __stdcall CloseHandle(HANDLE h)');
50
+ ```
51
+
52
+ ### Pointer to primitive types
53
+
54
+ In javascript, it is not possible to pass a primitive value by reference to another function. This means that you cannot call a function and expect it to modify the value of one of its number or string parameter.
55
+
56
+ However, arrays and objects (among others) are reference type values. Assigning an array or an object from one variable to another does not invole any copy. Instead, as the following example illustrates, the new variable references the same array as the first:
57
+
58
+ ```js
59
+ let list1 = [1, 2];
60
+ let list2 = list1;
61
+
62
+ list2[1] = 42;
63
+
64
+ console.log(list1); // Prints [1, 42]
65
+ ```
66
+
67
+ All of this means that C functions that are expected to modify their primitive output values (such as an `int *` parameter) cannot be used directly. However, thanks to Koffi's transparent array support, you can use Javascript arrays to approximate reference semantics with single-element arrays.
68
+
69
+ Below, you can find an example of an addition function where the result is stored in an `int *` input/output parameter and how to use this function from Koffi.
70
+
71
+ ```c
72
+ void AddInt(int *dest, int add)
73
+ {
74
+ *dest += add;
75
+ }
76
+ ```
77
+
78
+ You can simply pass a single-element array as the first argument:
79
+
80
+ ```js
81
+ const AddInt = lib.func('void AddInt(_Inout_ int *dest, int add)');
82
+
83
+ let sum = [36];
84
+ AddInt(sum, 6);
85
+
86
+ console.log(sum[0]); // Prints 42
87
+ ```
88
+
89
+ ### Array pointers (dynamic arrays)
90
+
91
+ In C, dynamically-sized arrays are usually passed around as pointers. The length is either passed as an additional argument, or inferred from the array content itself, for example with a terminating sentinel value (such as a NULL pointers in the case of an array of strings).
92
+
93
+ Koffi can translate JS arrays and TypedArrays to pointer arguments. However, because C does not have a proper notion of dynamically-sized arrays (fat pointers), you need to provide the length or the sentinel value yourself depending on the API.
94
+
95
+ Here is a simple example of a C function taking a NULL-terminated list of strings as input, to calculate the total length of all strings.
96
+
97
+ ```c
98
+ // Build with: clang -fPIC -o length.so -shared length.c -Wall -O2
99
+
100
+ #include <stdlib.h>
101
+ #include <stdint.h>
102
+ #include <string.h>
103
+
104
+ int64_t ComputeTotalLength(const char **strings)
105
+ {
106
+ int64_t total = 0;
107
+
108
+ for (const char **ptr = strings; *ptr; ptr++) {
109
+ const char *str = *ptr;
110
+ total += strlen(str);
111
+ }
112
+
113
+ return total;
114
+ }
115
+ ```
116
+
117
+ ```js
118
+ const koffi = require('koffi');
119
+ const lib = koffi.load('./length.so');
120
+
121
+ const ComputeTotalLength = lib.func('int64_t ComputeTotalLength(const char **strings)');
122
+
123
+ let strings = ['Get', 'Total', 'Length', null];
124
+ let total = ComputeTotalLength(strings);
125
+
126
+ console.log(total); // Prints 14
127
+ ```
128
+
129
+ By default, just like for objects, array arguments are copied from JS to C but not vice-versa. You can however change the direction as documented in the section on [output parameters](calls.md#output-parameters).
130
+
131
+ ## Disposable types
132
+
133
+ Disposable types allow you to register a function that will automatically called after each C to JS conversion performed by Koffi. This can be used to avoid leaking heap-allocated strings, for example.
134
+
135
+ Read the documentation for [disposable types](calls.md#heap-allocated-values) on the page about function calls.
@@ -1,14 +1,14 @@
1
1
  /* This program is free software: you can redistribute it and/or modify
2
- it under the terms of the GNU Affero General Public License as published by
2
+ it under the terms of the GNU Lesser General Public License as published by
3
3
  the Free Software Foundation, either version 3 of the License, or
4
4
  (at your option) any later version.
5
5
 
6
6
  This program is distributed in the hope that it will be useful,
7
7
  but WITHOUT ANY WARRANTY; without even the implied warranty of
8
8
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
- GNU Affero General Public License for more details.
9
+ GNU Lesser General Public License for more details.
10
10
 
11
- You should have received a copy of the GNU Affero General Public License
11
+ You should have received a copy of the GNU Lesser General Public License
12
12
  along with this program. If not, see https://www.gnu.org/licenses/. */
13
13
 
14
14
  aside.footnote {
package/doc/types.md CHANGED
@@ -142,14 +142,14 @@ const Function2 = lib.func('Function', A, [A]);
142
142
 
143
143
  Many C libraries use some kind of object-oriented API, with a pair of functions dedicated to create and delete objects. An obvious example of this can be found in stdio.h, with the opaque `FILE *` pointer. You can open and close files with `fopen()` and `fclose()`, and manipule the opaque pointer with other functions such as `fread()` or `ftell()`.
144
144
 
145
- In Koffi, you can manage this with opaque types. Declare the opaque type with `koffi.opaque(name)`, and use a pointer to this type either as a return type or some kind of [output parameter](functions.md#output-parameters) (with a double pointer).
145
+ In Koffi, you can manage this with opaque types. Declare the opaque type with `koffi.opaque(name)`, and use a pointer to this type either as a return type or some kind of [output parameter](calls.md#output-parameters) (with a double pointer).
146
146
 
147
147
  ```{note}
148
148
  Opaque types **have changed in version 2.0, and again in version 2.1**.
149
149
 
150
150
  In Koffi 1.x, opaque handles were defined in a way that made them usable directly as parameter and return types, obscuring the underlying pointer.
151
151
 
152
- Now, you must use them through a pointer, and use an array for output parameters. This is shown in the example below (look for the call to `ConcatNewOut` in the JS part), and is described in the section on [output parameters](functions.md#output-parameters).
152
+ Now, you must use them through a pointer, and use an array for output parameters. This is shown in the example below (look for the call to `ConcatNewOut` in the JS part), and is described in the section on [output parameters](calls.md#output-parameters).
153
153
 
154
154
  In addition to this, you should use `koffi.opaque()` (introduced in Koffi 2.1) instead of `koffi.handle()` which is deprecated, and will be removed eventually in Koffi 3.0.
155
155
 
@@ -330,90 +330,6 @@ try {
330
330
  }
331
331
  ```
332
332
 
333
- ## Pointer types
334
-
335
- In C, pointer arguments are used for differenty purposes. It is important to distinguish these use cases because Koffi provides different ways to deal with each of them:
336
-
337
- - **Struct pointers**: Use of struct pointers by C libraries fall in two cases: avoid (potentially) expensive copies, and to let the function change struct contents (output or input/output arguments).
338
- - **Opaque pointers**: the library does not expose the contents of the structs, and only provides you with a pointer to it (e.g. `FILE *`). Only the functions provided by the library can do something with this pointer, in Koffi we call this an opaque type. This is usually done for ABI-stability reason, and to prevent library users from messing directly with library internals.
339
- - **Pointers to primitive types**: This is more rare, and generally used for output or input/output arguments. The Win32 API has a lot of these.
340
- - **Arrays**: in C, you dynamically-sized arrays are usually passed to functions with pointers, either NULL-terminated (or any other sentinel value) or with an additional length argument.
341
-
342
- ### Struct pointers
343
-
344
- The following Win32 example uses `GetCursorPos()` (with an output parameter) to retrieve and show the current cursor position.
345
-
346
- ```js
347
- const koffi = require('koffi');
348
- const lib = koffi.load('kernel32.dll');
349
-
350
- // Type declarations
351
- const POINT = koffi.struct('POINT', {
352
- x: 'long',
353
- y: 'long'
354
- });
355
-
356
- // Functions declarations
357
- const GetCursorPos = lib.func('int __stdcall GetCursorPos(_Out_ POINT *pos)');
358
-
359
- // Get and show cursor position
360
- let pos = {};
361
- if (!GetCursorPos(pos))
362
- throw new Error('Failed to get cursor position');
363
- console.log(pos);
364
- ```
365
-
366
- ### Named pointer types
367
-
368
- *New in Koffi 2.0*
369
-
370
- Some C libraries use handles, which behave as pointers to opaque structs. An example of this is the HANDLE type in the Win32 API. If you want to reproduce this behavior, you can define a **named pointer type** to an opaque type, like so:
371
-
372
- ```js
373
- const HANDLE = koffi.pointer('HANDLE', koffi.opaque());
374
-
375
- // And now you get to use it this way:
376
- const GetHandleInformation = lib.func('bool __stdcall GetHandleInformation(HANDLE h, _Out_ uint32_t *flags)');
377
- const CloseHandle = lib.func('bool __stdcall CloseHandle(HANDLE h)');
378
- ```
379
-
380
- ### Pointers to primitive types
381
-
382
- In javascript, it is not possible to pass a primitive value by reference to another function. This means that you cannot call a function and expect it to modify the value of one of its number or string parameter.
383
-
384
- However, arrays and objects (among others) are reference type values. Assigning an array or an object from one variable to another does not invole any copy. Instead, as the following example illustrates, the new variable references the same array as the first:
385
-
386
- ```js
387
- let list1 = [1, 2];
388
- let list2 = list1;
389
-
390
- list2[1] = 42;
391
-
392
- console.log(list1); // Prints [1, 42]
393
- ```
394
-
395
- All of this means that C functions that are expected to modify their primitive output values (such as an `int *` parameter) cannot be used directly. However, thanks to Koffi's transparent array support, you can use Javascript arrays to approximate reference semantics with single-element arrays.
396
-
397
- Below, you can find an example of an addition function where the result is stored in an `int *` input/output parameter and how to use this function from Koffi.
398
-
399
- ```c
400
- void AddInt(int *dest, int add)
401
- {
402
- *dest += add;
403
- }
404
- ```
405
-
406
- You can simply pass a single-element array as the first argument:
407
-
408
- ```js
409
- const AddInt = lib.func('void AddInt(_Inout_ int *dest, int add)');
410
-
411
- let sum = [36];
412
- AddInt(sum, 6);
413
-
414
- console.log(sum[0]); // Prints 42
415
- ```
416
-
417
333
  ## Array types
418
334
 
419
335
  ### Fixed-size C arrays
@@ -459,110 +375,4 @@ The reverse case is also true, Koffi can convert a C fixed-size buffer to a JS s
459
375
 
460
376
  ### Array pointers (dynamic arrays)
461
377
 
462
- In C, dynamically-sized arrays are usually passed around as pointers. The length is either passed as an additional argument, or inferred from the array content itself, for example with a terminating sentinel value (such as a NULL pointers in the case of an array of strings).
463
-
464
- Koffi can translate JS arrays and TypedArrays to pointer arguments. However, because C does not have a proper notion of dynamically-sized arrays (fat pointers), you need to provide the length or the sentinel value yourself depending on the API.
465
-
466
- Here is a simple example of a C function taking a NULL-terminated list of strings as input, to calculate the total length of all strings.
467
-
468
- ```c
469
- // Build with: clang -fPIC -o length.so -shared length.c -Wall -O2
470
-
471
- #include <stdlib.h>
472
- #include <stdint.h>
473
- #include <string.h>
474
-
475
- int64_t ComputeTotalLength(const char **strings)
476
- {
477
- int64_t total = 0;
478
-
479
- for (const char **ptr = strings; *ptr; ptr++) {
480
- const char *str = *ptr;
481
- total += strlen(str);
482
- }
483
-
484
- return total;
485
- }
486
- ```
487
-
488
- ```js
489
- const koffi = require('koffi');
490
- const lib = koffi.load('./length.so');
491
-
492
- const ComputeTotalLength = lib.func('int64_t ComputeTotalLength(const char **strings)');
493
-
494
- let strings = ['Get', 'Total', 'Length', null];
495
- let total = ComputeTotalLength(strings);
496
-
497
- console.log(total); // Prints 14
498
- ```
499
-
500
- By default, just like for objects, array arguments are copied from JS to C but not vice-versa. You can however change the direction as documented in the section on [output parameters](functions.md#output-parameters).
501
-
502
- ## Disposable types
503
-
504
- Disposable types allow you to register a function that will automatically called after each C to JS conversion performed by Koffi. This can be used to avoid leaking heap-allocated strings, for example.
505
-
506
- Read the documentation for [disposable types](functions.md#heap-allocated-values) on the page about function calls.
507
-
508
- ## Utility functions
509
-
510
- ### Type introspection
511
-
512
- *New in Koffi 2.0: `koffi.resolve()`, new in Koffi 2.2: `koffi.offsetof()`*
513
-
514
- ```{note}
515
- The value returned by `introspect()` has **changed in version 2.0 and in version 2.2**.
516
-
517
- In Koffi 1.x, it could only be used with struct types and returned the object passed to koffi.struct() with the member names and types.
518
-
519
- Starting in Koffi 2.2, each record member is exposed as an object containing the name, the type and the offset within the record.
520
-
521
- Consult the [migration guide](changes.md) for more information.
522
- ```
523
-
524
- Use `koffi.introspect(type)` to get detailed information about a type: name, primitive, size, alignment, members (record types), reference type (array, pointer) and length (array).
525
-
526
- ```js
527
- const FoobarType = koffi.struct('FoobarType', {
528
- a: 'int',
529
- b: 'char *',
530
- c: 'double'
531
- });
532
-
533
- console.log(koffi.introspect(FoobarType));
534
-
535
- // Expected result on 64-bit machines:
536
- // {
537
- // name: 'FoobarType',
538
- // primitive: 'Record',
539
- // size: 24,
540
- // alignment: 8,
541
- // members: {
542
- // a: { name: 'a', type: [External: 4b28a60], offset: 0 },
543
- // b: { name: 'b', type: [External: 4b292e0], offset: 8 },
544
- // c: { name: 'c', type: [External: 4b29260], offset: 16 }
545
- // }
546
- // }
547
- ```
548
-
549
- Koffi also exposes a few more utility functions to get a subset of this information:
550
-
551
- - `koffi.sizeof(type)` to get the size of a type
552
- - `koffi.alignof(type)` to get the alignment of a type
553
- - `koffi.offsetof(type, member_name)` to get the offset of a record member
554
- - `koffi.resolve(type)` to get the resolved type object from a type string
555
-
556
- Just like before, you can refer to primitive types by their name or through `koffi.types`:
557
-
558
- ```js
559
- // These two lines do the same:
560
- console.log(koffi.sizeof('long'));
561
- console.log(koffi.sizeof(koffi.types.long));
562
- ```
563
-
564
- ### Type aliases
565
-
566
- *New in Koffi 2.0*
567
-
568
- You can alias a type with `koffi.alias(name, type)`. Aliased types are completely equivalent.
378
+ In C, dynamically-sized arrays are usually passed around as pointers. Read more about [array pointers](pointers.md#array-pointers-dynamic-arrays) in the relevant section.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.2.3",
4
- "stable": "2.2.3",
3
+ "version": "2.2.5",
4
+ "stable": "2.2.5",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
7
7
  "foreign",
@@ -23,9 +23,9 @@
23
23
  "install": "cnoke --prebuild -d src/koffi",
24
24
  "test": "node src/koffi/qemu/qemu.js test"
25
25
  },
26
- "license": "AGPL-3.0",
26
+ "license": "LGPL-3.0",
27
27
  "dependencies": {
28
- "cnoke": "^3.2.1"
28
+ "cnoke": "^3.2.2"
29
29
  },
30
30
  "devDependencies": {
31
31
  "chalk": "^4.1.2",