@lakutata/ffi 0.0.0-stage → 3.1.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.
Files changed (129) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +163 -2
  3. package/README.zh-CN.md +140 -0
  4. package/dist/cjs/components/NativeLibrary.d.ts +229 -0
  5. package/dist/cjs/components/NativeLibrary.js +346 -0
  6. package/dist/cjs/decorators/Library.d.ts +61 -0
  7. package/dist/cjs/decorators/Library.js +44 -0
  8. package/dist/cjs/decorators/Native.d.ts +69 -0
  9. package/dist/cjs/decorators/Native.js +90 -0
  10. package/dist/cjs/exceptions/NativeDeclarationException.d.ts +12 -0
  11. package/dist/cjs/exceptions/NativeDeclarationException.js +19 -0
  12. package/dist/cjs/exceptions/NativeLibraryClosedException.d.ts +12 -0
  13. package/dist/cjs/exceptions/NativeLibraryClosedException.js +19 -0
  14. package/dist/cjs/exceptions/NativeLibraryNotFoundException.d.ts +11 -0
  15. package/dist/cjs/exceptions/NativeLibraryNotFoundException.js +18 -0
  16. package/dist/cjs/exceptions/NativeMemoryException.d.ts +11 -0
  17. package/dist/cjs/exceptions/NativeMemoryException.js +18 -0
  18. package/dist/cjs/exports/FFI.d.ts +12 -0
  19. package/dist/cjs/exports/FFI.js +40 -0
  20. package/dist/cjs/exports/Generator.d.ts +2 -0
  21. package/dist/cjs/exports/Generator.js +10 -0
  22. package/dist/cjs/lib/Memory.d.ts +188 -0
  23. package/dist/cjs/lib/Memory.js +227 -0
  24. package/dist/cjs/lib/Runtime.d.ts +98 -0
  25. package/dist/cjs/lib/Runtime.js +209 -0
  26. package/dist/cjs/lib/backend/Functions.d.ts +65 -0
  27. package/dist/cjs/lib/backend/Functions.js +214 -0
  28. package/dist/cjs/lib/backend/KoffiTypes.d.ts +53 -0
  29. package/dist/cjs/lib/backend/KoffiTypes.js +426 -0
  30. package/dist/cjs/lib/declarations/Builtins.d.ts +33 -0
  31. package/dist/cjs/lib/declarations/Builtins.js +179 -0
  32. package/dist/cjs/lib/declarations/Layout.d.ts +41 -0
  33. package/dist/cjs/lib/declarations/Layout.js +180 -0
  34. package/dist/cjs/lib/declarations/Lexer.d.ts +40 -0
  35. package/dist/cjs/lib/declarations/Lexer.js +325 -0
  36. package/dist/cjs/lib/declarations/Parser.d.ts +90 -0
  37. package/dist/cjs/lib/declarations/Parser.js +1425 -0
  38. package/dist/cjs/lib/declarations/Platform.d.ts +50 -0
  39. package/dist/cjs/lib/declarations/Platform.js +27 -0
  40. package/dist/cjs/lib/declarations/Types.d.ts +173 -0
  41. package/dist/cjs/lib/declarations/Types.js +179 -0
  42. package/dist/cjs/lib/generator/CText.d.ts +14 -0
  43. package/dist/cjs/lib/generator/CText.js +133 -0
  44. package/dist/cjs/lib/generator/Generator.d.ts +136 -0
  45. package/dist/cjs/lib/generator/Generator.js +395 -0
  46. package/dist/cjs/lib/generator/Preprocessor.d.ts +60 -0
  47. package/dist/cjs/lib/generator/Preprocessor.js +94 -0
  48. package/dist/cjs/lib/generator/TypeScriptTypes.d.ts +35 -0
  49. package/dist/cjs/lib/generator/TypeScriptTypes.js +153 -0
  50. package/dist/cjs/lib/library/NativeCallback.d.ts +44 -0
  51. package/dist/cjs/lib/library/NativeCallback.js +66 -0
  52. package/dist/cjs/lib/library/NativeLibraryHandle.d.ts +268 -0
  53. package/dist/cjs/lib/library/NativeLibraryHandle.js +540 -0
  54. package/dist/cjs/lib/library/Resolve.d.ts +49 -0
  55. package/dist/cjs/lib/library/Resolve.js +130 -0
  56. package/dist/cjs/lib/thread/NativeThreads.d.ts +80 -0
  57. package/dist/cjs/lib/thread/NativeThreads.js +384 -0
  58. package/dist/cjs/lib/thread/NativeWorker.d.ts +6 -0
  59. package/dist/cjs/lib/thread/NativeWorker.js +96 -0
  60. package/dist/cjs/lib/thread/NativeWorkerCode.d.ts +5 -0
  61. package/dist/cjs/lib/thread/NativeWorkerCode.js +4 -0
  62. package/dist/cjs/lib/thread/PackageRequire.d.ts +6 -0
  63. package/dist/cjs/lib/thread/PackageRequire.js +4 -0
  64. package/dist/cjs/lib/thread/ThreadProtocol.d.ts +77 -0
  65. package/dist/cjs/lib/thread/ThreadProtocol.js +2 -0
  66. package/dist/cjs/package.json +1 -0
  67. package/dist/esm/components/NativeLibrary.js +338 -0
  68. package/dist/esm/decorators/Library.js +40 -0
  69. package/dist/esm/decorators/Native.js +85 -0
  70. package/dist/esm/exceptions/NativeDeclarationException.js +15 -0
  71. package/dist/esm/exceptions/NativeLibraryClosedException.js +15 -0
  72. package/dist/esm/exceptions/NativeLibraryNotFoundException.js +14 -0
  73. package/dist/esm/exceptions/NativeMemoryException.js +14 -0
  74. package/dist/esm/exports/FFI.js +10 -0
  75. package/dist/esm/exports/Generator.js +2 -0
  76. package/dist/esm/lib/Memory.js +210 -0
  77. package/dist/esm/lib/Runtime.js +192 -0
  78. package/dist/esm/lib/backend/Functions.js +206 -0
  79. package/dist/esm/lib/backend/KoffiTypes.js +418 -0
  80. package/dist/esm/lib/declarations/Builtins.js +174 -0
  81. package/dist/esm/lib/declarations/Layout.js +175 -0
  82. package/dist/esm/lib/declarations/Lexer.js +321 -0
  83. package/dist/esm/lib/declarations/Parser.js +1421 -0
  84. package/dist/esm/lib/declarations/Platform.js +23 -0
  85. package/dist/esm/lib/declarations/Types.js +170 -0
  86. package/dist/esm/lib/generator/CText.js +129 -0
  87. package/dist/esm/lib/generator/Generator.js +388 -0
  88. package/dist/esm/lib/generator/Preprocessor.js +87 -0
  89. package/dist/esm/lib/generator/TypeScriptTypes.js +149 -0
  90. package/dist/esm/lib/library/NativeCallback.js +59 -0
  91. package/dist/esm/lib/library/NativeLibraryHandle.js +532 -0
  92. package/dist/esm/lib/library/Resolve.js +122 -0
  93. package/dist/esm/lib/thread/NativeThreads.js +377 -0
  94. package/dist/esm/lib/thread/NativeWorker.js +93 -0
  95. package/dist/esm/lib/thread/NativeWorkerCode.js +1 -0
  96. package/dist/esm/lib/thread/PackageRequire.js +2 -0
  97. package/dist/esm/lib/thread/ThreadProtocol.js +1 -0
  98. package/dist/types/components/NativeLibrary.d.ts +229 -0
  99. package/dist/types/decorators/Library.d.ts +61 -0
  100. package/dist/types/decorators/Native.d.ts +69 -0
  101. package/dist/types/exceptions/NativeDeclarationException.d.ts +12 -0
  102. package/dist/types/exceptions/NativeLibraryClosedException.d.ts +12 -0
  103. package/dist/types/exceptions/NativeLibraryNotFoundException.d.ts +11 -0
  104. package/dist/types/exceptions/NativeMemoryException.d.ts +11 -0
  105. package/dist/types/exports/FFI.d.ts +12 -0
  106. package/dist/types/exports/Generator.d.ts +2 -0
  107. package/dist/types/lib/Memory.d.ts +188 -0
  108. package/dist/types/lib/Runtime.d.ts +98 -0
  109. package/dist/types/lib/backend/Functions.d.ts +65 -0
  110. package/dist/types/lib/backend/KoffiTypes.d.ts +53 -0
  111. package/dist/types/lib/declarations/Builtins.d.ts +33 -0
  112. package/dist/types/lib/declarations/Layout.d.ts +41 -0
  113. package/dist/types/lib/declarations/Lexer.d.ts +40 -0
  114. package/dist/types/lib/declarations/Parser.d.ts +90 -0
  115. package/dist/types/lib/declarations/Platform.d.ts +50 -0
  116. package/dist/types/lib/declarations/Types.d.ts +173 -0
  117. package/dist/types/lib/generator/CText.d.ts +14 -0
  118. package/dist/types/lib/generator/Generator.d.ts +136 -0
  119. package/dist/types/lib/generator/Preprocessor.d.ts +60 -0
  120. package/dist/types/lib/generator/TypeScriptTypes.d.ts +35 -0
  121. package/dist/types/lib/library/NativeCallback.d.ts +44 -0
  122. package/dist/types/lib/library/NativeLibraryHandle.d.ts +268 -0
  123. package/dist/types/lib/library/Resolve.d.ts +49 -0
  124. package/dist/types/lib/thread/NativeThreads.d.ts +80 -0
  125. package/dist/types/lib/thread/NativeWorker.d.ts +6 -0
  126. package/dist/types/lib/thread/NativeWorkerCode.d.ts +5 -0
  127. package/dist/types/lib/thread/PackageRequire.d.ts +6 -0
  128. package/dist/types/lib/thread/ThreadProtocol.d.ts +77 -0
  129. package/package.json +85 -4
