koffi 1.2.4 → 1.3.0-rc.1
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.
- package/README.md +7 -489
- package/benchmark/CMakeLists.txt +13 -9
- package/benchmark/raylib_node_raylib.js +67 -0
- package/build/qemu/1.3.0-rc.1/koffi_darwin_arm64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_darwin_x64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_freebsd_arm64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_freebsd_ia32.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_freebsd_x64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_linux_arm32hf.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_linux_arm64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_linux_ia32.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_linux_riscv64hf64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_linux_x64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_openbsd_ia32.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_openbsd_x64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_win32_arm64.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_win32_ia32.tar.gz +0 -0
- package/build/qemu/1.3.0-rc.1/koffi_win32_x64.tar.gz +0 -0
- package/doc/Makefile +20 -0
- package/doc/_static/bench_linux.png +0 -0
- package/doc/_static/bench_windows.png +0 -0
- package/doc/_static/custom.css +22 -0
- package/doc/benchmarks.md +113 -0
- package/doc/benchmarks.xlsx +0 -0
- package/doc/conf.py +54 -0
- package/doc/contribute.md +115 -0
- package/doc/dist/doctrees/benchmarks.doctree +0 -0
- package/doc/dist/doctrees/contribute.doctree +0 -0
- package/doc/dist/doctrees/environment.pickle +0 -0
- package/doc/dist/doctrees/functions.doctree +0 -0
- package/doc/dist/doctrees/index.doctree +0 -0
- package/doc/dist/doctrees/memory.doctree +0 -0
- package/doc/dist/doctrees/platforms.doctree +0 -0
- package/doc/dist/doctrees/start.doctree +0 -0
- package/doc/dist/doctrees/types.doctree +0 -0
- package/doc/dist/html/.buildinfo +4 -0
- package/doc/dist/html/_sources/benchmarks.md.txt +113 -0
- package/doc/dist/html/_sources/contribute.md.txt +115 -0
- package/doc/dist/html/_sources/functions.md.txt +224 -0
- package/doc/dist/html/_sources/index.rst.txt +33 -0
- package/doc/dist/html/_sources/memory.md.txt +29 -0
- package/doc/dist/html/_sources/platforms.md.txt +17 -0
- package/doc/dist/html/_sources/start.md.txt +89 -0
- package/doc/dist/html/_sources/types.md.txt +514 -0
- package/doc/dist/html/_static/_sphinx_javascript_frameworks_compat.js +134 -0
- package/doc/dist/html/_static/basic.css +932 -0
- package/doc/dist/html/_static/bench_linux.png +0 -0
- package/doc/dist/html/_static/bench_windows.png +0 -0
- package/doc/dist/html/_static/custom.css +22 -0
- package/doc/dist/html/_static/debug.css +69 -0
- package/doc/dist/html/_static/doctools.js +264 -0
- package/doc/dist/html/_static/documentation_options.js +14 -0
- package/doc/dist/html/_static/file.png +0 -0
- package/doc/dist/html/_static/jquery-3.6.0.js +10881 -0
- package/doc/dist/html/_static/jquery.js +2 -0
- package/doc/dist/html/_static/language_data.js +199 -0
- package/doc/dist/html/_static/minus.png +0 -0
- package/doc/dist/html/_static/plus.png +0 -0
- package/doc/dist/html/_static/pygments.css +252 -0
- package/doc/dist/html/_static/scripts/furo-extensions.js +0 -0
- package/doc/dist/html/_static/scripts/furo.js +3 -0
- package/doc/dist/html/_static/scripts/furo.js.LICENSE.txt +7 -0
- package/doc/dist/html/_static/scripts/furo.js.map +1 -0
- package/doc/dist/html/_static/searchtools.js +531 -0
- package/doc/dist/html/_static/skeleton.css +296 -0
- package/doc/dist/html/_static/styles/furo-extensions.css +2 -0
- package/doc/dist/html/_static/styles/furo-extensions.css.map +1 -0
- package/doc/dist/html/_static/styles/furo.css +2 -0
- package/doc/dist/html/_static/styles/furo.css.map +1 -0
- package/doc/dist/html/_static/underscore-1.13.1.js +2042 -0
- package/doc/dist/html/_static/underscore.js +6 -0
- package/doc/dist/html/benchmarks.html +547 -0
- package/doc/dist/html/contribute.html +382 -0
- package/doc/dist/html/functions.html +530 -0
- package/doc/dist/html/genindex.html +249 -0
- package/doc/dist/html/index.html +342 -0
- package/doc/dist/html/memory.html +337 -0
- package/doc/dist/html/objects.inv +0 -0
- package/doc/dist/html/platforms.html +332 -0
- package/doc/dist/html/search.html +257 -0
- package/doc/dist/html/searchindex.js +1 -0
- package/doc/dist/html/start.html +367 -0
- package/doc/dist/html/types.html +1001 -0
- package/doc/functions.md +224 -0
- package/doc/index.rst +33 -0
- package/doc/make.bat +35 -0
- package/doc/memory.md +29 -0
- package/doc/platforms.md +17 -0
- package/doc/start.md +89 -0
- package/doc/types.md +514 -0
- package/package.json +5 -2
- package/qemu/qemu.js +41 -27
- package/qemu/registry/machines.json +59 -79
- package/qemu/registry/sha256sum.txt +4 -4
- package/src/abi_arm32.cc +20 -48
- package/src/abi_arm64.cc +18 -46
- package/src/abi_arm64_fwd.S +5 -0
- package/src/abi_riscv64.cc +19 -47
- package/src/abi_x64_sysv.cc +18 -46
- package/src/abi_x64_win.cc +19 -47
- package/src/abi_x86.cc +21 -49
- package/src/call.cc +505 -242
- package/src/call.hh +14 -7
- package/src/ffi.cc +47 -26
- package/src/ffi.hh +1 -1
- package/src/parser.cc +2 -20
- package/src/util.cc +50 -11
- package/src/util.hh +2 -0
- package/test/misc.c +31 -0
- package/test/sync.js +41 -4
- package/benchmark/atoi_cc.cc +0 -59
- package/build/qemu/1.2.4/koffi_darwin_arm64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_darwin_x64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_freebsd_arm64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_freebsd_ia32.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_freebsd_x64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_linux_arm.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_linux_arm64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_linux_ia32.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_linux_riscv64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_linux_x64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_openbsd_ia32.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_openbsd_x64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_win32_arm64.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_win32_ia32.tar.gz +0 -0
- package/build/qemu/1.2.4/koffi_win32_x64.tar.gz +0 -0
package/README.md
CHANGED
|
@@ -1,27 +1,8 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
- [Introduction](#introduction)
|
|
4
|
-
- [Get started](#get-started)
|
|
5
|
-
- [Extra features](#extra-features)
|
|
6
|
-
* [Type information](#type-information)
|
|
7
|
-
* [C arrays](#c-arrays)
|
|
8
|
-
* [Variadic functions](#variadic-functions)
|
|
9
|
-
* [Asynchronous calls](#asynchronous-calls)
|
|
10
|
-
* [Callbacks](#callbacks)
|
|
11
|
-
* [Memory settings](#memory-settings)
|
|
12
|
-
- [Benchmarks](#benchmarks)
|
|
13
|
-
* [atoi results](#atoi-results)
|
|
14
|
-
* [Raylib results](#raylib-results)
|
|
15
|
-
- [Tests](#tests)
|
|
16
|
-
- [Compilation](#compilation)
|
|
17
|
-
* [Windows](#windows-1)
|
|
18
|
-
* [Other platforms](#other-platforms)
|
|
19
|
-
|
|
20
|
-
# Introduction
|
|
1
|
+
# Overview
|
|
21
2
|
|
|
22
3
|
Koffi is a fast and easy-to-use C FFI module for Node.js, featuring:
|
|
23
4
|
|
|
24
|
-
* Low-overhead and fast performance (see [benchmarks](
|
|
5
|
+
* Low-overhead and fast performance (see [benchmarks](https://koffi.dev/benchmarks))
|
|
25
6
|
* Support for primitive and aggregate data types (structs and fixed-size arrays), both by reference (pointer) and by value
|
|
26
7
|
* Javascript functions can be used as C callbacks (since 1.2.0)
|
|
27
8
|
* Well-tested code base for popular OS/architecture combinations
|
|
@@ -38,475 +19,12 @@ RISC-V 64 [^3] | ⬜️ *N/A* | ✅ Yes | ⬜️ *N/A* | 🟨 Probab
|
|
|
38
19
|
|
|
39
20
|
[^1]: The following call conventions are supported: cdecl, stdcall, MS fastcall, thiscall.
|
|
40
21
|
[^2]: The prebuilt binary uses the hard float ABI and expects a VFP coprocessor. Build from source to use Koffi with a different ABI (softfp, soft).
|
|
41
|
-
[^3]:
|
|
42
|
-
|
|
43
|
-
The following features are planned in the near future:
|
|
44
|
-
|
|
45
|
-
* 1.3: Real-world examples, optimize passing of structs and arrays
|
|
46
|
-
* 1.4: Type parser, unions
|
|
47
|
-
|
|
48
|
-
# Get started
|
|
49
|
-
|
|
50
|
-
Once you have installed koffi with `npm install koffi`, you can start by loading it this way:
|
|
51
|
-
|
|
52
|
-
```js
|
|
53
|
-
const koffi = require('koffi');
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Below you can find three examples:
|
|
57
|
-
|
|
58
|
-
* The first one runs on Linux. The functions are declared with the C-like prototype language.
|
|
59
|
-
* The second one runs on Windows, and uses the node-ffi like syntax to declare functions.
|
|
60
|
-
* The third one is more complex and uses Raylib to animate "Hello World" in a window.
|
|
61
|
-
|
|
62
|
-
## Small Linux example
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
const koffi = require('koffi');
|
|
66
|
-
const lib = koffi.load('libc.so.6');
|
|
67
|
-
|
|
68
|
-
// Declare types
|
|
69
|
-
const timeval = koffi.struct('timeval', {
|
|
70
|
-
tv_sec: 'unsigned int',
|
|
71
|
-
tv_usec: 'unsigned int'
|
|
72
|
-
});
|
|
73
|
-
const timezone = koffi.struct('timezone', {
|
|
74
|
-
tz_minuteswest: 'int',
|
|
75
|
-
tz_dsttime: 'int'
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// Declare functions
|
|
79
|
-
const gettimeofday = lib.func('int gettimeofday(_Out_ timeval *tv, _Out_ timezone *tz)');
|
|
80
|
-
const printf = lib.func('int printf(const char *format, ...)');
|
|
81
|
-
|
|
82
|
-
let tv = {};
|
|
83
|
-
let tz = {};
|
|
84
|
-
gettimeofday(tv, tz);
|
|
85
|
-
|
|
86
|
-
printf('Hello World!, it is: %d\n', 'int', tv.tv_sec);
|
|
87
|
-
console.log(tz);
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
## Small Windows example
|
|
91
|
-
|
|
92
|
-
```js
|
|
93
|
-
const koffi = require('koffi');
|
|
94
|
-
const lib = koffi.load('user32.dll');
|
|
95
|
-
|
|
96
|
-
const MessageBoxA = lib.stdcall('MessageBoxA', 'int', ['void *', 'string', 'string', 'uint']);
|
|
97
|
-
const MB_ICONINFORMATION = 0x40;
|
|
98
|
-
|
|
99
|
-
MessageBoxA(null, 'Hello', 'Foobar', MB_ICONINFORMATION);
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
## Raylib example
|
|
103
|
-
|
|
104
|
-
This section assumes you know how to build C shared libraries, such as Raylib. You may need to fix the path to the library before you can do anything.
|
|
105
|
-
|
|
106
|
-
```js
|
|
107
|
-
const koffi = require('koffi');
|
|
108
|
-
let lib = koffi.load('raylib.dll'); // Fix path if needed
|
|
109
|
-
|
|
110
|
-
const Color = koffi.struct('Color', {
|
|
111
|
-
r: 'uchar',
|
|
112
|
-
g: 'uchar',
|
|
113
|
-
b: 'uchar',
|
|
114
|
-
a: 'uchar'
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
const Image = koffi.struct('Image', {
|
|
118
|
-
data: koffi.pointer('void'),
|
|
119
|
-
width: 'int',
|
|
120
|
-
height: 'int',
|
|
121
|
-
mipmaps: 'int',
|
|
122
|
-
format: 'int'
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
const GlyphInfo = koffi.struct('GlyphInfo', {
|
|
126
|
-
value: 'int',
|
|
127
|
-
offsetX: 'int',
|
|
128
|
-
offsetY: 'int',
|
|
129
|
-
advanceX: 'int',
|
|
130
|
-
image: Image
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
const Vector2 = koffi.struct('Vector2', {
|
|
134
|
-
x: 'float',
|
|
135
|
-
y: 'float'
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
const Rectangle = koffi.struct('Rectangle', {
|
|
139
|
-
x: 'float',
|
|
140
|
-
y: 'float',
|
|
141
|
-
width: 'float',
|
|
142
|
-
height: 'float'
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const Texture = koffi.struct('Texture', {
|
|
146
|
-
id: 'uint',
|
|
147
|
-
width: 'int',
|
|
148
|
-
height: 'int',
|
|
149
|
-
mipmaps: 'int',
|
|
150
|
-
format: 'int'
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
const Font = koffi.struct('Font', {
|
|
154
|
-
baseSize: 'int',
|
|
155
|
-
glyphCount: 'int',
|
|
156
|
-
glyphPadding: 'int',
|
|
157
|
-
texture: Texture,
|
|
158
|
-
recs: koffi.pointer(Rectangle),
|
|
159
|
-
glyphs: koffi.pointer(GlyphInfo)
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// Classic function declaration
|
|
163
|
-
const InitWindow = lib.func('InitWindow', 'void', ['int', 'int', 'string']);
|
|
164
|
-
const SetTargetFPS = lib.func('SetTargetFPS', 'void', ['int']);
|
|
165
|
-
const GetScreenWidth = lib.func('GetScreenWidth', 'int', []);
|
|
166
|
-
const GetScreenHeight = lib.func('GetScreenHeight', 'int', []);
|
|
167
|
-
const ClearBackground = lib.func('ClearBackground', 'void', [Color]);
|
|
168
|
-
|
|
169
|
-
// Prototype parser
|
|
170
|
-
const BeginDrawing = lib.func('void BeginDrawing()');
|
|
171
|
-
const EndDrawing = lib.func('void EndDrawing()');
|
|
172
|
-
const WindowShouldClose = lib.func('void WindowShouldClose(bool)');
|
|
173
|
-
const GetFontDefault = lib.func('Font GetFontDefault()');
|
|
174
|
-
const MeasureTextEx = lib.func('Vector2 MeasureTextEx(Font, const char *, float, float)');
|
|
175
|
-
const DrawTextEx = lib.func('void DrawTextEx(Font font, const char *text, Vector2 pos, float size, float spacing, Color tint)');
|
|
176
|
-
|
|
177
|
-
InitWindow(800, 600, 'Test Raylib');
|
|
178
|
-
SetTargetFPS(60);
|
|
179
|
-
|
|
180
|
-
let angle = 0;
|
|
181
|
-
|
|
182
|
-
while (!WindowShouldClose()) {
|
|
183
|
-
BeginDrawing();
|
|
184
|
-
ClearBackground({ r: 0, g: 0, b: 0, a: 255 }); // black
|
|
185
|
-
|
|
186
|
-
let win_width = GetScreenWidth();
|
|
187
|
-
let win_height = GetScreenHeight();
|
|
188
|
-
|
|
189
|
-
let text = 'Hello World!';
|
|
190
|
-
let text_width = MeasureTextEx(GetFontDefault(), text, 32, 1).x;
|
|
191
|
-
|
|
192
|
-
let color = {
|
|
193
|
-
r: 127.5 + 127.5 * Math.sin(angle),
|
|
194
|
-
g: 127.5 + 127.5 * Math.sin(angle + Math.PI / 2),
|
|
195
|
-
b: 127.5 + 127.5 * Math.sin(angle + Math.PI),
|
|
196
|
-
a: 255
|
|
197
|
-
};
|
|
198
|
-
let pos = {
|
|
199
|
-
x: (win_width / 2 - text_width / 2) + 120 * Math.cos(angle - Math.PI / 2),
|
|
200
|
-
y: (win_height / 2 - 16) + 120 * Math.sin(angle - Math.PI / 2)
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
DrawTextEx(GetFontDefault(), text, pos, 32, 1, color);
|
|
204
|
-
|
|
205
|
-
EndDrawing();
|
|
206
|
-
|
|
207
|
-
angle += Math.PI / 180;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
# Extra features
|
|
213
|
-
|
|
214
|
-
## Type information
|
|
215
|
-
|
|
216
|
-
Koffi exposes three functions to explore type information:
|
|
217
|
-
- `koffi.sizeof(type)` to get the size of a type
|
|
218
|
-
- `koffi.alignof(type)` to get the alignment of a type
|
|
219
|
-
- `koffi.introspect(type)` to get the definition of a type (only for structs for now)
|
|
220
|
-
|
|
221
|
-
## C arrays
|
|
222
|
-
|
|
223
|
-
Fixed-size arrays are declared with `koffi.array(type, length)`. Just like in C, they cannot be passed
|
|
224
|
-
as functions parameters (they degenerate to pointers), or returned by value. You can however embed them in struct types.
|
|
225
|
-
|
|
226
|
-
### JS typed arrays
|
|
227
|
-
|
|
228
|
-
Special rules apply for arrays of primitive integer and float types (uint32_t, double, etc...):
|
|
229
|
-
- When converting from JS to C, Koffi can take a normal Array (e.g. `[1, 2]`) or a TypedArray of the correct type (e.g. `Uint8Array` for an array of `uint8_t` numbers)
|
|
230
|
-
- When converting from C to JS (for return value or output parameters), Koffi will by default use a TypedArray. But you can change this behavior when you create the array type with the optional hint argument: `koffi.array('uint8_t', 64, 'array')`
|
|
231
|
-
|
|
232
|
-
See the example below:
|
|
233
|
-
|
|
234
|
-
```js
|
|
235
|
-
const koffi = require('koffi');
|
|
236
|
-
|
|
237
|
-
// Those two structs are exactly the same, only the array conversion hint is different
|
|
238
|
-
const Foo1 = koffi.struct('Foo', {
|
|
239
|
-
i: 'int',
|
|
240
|
-
a16: koffi.array('int16_t', 8)
|
|
241
|
-
});
|
|
242
|
-
const Foo2 = koffi.struct('Foo', {
|
|
243
|
-
i: 'int',
|
|
244
|
-
a16: koffi.array('int16_t', 8, 'array')
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
// Uses an hypothetical C function that just returns the struct passed as a parameter
|
|
248
|
-
const ReturnFoo1 = lib.func('Foo1 ReturnFoo(Foo1 p)');
|
|
249
|
-
const ReturnFoo2 = lib.func('Foo2 ReturnFoo(Foo2 p)');
|
|
250
|
-
|
|
251
|
-
console.log(ReturnFoo1({ i: 5, a16: [6, 8] })) // Prints { i: 5, a16: Int16Array(2) [6, 8] }
|
|
252
|
-
console.log(ReturnFoo2({ i: 5, a16: [6, 8] })) // Prints { i: 5, a16: [6, 8] }
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
### C strings
|
|
256
|
-
|
|
257
|
-
Koffi can also convert JS strings to fixed-sized arrays in the following cases:
|
|
258
|
-
- char (or int8_t) arrays are filled with the UTF-8 encoded string, truncated if needed. The buffer is always NUL-terminated.
|
|
259
|
-
- char16 (or int16_t) arrays are filled with the UTF-16 encoded string, truncated if needed. The buffer is always NUL-terminated.
|
|
260
|
-
|
|
261
|
-
The reverse case is also true, Koffi can convert a C fixed-size buffer to a JS string. Use the `string` array hint to do this (e.g. `koffi.array('char', 8, 'string')`).
|
|
262
|
-
|
|
263
|
-
## Variadic functions
|
|
264
|
-
|
|
265
|
-
Variadic functions are declared with an ellipsis as the last argument.
|
|
266
|
-
|
|
267
|
-
In order to call a variadic function, you must provide two Javascript arguments for each C parameter, the first one is the expected type and the second one is the value.
|
|
268
|
-
|
|
269
|
-
```js
|
|
270
|
-
const printf = lib.func('printf', 'int', ['string', '...']);
|
|
271
|
-
|
|
272
|
-
// The variadic arguments are: 6 (int), 8.5 (double), 'THE END' (const char *)
|
|
273
|
-
printf('Integer %d, double %g, string %s', 'int', 6, 'double', 8.5, 'string', 'THE END');
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
## Asynchronous calls
|
|
277
|
-
|
|
278
|
-
You can issue asynchronous calls by calling the function through its async member. In this case, you need to provide a callback function as the last argument, with `(err, res)` parameters.
|
|
279
|
-
|
|
280
|
-
```js
|
|
281
|
-
const koffi = require('koffi');
|
|
282
|
-
const lib = koffi.load('libc.so.6');
|
|
283
|
-
|
|
284
|
-
const atoi = lib.func('int atoi(const char *str)');
|
|
285
|
-
|
|
286
|
-
atoi.async('1257', (err, res) => {
|
|
287
|
-
console.log('Result:', res);
|
|
288
|
-
})
|
|
289
|
-
console.log('Hello World!');
|
|
290
|
-
|
|
291
|
-
// This program will print "Hello World!", and then "Result: 1257"
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
You can easily convert this callback-style async function to a promise-based version with `util.promisify()` from the Node.js standard library.
|
|
295
|
-
|
|
296
|
-
Variadic functions do not support async.
|
|
297
|
-
|
|
298
|
-
## Callbacks
|
|
299
|
-
|
|
300
|
-
In order to pass a JS function to a C function expecting a callback, you must first create a callback type with the expected return type and parameters. The syntax is similar to the one used to load functions from a shared library.
|
|
301
|
-
|
|
302
|
-
```js
|
|
303
|
-
const koffi = require('koffi');
|
|
304
|
-
|
|
305
|
-
// With the classic syntax, this callback expects an integer and returns nothing
|
|
306
|
-
const ExampleCallback = koffi.callback('ExampleCallback', 'void', ['int']);
|
|
307
|
-
|
|
308
|
-
// With the prototype parser, this callback expects a double and float, and returns the sum as a double
|
|
309
|
-
const AddDoubleFloat = koffi.callback('double AddDoubleFloat(double d, float f)');
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
Once your callback type is declared, you can use them in struct definitions, or as function parameter and/or return type.
|
|
313
|
-
|
|
314
|
-
Here is a small example with the C part and the JS part.
|
|
315
|
-
|
|
316
|
-
```c
|
|
317
|
-
#include <string.h>
|
|
318
|
-
|
|
319
|
-
int TransferToJS(const char *str, int (*cb)(const char *str))
|
|
320
|
-
{
|
|
321
|
-
char buf[64];
|
|
322
|
-
snprintf(buf, sizeof(buf), "Hello %s!", str);
|
|
323
|
-
return cb(buf);
|
|
324
|
-
}
|
|
325
|
-
```
|
|
326
|
-
|
|
327
|
-
```js
|
|
328
|
-
const koffi = require('koffi');
|
|
329
|
-
|
|
330
|
-
const TransferCallback = koffi.callback('int TransferCallback(const char *str)');
|
|
331
|
-
|
|
332
|
-
const TransferToJS = lib.func('int TransferToJS(const char *str, TransferCallback cb)');
|
|
333
|
-
|
|
334
|
-
let ret = TransferToJS('Niels', (str) => {
|
|
335
|
-
console.log(str);
|
|
336
|
-
return 42;
|
|
337
|
-
});
|
|
338
|
-
console.log(ret);
|
|
339
|
-
|
|
340
|
-
// This example prints "Hello Niels!" first, and then prints 42
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
On x86 platforms, only Cdecl and Stdcall callbacks are supported.
|
|
344
|
-
|
|
345
|
-
## Memory settings
|
|
346
|
-
|
|
347
|
-
For synchronous/normal calls, Koffi uses two preallocated memory blocks, one to construct the C stack and the other to allocate strings and big objects/structs. Unless very big strings or objects (at least more than one page of memory) are used, no extra allocation is needed during calls or callbacks.
|
|
348
|
-
|
|
349
|
-
The size (in bytes) of these preallocated blocks can be changed. Use `koffi.config()` to get an object with the settings, and `koffi.config(obj)` to apply new settings.
|
|
350
|
-
|
|
351
|
-
```js
|
|
352
|
-
let config = koffi.config();
|
|
353
|
-
console.log(config);
|
|
354
|
-
|
|
355
|
-
// {
|
|
356
|
-
// sync_stack_size: 1048576,
|
|
357
|
-
// sync_heap_size: 2097152,
|
|
358
|
-
// async_stack_size: 524288,
|
|
359
|
-
// async_heap_size: 1048576,
|
|
360
|
-
// resident_async_pools: 2
|
|
361
|
-
// }
|
|
362
|
-
```
|
|
363
|
-
|
|
364
|
-
The same is true for asynchronous calls. When an asynchronous call is made, Koffi will allocate new blocks unless there is an unused set of blocks still available. Once the asynchronous call is finished, these blocks are freed if there are more than `resident_async_pools` sets of blocks left around.
|
|
365
|
-
|
|
366
|
-
# Benchmarks
|
|
367
|
-
|
|
368
|
-
In order to run it, go to `koffi/benchmark` and run `../../cnoke/cnoke.js` (or `node ..\..\cnoke\cnoke.js` on Windows) before doing anything else.
|
|
369
|
-
|
|
370
|
-
Once this is done, you can execute each implementation, e.g. `build/atoi_cc` or `./atoi_koffi.js`. You can optionally define a custom number of iterations, e.g. `./atoi_koffi.js 10000000`.
|
|
371
|
-
|
|
372
|
-
## atoi results
|
|
373
|
-
|
|
374
|
-
This test is based around repeated calls to a simple standard C function atoi, and has three implementations:
|
|
375
|
-
- the first one is the reference, it calls atoi through an N-API module, and is close to the theoretical limit of a perfect (no overhead) Node.js > C FFI implementation.
|
|
376
|
-
- the second one calls atoi through Koffi
|
|
377
|
-
- the third one uses the official Node.js FFI implementation, node-ffi-napi
|
|
378
|
-
|
|
379
|
-
Because atoi is a small call, the FFI overhead is clearly visible.
|
|
380
|
-
|
|
381
|
-
### Linux
|
|
382
|
-
|
|
383
|
-
The results below were measured on my x86_64 Linux machine (AMD® Ryzen™ 7 5800H 16G):
|
|
384
|
-
|
|
385
|
-
Benchmark | Iterations | Total time | Overhead
|
|
386
|
-
------------- | ---------- | ----------- | ----------
|
|
387
|
-
atoi_napi | 20000000 | 1.10s | (baseline)
|
|
388
|
-
atoi_koffi | 20000000 | 1.91s | x1.73
|
|
389
|
-
atoi_node_ffi | 20000000 | 640.49s | x582
|
|
390
|
-
|
|
391
|
-
### Windows
|
|
392
|
-
|
|
393
|
-
The results below were measured on my x86_64 Windows machine (AMD® Ryzen™ 7 5800H 16G):
|
|
394
|
-
|
|
395
|
-
Benchmark | Iterations | Total time | Overhead
|
|
396
|
-
------------- | ---------- | ----------- | ----------
|
|
397
|
-
atoi_napi | 20000000 | 1.94s | (baseline)
|
|
398
|
-
atoi_koffi | 20000000 | 3.15s | x1.62
|
|
399
|
-
atoi_node_ffi | 20000000 | 640.49s | x242
|
|
400
|
-
|
|
401
|
-
## Raylib results
|
|
402
|
-
|
|
403
|
-
This benchmark uses the CPU-based image drawing functions in Raylib. The calls are much heavier than in the atoi benchmark, thus the FFI overhead is reduced. In this implemenetation, the baseline is a full C++ version of the code.
|
|
404
|
-
|
|
405
|
-
### Linux
|
|
406
|
-
|
|
407
|
-
The results below were measured on my x86_64 Linux machine (AMD® Ryzen™ 7 5800H 16G):
|
|
408
|
-
|
|
409
|
-
Benchmark | Iterations | Total time | Overhead
|
|
410
|
-
--------------- | ---------- | ----------- | ----------
|
|
411
|
-
raylib_cc | 100 | 4.14s | (baseline)
|
|
412
|
-
raylib_koffi | 100 | 6.25s | x1.51
|
|
413
|
-
raylib_node_ffi | 100 | 27.13s | x6.55
|
|
414
|
-
|
|
415
|
-
### Windows
|
|
416
|
-
|
|
417
|
-
The results below were measured on my x86_64 Windows machine (AMD® Ryzen™ 7 5800H 16G):
|
|
418
|
-
|
|
419
|
-
Benchmark | Iterations | Total time | Overhead
|
|
420
|
-
--------------- | ---------- | ----------- | ----------
|
|
421
|
-
raylib_cc | 100 | 8.39s | (baseline)
|
|
422
|
-
raylib_koffi | 100 | 11.51s | x1.37
|
|
423
|
-
raylib_node_ffi | 100 | 31.47s | x3.8
|
|
424
|
-
|
|
425
|
-
# Tests
|
|
426
|
-
|
|
427
|
-
Koffi is tested on multiple architectures using emulated (accelerated when possible) QEMU machines. First, you need to install qemu packages, such as `qemu-system` (or even `qemu-system-gui`) on Ubuntu.
|
|
428
|
-
|
|
429
|
-
These machines are not included directly in this repository (for license and size reasons), but they are available here: https://koromix.dev/files/machines/
|
|
430
|
-
|
|
431
|
-
For example, if you want to run the tests on Debian ARM64, run the following commands:
|
|
432
|
-
|
|
433
|
-
```sh
|
|
434
|
-
cd luigi/koffi/qemu/
|
|
435
|
-
wget -q -O- https://koromix.dev/files/machines/qemu_debian_arm64.tar.zst | zstd -d | tar xv
|
|
436
|
-
sha256sum -c --ignore-missing registry/sha256sum.txt
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
Note that the machine disk content may change each time the machine runs, so the checksum test will fail once a machine has been used at least once.
|
|
440
|
-
|
|
441
|
-
And now you can run the tests with:
|
|
442
|
-
|
|
443
|
-
```sh
|
|
444
|
-
node qemu.js # Several options are available, use --help
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
And be patient, this can be pretty slow for emulated machines. The Linux machines have and use ccache to build Koffi, so subsequent build steps will get much more tolerable.
|
|
448
|
-
|
|
449
|
-
By default, machines are started and stopped for each test. But you can start the machines ahead of time and run the tests multiple times instead:
|
|
450
|
-
|
|
451
|
-
```sh
|
|
452
|
-
node qemu.js start # Start the machines
|
|
453
|
-
node qemu.js # Test (without shutting down)
|
|
454
|
-
node qemu.js # Test again
|
|
455
|
-
node qemu.js stop # Stop everything
|
|
456
|
-
```
|
|
457
|
-
|
|
458
|
-
You can also restrict the test to a subset of machines:
|
|
459
|
-
|
|
460
|
-
```sh
|
|
461
|
-
# Full test cycle
|
|
462
|
-
node qemu.js test debian_x64 debian_i386
|
|
463
|
-
|
|
464
|
-
# Separate start, test, shutdown
|
|
465
|
-
node qemu.js start debian_x64 debian_i386
|
|
466
|
-
node qemu.js test debian_x64 debian_i386
|
|
467
|
-
node qemu.js stop
|
|
468
|
-
```
|
|
469
|
-
|
|
470
|
-
Finally, you can join a running machine with SSH with the following shortcut, if you need to do some debugging or any other manual procedure:
|
|
471
|
-
|
|
472
|
-
```sh
|
|
473
|
-
node qemu.js ssh debian_i386
|
|
474
|
-
```
|
|
475
|
-
|
|
476
|
-
Each machine is configured to run a VNC server available locally, which you can use to access the display, using KRDC or any other compatible viewer. Use the `info` command to get the VNC port.
|
|
477
|
-
|
|
478
|
-
```sh
|
|
479
|
-
node qemu.js info debian_x64
|
|
480
|
-
```
|
|
481
|
-
|
|
482
|
-
# Compilation
|
|
483
|
-
|
|
484
|
-
We provide prebuilt binaries, packaged in the NPM archive, so in most cases it should be as simple as `npm install koffi`. If you want to hack Koffi or use a specific platform, follow the instructions below.
|
|
485
|
-
|
|
486
|
-
## Windows
|
|
487
|
-
|
|
488
|
-
First, make sure the following dependencies are met:
|
|
489
|
-
|
|
490
|
-
* The "Desktop development with C++" workload from [Visual Studio 2022 or 2019](https://visualstudio.microsoft.com/downloads/) or the "C++ build tools" workload from the [Build Tools](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022), with the default optional components.
|
|
491
|
-
* [CMake meta build system](https://cmake.org/)
|
|
492
|
-
* [Node 16 LTS](https://nodejs.org/), but a newer version should work too
|
|
493
|
-
|
|
494
|
-
Once this is done, run this command from the project root:
|
|
495
|
-
|
|
496
|
-
```sh
|
|
497
|
-
npm install koffi
|
|
498
|
-
```
|
|
499
|
-
|
|
500
|
-
## Other platforms
|
|
22
|
+
[^3]: The prebuilt binary uses the LP64D (double-precision float) ABI. The LP64 ABI is supported in theory if you build Koffi from source (untested), the LP64F ABI is not supported.
|
|
501
23
|
|
|
502
|
-
|
|
24
|
+
Go to the web site for more information: https://koffi.dev/
|
|
503
25
|
|
|
504
|
-
|
|
505
|
-
* GNU Make 3.81 or newer
|
|
506
|
-
* [CMake meta build system](https://cmake.org/)
|
|
26
|
+
# License
|
|
507
27
|
|
|
508
|
-
|
|
28
|
+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
509
29
|
|
|
510
|
-
|
|
511
|
-
npm install koffi
|
|
512
|
-
```
|
|
30
|
+
Find more information here: https://www.gnu.org/licenses/
|
package/benchmark/CMakeLists.txt
CHANGED
|
@@ -36,15 +36,6 @@ endif()
|
|
|
36
36
|
|
|
37
37
|
# ---- atoi ----
|
|
38
38
|
|
|
39
|
-
add_executable(atoi_cc atoi_cc.cc ../vendor/libcc/libcc.cc)
|
|
40
|
-
target_include_directories(atoi_cc PRIVATE ..)
|
|
41
|
-
target_link_libraries(atoi_cc PRIVATE Threads::Threads)
|
|
42
|
-
|
|
43
|
-
if(WIN32)
|
|
44
|
-
target_compile_definitions(atoi_cc PRIVATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
|
|
45
|
-
target_link_libraries(atoi_cc PRIVATE ws2_32)
|
|
46
|
-
endif()
|
|
47
|
-
|
|
48
39
|
add_node_addon(NAME atoi_napi SOURCES atoi_napi.cc ../vendor/libcc/libcc.cc)
|
|
49
40
|
target_include_directories(atoi_napi PRIVATE .. ../vendor/node-addon-api)
|
|
50
41
|
target_link_libraries(atoi_napi PRIVATE Threads::Threads)
|
|
@@ -56,6 +47,19 @@ else()
|
|
|
56
47
|
target_link_libraries(atoi_napi PRIVATE dl)
|
|
57
48
|
endif()
|
|
58
49
|
|
|
50
|
+
# ---- rand ----
|
|
51
|
+
|
|
52
|
+
add_node_addon(NAME rand_napi SOURCES rand_napi.cc ../vendor/libcc/libcc.cc)
|
|
53
|
+
target_include_directories(rand_napi PRIVATE .. ../vendor/node-addon-api)
|
|
54
|
+
target_link_libraries(rand_napi PRIVATE Threads::Threads)
|
|
55
|
+
|
|
56
|
+
if(WIN32)
|
|
57
|
+
target_compile_definitions(rand_napi PRIVATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
|
|
58
|
+
target_link_libraries(rand_napi PRIVATE ws2_32)
|
|
59
|
+
else()
|
|
60
|
+
target_link_libraries(rand_napi PRIVATE dl)
|
|
61
|
+
endif()
|
|
62
|
+
|
|
59
63
|
# ---- Raylib ----
|
|
60
64
|
|
|
61
65
|
add_executable(raylib_cc raylib_cc.cc ../vendor/libcc/libcc.cc)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// This program is free software: you can redistribute it and/or modify
|
|
4
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
5
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
6
|
+
// (at your option) any later version.
|
|
7
|
+
//
|
|
8
|
+
// This program is distributed in the hope that it will be useful,
|
|
9
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
+
// GNU Affero General Public License for more details.
|
|
12
|
+
//
|
|
13
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
14
|
+
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
15
|
+
|
|
16
|
+
const r = require('raylib');
|
|
17
|
+
|
|
18
|
+
main();
|
|
19
|
+
|
|
20
|
+
function main() {
|
|
21
|
+
let iterations = 100;
|
|
22
|
+
|
|
23
|
+
if (process.argv.length >= 3) {
|
|
24
|
+
iterations = parseInt(process.argv[2], 10);
|
|
25
|
+
if (Number.isNaN(iterations))
|
|
26
|
+
throw new Error('Not a valid number');
|
|
27
|
+
if (iterations < 1)
|
|
28
|
+
throw new Error('Value must be positive');
|
|
29
|
+
}
|
|
30
|
+
console.log('Iterations:', iterations);
|
|
31
|
+
|
|
32
|
+
// We need to call InitWindow before using anything else (such as fonts)
|
|
33
|
+
r.SetTraceLogLevel(4); // Warnings
|
|
34
|
+
r.SetWindowState(0x80); // Hidden
|
|
35
|
+
r.InitWindow(640, 480, "Raylib Test");
|
|
36
|
+
|
|
37
|
+
let img = r.GenImageColor(800, 600, { r: 0, g: 0, b: 0, a: 255 });
|
|
38
|
+
let font = r.GetFontDefault();
|
|
39
|
+
|
|
40
|
+
let start = performance.now();
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < iterations; i++) {
|
|
43
|
+
r.ImageClearBackground(img, { r: 0, g: 0, b: 0, a: 255 });
|
|
44
|
+
|
|
45
|
+
for (let j = 0; j < 3600; j++) {
|
|
46
|
+
let text = 'Hello World!';
|
|
47
|
+
let text_width = r.MeasureTextEx(font, text, 10, 1).x;
|
|
48
|
+
|
|
49
|
+
let angle = (j * 7) * Math.PI / 180;
|
|
50
|
+
let color = {
|
|
51
|
+
r: 127.5 + 127.5 * Math.sin(angle),
|
|
52
|
+
g: 127.5 + 127.5 * Math.sin(angle + Math.PI / 2),
|
|
53
|
+
b: 127.5 + 127.5 * Math.sin(angle + Math.PI),
|
|
54
|
+
a: 255
|
|
55
|
+
};
|
|
56
|
+
let pos = {
|
|
57
|
+
x: (img.width / 2 - text_width / 2) + j * 0.1 * Math.cos(angle - Math.PI / 2),
|
|
58
|
+
y: (img.height / 2 - 16) + j * 0.1 * Math.sin(angle - Math.PI / 2)
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
r.ImageDrawTextEx(img, font, text, pos, 10, 1, color);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let time = performance.now()- start;
|
|
66
|
+
console.log('Time:', (time / 1000.0).toFixed(2) + 's');
|
|
67
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/doc/Makefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Minimal makefile for Sphinx documentation
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
# You can set these variables from the command line, and also
|
|
5
|
+
# from the environment for the first two.
|
|
6
|
+
SPHINXOPTS ?=
|
|
7
|
+
SPHINXBUILD ?= sphinx-build
|
|
8
|
+
SOURCEDIR = .
|
|
9
|
+
BUILDDIR = dist
|
|
10
|
+
|
|
11
|
+
# Put it first so that "make" without argument is like "make help".
|
|
12
|
+
help:
|
|
13
|
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
14
|
+
|
|
15
|
+
.PHONY: help Makefile
|
|
16
|
+
|
|
17
|
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
|
18
|
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
|
19
|
+
%: Makefile
|
|
20
|
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
Binary file
|
|
Binary file
|