koffi 1.1.5 → 1.2.0-alpha.3

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 CHANGED
@@ -22,8 +22,9 @@ Koffi is a fast and easy-to-use C FFI module for Node.js, with support for primi
22
22
 
23
23
  The following features are planned in the near future:
24
24
 
25
- * 1.2: C to JS callbacks
26
- * 1.3: Type parser
25
+ * 1.2: C to JS callbacks (⚠️ partially working in master branch)
26
+ * 1.3: Real-world examples, optimize passing of structs and arrays
27
+ * 1.4: Type parser, unions
27
28
 
28
29
  The following combinations of OS and architectures __are officially supported and tested__ at the moment:
29
30
 
@@ -294,7 +295,50 @@ Variadic functions do not support async.
294
295
 
295
296
  ## Callbacks
296
297
 
297
- Koffi does not yet support passing JS functions as callbacks. This is planned for version 1.2.
298
+ ⚠️ Support for callbacks **is in development, don't expect it to run reliably**.
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
+ ```
298
342
 
299
343
  # Benchmarks
300
344
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "1.1.5",
3
+ "version": "1.2.0-alpha.3",
4
4
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
5
5
  "keywords": [
6
6
  "foreign",
@@ -19,8 +19,7 @@
19
19
  "main": "src/index.js",
20
20
  "scripts": {
21
21
  "install": "cnoke --prebuild build/qemu/{{version}}/koffi_{{platform}}_{{arch}}.tar.gz",
22
- "test": "node qemu/qemu.js",
23
- "prepublishOnly": "node qemu/qemu.js pack"
22
+ "test": "node qemu/qemu.js"
24
23
  },
25
24
  "license": "AGPL-3.0",
26
25
  "dependencies": {
@@ -38,7 +38,8 @@
38
38
  "Test Sync": "node test/sync.js",
39
39
  "Test Async": "node test/async.js",
40
40
  "Test Raylib": "xvfb-run node test/raylib.js",
41
- "Test SQLite": "node test/sqlite.js"
41
+ "Test SQLite": "node test/sqlite.js",
42
+ "Test Callbacks": "node test/callbacks.js"
42
43
  }
43
44
  }
44
45
  }
@@ -83,7 +84,8 @@
83
84
  "Test Sync": "node test/sync.js",
84
85
  "Test Async": "node test/async.js",
85
86
  "Test Raylib": "xvfb-run node test/raylib.js",
86
- "Test SQLite": "node test/sqlite.js"
87
+ "Test SQLite": "node test/sqlite.js",
88
+ "Test Callbacks": "node test/callbacks.js"
87
89
  }
88
90
  }
89
91
  }
@@ -128,7 +130,8 @@
128
130
  "Test Sync": "node test/sync.js",
129
131
  "Test Async": "node test/async.js",
130
132
  "Test Raylib": "xvfb-run node test/raylib.js",
131
- "Test SQLite": "node test/sqlite.js"
133
+ "Test SQLite": "node test/sqlite.js",
134
+ "Test Callbacks": "node test/callbacks.js"
132
135
  }
133
136
  }
134
137
  }
@@ -173,7 +176,8 @@
173
176
  "Test Sync": "node test/sync.js",
174
177
  "Test Async": "node test/async.js",
175
178
  "Test Raylib": "xvfb-run node test/raylib.js",
176
- "Test SQLite": "node test/sqlite.js"
179
+ "Test SQLite": "node test/sqlite.js",
180
+ "Test Callbacks": "node test/callbacks.js"
177
181
  }
178
182
  }
179
183
  }
@@ -225,7 +229,8 @@
225
229
  "Test Sync": "C:\\Node32\\node32.cmd node test/sync.js",
226
230
  "Test Async": "C:\\Node32\\node32.cmd node test/async.js",
227
231
  "Test Raylib": "seatsh C:\\Node32\\node32.cmd node test/raylib.js",
228
- "Test SQLite": "C:\\Node32\\node32.cmd node test/sqlite.js"
232
+ "Test SQLite": "C:\\Node32\\node32.cmd node test/sqlite.js",
233
+ "Test Callbacks": "C:\\Node32\\node32.cmd node test/callbacks.js"
229
234
  }
230
235
  },
231
236
 
@@ -238,7 +243,8 @@
238
243
  "Test Sync": "C:\\Node64\\node64.cmd node test/sync.js",
239
244
  "Test Async": "C:\\Node64\\node64.cmd node test/async.js",
240
245
  "Test Raylib": "seatsh C:\\Node64\\node64.cmd node test/raylib.js",
241
- "Test SQLite": "C:\\Node64\\node64.cmd node test/sqlite.js"
246
+ "Test SQLite": "C:\\Node64\\node64.cmd node test/sqlite.js",
247
+ "Test Callbacks": "C:\\Node64\\node64.cmd node test/callbacks.js"
242
248
  }
243
249
  }
244
250
  }
@@ -283,7 +289,8 @@
283
289
  "Test Sync": "node test/sync.js",
284
290
  "Test Async": "node test/async.js",
285
291
  "Test Raylib": "xvfb-run node test/raylib.js",
286
- "Test SQLite": "node test/sqlite.js"
292
+ "Test SQLite": "node test/sqlite.js",
293
+ "Test Callbacks": "node test/callbacks.js"
287
294
  }
288
295
  }
289
296
  }
@@ -328,7 +335,8 @@
328
335
  "Test Sync": "node test/sync.js",
329
336
  "Test Async": "node test/async.js",
330
337
  "Test Raylib": "xvfb-run node test/raylib.js",
331
- "Test SQLite": "node test/sqlite.js"
338
+ "Test SQLite": "node test/sqlite.js",
339
+ "Test Callbacks": "node test/callbacks.js"
332
340
  }
333
341
  }
334
342
  }
@@ -373,7 +381,8 @@
373
381
  "Test Sync": "node test/sync.js",
374
382
  "Test Async": "node test/async.js",
375
383
  "Test Raylib": "xvfb-run node test/raylib.js",
376
- "Test SQLite": "node test/sqlite.js"
384
+ "Test SQLite": "node test/sqlite.js",
385
+ "Test Callbacks": "node test/callbacks.js"
377
386
  }
378
387
  }
379
388
  }
@@ -417,7 +426,8 @@
417
426
  "commands": {
418
427
  "Test Sync": "PATH=/usr/local/bin:/usr/bin:/bin node test/sync.js",
419
428
  "Test Async": "PATH=/usr/local/bin:/usr/bin:/bin node test/async.js",
420
- "Test SQLite": "PATH=/usr/local/bin:/usr/bin:/bin node test/sqlite.js"
429
+ "Test SQLite": "PATH=/usr/local/bin:/usr/bin:/bin node test/sqlite.js",
430
+ "Test Callbacks": "PATH=/usr/local/bin:/usr/bin:/bin node test/callbacks.js"
421
431
  }
422
432
  }
423
433
  }
@@ -462,7 +472,8 @@
462
472
  "Test Sync": "node test/sync.js",
463
473
  "Test Async": "node test/async.js",
464
474
  "Test Raylib": "xvfb-run node test/raylib.js",
465
- "Test SQLite": "node test/sqlite.js"
475
+ "Test SQLite": "node test/sqlite.js",
476
+ "Test Callbacks": "node test/callbacks.js"
466
477
  }
467
478
  }
468
479
  }
@@ -507,7 +518,8 @@
507
518
  "Test Sync": "node test/sync.js",
508
519
  "Test Async": "node test/async.js",
509
520
  "Test Raylib": "xvfb-run node test/raylib.js",
510
- "Test SQLite": "node test/sqlite.js"
521
+ "Test SQLite": "node test/sqlite.js",
522
+ "Test Callbacks": "node test/callbacks.js"
511
523
  }
512
524
  }
513
525
  }
@@ -552,7 +564,8 @@
552
564
  "Test Sync": "node test/sync.js",
553
565
  "Test Async": "node test/async.js",
554
566
  "Test Raylib": "xvfb-run node test/raylib.js",
555
- "Test SQLite": "node test/sqlite.js"
567
+ "Test SQLite": "node test/sqlite.js",
568
+ "Test Callbacks": "node test/callbacks.js"
556
569
  }
557
570
  }
558
571
  }