@@ -0,0 +1,188 @@
1
+ import { type Pointer } from './Runtime.js';
2
+ /**
3
+ * An output parameter of a pointer or an integer: an array of one element, null before the call, set by the function
4
+ * (`[null]` given to `_Out_ sqlite3 **db`, then `db[0]`)
5
+ */
6
+ export type Out<T> = [T | null];
7
+ /**
8
+ * Read a C string (UTF-8, ended by a NUL byte) from native memory: a `char *` given as a pointer (a `void *` result, an
9
+ * argument of a callback, a member of a struct).
10
+ * @param pointer Its address.
11
+ * @param length Its length in bytes, when it is not ended by a NUL byte.
12
+ * @returns The string.
13
+ * @example
14
+ * ```typescript
15
+ * import {readCString, type Pointer} from '@lakutata/ffi'
16
+ *
17
+ * export function messageOf(pointer: Pointer): string {
18
+ * return readCString(pointer)
19
+ * }
20
+ * ```
21
+ */
22
+ export declare function readCString(pointer: Pointer, length?: number): string;
23
+ /**
24
+ * Read a wide string from native memory: a `wchar_t *` (UTF-16 on Windows, UTF-32 elsewhere), ended by a NUL character.
25
+ * @param pointer Its address.
26
+ * @param length Its length in characters, when it is not ended by a NUL character.
27
+ * @returns The string.
28
+ * @example
29
+ * ```typescript
30
+ * import {readWString, type Pointer} from '@lakutata/ffi'
31
+ *
32
+ * export function titleOf(pointer: Pointer): string {
33
+ * return readWString(pointer)
34
+ * }
35
+ * ```
36
+ */
37
+ export declare function readWString(pointer: Pointer, length?: number): string;
38
+ /**
39
+ * Copy native memory into a buffer of its own, which stays valid once the native memory is freed (about 10 GB/s).
40
+ * @param pointer The address of the memory.
41
+ * @param length Its length in bytes.
42
+ * @returns The copy.
43
+ * @example
44
+ * ```typescript
45
+ * import {readBuffer, type Pointer} from '@lakutata/ffi'
46
+ *
47
+ * export function frameOf(pixels: Pointer, width: number, height: number): Buffer {
48
+ * return readBuffer(pixels, width * height * 4)
49
+ * }
50
+ * ```
51
+ */
52
+ export declare function readBuffer(pointer: Pointer, length: number): Buffer;
53
+ /**
54
+ * A view of native memory, without copy: worth it for large blocks read often (8 MiB: about 0.13 ms rather than 1 ms
55
+ * for a copy). The view must not be used once the native memory is freed: the process would crash. Electron forbids
56
+ * these views: it throws there (use readBuffer).
57
+ * @param pointer The address of the memory.
58
+ * @param length Its length in bytes.
59
+ * @returns The memory, as an ArrayBuffer.
60
+ * @throws {NativeMemoryException} In Electron, or for a null pointer.
61
+ * @example
62
+ * ```typescript
63
+ * import {viewBuffer, type Pointer} from '@lakutata/ffi'
64
+ *
65
+ * export function firstPixel(pixels: Pointer): number {
66
+ * return new Uint32Array(viewBuffer(pixels, 4))[0]
67
+ * }
68
+ * ```
69
+ */
70
+ export declare function viewBuffer(pointer: Pointer, length: number): ArrayBuffer;
71
+ /**
72
+ * Read a value of a built-in type from native memory (`int`, `double`, `uint64_t`, `char *`…): the 64-bit integers as
73
+ * bigints. The types of a library are read by `read()` of the library.
74
+ * @param pointer Its address.
75
+ * @param type Its type.
76
+ * @returns The value.
77
+ * @example
78
+ * ```typescript
79
+ * import {readValue, type Pointer} from '@lakutata/ffi'
80
+ *
81
+ * export function countAt(pointer: Pointer): number {
82
+ * return readValue(pointer, 'int32_t') as number
83
+ * }
84
+ * ```
85
+ */
86
+ export declare function readValue(pointer: Pointer, type: string): unknown;
87
+ /**
88
+ * Read an array of values of a built-in type from native memory.
89
+ * @param pointer The address of its first element.
90
+ * @param type The type of its elements (`char *` reads strings).
91
+ * @param count Its length.
92
+ * @returns The values.
93
+ * @example
94
+ * ```typescript
95
+ * import {readArray, type Pointer} from '@lakutata/ffi'
96
+ *
97
+ * //The row of a callback of sqlite3_exec: char **values
98
+ * export function rowOf(values: Pointer, count: number): string[] {
99
+ * return readArray(values, 'char *', count) as string[]
100
+ * }
101
+ * ```
102
+ */
103
+ export declare function readArray(pointer: Pointer, type: string, count: number): unknown[];
104
+ /**
105
+ * Write a value of a built-in type to native memory.
106
+ * @param pointer Its address.
107
+ * @param type Its type.
108
+ * @param value The value.
109
+ * @example
110
+ * ```typescript
111
+ * import {allocMemory, freeMemory, readValue, writeValue} from '@lakutata/ffi'
112
+ *
113
+ * const counter: bigint = allocMemory('int64_t')
114
+ * writeValue(counter, 'int64_t', 42n)
115
+ * console.log(readValue(counter, 'int64_t'))
116
+ * freeMemory(counter)
117
+ * ```
118
+ */
119
+ export declare function writeValue(pointer: Pointer, type: string, value: unknown): void;
120
+ /**
121
+ * Allocate native memory for values of a built-in type: memory that does not move, which a library can keep (a buffer
122
+ * given to a function is valid during the call only). Freed by freeMemory.
123
+ * @param type The type of the values.
124
+ * @param count Their number.
125
+ * @returns Its address.
126
+ * @example
127
+ * ```typescript
128
+ * import {allocMemory, freeMemory} from '@lakutata/ffi'
129
+ *
130
+ * const buffer: bigint = allocMemory('uint8_t', 4096)
131
+ * freeMemory(buffer)
132
+ * ```
133
+ */
134
+ export declare function allocMemory(type: string, count?: number): Pointer;
135
+ /**
136
+ * Free native memory allocated by allocMemory.
137
+ * @param pointer Its address.
138
+ * @example
139
+ * ```typescript
140
+ * import {allocMemory, freeMemory} from '@lakutata/ffi'
141
+ *
142
+ * freeMemory(allocMemory('double', 16))
143
+ * ```
144
+ */
145
+ export declare function freeMemory(pointer: Pointer): void;
146
+ /**
147
+ * The size of a built-in type on the platform, in bytes (`long` is 8 on 64-bit Linux and macOS, 4 on Windows).
148
+ * @param type The type.
149
+ * @returns Its size.
150
+ * @example
151
+ * ```typescript
152
+ * import {sizeOf} from '@lakutata/ffi'
153
+ *
154
+ * console.log(sizeOf('long'), sizeOf('wchar_t'), sizeOf('void *'))
155
+ * ```
156
+ */
157
+ export declare function sizeOf(type: string): number;
158
+ /**
159
+ * The errno of the last native call of the thread. On Windows, each C library has its own errno: the one of the C
160
+ * library of Microsoft (ucrtbase.dll) by default.
161
+ * @param crt On Windows, the C library the function called uses (`msvcrt.dll` for the old libraries).
162
+ * @returns The errno.
163
+ * @example
164
+ * ```typescript
165
+ * import {lastErrno, loadNativeLibrary} from '@lakutata/ffi'
166
+ *
167
+ * const libc = loadNativeLibrary('c')
168
+ * const open = libc.func<(path: string, flags: number) => number>('int open(const char *path, int flags, ...)')
169
+ * if (open('/surely/missing', 0) < 0) console.log('errno', lastErrno())
170
+ * ```
171
+ */
172
+ export declare function lastErrno(crt?: string): number;
173
+ /**
174
+ * The last error of the Windows API of the thread (GetLastError), kept after each native call; undefined on the other
175
+ * systems.
176
+ * @returns The error.
177
+ * @example
178
+ * ```typescript
179
+ * import {lastWindowsError, loadNativeLibrary} from '@lakutata/ffi'
180
+ *
181
+ * if (process.platform === 'win32') {
182
+ * const kernel32 = loadNativeLibrary('kernel32.dll')
183
+ * const attributes = kernel32.func<(path: string) => number>('DWORD WINAPI GetFileAttributesW(LPCWSTR path)')
184
+ * if (attributes('C:\\missing') === 0xFFFFFFFF) console.log('error', lastWindowsError())
185
+ * }
186
+ * ```
187
+ */
188
+ export declare function lastWindowsError(): number | undefined;
@@ -0,0 +1,98 @@
1
+ import { type KoffiType, KoffiTypes } from './backend/KoffiTypes.js';
2
+ import { DeclarationParser } from './declarations/Parser.js';
3
+ import type { CType } from './declarations/Types.js';
4
+ /**
5
+ * The koffi types of the process: one per C type, whatever library declares it
6
+ */
7
+ export declare const TYPES: KoffiTypes;
8
+ /**
9
+ * A pointer to native memory: a bigint (its address), null for NULL
10
+ */
11
+ export type Pointer = bigint;
12
+ /**
13
+ * The C types read from their names, with the declarations of a library or the built-in types only
14
+ */
15
+ export declare class TypeNames {
16
+ #private;
17
+ /**
18
+ * The type names of a library.
19
+ * @param parser Its declarations.
20
+ */
21
+ constructor(parser: DeclarationParser);
22
+ /**
23
+ * A type, from its name.
24
+ * @param text Its name: `int`, `struct timeval`, `const char *`.
25
+ */
26
+ type(text: string): CType;
27
+ /**
28
+ * The koffi type of a type name.
29
+ * @param text Its name.
30
+ */
31
+ koffi(text: string): KoffiType;
32
+ }
33
+ /**
34
+ * The type names of the built-in types (of no library)
35
+ */
36
+ export declare const BUILTIN_TYPES: TypeNames;
37
+ /**
38
+ * A pointer checked: not null, a bigint or a number
39
+ * @param pointer
40
+ * @param what
41
+ */
42
+ export declare function checkedPointer(pointer: unknown, what: string): Pointer;
43
+ /**
44
+ * A copy of native memory, in a buffer of its own.
45
+ * @param pointer The address of the memory.
46
+ * @param length Its length in bytes.
47
+ */
48
+ export declare function copyBuffer(pointer: Pointer, length: number): Buffer;
49
+ /**
50
+ * A view of native memory, without copy: refused in Electron, which forbids them (koffi would end the process).
51
+ * @param pointer The address of the memory.
52
+ * @param length Its length in bytes.
53
+ */
54
+ export declare function nativeView(pointer: Pointer, length: number): ArrayBuffer;
55
+ /**
56
+ * A value read from native memory, converted as the package gives values (64-bit integers as bigints).
57
+ * @param names The type names to read the type with.
58
+ * @param pointer The address.
59
+ * @param type The type name.
60
+ * @param count The number of values, for an array.
61
+ */
62
+ export declare function readNative(names: TypeNames, pointer: Pointer, type: string, count?: number): unknown;
63
+ /**
64
+ * A value written to native memory.
65
+ * @param names The type names to read the type with.
66
+ * @param pointer The address.
67
+ * @param type The type name.
68
+ * @param value The value.
69
+ */
70
+ export declare function writeNative(names: TypeNames, pointer: Pointer, type: string, value: unknown): void;
71
+ /**
72
+ * Native memory allocated for values of a type (C's malloc: freed by freeNative).
73
+ * @param names The type names to read the type with.
74
+ * @param type The type name.
75
+ * @param count The number of values.
76
+ */
77
+ export declare function allocateNative(names: TypeNames, type: string, count?: number): Pointer;
78
+ /**
79
+ * Free native memory allocated by allocateNative (or by the malloc of the C library the package is built with).
80
+ * @param pointer The address.
81
+ */
82
+ export declare function freeNative(pointer: Pointer): void;
83
+ /**
84
+ * The errno of the last call made by the thread: on Windows, the one of the C library of Microsoft (ucrtbase.dll), each
85
+ * C library having its own; elsewhere the one koffi keeps after each call.
86
+ * @param crt On Windows, the C library the function called uses.
87
+ */
88
+ export declare function nativeErrno(crt?: string): number;
89
+ /**
90
+ * The last error of the Windows API of the thread (GetLastError), kept by koffi after each call; undefined elsewhere.
91
+ */
92
+ export declare function nativeWindowsError(): number | undefined;
93
+ /**
94
+ * The size of a type on the platform, by its name.
95
+ * @param names The type names.
96
+ * @param type The type name.
97
+ */
98
+ export declare function typeSize(names: TypeNames, type: string): number;
@@ -0,0 +1,65 @@
1
+ import { type LibraryHandle } from 'koffi';
2
+ import { type FunctionDeclaration, type FunctionType } from '../declarations/Types.js';
3
+ import { type KoffiType, type KoffiTypes } from './KoffiTypes.js';
4
+ /**
5
+ * How a function is bound
6
+ */
7
+ export interface BindOptions {
8
+ /**
9
+ * Whether it runs in a thread of the pool, returning a promise
10
+ */
11
+ readonly async?: boolean;
12
+ /**
13
+ * Frees the strings it gives (its result, its output strings) once read
14
+ */
15
+ readonly free?: (pointer: unknown) => void;
16
+ /**
17
+ * Its 64-bit integers (its result, its output integers) as numbers rather than bigints: a RangeError when one is not
18
+ * a safe integer
19
+ */
20
+ readonly int64?: 'bigint' | 'number';
21
+ /**
22
+ * The asynchronous calls of the library: counted, refused once it is closed
23
+ */
24
+ readonly calls?: CallTracker;
25
+ }
26
+ /**
27
+ * Counts the asynchronous calls of a library
28
+ */
29
+ export interface CallTracker {
30
+ /**
31
+ * Before a call: throws once the library is closed
32
+ */
33
+ start(): void;
34
+ /**
35
+ * Once a call has returned
36
+ */
37
+ end(): void;
38
+ }
39
+ /**
40
+ * The koffi types of the extra arguments of a variadic function, by the C type names given before them
41
+ */
42
+ export type VariadicTypes = (text: string) => KoffiType;
43
+ /**
44
+ * A 64-bit integer as a number, when it is a safe one
45
+ * @param value
46
+ */
47
+ export declare function toSafeNumber(value: unknown): unknown;
48
+ /**
49
+ * A JS function given as a callback, its arguments and its result converted when its prototype has 64-bit integers or
50
+ * structs with bitfields; the same function otherwise
51
+ * @param type
52
+ * @param types
53
+ * @param callback
54
+ */
55
+ export declare function convertedCallback(type: FunctionType, types: KoffiTypes, callback: (...args: any[]) => any): (...args: any[]) => any;
56
+ /**
57
+ * A function of a library, bound by its declaration: the function of koffi itself when nothing has to be converted,
58
+ * else a function converting its arguments and its result; a function returning a promise when asynchronous
59
+ * @param library
60
+ * @param declaration
61
+ * @param types
62
+ * @param variadicTypes
63
+ * @param options
64
+ */
65
+ export declare function bindFunction(library: LibraryHandle, declaration: FunctionDeclaration, types: KoffiTypes, variadicTypes: VariadicTypes, options?: BindOptions): (...args: any[]) => any;
@@ -0,0 +1,53 @@
1
+ import koffi, { type TypeObject } from 'koffi';
2
+ import type { Platform } from '../declarations/Platform.js';
3
+ import { type CType, type FunctionType } from '../declarations/Types.js';
4
+ /**
5
+ * A type as koffi takes it: its name, its object, or its object with a direction (an output parameter)
6
+ */
7
+ export type KoffiType = string | TypeObject | ReturnType<typeof koffi.out>;
8
+ /**
9
+ * How a value goes between the user and koffi, when koffi does not give or take it as the package does: the 64-bit
10
+ * integers always bigints, the bitfields of a struct read and written by their names
11
+ */
12
+ export interface Converter {
13
+ /**
14
+ * A value of the user to the value koffi takes
15
+ */
16
+ readonly toNative?: (value: any) => any;
17
+ /**
18
+ * A value koffi gives to the value of the user
19
+ */
20
+ readonly fromNative?: (value: any) => any;
21
+ }
22
+ /**
23
+ * A value of a 64-bit integer type, as a bigint
24
+ * @param value
25
+ */
26
+ export declare function toBigInt(value: unknown): unknown;
27
+ /**
28
+ * The koffi types of the C types of the declarations, made once each (koffi never frees a type): anonymous, so the
29
+ * types of two libraries never meet; the structs koffi cannot lay out (bitfields, packing) laid out by the package
30
+ */
31
+ export declare class KoffiTypes {
32
+ #private;
33
+ /**
34
+ * The koffi types of a platform.
35
+ * @param platform The platform of the process.
36
+ */
37
+ constructor(platform: Platform);
38
+ /**
39
+ * The koffi type of a C type, as a value: a member, a result, a parameter without direction.
40
+ * @param type The C type.
41
+ */
42
+ type(type: CType): KoffiType;
43
+ /**
44
+ * The koffi prototype of a function type: the type of its callbacks.
45
+ * @param type The function type.
46
+ */
47
+ prototype(type: FunctionType): TypeObject;
48
+ /**
49
+ * The converter of a C type, null when koffi gives and takes its values as the package does.
50
+ * @param type The C type.
51
+ */
52
+ converter(type: CType): Converter | null;
53
+ }
@@ -0,0 +1,33 @@
1
+ import type { Platform } from './Platform.js';
2
+ import type { CallingConvention, CType, Direction } from './Types.js';
3
+ /**
4
+ * The types every declaration knows without declaring them: the ones of stdint.h and stddef.h, the ones of POSIX with
5
+ * their size on the platform, the ones of the Windows API (the same on every platform: DWORD is 32-bit everywhere).
6
+ * A declaration of the same name replaces them
7
+ * @param platform
8
+ */
9
+ export declare function builtinTypes(platform: Platform): Map<string, CType>;
10
+ /**
11
+ * The words naming a calling convention: the keywords of the compilers, the macros of the Windows API
12
+ */
13
+ export declare const CONVENTIONS: ReadonlyMap<string, CallingConvention>;
14
+ /**
15
+ * The words read and ignored where qualifiers go: the qualifiers that do not change how a value is passed, the macros of
16
+ * the Windows API that are empty
17
+ */
18
+ export declare const IGNORED: ReadonlySet<string>;
19
+ /**
20
+ * The words read as const
21
+ */
22
+ export declare const CONSTS: ReadonlySet<string>;
23
+ /**
24
+ * The words of inline functions, whose bodies are skipped
25
+ */
26
+ export declare const INLINES: ReadonlySet<string>;
27
+ /**
28
+ * The direction an annotation gives a parameter, if it is one: the ones of Microsoft (_Out_, _Inout_opt_,
29
+ * _Outptr_result_maybenull_…) and the macros IN and OUT of the Windows API; null for an annotation that gives none,
30
+ * undefined for a word that is not an annotation
31
+ * @param word
32
+ */
33
+ export declare function annotationDirection(word: string): Direction | null | undefined;
@@ -0,0 +1,41 @@
1
+ import type { Platform } from './Platform.js';
2
+ import { type CType, type Member, type RecordType } from './Types.js';
3
+ /**
4
+ * Where a member of a struct is: its offset in bytes; for a bitfield, its first bit (from the start of the struct, the
5
+ * lowest bits first) and its width
6
+ */
7
+ export interface MemberLayout {
8
+ readonly member: Member;
9
+ readonly offset: number;
10
+ readonly size: number;
11
+ readonly bitOffset?: number;
12
+ readonly bits?: number;
13
+ }
14
+ export interface RecordLayout {
15
+ readonly size: number;
16
+ readonly alignment: number;
17
+ readonly members: readonly MemberLayout[];
18
+ /**
19
+ * Whether it has bitfields (koffi does not lay them out: the package does)
20
+ */
21
+ readonly bitfields: boolean;
22
+ }
23
+ /**
24
+ * The size of a type, in bytes
25
+ * @param type
26
+ * @param platform
27
+ */
28
+ export declare function sizeOf(type: CType, platform: Platform): number;
29
+ /**
30
+ * The alignment of a type, in bytes
31
+ * @param type
32
+ * @param platform
33
+ */
34
+ export declare function alignmentOf(type: CType, platform: Platform): number;
35
+ /**
36
+ * The layout of a struct or of a union, as the compilers of the platform lay it out (GCC and Clang, or Microsoft's for
37
+ * the bitfields): the alignment of each member capped by #pragma pack, raised by its aligned attribute
38
+ * @param record
39
+ * @param platform
40
+ */
41
+ export declare function layoutOf(record: RecordType, platform: Platform): RecordLayout;
@@ -0,0 +1,40 @@
1
+ import { NativeDeclarationException } from '../../exceptions/NativeDeclarationException.js';
2
+ export type TokenKind = 'identifier' | 'number' | 'char' | 'string' | 'punctuator' | 'pragma' | 'define' | 'undef' | 'end';
3
+ export interface Token {
4
+ readonly kind: TokenKind;
5
+ readonly text: string;
6
+ /**
7
+ * The value of an integer or of a character
8
+ */
9
+ readonly value?: bigint;
10
+ /**
11
+ * Whether a number is a floating-point one
12
+ */
13
+ readonly float?: boolean;
14
+ readonly line: number;
15
+ readonly column: number;
16
+ /**
17
+ * In a preprocessed text, the file it comes from and its line there (from the line markers)
18
+ */
19
+ readonly file?: string;
20
+ readonly fileLine?: number;
21
+ /**
22
+ * The text of a string as written, its quotes and escapes included
23
+ */
24
+ readonly source?: string;
25
+ }
26
+ /**
27
+ * The exception of a declaration that cannot be read, at a line and a column
28
+ * @param message
29
+ * @param line
30
+ * @param column
31
+ */
32
+ export declare function declarationError(message: string, line: number, column: number): NativeDeclarationException;
33
+ /**
34
+ * The tokens of C declarations. Comments are skipped; of the directives of the preprocessor, #pragma pack, #define and
35
+ * #undef are kept (as tokens holding the rest of their line), #include and the other #pragma are skipped, and the
36
+ * conditional ones (#if, #ifdef…) are refused: what they select is the work of a preprocessor. The line markers of a
37
+ * preprocessed text give each token the file and the line it comes from
38
+ * @param text
39
+ */
40
+ export declare function tokenize(text: string): Token[];
@@ -0,0 +1,90 @@
1
+ import { type Token } from './Lexer.js';
2
+ import type { Platform } from './Platform.js';
3
+ import { type CType, type Declarations, type FunctionDeclaration } from './Types.js';
4
+ /**
5
+ * A declaration of a preprocessed header, as readSource reads it (for the generator of libraries)
6
+ */
7
+ export interface SourceDeclaration {
8
+ /**
9
+ * The file it comes from, and its line there (from the line markers of the preprocessor)
10
+ */
11
+ readonly file?: string;
12
+ readonly line?: number;
13
+ /**
14
+ * Its tokens, the macros expanded
15
+ */
16
+ readonly tokens: readonly Token[];
17
+ /**
18
+ * What it declares: `type name`, `struct tag`, `union tag`, `enum tag` (defined, not only named), `function name`,
19
+ * `variable name`, `constant name`
20
+ */
21
+ readonly names: readonly string[];
22
+ /**
23
+ * The packing of #pragma pack it was read with
24
+ */
25
+ readonly pack?: number;
26
+ /**
27
+ * Why it could not be read
28
+ */
29
+ readonly error?: string;
30
+ }
31
+ /**
32
+ * Where a macro is defined, in a preprocessed header
33
+ */
34
+ export interface MacroOrigin {
35
+ readonly file?: string;
36
+ readonly line?: number;
37
+ }
38
+ /**
39
+ * A preprocessed header read by readSource: its declarations, each one as it is written, and where its macros are
40
+ */
41
+ export interface SourceDeclarations {
42
+ readonly declarations: Declarations;
43
+ readonly items: readonly SourceDeclaration[];
44
+ readonly macros: ReadonlyMap<string, MacroOrigin>;
45
+ }
46
+ /**
47
+ * Reads C declarations: typedefs, structs, unions, enums, function prototypes and variables, for one platform. Each
48
+ * library has its own declarations: their names do not meet the ones of another library
49
+ */
50
+ export declare class DeclarationParser {
51
+ #private;
52
+ /**
53
+ * Read declarations for a platform.
54
+ * @param platform The platform: the sizes of the types and the layout of the structs depend on it.
55
+ * @param declarations Declarations already read, added to (the ones of the library, when a prototype is read).
56
+ */
57
+ constructor(platform: Platform, declarations?: Declarations);
58
+ /**
59
+ * The declarations read so far.
60
+ */
61
+ get declarations(): Declarations;
62
+ /**
63
+ * Read a text of declarations, adding what it declares.
64
+ * @param text The declarations, as a header writes them (preprocessed or not: #pragma pack is read, #define and
65
+ * #include are skipped, #if and its kind are refused).
66
+ */
67
+ parse(text: string): Declarations;
68
+ /**
69
+ * Read a preprocessed header for the generator of libraries: each declaration kept with the file it comes from and
70
+ * what it declares; one that cannot be read is skipped, its error kept, and the next ones are read.
71
+ * @param text The output of the preprocessor (`cc -E -dD`, `cl /P`): its line markers give the files.
72
+ * @returns The declarations, each declaration as it is written, and where the macros are defined.
73
+ */
74
+ readSource(text: string): SourceDeclarations;
75
+ /**
76
+ * Read the prototype of one function (a trailing semicolon is optional).
77
+ * @param text The prototype: `int sqlite3_open(const char *filename, _Out_ sqlite3 **db)`.
78
+ */
79
+ prototype(text: string): FunctionDeclaration;
80
+ /**
81
+ * Read a type name: `struct timeval`, `const char *`, `int[4]`.
82
+ * @param text The type.
83
+ */
84
+ typeName(text: string): CType;
85
+ /**
86
+ * The value of a constant expression of the declarations (`SQLITE_OK + 1`, `1 << 4`).
87
+ * @param text The expression.
88
+ */
89
+ constant(text: string): bigint;
90
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * What the sizes of the C types and the layout of the structs depend on
3
+ */
4
+ export interface Platform {
5
+ /**
6
+ * The operating system, as process.platform names it
7
+ */
8
+ readonly os: string;
9
+ /**
10
+ * The architecture, as process.arch names it
11
+ */
12
+ readonly arch: string;
13
+ /**
14
+ * The size of a pointer (and of size_t), in bytes
15
+ */
16
+ readonly pointerSize: 4 | 8;
17
+ /**
18
+ * The size of long: 8 on 64-bit Unix systems (LP64), 4 on Windows (LLP64) and on 32-bit systems
19
+ */
20
+ readonly longSize: 4 | 8;
21
+ /**
22
+ * The size of wchar_t: 2 on Windows, 4 elsewhere
23
+ */
24
+ readonly wcharSize: 2 | 4;
25
+ /**
26
+ * The alignment of the 8-byte types (long long, double) as members: 4 on 32-bit Linux (i386), 8 elsewhere
27
+ */
28
+ readonly int64Alignment: 4 | 8;
29
+ /**
30
+ * How bitfields are laid out: by the rules of GCC and Clang (sysv), of the ARM procedure call standard where the
31
+ * type of every bitfield aligns the struct, the unnamed and the empty ones too (aapcs: ARM but on Apple systems), or
32
+ * of the compiler of Microsoft (msvc)
33
+ */
34
+ readonly bitfields: 'sysv' | 'aapcs' | 'msvc';
35
+ /**
36
+ * Whether the symbols of the C functions start with an underscore in the binaries (macOS): the labels of __asm
37
+ * name them so
38
+ */
39
+ readonly underscoredSymbols: boolean;
40
+ }
41
+ /**
42
+ * The platform of a system and an architecture
43
+ * @param os
44
+ * @param arch
45
+ */
46
+ export declare function platformOf(os: string, arch: string): Platform;
47
+ /**
48
+ * The platform the process runs on
49
+ */
50
+ export declare const CURRENT_PLATFORM: Platform;