@lakutata/ffi 0.0.0-stage → 3.1.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 (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,173 @@
1
+ import type { Platform } from './Platform.js';
2
+ /**
3
+ * The arithmetic types of C, by their meaning (long is not int64: their sizes differ between the platforms)
4
+ */
5
+ export type PrimitiveName = 'void' | 'bool' | 'char' | 'schar' | 'uchar' | 'short' | 'ushort' | 'int' | 'uint' | 'long' | 'ulong' | 'longlong' | 'ulonglong' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'size' | 'ssize' | 'intptr' | 'uintptr' | 'ptrdiff' | 'char16' | 'char32' | 'wchar' | 'float' | 'double';
6
+ /**
7
+ * How the arguments of a function are passed: different on 32-bit Windows only
8
+ */
9
+ export type CallingConvention = 'cdecl' | 'stdcall' | 'fastcall' | 'thiscall';
10
+ /**
11
+ * Which way a pointer argument carries its value: into the function, out of it (the object or the array given is
12
+ * filled once it returns), or both
13
+ */
14
+ export type Direction = 'in' | 'out' | 'inout';
15
+ export interface PrimitiveType {
16
+ readonly kind: 'primitive';
17
+ readonly name: PrimitiveName;
18
+ }
19
+ export interface PointerType {
20
+ readonly kind: 'pointer';
21
+ readonly to: CType;
22
+ /**
23
+ * Whether what it points to is const (const char * is a string given to the function)
24
+ */
25
+ readonly constTarget: boolean;
26
+ }
27
+ export interface ArrayType {
28
+ readonly kind: 'array';
29
+ readonly of: CType;
30
+ readonly length: number;
31
+ }
32
+ export interface Member {
33
+ /**
34
+ * Its name; the anonymous structs and unions inside a struct are named _0, _1… in the order of the struct
35
+ */
36
+ readonly name: string;
37
+ readonly type: CType;
38
+ /**
39
+ * The width of a bitfield
40
+ */
41
+ readonly bits?: number;
42
+ /**
43
+ * The alignment its aligned attribute asks for, in bytes
44
+ */
45
+ readonly align?: number;
46
+ }
47
+ /**
48
+ * A struct or a union. One without members is opaque (declared, not defined); it is completed in place when defined
49
+ */
50
+ export interface RecordType {
51
+ readonly kind: 'struct' | 'union';
52
+ readonly tag?: string;
53
+ members?: Member[];
54
+ /**
55
+ * The alignment its members are capped at (#pragma pack, the packed attribute)
56
+ */
57
+ pack?: number;
58
+ /**
59
+ * The alignment its aligned attribute asks for, in bytes
60
+ */
61
+ align?: number;
62
+ }
63
+ export interface EnumType {
64
+ readonly kind: 'enum';
65
+ readonly tag?: string;
66
+ /**
67
+ * The integer type holding its values
68
+ */
69
+ readonly base: PrimitiveName;
70
+ readonly values: ReadonlyMap<string, bigint>;
71
+ }
72
+ export interface Parameter {
73
+ readonly name?: string;
74
+ readonly type: CType;
75
+ readonly direction: Direction;
76
+ }
77
+ export interface FunctionType {
78
+ readonly kind: 'function';
79
+ readonly result: CType;
80
+ readonly parameters: readonly Parameter[];
81
+ readonly variadic: boolean;
82
+ readonly convention: CallingConvention;
83
+ }
84
+ /**
85
+ * A C type, as the declarations describe it
86
+ */
87
+ export type CType = PrimitiveType | PointerType | ArrayType | RecordType | EnumType | FunctionType;
88
+ /**
89
+ * A function the library exports
90
+ */
91
+ export interface FunctionDeclaration {
92
+ readonly name: string;
93
+ /**
94
+ * The name of its symbol in the library: its name, or the one of its __asm label
95
+ */
96
+ readonly symbol: string;
97
+ readonly type: FunctionType;
98
+ }
99
+ /**
100
+ * A variable the library exports
101
+ */
102
+ export interface VariableDeclaration {
103
+ readonly name: string;
104
+ readonly symbol: string;
105
+ readonly type: CType;
106
+ readonly constant: boolean;
107
+ }
108
+ /**
109
+ * What a text of declarations declares
110
+ */
111
+ export interface Declarations {
112
+ /**
113
+ * The typedefs
114
+ */
115
+ readonly types: Map<string, CType>;
116
+ /**
117
+ * The tags of the structs, unions and enums ("struct timeval")
118
+ */
119
+ readonly tags: Map<string, RecordType | EnumType>;
120
+ readonly functions: Map<string, FunctionDeclaration>;
121
+ readonly variables: Map<string, VariableDeclaration>;
122
+ /**
123
+ * The values of the enumerations, of the static constants and of the macros whose value is an integer
124
+ */
125
+ readonly constants: Map<string, bigint>;
126
+ /**
127
+ * The macros without arguments (#define): their tokens, empty for the ones that are removed where used (the
128
+ * markers of exported functions); null for the macros with arguments, which cannot be used
129
+ */
130
+ readonly macros: Map<string, MacroToken[] | null>;
131
+ }
132
+ /**
133
+ * A token of a macro: its kind and its text (and its value, for a number or a character)
134
+ */
135
+ export interface MacroToken {
136
+ readonly kind: 'identifier' | 'number' | 'char' | 'string' | 'punctuator';
137
+ readonly text: string;
138
+ readonly value?: bigint;
139
+ readonly float?: boolean;
140
+ }
141
+ /**
142
+ * An empty set of declarations
143
+ */
144
+ export declare function emptyDeclarations(): Declarations;
145
+ export declare const VOID: PrimitiveType;
146
+ /**
147
+ * The size of an arithmetic type, in bytes
148
+ * @param name
149
+ * @param platform
150
+ */
151
+ export declare function primitiveSize(name: PrimitiveName, platform: Platform): number;
152
+ /**
153
+ * Whether an arithmetic type is signed
154
+ * @param name
155
+ * @param platform
156
+ */
157
+ export declare function primitiveSigned(name: PrimitiveName, platform: Platform): boolean;
158
+ /**
159
+ * Whether an arithmetic type is an integer
160
+ * @param name
161
+ */
162
+ export declare function primitiveInteger(name: PrimitiveName): boolean;
163
+ /**
164
+ * Whether an integer type is 64-bit on one of the platforms at least: its values are bigints (long is 64-bit but on
165
+ * Windows, size_t but on 32-bit systems)
166
+ * @param name
167
+ */
168
+ export declare function primitiveBig(name: PrimitiveName): boolean;
169
+ /**
170
+ * A type, as C writes it (for the messages)
171
+ * @param type
172
+ */
173
+ export declare function typeText(type: CType): string;
@@ -0,0 +1,14 @@
1
+ import type { Token } from '../declarations/Lexer.js';
2
+ /**
3
+ * The tokens of a declaration without what changes nothing of what it declares: the attributes but packed and aligned,
4
+ * the __declspec but align, the qualifiers the parser ignores
5
+ * @param tokens
6
+ */
7
+ export declare function prunedTokens(tokens: readonly Token[]): Token[];
8
+ /**
9
+ * A declaration written as C, one member of a struct or a value of an enum by line
10
+ * @param tokens Its tokens.
11
+ * @param indent The indentation of a level.
12
+ * @returns Its lines.
13
+ */
14
+ export declare function declarationText(tokens: readonly Token[], indent?: string): string[];
@@ -0,0 +1,136 @@
1
+ import { type Platform } from '../declarations/Platform.js';
2
+ import { type PreprocessedHeader, type PreprocessOptions } from './Preprocessor.js';
3
+ /**
4
+ * How a library is generated from its header
5
+ */
6
+ export interface GenerateOptions extends PreprocessOptions {
7
+ /**
8
+ * The library its functions are checked against: a name, as `@Library` takes it, or a file. The name of the header
9
+ * by default (`sqlite3.h`: `sqlite3`): give it when that library cannot be loaded (the report tells why the
10
+ * functions were not checked).
11
+ */
12
+ readonly library?: string;
13
+ /**
14
+ * The name of the class. The name of the header in PascalCase by default (`sqlite3.h`: `Sqlite3`).
15
+ */
16
+ readonly className?: string;
17
+ /**
18
+ * More directories whose headers are the ones of the library: the header is, and so are the headers of its
19
+ * directory when it is not a directory of the system.
20
+ */
21
+ readonly own?: readonly string[];
22
+ /**
23
+ * Whether the functions and the variables are checked against the library, the ones it cannot give left out.
24
+ * @default true
25
+ */
26
+ readonly check?: boolean;
27
+ }
28
+ /**
29
+ * A declaration of the header that was left out, and why
30
+ */
31
+ export interface SkippedDeclaration {
32
+ /**
33
+ * Its name, or the start of its text
34
+ */
35
+ readonly name: string;
36
+ /**
37
+ * The file it comes from
38
+ */
39
+ readonly file?: string;
40
+ /**
41
+ * Its line in that file
42
+ */
43
+ readonly line?: number;
44
+ /**
45
+ * Why it was left out
46
+ */
47
+ readonly reason: string;
48
+ }
49
+ /**
50
+ * What the generation did
51
+ */
52
+ export interface GenerationReport {
53
+ /**
54
+ * The command that ran the preprocessor
55
+ */
56
+ readonly command: string;
57
+ /**
58
+ * The platform the library was generated for (`darwin-arm64`)
59
+ */
60
+ readonly platform: string;
61
+ /**
62
+ * The file of the library the functions were checked against
63
+ */
64
+ readonly library?: string;
65
+ /**
66
+ * Why they were not checked
67
+ */
68
+ readonly unchecked?: string;
69
+ /**
70
+ * The functions of the class
71
+ */
72
+ readonly functions: number;
73
+ /**
74
+ * The variables of the class
75
+ */
76
+ readonly variables: number;
77
+ /**
78
+ * The interfaces of the structs and unions
79
+ */
80
+ readonly interfaces: number;
81
+ /**
82
+ * The constants of the declarations: the integer macros, the values of the enums, the static constants
83
+ */
84
+ readonly constants: number;
85
+ /**
86
+ * The declarations of the library left out: the ones that cannot be read, the functions and variables the library
87
+ * cannot give
88
+ */
89
+ readonly skipped: readonly SkippedDeclaration[];
90
+ }
91
+ /**
92
+ * A library generated from its header
93
+ */
94
+ export interface GeneratedLibrary {
95
+ /**
96
+ * The name of its class
97
+ */
98
+ readonly className: string;
99
+ /**
100
+ * The TypeScript code of its class
101
+ */
102
+ readonly code: string;
103
+ /**
104
+ * Its C declarations, as the class gives them to `@Library`
105
+ */
106
+ readonly declarations: string;
107
+ /**
108
+ * What was generated, and what was left out
109
+ */
110
+ readonly report: GenerationReport;
111
+ }
112
+ /**
113
+ * Generate the class of a library from a header already run through the preprocessor (see generateNativeLibrary).
114
+ * @param preprocessed The output of the preprocessor, the header, the directories of the system.
115
+ * @param options The library, the class, the directories of the library.
116
+ * @param platform The platform the header was preprocessed for.
117
+ * @returns The code of the class and the report.
118
+ */
119
+ export declare function generateFromPreprocessed(preprocessed: PreprocessedHeader, options?: Omit<GenerateOptions, 'header'>, platform?: Platform): Promise<GeneratedLibrary>;
120
+ /**
121
+ * Generate the class of a native library from its header: the header is run through the preprocessor of the C compiler,
122
+ * its declarations are read, the functions and the variables the library cannot give are left out, and the class gets a
123
+ * property by function (named as the function) with its TypeScript type.
124
+ * @param options The header, the library, the class, the compiler and its options.
125
+ * @returns The code of the class, and what was generated and left out.
126
+ * @example
127
+ * ```typescript
128
+ * import {writeFileSync} from 'node:fs'
129
+ * import {generateNativeLibrary} from '@lakutata/ffi/generator'
130
+ *
131
+ * const {code, report} = await generateNativeLibrary({header: 'vendor/include/vendor.h', library: 'vendor/lib/libvendor.so'})
132
+ * writeFileSync('src/native/Vendor.ts', code)
133
+ * for (const skipped of report.skipped) console.log(`${skipped.name}: ${skipped.reason}`)
134
+ * ```
135
+ */
136
+ export declare function generateNativeLibrary(options: GenerateOptions): Promise<GeneratedLibrary>;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * How a header is run through the preprocessor
3
+ */
4
+ export interface PreprocessOptions {
5
+ /**
6
+ * The header
7
+ */
8
+ readonly header: string;
9
+ /**
10
+ * The compiler whose preprocessor is run: cc, gcc, clang (their -E -dD), or cl, clang-cl (cl /P /d1PP)
11
+ */
12
+ readonly compiler?: string;
13
+ /**
14
+ * The directories of the headers it includes
15
+ */
16
+ readonly include?: readonly string[];
17
+ /**
18
+ * The macros defined before it: NAME, or NAME=VALUE
19
+ */
20
+ readonly define?: readonly string[];
21
+ }
22
+ /**
23
+ * A header run through the preprocessor
24
+ */
25
+ export interface PreprocessedHeader {
26
+ /**
27
+ * The output of the preprocessor: the declarations, the #define kept, the line markers naming the files
28
+ */
29
+ readonly text: string;
30
+ /**
31
+ * The header, as the line markers name it
32
+ */
33
+ readonly header: string;
34
+ /**
35
+ * The directories of the headers of the system: their headers are not the ones of the library
36
+ */
37
+ readonly systemDirectories: readonly string[];
38
+ /**
39
+ * The command run
40
+ */
41
+ readonly command: string;
42
+ }
43
+ /**
44
+ * The C compiler whose preprocessor reads the headers by default: the one of the CC variable, else cl on Windows (run
45
+ * in the Developer Command Prompt of Visual Studio), cc elsewhere.
46
+ * @returns The command of the compiler.
47
+ * @example
48
+ * ```typescript
49
+ * import {defaultCompiler} from '@lakutata/ffi/generator'
50
+ *
51
+ * console.log(defaultCompiler())
52
+ * ```
53
+ */
54
+ export declare function defaultCompiler(): string;
55
+ /**
56
+ * Run a header through the preprocessor of a compiler, keeping its #define and the line markers naming the files.
57
+ * @param options The header, the compiler, the directories and the macros.
58
+ * @returns The output, the header as the line markers name it, the directories of the system.
59
+ */
60
+ export declare function preprocessHeader(options: PreprocessOptions): PreprocessedHeader;
@@ -0,0 +1,35 @@
1
+ import { type CType, type FunctionType, type RecordType } from '../declarations/Types.js';
2
+ /**
3
+ * Where a value is: given to a native function (a parameter, the result of a JS callback), or given by it (a result, a
4
+ * member of a struct, an argument a JS callback receives)
5
+ */
6
+ export type Flow = 'given' | 'received';
7
+ /**
8
+ * The TypeScript types of C types, as the package converts their values
9
+ */
10
+ export declare class TypeScriptTypes {
11
+ #private;
12
+ /**
13
+ * Types for the declarations of a library.
14
+ * @param names The TypeScript names of its structs and unions.
15
+ */
16
+ constructor(names: ReadonlyMap<CType, string>);
17
+ /**
18
+ * The TypeScript type of a value of a C type.
19
+ * @param type The C type.
20
+ * @param flow Whether the value is given to the native code or received from it.
21
+ */
22
+ type(type: CType, flow: Flow): string;
23
+ /**
24
+ * The TypeScript type of a function: of a native function, or of a JS callback the native code calls.
25
+ * @param type The function type.
26
+ * @param callback Whether it is a JS callback (its arguments received, its result given).
27
+ */
28
+ functionType(type: FunctionType, callback?: boolean): string;
29
+ /**
30
+ * The members of a struct or a union, as the lines of an interface (or of an object type).
31
+ * @param record The struct or the union.
32
+ * @param indent The indentation of the lines.
33
+ */
34
+ members(record: RecordType, indent: string): string[];
35
+ }
@@ -0,0 +1,44 @@
1
+ import type { Pointer } from '../Runtime.js';
2
+ /**
3
+ * A JS function a library can call at any time (a persistent callback), made by `callback()` of a library: give its
4
+ * `pointer` to the library, and release it when the library no longer calls it. The callbacks of a library are released
5
+ * when it is closed; a process has at most 8192 of them at a time.
6
+ *
7
+ * When it is called from another thread of the library, it runs in the JS thread once the event loop gets to it. An
8
+ * exception it throws goes to the error handler of its library (the `error` event of a component), and the library gets
9
+ * zero (or a null pointer) as its result.
10
+ * @example
11
+ * ```typescript
12
+ * import {loadNativeLibrary, type NativeCallback} from '@lakutata/ffi'
13
+ *
14
+ * const vendor = loadNativeLibrary('vendor', 'typedef void (*vendor_log)(int level, const char *message);')
15
+ * const setLogger = vendor.func<(logger: bigint | null) => void>('void vendor_set_logger(vendor_log logger)')
16
+ * const logger: NativeCallback = vendor.callback('vendor_log', (level: number, message: string): void => console.log(level, message))
17
+ * setLogger(logger.pointer)
18
+ * //Once the library no longer calls it
19
+ * setLogger(null)
20
+ * logger.release()
21
+ * ```
22
+ */
23
+ export declare class NativeCallback {
24
+ #private;
25
+ /**
26
+ * A registered callback (made by `callback()` of a library, not by this constructor).
27
+ * @param pointer Its pointer.
28
+ * @param onRelease Called once it is released.
29
+ */
30
+ constructor(pointer: Pointer, onRelease: (callback: NativeCallback) => void);
31
+ /**
32
+ * The pointer to give to the library: the address of a C function calling the JS function.
33
+ */
34
+ get pointer(): Pointer;
35
+ /**
36
+ * Whether it is released: the library must not call it any more.
37
+ */
38
+ get released(): boolean;
39
+ /**
40
+ * Release it: its pointer is no longer valid, its slot is free for another callback. Releasing it again does
41
+ * nothing.
42
+ */
43
+ release(): void;
44
+ }