koffi 3.0.0-alpha.8 → 3.0.0-alpha.9

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 (85) hide show
  1. package/LICENSE.txt +1 -1
  2. package/build/koffi/darwin_arm64/koffi.node +0 -0
  3. package/build/koffi/darwin_x64/koffi.node +0 -0
  4. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  5. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  6. package/build/koffi/freebsd_x64/koffi.node +0 -0
  7. package/build/koffi/linux_arm64/koffi.node +0 -0
  8. package/build/koffi/linux_ia32/koffi.node +0 -0
  9. package/build/koffi/linux_x64/koffi.node +0 -0
  10. package/build/koffi/musl_arm64/koffi.node +0 -0
  11. package/build/koffi/musl_x64/koffi.node +0 -0
  12. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  13. package/build/koffi/openbsd_x64/koffi.node +0 -0
  14. package/{bin → build}/koffi/win32_ia32/koffi.exp +0 -0
  15. package/{bin → build}/koffi/win32_ia32/koffi.node +0 -0
  16. package/{bin → build}/koffi/win32_x64/koffi.exp +0 -0
  17. package/{bin → build}/koffi/win32_x64/koffi.node +0 -0
  18. package/{build.cjs → cnoke.cjs} +2 -2
  19. package/doc/callbacks.md +8 -8
  20. package/doc/functions.md +6 -6
  21. package/doc/input.md +6 -6
  22. package/doc/misc.md +2 -2
  23. package/doc/output.md +10 -10
  24. package/doc/packaging.md +6 -6
  25. package/doc/pointers.md +12 -12
  26. package/doc/start.md +4 -4
  27. package/doc/unions.md +2 -2
  28. package/index.d.ts +84 -89
  29. package/lib/native/base/base.cc +8 -6
  30. package/lib/native/base/base.hh +1 -1
  31. package/lib/native/base/unicode.inc +1 -1
  32. package/package.json +3 -3
  33. package/src/koffi/CMakeLists.txt +2 -2
  34. package/src/koffi/index.cjs +27 -20
  35. package/src/koffi/index.js +21 -17
  36. package/src/koffi/indirect.cjs +27 -20
  37. package/src/koffi/indirect.js +7 -3
  38. package/src/koffi/src/abi/arm32.cc +3 -3
  39. package/src/koffi/src/abi/arm32_asm.S +1 -1
  40. package/src/koffi/src/abi/arm64.cc +7 -7
  41. package/src/koffi/src/abi/arm64_asm.S +1 -1
  42. package/src/koffi/src/abi/arm64_asm.asm +1 -1
  43. package/src/koffi/src/abi/loong64.cc +1 -1
  44. package/src/koffi/src/abi/loong64_asm.S +1 -1
  45. package/src/koffi/src/abi/riscv64.cc +3 -3
  46. package/src/koffi/src/abi/riscv64_asm.S +1 -1
  47. package/src/koffi/src/abi/x64sysv.cc +7 -7
  48. package/src/koffi/src/abi/x64sysv_asm.S +1 -1
  49. package/src/koffi/src/abi/x64win.cc +7 -7
  50. package/src/koffi/src/abi/x64win_asm.S +1 -1
  51. package/src/koffi/src/abi/x64win_asm.asm +1 -1
  52. package/src/koffi/src/abi/x86.cc +7 -7
  53. package/src/koffi/src/abi/x86_asm.S +1 -1
  54. package/src/koffi/src/abi/x86_asm.asm +1 -1
  55. package/src/koffi/src/call.cc +17 -9
  56. package/src/koffi/src/call.hh +1 -1
  57. package/src/koffi/src/errno.inc +1 -1
  58. package/src/koffi/src/ffi.cc +89 -110
  59. package/src/koffi/src/ffi.hh +46 -9
  60. package/src/koffi/src/parser.cc +1 -1
  61. package/src/koffi/src/parser.hh +1 -1
  62. package/src/koffi/src/primitives.inc +1 -1
  63. package/src/koffi/src/trampolines.cjs +1 -1
  64. package/src/koffi/src/util.cc +154 -47
  65. package/src/koffi/src/util.hh +34 -19
  66. package/src/koffi/src/uv.cc +36 -19
  67. package/src/koffi/src/uv.def +1 -0
  68. package/src/koffi/src/uv.hh +27 -6
  69. package/src/koffi/src/win32.cc +1 -1
  70. package/src/koffi/src/win32.hh +1 -1
  71. package/vendor/node-addon-api/napi-inl.h +28 -28
  72. package/bin/koffi/darwin_arm64/koffi.node +0 -0
  73. package/bin/koffi/darwin_x64/koffi.node +0 -0
  74. package/bin/koffi/freebsd_arm64/koffi.node +0 -0
  75. package/bin/koffi/freebsd_ia32/koffi.node +0 -0
  76. package/bin/koffi/freebsd_x64/koffi.node +0 -0
  77. package/bin/koffi/linux_arm64/koffi.node +0 -0
  78. package/bin/koffi/linux_ia32/koffi.node +0 -0
  79. package/bin/koffi/linux_x64/koffi.node +0 -0
  80. package/bin/koffi/musl_arm64/koffi.node +0 -0
  81. package/bin/koffi/musl_x64/koffi.node +0 -0
  82. package/bin/koffi/openbsd_ia32/koffi.node +0 -0
  83. package/bin/koffi/openbsd_x64/koffi.node +0 -0
  84. /package/{bin → build}/koffi/win32_ia32/koffi.lib +0 -0
  85. /package/{bin → build}/koffi/win32_x64/koffi.lib +0 -0
