@@ -6,6 +6,8 @@ Foreign Function Interface helper. Provides a friendly abstraction/API for:
66- [ ffi-napi] ( https://www.npmjs.com/package/ffi-napi ) (MIT)
77- [ koffi] ( https://www.npmjs.com/package/koffi ) (MIT)
88
9+ Syntax is inspired by Deno FFI. The goal was to be able to easily switch from ` ffi-napi ` to ` koffi ` or vice versa.
10+
911📦 Scoped ` @xan105 ` packages are for my own personal use but feel free to use them.
1012
1113Example
@@ -14,9 +16,7 @@ Example
1416Loading a library with Deno like syntax
1517
1618``` js
17- import { dlopen } from " @xan105/ffi/napi" ;
18- // OR
19- import { dlopen } from " @xan105/ffi/koffi" ;
19+ import { dlopen } from " @xan105/ffi/[ napi | koffi ]" ;
2020
2121const lib = dlopen (" libm" , {
2222 ceil: {
@@ -27,23 +27,36 @@ const lib = dlopen("libm", {
2727lib .ceil (1.5 ); // 2
2828```
2929
30- Calling from a library
30+ Calling directly from a library
3131
3232``` js
33- import { load , types } from " @xan105/ffi/napi" ;
34- // OR
35- import { load , types } from " @xan105/ffi/koffi" ;
33+ import { load , types } from " @xan105/ffi/[ napi | koffi ]" ;
3634
3735const call = load (" user32.dll" , { abi: " stdcall" });
38- const MessageBoxA = call (" MessageBoxA" , " int" , [" void *" , types .LPCSTR , types .LPCSTR , " uint" ]);
36+ const MessageBoxA = call (" MessageBoxA" , " int" , [" void *" , types .win32 . LPCSTR , types . win32 .LPCSTR , " uint" ]);
3937
4038const MB_ICONINFORMATION = 0x40 ;
4139MessageBoxA (null , " Hello World!" , " Message" , MB_ICONINFORMATION );
4240```
4341
44- Callback with Deno like syntax
42+ Async
4543
44+ ``` js
45+ import { dlopen } from " @xan105/ffi/[ napi | koffi ]" ;
46+
47+ const lib = dlopen (" libm" , {
48+ ceil: {
49+ result: " double" ,
50+ parameters: [ " double" ],
51+ nonblocking: true
52+ }
53+ });
54+ await lib .ceil (1.5 ); // 2
4655```
56+
57+ Callback with Deno like syntax
58+
59+ ``` js
4760import { dlopen , Callback } from " @xan105/ffi/koffi" ;
4861
4962const library = dlopen (
@@ -132,7 +145,7 @@ See the corresponding FFI library for more information on what to pass for `resu
132145** Example** :
133146
134147``` js
135- import { load } from " @xan105/ffi/... " ;
148+ import { load } from " @xan105/ffi/[ napi | koffi ] " ;
136149const call = load (" libm" );
137150
138151const ceil = call (" ceil" , " double" , [" double" ])
@@ -184,8 +197,8 @@ If you ever use ffi-napi `ffi.Library()` this will be familiar.
184197** Example**
185198
186199``` js
187- import { dlopen , types } from " @xan105/ffi/... " ;
188- const { BOOL } = types;
200+ import { dlopen , types } from " @xan105/ffi/[ napi | koffi ] " ;
201+ const { BOOL } = types . win32 ;
189202
190203const lib = dlopen (" xinput1_4" , {
191204 " XInputEnable" : {
@@ -280,7 +293,7 @@ This is a class wrapper to the FFI library's callback function(s) inspired by De
280293##### Example
281294
282295``` js
283- import { dlopen , types , Callback } from " @xan105/ffi/... " ;
296+ import { dlopen , types , Callback } from " @xan105/ffi/[ napi | koffi ] " ;
284297
285298const library = dlopen (" ./callback.so" , {
286299 setCallback: {
@@ -308,7 +321,7 @@ callback.close();
308321You can also register the callback at a later time:
309322
310323``` js
311- import { dlopen , Callback } from " @xan105/ffi/... " ;
324+ import { dlopen , Callback } from " @xan105/ffi/[ napi | koffi ] " ;
312325
313326const callback = new Callback (
314327 { parameters: [], result: " void" }
@@ -343,7 +356,10 @@ Just a shorthand to `ref.refType(x)` (ffi-napi) and `koffi.out/inout(koffi.point
343356Allocate a buffer and get the corresponding data when passing a pointer to allow the called function to manipulate memory.
344357
345358``` js
346- const number = alloc (" int" ); // allocate Buffer for the output data
359+ import { dlopen , alloc } from " @xan105/ffi/[ napi | koffi ]" ;
360+ const dylib = dlopen (... ); // lib loading
361+
362+ const number = alloc (" int" ); // allocate Buffer for the output data
347363dylib .manipulate_number (number .pointer );
348364const result = number .get ();
349365```
0 commit comments