@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,11 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown when a library cannot be loaded: its message lists each file tried and why it could not be loaded (not found,
4
+ * built for another architecture, a library it needs missing). Its `errno` is `'E_NATIVE_LIBRARY_NOT_FOUND'`.
5
+ */
6
+ export declare class NativeLibraryNotFoundException extends Exception {
7
+ /**
8
+ * `'E_NATIVE_LIBRARY_NOT_FOUND'`.
9
+ */
10
+ errno: string | number;
11
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NativeLibraryNotFoundException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown when a library cannot be loaded: its message lists each file tried and why it could not be loaded (not found,
7
+ * built for another architecture, a library it needs missing). Its `errno` is `'E_NATIVE_LIBRARY_NOT_FOUND'`.
8
+ */
9
+ class NativeLibraryNotFoundException extends core_1.Exception {
10
+ constructor() {
11
+ super(...arguments);
12
+ /**
13
+ * `'E_NATIVE_LIBRARY_NOT_FOUND'`.
14
+ */
15
+ this.errno = 'E_NATIVE_LIBRARY_NOT_FOUND';
16
+ }
17
+ }
18
+ exports.NativeLibraryNotFoundException = NativeLibraryNotFoundException;
@@ -0,0 +1,11 @@
1
+ import { Exception } from '@lakutata/core';
2
+ /**
3
+ * Thrown when native memory cannot be read or written as asked: a null pointer, a negative length, a view of native
4
+ * memory where the runtime forbids it (Electron). Its `errno` is `'E_NATIVE_MEMORY'`.
5
+ */
6
+ export declare class NativeMemoryException extends Exception {
7
+ /**
8
+ * `'E_NATIVE_MEMORY'`.
9
+ */
10
+ errno: string | number;
11
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NativeMemoryException = void 0;
4
+ const core_1 = require("@lakutata/core");
5
+ /**
6
+ * Thrown when native memory cannot be read or written as asked: a null pointer, a negative length, a view of native
7
+ * memory where the runtime forbids it (Electron). Its `errno` is `'E_NATIVE_MEMORY'`.
8
+ */
9
+ class NativeMemoryException extends core_1.Exception {
10
+ constructor() {
11
+ super(...arguments);
12
+ /**
13
+ * `'E_NATIVE_MEMORY'`.
14
+ */
15
+ this.errno = 'E_NATIVE_MEMORY';
16
+ }
17
+ }
18
+ exports.NativeMemoryException = NativeMemoryException;
@@ -0,0 +1,12 @@
1
+ export { NativeDeclarationException } from '../exceptions/NativeDeclarationException.js';
2
+ export { NativeLibraryNotFoundException } from '../exceptions/NativeLibraryNotFoundException.js';
3
+ export { NativeLibraryClosedException } from '../exceptions/NativeLibraryClosedException.js';
4
+ export { NativeMemoryException } from '../exceptions/NativeMemoryException.js';
5
+ export { type Pointer } from '../lib/Runtime.js';
6
+ export { type LibraryNames } from '../lib/library/Resolve.js';
7
+ export { NativeCallback } from '../lib/library/NativeCallback.js';
8
+ export { loadNativeLibrary, NativeLibraryHandle, type NativeFunctionOptions, type NativeLibraryOptions, type NativeVariableAccessor } from '../lib/library/NativeLibraryHandle.js';
9
+ export { allocMemory, freeMemory, lastErrno, lastWindowsError, type Out, readArray, readBuffer, readCString, readValue, readWString, sizeOf, viewBuffer, writeValue } from '../lib/Memory.js';
10
+ export { Library, type LibraryOptions } from '../decorators/Library.js';
11
+ export { Native, NativeVariable } from '../decorators/Native.js';
12
+ export { buildNativeLibraryOptions, NativeLibrary, type NativeLibraryConfig } from '../components/NativeLibrary.js';
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NativeLibrary = exports.buildNativeLibraryOptions = exports.NativeVariable = exports.Native = exports.Library = exports.writeValue = exports.viewBuffer = exports.sizeOf = exports.readWString = exports.readValue = exports.readCString = exports.readBuffer = exports.readArray = exports.lastWindowsError = exports.lastErrno = exports.freeMemory = exports.allocMemory = exports.NativeLibraryHandle = exports.loadNativeLibrary = exports.NativeCallback = exports.NativeMemoryException = exports.NativeLibraryClosedException = exports.NativeLibraryNotFoundException = exports.NativeDeclarationException = void 0;
4
+ var NativeDeclarationException_js_1 = require("../exceptions/NativeDeclarationException.js");
5
+ Object.defineProperty(exports, "NativeDeclarationException", { enumerable: true, get: function () { return NativeDeclarationException_js_1.NativeDeclarationException; } });
6
+ var NativeLibraryNotFoundException_js_1 = require("../exceptions/NativeLibraryNotFoundException.js");
7
+ Object.defineProperty(exports, "NativeLibraryNotFoundException", { enumerable: true, get: function () { return NativeLibraryNotFoundException_js_1.NativeLibraryNotFoundException; } });
8
+ var NativeLibraryClosedException_js_1 = require("../exceptions/NativeLibraryClosedException.js");
9
+ Object.defineProperty(exports, "NativeLibraryClosedException", { enumerable: true, get: function () { return NativeLibraryClosedException_js_1.NativeLibraryClosedException; } });
10
+ var NativeMemoryException_js_1 = require("../exceptions/NativeMemoryException.js");
11
+ Object.defineProperty(exports, "NativeMemoryException", { enumerable: true, get: function () { return NativeMemoryException_js_1.NativeMemoryException; } });
12
+ var NativeCallback_js_1 = require("../lib/library/NativeCallback.js");
13
+ Object.defineProperty(exports, "NativeCallback", { enumerable: true, get: function () { return NativeCallback_js_1.NativeCallback; } });
14
+ var NativeLibraryHandle_js_1 = require("../lib/library/NativeLibraryHandle.js");
15
+ Object.defineProperty(exports, "loadNativeLibrary", { enumerable: true, get: function () { return NativeLibraryHandle_js_1.loadNativeLibrary; } });
16
+ Object.defineProperty(exports, "NativeLibraryHandle", { enumerable: true, get: function () { return NativeLibraryHandle_js_1.NativeLibraryHandle; } });
17
+ var Memory_js_1 = require("../lib/Memory.js");
18
+ Object.defineProperty(exports, "allocMemory", { enumerable: true, get: function () { return Memory_js_1.allocMemory; } });
19
+ Object.defineProperty(exports, "freeMemory", { enumerable: true, get: function () { return Memory_js_1.freeMemory; } });
20
+ Object.defineProperty(exports, "lastErrno", { enumerable: true, get: function () { return Memory_js_1.lastErrno; } });
21
+ Object.defineProperty(exports, "lastWindowsError", { enumerable: true, get: function () { return Memory_js_1.lastWindowsError; } });
22
+ Object.defineProperty(exports, "readArray", { enumerable: true, get: function () { return Memory_js_1.readArray; } });
23
+ Object.defineProperty(exports, "readBuffer", { enumerable: true, get: function () { return Memory_js_1.readBuffer; } });
24
+ Object.defineProperty(exports, "readCString", { enumerable: true, get: function () { return Memory_js_1.readCString; } });
25
+ Object.defineProperty(exports, "readValue", { enumerable: true, get: function () { return Memory_js_1.readValue; } });
26
+ Object.defineProperty(exports, "readWString", { enumerable: true, get: function () { return Memory_js_1.readWString; } });
27
+ Object.defineProperty(exports, "sizeOf", { enumerable: true, get: function () { return Memory_js_1.sizeOf; } });
28
+ Object.defineProperty(exports, "viewBuffer", { enumerable: true, get: function () { return Memory_js_1.viewBuffer; } });
29
+ Object.defineProperty(exports, "writeValue", { enumerable: true, get: function () { return Memory_js_1.writeValue; } });
30
+ var Library_js_1 = require("../decorators/Library.js");
31
+ Object.defineProperty(exports, "Library", { enumerable: true, get: function () { return Library_js_1.Library; } });
32
+ var Native_js_1 = require("../decorators/Native.js");
33
+ Object.defineProperty(exports, "Native", { enumerable: true, get: function () { return Native_js_1.Native; } });
34
+ Object.defineProperty(exports, "NativeVariable", { enumerable: true, get: function () { return Native_js_1.NativeVariable; } });
35
+ var NativeLibrary_js_1 = require("../components/NativeLibrary.js");
36
+ Object.defineProperty(exports, "buildNativeLibraryOptions", { enumerable: true, get: function () { return NativeLibrary_js_1.buildNativeLibraryOptions; } });
37
+ Object.defineProperty(exports, "NativeLibrary", { enumerable: true, get: function () { return NativeLibrary_js_1.NativeLibrary; } });
38
+
39
+ //The default of the entry: the entry itself (scripts/package-build/cjs-package.mjs)
40
+ if (!Object.prototype.hasOwnProperty.call(exports, 'default')) Object.defineProperty(exports, 'default', {value: exports})
@@ -0,0 +1,2 @@
1
+ export { generateNativeLibrary, type GeneratedLibrary, type GenerateOptions, type GenerationReport, type SkippedDeclaration } from '../lib/generator/Generator.js';
2
+ export { defaultCompiler, type PreprocessOptions } from '../lib/generator/Preprocessor.js';
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultCompiler = exports.generateNativeLibrary = void 0;
4
+ var Generator_js_1 = require("../lib/generator/Generator.js");
5
+ Object.defineProperty(exports, "generateNativeLibrary", { enumerable: true, get: function () { return Generator_js_1.generateNativeLibrary; } });
6
+ var Preprocessor_js_1 = require("../lib/generator/Preprocessor.js");
7
+ Object.defineProperty(exports, "defaultCompiler", { enumerable: true, get: function () { return Preprocessor_js_1.defaultCompiler; } });
8
+
9
+ //The default of the entry: the entry itself (scripts/package-build/cjs-package.mjs)
10
+ if (!Object.prototype.hasOwnProperty.call(exports, 'default')) Object.defineProperty(exports, 'default', {value: exports})
@@ -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,227 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.readCString = readCString;
7
+ exports.readWString = readWString;
8
+ exports.readBuffer = readBuffer;
9
+ exports.viewBuffer = viewBuffer;
10
+ exports.readValue = readValue;
11
+ exports.readArray = readArray;
12
+ exports.writeValue = writeValue;
13
+ exports.allocMemory = allocMemory;
14
+ exports.freeMemory = freeMemory;
15
+ exports.sizeOf = sizeOf;
16
+ exports.lastErrno = lastErrno;
17
+ exports.lastWindowsError = lastWindowsError;
18
+ const koffi_1 = __importDefault(require("koffi"));
19
+ const Runtime_js_1 = require("./Runtime.js");
20
+ /**
21
+ * Read a C string (UTF-8, ended by a NUL byte) from native memory: a `char *` given as a pointer (a `void *` result, an
22
+ * argument of a callback, a member of a struct).
23
+ * @param pointer Its address.
24
+ * @param length Its length in bytes, when it is not ended by a NUL byte.
25
+ * @returns The string.
26
+ * @example
27
+ * ```typescript
28
+ * import {readCString, type Pointer} from '@lakutata/ffi'
29
+ *
30
+ * export function messageOf(pointer: Pointer): string {
31
+ * return readCString(pointer)
32
+ * }
33
+ * ```
34
+ */
35
+ function readCString(pointer, length) {
36
+ const address = (0, Runtime_js_1.checkedPointer)(pointer, 'readCString');
37
+ return length === undefined ? koffi_1.default.decode.string(address) : koffi_1.default.decode.string(address, length);
38
+ }
39
+ /**
40
+ * Read a wide string from native memory: a `wchar_t *` (UTF-16 on Windows, UTF-32 elsewhere), ended by a NUL character.
41
+ * @param pointer Its address.
42
+ * @param length Its length in characters, when it is not ended by a NUL character.
43
+ * @returns The string.
44
+ * @example
45
+ * ```typescript
46
+ * import {readWString, type Pointer} from '@lakutata/ffi'
47
+ *
48
+ * export function titleOf(pointer: Pointer): string {
49
+ * return readWString(pointer)
50
+ * }
51
+ * ```
52
+ */
53
+ function readWString(pointer, length) {
54
+ const address = (0, Runtime_js_1.checkedPointer)(pointer, 'readWString');
55
+ return length === undefined ? koffi_1.default.decode.wstring(address) : koffi_1.default.decode.wstring(address, length);
56
+ }
57
+ /**
58
+ * Copy native memory into a buffer of its own, which stays valid once the native memory is freed (about 10 GB/s).
59
+ * @param pointer The address of the memory.
60
+ * @param length Its length in bytes.
61
+ * @returns The copy.
62
+ * @example
63
+ * ```typescript
64
+ * import {readBuffer, type Pointer} from '@lakutata/ffi'
65
+ *
66
+ * export function frameOf(pixels: Pointer, width: number, height: number): Buffer {
67
+ * return readBuffer(pixels, width * height * 4)
68
+ * }
69
+ * ```
70
+ */
71
+ function readBuffer(pointer, length) {
72
+ return (0, Runtime_js_1.copyBuffer)(pointer, length);
73
+ }
74
+ /**
75
+ * A view of native memory, without copy: worth it for large blocks read often (8 MiB: about 0.13 ms rather than 1 ms
76
+ * for a copy). The view must not be used once the native memory is freed: the process would crash. Electron forbids
77
+ * these views: it throws there (use readBuffer).
78
+ * @param pointer The address of the memory.
79
+ * @param length Its length in bytes.
80
+ * @returns The memory, as an ArrayBuffer.
81
+ * @throws {NativeMemoryException} In Electron, or for a null pointer.
82
+ * @example
83
+ * ```typescript
84
+ * import {viewBuffer, type Pointer} from '@lakutata/ffi'
85
+ *
86
+ * export function firstPixel(pixels: Pointer): number {
87
+ * return new Uint32Array(viewBuffer(pixels, 4))[0]
88
+ * }
89
+ * ```
90
+ */
91
+ function viewBuffer(pointer, length) {
92
+ return (0, Runtime_js_1.nativeView)(pointer, length);
93
+ }
94
+ /**
95
+ * Read a value of a built-in type from native memory (`int`, `double`, `uint64_t`, `char *`…): the 64-bit integers as
96
+ * bigints. The types of a library are read by `read()` of the library.
97
+ * @param pointer Its address.
98
+ * @param type Its type.
99
+ * @returns The value.
100
+ * @example
101
+ * ```typescript
102
+ * import {readValue, type Pointer} from '@lakutata/ffi'
103
+ *
104
+ * export function countAt(pointer: Pointer): number {
105
+ * return readValue(pointer, 'int32_t') as number
106
+ * }
107
+ * ```
108
+ */
109
+ function readValue(pointer, type) {
110
+ return (0, Runtime_js_1.readNative)(Runtime_js_1.BUILTIN_TYPES, pointer, type);
111
+ }
112
+ /**
113
+ * Read an array of values of a built-in type from native memory.
114
+ * @param pointer The address of its first element.
115
+ * @param type The type of its elements (`char *` reads strings).
116
+ * @param count Its length.
117
+ * @returns The values.
118
+ * @example
119
+ * ```typescript
120
+ * import {readArray, type Pointer} from '@lakutata/ffi'
121
+ *
122
+ * //The row of a callback of sqlite3_exec: char **values
123
+ * export function rowOf(values: Pointer, count: number): string[] {
124
+ * return readArray(values, 'char *', count) as string[]
125
+ * }
126
+ * ```
127
+ */
128
+ function readArray(pointer, type, count) {
129
+ return (0, Runtime_js_1.readNative)(Runtime_js_1.BUILTIN_TYPES, pointer, type, count);
130
+ }
131
+ /**
132
+ * Write a value of a built-in type to native memory.
133
+ * @param pointer Its address.
134
+ * @param type Its type.
135
+ * @param value The value.
136
+ * @example
137
+ * ```typescript
138
+ * import {allocMemory, freeMemory, readValue, writeValue} from '@lakutata/ffi'
139
+ *
140
+ * const counter: bigint = allocMemory('int64_t')
141
+ * writeValue(counter, 'int64_t', 42n)
142
+ * console.log(readValue(counter, 'int64_t'))
143
+ * freeMemory(counter)
144
+ * ```
145
+ */
146
+ function writeValue(pointer, type, value) {
147
+ (0, Runtime_js_1.writeNative)(Runtime_js_1.BUILTIN_TYPES, pointer, type, value);
148
+ }
149
+ /**
150
+ * Allocate native memory for values of a built-in type: memory that does not move, which a library can keep (a buffer
151
+ * given to a function is valid during the call only). Freed by freeMemory.
152
+ * @param type The type of the values.
153
+ * @param count Their number.
154
+ * @returns Its address.
155
+ * @example
156
+ * ```typescript
157
+ * import {allocMemory, freeMemory} from '@lakutata/ffi'
158
+ *
159
+ * const buffer: bigint = allocMemory('uint8_t', 4096)
160
+ * freeMemory(buffer)
161
+ * ```
162
+ */
163
+ function allocMemory(type, count = 1) {
164
+ return (0, Runtime_js_1.allocateNative)(Runtime_js_1.BUILTIN_TYPES, type, count);
165
+ }
166
+ /**
167
+ * Free native memory allocated by allocMemory.
168
+ * @param pointer Its address.
169
+ * @example
170
+ * ```typescript
171
+ * import {allocMemory, freeMemory} from '@lakutata/ffi'
172
+ *
173
+ * freeMemory(allocMemory('double', 16))
174
+ * ```
175
+ */
176
+ function freeMemory(pointer) {
177
+ (0, Runtime_js_1.freeNative)(pointer);
178
+ }
179
+ /**
180
+ * The size of a built-in type on the platform, in bytes (`long` is 8 on 64-bit Linux and macOS, 4 on Windows).
181
+ * @param type The type.
182
+ * @returns Its size.
183
+ * @example
184
+ * ```typescript
185
+ * import {sizeOf} from '@lakutata/ffi'
186
+ *
187
+ * console.log(sizeOf('long'), sizeOf('wchar_t'), sizeOf('void *'))
188
+ * ```
189
+ */
190
+ function sizeOf(type) {
191
+ return (0, Runtime_js_1.typeSize)(Runtime_js_1.BUILTIN_TYPES, type);
192
+ }
193
+ /**
194
+ * The errno of the last native call of the thread. On Windows, each C library has its own errno: the one of the C
195
+ * library of Microsoft (ucrtbase.dll) by default.
196
+ * @param crt On Windows, the C library the function called uses (`msvcrt.dll` for the old libraries).
197
+ * @returns The errno.
198
+ * @example
199
+ * ```typescript
200
+ * import {lastErrno, loadNativeLibrary} from '@lakutata/ffi'
201
+ *
202
+ * const libc = loadNativeLibrary('c')
203
+ * const open = libc.func<(path: string, flags: number) => number>('int open(const char *path, int flags, ...)')
204
+ * if (open('/surely/missing', 0) < 0) console.log('errno', lastErrno())
205
+ * ```
206
+ */
207
+ function lastErrno(crt) {
208
+ return (0, Runtime_js_1.nativeErrno)(crt);
209
+ }
210
+ /**
211
+ * The last error of the Windows API of the thread (GetLastError), kept after each native call; undefined on the other
212
+ * systems.
213
+ * @returns The error.
214
+ * @example
215
+ * ```typescript
216
+ * import {lastWindowsError, loadNativeLibrary} from '@lakutata/ffi'
217
+ *
218
+ * if (process.platform === 'win32') {
219
+ * const kernel32 = loadNativeLibrary('kernel32.dll')
220
+ * const attributes = kernel32.func<(path: string) => number>('DWORD WINAPI GetFileAttributesW(LPCWSTR path)')
221
+ * if (attributes('C:\\missing') === 0xFFFFFFFF) console.log('error', lastWindowsError())
222
+ * }
223
+ * ```
224
+ */
225
+ function lastWindowsError() {
226
+ return (0, Runtime_js_1.nativeWindowsError)();
227
+ }
@@ -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;