package/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (C) 2025 Niels Martignène <niels.martignene@protonmail.com>
3
+ Copyright (C) 2026 Niels Martignène <niels.martignene@protonmail.com>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the “Software”), to deal in
Binary file
Binary file
@@ -196,7 +196,7 @@ function compareVersions(ver1, ver2) {
196
196
 
197
197
  // ../cnoke/src/assets.js
198
198
  var FIND_CNOKE_CMAKE = `# SPDX-License-Identifier: MIT
199
- # SPDX-FileCopyrightText: 2025 Niels Martign\xE8ne <niels.martignene@protonmail.com>
199
+ # SPDX-FileCopyrightText: 2026 Niels Martign\xE8ne <niels.martignene@protonmail.com>
200
200
 
201
201
  if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR
202
202
  CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
@@ -324,7 +324,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU
324
324
  endif()
325
325
  `;
326
326
  var WIN_DELAY_HOOK_C = `// SPDX-License-Identifier: MIT
327
- // SPDX-FileCopyrightText: 2025 Niels Martign\xE8ne <niels.martignene@protonmail.com>
327
+ // SPDX-FileCopyrightText: 2026 Niels Martign\xE8ne <niels.martignene@protonmail.com>
328
328
 
329
329
  #include <stdlib.h>
330
330
  #if !defined(NOMINMAX)
package/doc/callbacks.md CHANGED
@@ -5,8 +5,8 @@
5
5
  In order to pass a JS function to a C function expecting a callback, you must first create a callback type with the expected return type and parameters. The syntax is similar to the one used to load functions from a shared library.
6
6
 
7
7
  ```js
8
- // ES6 syntax: import koffi from 'koffi';
9
- const koffi = require('koffi');
8
+ import koffi from 'koffi';
9
+ // CJS: const koffi = require('koffi');
10
10
 
11
11
  // With the classic syntax, this callback expects an integer and returns nothing
12
12
  const ExampleCallback = koffi.proto('ExampleCallback', 'void', ['int']);
@@ -71,8 +71,8 @@ int TransferToJS(const char *name, int age, int (*cb)(const char *str, int age))
71
71
  ```
72
72
 
73
73
  ```js
74
- // ES6 syntax: import koffi from 'koffi';
75
- const koffi = require('koffi');
74
+ import koffi from 'koffi';
75
+ // CJS: const koffi = require('koffi');
76
76
 
77
77
  const lib = koffi.load('./callbacks.so'); // Fake path
78
78
 
@@ -121,8 +121,8 @@ void SayIt(const char *name)
121
121
  ```
122
122
 
123
123
  ```js
124
- // ES6 syntax: import koffi from 'koffi';
125
- const koffi = require('koffi');
124
+ import koffi from 'koffi';
125
+ // CJS: const koffi = require('koffi');
126
126
 
127
127
  const lib = koffi.load('./callbacks.so'); // Fake path
128
128
 
@@ -169,8 +169,8 @@ You can pass this value through to another C function that expects a pointer of
169
169
  The following examples uses it to sort an array of strings in-place with the standard C function `qsort()`:
170
170
 
171
171
  ```js
172
- // ES6 syntax: import koffi from 'koffi';
173
- const koffi = require('koffi');
172
+ import koffi from 'koffi';
173
+ // CJS: const koffi = require('koffi');
174
174
 
175
175
  const lib = koffi.load('libc.so.6');
176
176
 
package/doc/functions.md CHANGED
@@ -3,8 +3,8 @@
3
3
  To declare functions, start by loading the shared library with `koffi.load(filename)`.
4
4
 
5
5
  ```js
6
- // ES6 syntax: import koffi from 'koffi';
7
- const koffi = require('koffi');
6
+ import koffi from 'koffi';
7
+ // CJS: const koffi = require('koffi');
8
8
 
9
9
  const lib = koffi.load('/path/to/shared/library'); // File extension depends on platforms: .so, .dll, .dylib, etc.
10
10
  ```
@@ -105,8 +105,8 @@ You can safely use these on non-x86 platforms, they are simply ignored.
105
105
  Below you can find a small example showing how to use a non-default calling convention, with the two syntaxes:
106
106
 
107
107
  ```js
108
- // ES6 syntax: import koffi from 'koffi';
109
- const koffi = require('koffi');
108
+ import koffi from 'koffi';
109
+ // CJS: const koffi = require('koffi');
110
110
 
111
111
  const lib = koffi.load('user32.dll');
112
112
 
@@ -142,8 +142,8 @@ printf('Integer %d, double %g, str %s', 'int', 6, 'double', 8.5, 'str', 'THE END
142
142
  You can issue asynchronous calls by calling the function through its async member. In this case, you need to provide a callback function as the last argument, with `(err, res)` parameters.
143
143
 
144
144
  ```js
145
- // ES6 syntax: import koffi from 'koffi';
146
- const koffi = require('koffi');
145
+ import koffi from 'koffi';
146
+ // CJS: const koffi = require('koffi');
147
147
 
148
148
  const lib = koffi.load('libc.so.6');
149
149
 
package/doc/input.md CHANGED
@@ -278,8 +278,8 @@ const char *ConcatBuild(Concat *c)
278
278
  ```
279
279
 
280
280
  ```js
281
- // ES6 syntax: import koffi from 'koffi';
282
- const koffi = require('koffi');
281
+ import koffi from 'koffi';
282
+ // CJS: const koffi = require('koffi');
283
283
 
284
284
  const lib = koffi.load('./handles.so');
285
285
 
@@ -348,8 +348,8 @@ Koffi applies the following conversion rules when passing arrays to/from C:
348
348
  See the example below:
349
349
 
350
350
  ```js
351
- // ES6 syntax: import koffi from 'koffi';
352
- const koffi = require('koffi');
351
+ import koffi from 'koffi';
352
+ // CJS: const koffi = require('koffi');
353
353
 
354
354
  // Those two structs are exactly the same, only the array conversion hint is different
355
355
  const Foo1 = koffi.struct('Foo1', {
@@ -438,8 +438,8 @@ void AppendValues(struct FlexibleArray *arr, size_t count, int start, int step)
438
438
  ```
439
439
 
440
440
  ```js
441
- // ES6 syntax: import koffi from 'koffi';
442
- const koffi = require('koffi');
441
+ import koffi from 'koffi';
442
+ // CJS: const koffi = require('koffi');
443
443
 
444
444
  const lib = koffi.load('./flexible.so');
445
445
 
package/doc/misc.md CHANGED
@@ -151,8 +151,8 @@ The standard POSIX error codes are available in `koffi.os.errno`, as shown below
151
151
  ```js
152
152
  const assert = require('assert');
153
153
 
154
- // ES6 syntax: import koffi from 'koffi';
155
- const koffi = require('koffi');
154
+ import koffi from 'koffi';
155
+ // CJS: const koffi = require('koffi');
156
156
 
157
157
  const lib = koffi.load('libc.so.6');
158
158
 
package/doc/output.md CHANGED
@@ -29,8 +29,8 @@ The same can be done when declaring a function with a C-like prototype string, w
29
29
  This Windows example enumerate all Chrome windows along with their PID and their title. The `GetWindowThreadProcessId()` function illustrates how to get a primitive value from an output argument.
30
30
 
31
31
  ```js
32
- // ES6 syntax: import koffi from 'koffi';
33
- const koffi = require('koffi');
32
+ import koffi from 'koffi';
33
+ // CJS: const koffi = require('koffi');
34
34
 
35
35
  const user32 = koffi.load('user32.dll');
36
36
 
@@ -87,8 +87,8 @@ for (let hwnd = null;;) {
87
87
  This example calls the POSIX function `gettimeofday()`, and uses the prototype-like syntax.
88
88
 
89
89
  ```js
90
- // ES6 syntax: import koffi from 'koffi';
91
- const koffi = require('koffi');
90
+ import koffi from 'koffi';
91
+ // CJS: const koffi = require('koffi');
92
92
 
93
93
  const lib = koffi.load('libc.so.6');
94
94
 
@@ -115,8 +115,8 @@ console.log(tv);
115
115
  Many Win32 functions that use struct outputs require you to set a size member (often named `cbSize`). These functions won't work with `_Out_` because the size value must be copied from JS to C, use `_Inout_` in this case.
116
116
 
117
117
  ```js
118
- // ES6 syntax: import koffi from 'koffi';
119
- const koffi = require('koffi');
118
+ import koffi from 'koffi';
119
+ // CJS: const koffi = require('koffi');
120
120
 
121
121
  const user32 = koffi.load('user32.dll');
122
122
 
@@ -137,8 +137,8 @@ console.log(success, info);
137
137
  This example opens an in-memory SQLite database, and uses the node-ffi-style function declaration syntax.
138
138
 
139
139
  ```js
140
- // ES6 syntax: import koffi from 'koffi';
141
- const koffi = require('koffi');
140
+ import koffi from 'koffi';
141
+ // CJS: const koffi = require('koffi');
142
142
 
143
143
  const lib = koffi.load('sqlite3.so');
144
144
 
@@ -215,8 +215,8 @@ You can use buffers and typed arrays for output (and input/output) pointer param
215
215
  Once the native function returns, you can decode the content with `koffi.decode(value, type)` as in the following example:
216
216
 
217
217
  ```js
218
- // ES6 syntax: import koffi from 'koffi';
219
- const koffi = require('koffi');
218
+ import koffi from 'koffi';
219
+ // CJS: const koffi = require('koffi');
220
220
 
221
221
  const lib = koffi.load('libc.so.6');
222
222
 
package/doc/packaging.md CHANGED
@@ -45,7 +45,7 @@ MyApp.exe
45
45
 
46
46
  Some bundlers (such as vite) don't like when require is used with native modules.
47
47
 
48
- In this case, you can use `require('koffi/indirect')` but you will need to make sure that the native Koffi modules are packaged properly.
48
+ In this case, you can use `import koffi from 'koffi/indirect'` but you will need to make sure that the native Koffi modules are packaged properly.
49
49
 
50
50
  # Packaging examples
51
51
 
@@ -53,7 +53,7 @@ In this case, you can use `require('koffi/indirect')` but you will need to make
53
53
 
54
54
  Packaging with electron-builder should work as-is.
55
55
 
56
- Take a look at the full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/electron-builder).
56
+ Take a look at the full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/packaging/electron-builder).
57
57
 
58
58
  ## Electron Forge
59
59
 
@@ -63,13 +63,13 @@ Packaging with Electron Force should work as-is, even when using webpack as conf
63
63
  npm init electron-app@latest my-app -- --template=webpack
64
64
  ```
65
65
 
66
- Take a look at the full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/electron-forge).
66
+ Take a look at the full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/packaging/electron-forge).
67
67
 
68
68
  ## NW.js
69
69
 
70
70
  Packagers such as nw-builder should work as-is.
71
71
 
72
- You can find a full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/nwjs).
72
+ You can find a full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/packaging/nwjs).
73
73
 
74
74
  ## Node.js and esbuild
75
75
 
@@ -79,10 +79,10 @@ You can easily tell esbuild to copy the native files with the copy loader and th
79
79
  esbuild index.js --platform=node --bundle --loader:.node=copy --outdir=dist/
80
80
  ```
81
81
 
82
- You can find a full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/node-esbuild).
82
+ You can find a full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/packaging/node-esbuild).
83
83
 
84
84
  ## Node.js and yao-pkg
85
85
 
86
86
  Use [yao-pkg](https://github.com/yao-pkg/pkg) to make binary packages of your Node.js-based project.
87
87
 
88
- You can find a full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/yao-pkg).
88
+ You can find a full [working example in the repository](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi/examples/packaging/yao-pkg).
package/doc/pointers.md CHANGED
@@ -14,8 +14,8 @@ In C, pointer arguments are used for differenty purposes. It is important to dis
14
14
  The following Win32 example uses `GetCursorPos()` (with an output parameter) to retrieve and show the current cursor position.
15
15
 
16
16
  ```js
17
- // ES6 syntax: import koffi from 'koffi';
18
- const koffi = require('koffi');
17
+ import koffi from 'koffi';
18
+ // CJS: const koffi = require('koffi');
19
19
 
20
20
  const lib = koffi.load('user32.dll');
21
21
 
@@ -117,8 +117,8 @@ int64_t ComputeTotalLength(const char **strings)
117
117
  ```
118
118
 
119
119
  ```js
120
- // ES6 syntax: import koffi from 'koffi';
121
- const koffi = require('koffi');
120
+ import koffi from 'koffi';
121
+ // CJS: const koffi = require('koffi');
122
122
 
123
123
  const lib = koffi.load('./length.so');
124
124
 
@@ -146,8 +146,8 @@ Koffi provides two features to deal with this:
146
146
  The example below shows the use of `koffi.as()` to read the header of a PNG file with `fread()` directly to a JS object.
147
147
 
148
148
  ```js
149
- // ES6 syntax: import koffi from 'koffi';
150
- const koffi = require('koffi');
149
+ import koffi from 'koffi';
150
+ // CJS: const koffi = require('koffi');
151
151
 
152
152
  const lib = koffi.load('libc.so.6');
153
153
 
@@ -218,8 +218,8 @@ const ExplicitFree = koffi.disposable('HeapStr16', 'str16', koffi.free); // You
218
218
  The following example illustrates the use of a disposable type derived from *str*.
219
219
 
220
220
  ```js
221
- // ES6 syntax: import koffi from 'koffi';
222
- const koffi = require('koffi');
221
+ import koffi from 'koffi';
222
+ // CJS: const koffi = require('koffi');
223
223
 
224
224
  const lib = koffi.load('libc.so.6');
225
225
 
@@ -233,8 +233,8 @@ console.log(copy); // Prints Hello!
233
233
  When you declare functions with the [prototype-like syntax](functions#definition-syntax), you can either use named disposable types or use the '!' shortcut qualifier with compatibles types, as shown in the example below. This qualifier creates an anonymous disposable type that calls `koffi.free(ptr)`.
234
234
 
235
235
  ```js
236
- // ES6 syntax: import koffi from 'koffi';
237
- const koffi = require('koffi');
236
+ import koffi from 'koffi';
237
+ // CJS: const koffi = require('koffi');
238
238
 
239
239
  const lib = koffi.load('libc.so.6');
240
240
 
@@ -262,8 +262,8 @@ You can access unmanaged memory with `koffi.view(ptr, len)`. This function takes
262
262
  The following Linux example writes the string "Hello World!" to a file named "hello.txt" through mmaped memory, to demontrate the use of `koffi.view()`:
263
263
 
264
264
  ```js
265
- // ES6 syntax: import koffi from 'koffi';
266
- const koffi = require('koffi');
265
+ import koffi from 'koffi';
266
+ // CJS: const koffi = require('koffi');
267
267
 
268
268
  const libc = koffi.load('libc.so.6');
269
269
 
package/doc/start.md CHANGED
@@ -30,8 +30,8 @@ This is a small example for Linux systems, which uses `gettimeofday()`, `localti
30
30
  It illustrates the use of output parameters.
31
31
 
32
32
  ```js
33
- // ES6 syntax: import koffi from 'koffi';
34
- const koffi = require('koffi');
33
+ import koffi from 'koffi';
34
+ // CJS: const koffi = require('koffi');
35
35
 
36
36
  // Load the shared library
37
37
  const lib = koffi.load('libc.so.6');
@@ -81,8 +81,8 @@ This is a small example targeting the Win32 API, using `MessageBox()` to show a
81
81
  It illustrates the use of the x86 stdcall calling convention, and the use of UTF-8 and UTF-16 string arguments.
82
82
 
83
83
  ```js
84
- // ES6 syntax: import koffi from 'koffi';
85
- const koffi = require('koffi');
84
+ import koffi from 'koffi';
85
+ // CJS: const koffi = require('koffi');
86
86
 
87
87
  // Load the shared library
88
88
  const lib = koffi.load('user32.dll');
package/doc/unions.md CHANGED
@@ -56,8 +56,8 @@ DoSomething('string', { str: 'Hello!' });
56
56
  The following example uses the [SendInput](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput) Win32 API to emit the Win+D shortcut and hide windows (show the desktop).
57
57
 
58
58
  ```js
59
- // ES6 syntax: import koffi from 'koffi';
60
- const koffi = require('koffi');
59
+ import koffi from 'koffi';
60
+ // CJS: const koffi = require('koffi');
61
61
 
62
62
  // Win32 type and functions
63
63
 
package/index.d.ts CHANGED
@@ -1,17 +1,5 @@
1
1
  // SPDX-License-Identifier: MIT
2
- // SPDX-FileCopyrightText: 2025 Niels Martignène <niels.martignene@protonmail.com>
3
-
4
- type LoadOptions = {
5
- lazy?: boolean,
6
- global?: boolean,
7
- deep?: boolean
8
- };
9
-
10
- export function load(path: string | null, options?: LoadOptions): IKoffiLib;
11
-
12
- interface IKoffiCType { __brand: 'IKoffiCType' }
13
- interface IKoffiPointerCast { __brand: 'IKoffiPointerCast' }
14
- interface IKoffiRegisteredCallback { __brand: 'IKoffiRegisteredCallback' }
2
+ // SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
15
3
 
16
4
  type PrimitiveKind = 'Void' | 'Bool' | 'Int8' | 'UInt8' | 'Int16' | 'Int16S' | 'UInt16' | 'UInt16S' |
17
5
  'Int32' | 'Int32S' | 'UInt32' | 'UInt32S' | 'Int64' | 'Int64S' | 'UInt64' | 'UInt64S' |
@@ -21,11 +9,14 @@ type ArrayHint = 'Array' | 'Typed' | 'String';
21
9
 
22
10
  type PrototypeInfo = {
23
11
  name: string;
24
- arguments: TypeInfo[];
25
- result: TypeInfo
12
+ arguments: TypeObject[];
13
+ result: TypeObject
26
14
  };
27
15
 
28
- type TypeInfo = {
16
+ interface IKoffiDirectionMarker { __brand: 'IKoffiDirectionMarker' }
17
+ interface IKoffiPointerCast { __brand: 'IKoffiPointerCast' }
18
+
19
+ export type TypeObject = {
29
20
  name: string;
30
21
  primitive: PrimitiveKind;
31
22
  size: number;
@@ -33,12 +24,13 @@ type TypeInfo = {
33
24
  disposable: boolean;
34
25
  length?: number;
35
26
  hint?: ArrayHint;
36
- ref?: TypeInfo;
37
- members?: Record<string, { name: string, type: TypeInfo, offset: number }>;
27
+ ref?: TypeObject;
28
+ members?: Record<string, { name: string, type: TypeObject, offset: number }>;
38
29
  proto?: PrototypeInfo;
39
30
  values?: Record<string, number | bigint>;
40
31
  };
41
- type TypeSpec = string | TypeInfo | IKoffiCType;
32
+
33
+ type TypeSpec = string | TypeObject | IKoffiDirectionMarker;
42
34
  type TypeSpecWithAlignment = TypeSpec | [number, TypeSpec];
43
35
 
44
36
  type KoffiFunction = {
@@ -51,7 +43,15 @@ export type KoffiFunc<T extends (...args: any) => any> = T & {
51
43
  info: PrototypeInfo;
52
44
  };
53
45
 
54
- export interface IKoffiLib {
46
+ export type CallbackHandle = {};
47
+
48
+ type LoadOptions = {
49
+ lazy?: boolean,
50
+ global?: boolean,
51
+ deep?: boolean
52
+ };
53
+
54
+ export type LibraryHandle = {
55
55
  func(definition: string): KoffiFunction;
56
56
  func(name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
57
57
  func(convention: string, name: string | number, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
@@ -68,62 +68,57 @@ export interface IKoffiLib {
68
68
  symbol(name: string, type: TypeSpec): any;
69
69
 
70
70
  unload(): void;
71
- }
71
+ };
72
72
 
73
- export function struct(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeInfo;
74
- export function struct(ref: TypeInfo, def: Record<string, TypeSpecWithAlignment>): TypeInfo;
75
- export function struct(def: Record<string, TypeSpecWithAlignment>): TypeInfo;
76
- export function pack(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeInfo;
77
- export function pack(ref: TypeInfo, def: Record<string, TypeSpecWithAlignment>): TypeInfo;
78
- export function pack(def: Record<string, TypeSpecWithAlignment>): TypeInfo;
73
+ export function load(path: string | null, options?: LoadOptions): LibraryHandle;
79
74
 
80
- export function union(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeInfo;
81
- export function union(ref: TypeInfo, def: Record<string, TypeSpecWithAlignment>): TypeInfo;
82
- export function union(def: Record<string, TypeSpecWithAlignment>): TypeInfo;
75
+ export function struct(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeObject;
76
+ export function struct(ref: TypeObject, def: Record<string, TypeSpecWithAlignment>): TypeObject;
77
+ export function struct(def: Record<string, TypeSpecWithAlignment>): TypeObject;
78
+ export function pack(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeObject;
79
+ export function pack(ref: TypeObject, def: Record<string, TypeSpecWithAlignment>): TypeObject;
80
+ export function pack(def: Record<string, TypeSpecWithAlignment>): TypeObject;
81
+
82
+ export function union(name: string | null | undefined, def: Record<string, TypeSpecWithAlignment>): TypeObject;
83
+ export function union(ref: TypeObject, def: Record<string, TypeSpecWithAlignment>): TypeObject;
84
+ export function union(def: Record<string, TypeSpecWithAlignment>): TypeObject;
83
85
 
84
86
  export class Union {
85
87
  constructor(type: TypeSpec);
86
88
  [s: string]: any;
87
89
  }
88
90
 
89
- export function enumeration(name: string | null | undefined, def: Record<string, number | bigint>, storage?: TypeSpec | null) : TypeInfo;
90
- export function enumeration(def: Record<string, number | bigint>, storage?: TypeSpec | null) : TypeInfo;
91
-
92
- export function array(ref: TypeSpec, len: number, hint?: ArrayHint | null): TypeInfo;
93
- export function array(ref: TypeSpec, countedBy: string, maxLen: number, hint?: ArrayHint | null): TypeInfo;
94
-
95
- export function opaque(name: string | null | undefined): TypeInfo;
96
- export function opaque(): TypeInfo;
97
- /** @deprecated */ export function handle(name: string | null | undefined): TypeInfo;
98
- /** @deprecated */ export function handle(): TypeInfo;
99
-
100
- export function pointer(ref: TypeSpec): TypeInfo;
101
- export function pointer(ref: TypeSpec, count: number): TypeInfo;
102
- export function pointer(name: string | null | undefined, ref: TypeSpec, countedBy?: string | null): TypeInfo;
103
- export function pointer(name: string | null | undefined, ref: TypeSpec, count: number): TypeInfo;
104
-
105
- export function out(type: TypeSpec): IKoffiCType;
106
- export function inout(type: TypeSpec): IKoffiCType;
107
-
108
- export function disposable(type: TypeSpec): TypeInfo;
109
- export function disposable(type: TypeSpec, freeFunction: Function): TypeInfo;
110
- export function disposable(name: string | null | undefined, type: TypeSpec): TypeInfo;
111
- export function disposable(name: string | null | undefined, type: TypeSpec, freeFunction: Function): TypeInfo;
112
-
113
- export function proto(definition: string): TypeInfo;
114
- export function proto(result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
115
- export function proto(convention: string, result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
116
- export function proto(name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
117
- export function proto(convention: string, name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
118
- /** @deprecated */ export function callback(definition: string): TypeInfo;
119
- /** @deprecated */ export function callback(result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
120
- /** @deprecated */ export function callback(convention: string, result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
121
- /** @deprecated */ export function callback(name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
122
- /** @deprecated */ export function callback(convention: string, name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeInfo;
123
-
124
- export function register(callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
125
- export function register(thisValue: any, callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
126
- export function unregister(callback: IKoffiRegisteredCallback): void;
91
+ export function enumeration(name: string | null | undefined, def: Record<string, number | bigint>, storage?: TypeSpec | null) : TypeObject;
92
+ export function enumeration(def: Record<string, number | bigint>, storage?: TypeSpec | null) : TypeObject;
93
+
94
+ export function array(ref: TypeSpec, len: number, hint?: ArrayHint | null): TypeObject;
95
+ export function array(ref: TypeSpec, countedBy: string, maxLen: number, hint?: ArrayHint | null): TypeObject;
96
+
97
+ export function opaque(name: string | null | undefined): TypeObject;
98
+ export function opaque(): TypeObject;
99
+
100
+ export function pointer(ref: TypeSpec): TypeObject;
101
+ export function pointer(ref: TypeSpec, count: number): TypeObject;
102
+ export function pointer(name: string | null | undefined, ref: TypeSpec, countedBy?: string | null): TypeObject;
103
+ export function pointer(name: string | null | undefined, ref: TypeSpec, count: number): TypeObject;
104
+
105
+ export function out(type: TypeSpec): IKoffiDirectionMarker;
106
+ export function inout(type: TypeSpec): IKoffiDirectionMarker;
107
+
108
+ export function disposable(type: TypeSpec): TypeObject;
109
+ export function disposable(type: TypeSpec, freeFunction: Function): TypeObject;
110
+ export function disposable(name: string | null | undefined, type: TypeSpec): TypeObject;
111
+ export function disposable(name: string | null | undefined, type: TypeSpec, freeFunction: Function): TypeObject;
112
+
113
+ export function proto(definition: string): TypeObject;
114
+ export function proto(result: TypeSpec, arguments: TypeSpec[]): TypeObject;
115
+ export function proto(convention: string, result: TypeSpec, arguments: TypeSpec[]): TypeObject;
116
+ export function proto(name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeObject;
117
+ export function proto(convention: string, name: string | null | undefined, result: TypeSpec, arguments: TypeSpec[]): TypeObject;
118
+
119
+ export function register(callback: Function, type: TypeSpec): CallbackHandle;
120
+ export function register(thisValue: any, callback: Function, type: TypeSpec): CallbackHandle;
121
+ export function unregister(callback: CallbackHandle): void;
127
122
 
128
123
  export function as(value: any, type: TypeSpec): IKoffiPointerCast;
129
124
  export function decode(value: any, type: TypeSpec): any;
@@ -139,14 +134,14 @@ export function encode(ref: any, offset: number, type: TypeSpec, value: any): vo
139
134
  export function encode(ref: any, offset: number, type: TypeSpec, value: any, len: number): void;
140
135
  export function view(ref: any, len: number): ArrayBuffer;
141
136
 
142
- export function type(type: TypeSpec): TypeInfo;
137
+ export function type(type: TypeSpec): TypeObject;
143
138
  export function sizeof(type: TypeSpec): number;
144
139
  export function alignof(type: TypeSpec): number;
145
140
  export function offsetof(type: TypeSpec, member_name: string): number;
146
- /** @deprecated */ export function resolve(type: TypeSpec): TypeInfo;
147
- /** @deprecated */ export function introspect(type: TypeSpec): TypeInfo;
141
+ /** @deprecated */ export function resolve(type: TypeSpec): TypeObject;
142
+ /** @deprecated */ export function introspect(type: TypeSpec): TypeObject;
148
143
 
149
- export function alias(name: string, type: TypeSpec): TypeInfo;
144
+ export function alias(name: string, type: TypeSpec): TypeObject;
150
145
 
151
146
  type KoffiConfig = {
152
147
  sync_stack_size?: number;
@@ -261,32 +256,32 @@ type PrimitiveTypes =
261
256
  | 'void'
262
257
  | 'wchar'
263
258
  | 'wchar_t';
264
- export const types: Record<PrimitiveTypes, TypeInfo>;
265
-
266
- export interface IKoffiPollOptions {
267
- readable?: boolean;
268
- writable?: boolean;
269
- disconnect?: boolean;
270
- }
271
-
272
- export interface IKoffiPollEvents {
273
- readable: boolean;
274
- writable: boolean;
275
- disconnect: boolean;
276
- }
259
+ export const types: Record<PrimitiveTypes, TypeObject>;
277
260
 
278
261
  export namespace node {
279
262
  export const env: { __brand: 'IKoffiNodeEnv' };
280
263
 
264
+ export type PollOptions = {
265
+ readable?: boolean;
266
+ writable?: boolean;
267
+ disconnect?: boolean;
268
+ };
269
+
270
+ export type PollEvents = {
271
+ readable: boolean;
272
+ writable: boolean;
273
+ disconnect: boolean;
274
+ };
275
+
281
276
  export class PollHandle {
282
- start(opts: IKoffiPollOptions, callback: (ev: IKoffiPollEvents) => void): void;
283
- start(callback: (ev: IKoffiPollEvents) => void): void;
277
+ start(opts: PollOptions, callback: (ev: PollEvents) => void): void;
278
+ start(callback: (ev: PollEvents) => void): void;
284
279
  stop(): void;
285
280
  close(): void;
286
281
  unref(): void;
287
282
  ref(): void;
288
283
  }
289
284
 
290
- export function poll(fd: number, opts: IKoffiPollOptions, callback: (ev: IKoffiPollEvents) => void): PollHandle;
291
- export function poll(fd: number, callback: (ev: IKoffiPollEvents) => void): PollHandle;
285
+ export function poll(fd: number, opts: PollOptions, callback: (ev: PollEvents) => void): PollHandle;
286
+ export function poll(fd: number, callback: (ev: PollEvents) => void): PollHandle;
292
287
  }