@@ -19,7 +19,7 @@ import { dlopen } from "@xan105/ffi/napi";
1919import { dlopen } from " @xan105/ffi/koffi" ;
2020
2121const lib = dlopen (" libm" , {
22- " ceil" : {
22+ ceil: {
2323 result: " double" ,
2424 parameters: [ " double" ]
2525 }
@@ -41,6 +41,37 @@ const MB_ICONINFORMATION = 0x40;
4141MessageBoxA (null , " Hello World!" , " Message" , MB_ICONINFORMATION );
4242```
4343
44+ Callback with Deno like syntax
45+
46+ ```
47+ import { dlopen, Callback} from "@xan105/ffi/koffi";
48+
49+ const library = dlopen(
50+ "./callback.so",
51+ {
52+ set_status_callback: {
53+ parameters: ["function"],
54+ result: "void"
55+ },
56+ start_long_operation: {
57+ parameters: [],
58+ result: "void"
59+ }
60+ }
61+ );
62+
63+ const callback = new Callback(
64+ {
65+ parameters: ["u8"],
66+ result: "void",
67+ },
68+ (success) => {}
69+ );
70+
71+ library.set_status_callback(callback.pointer);
72+ library.start_long_operation();
73+ ```
74+
4475Install
4576=======
4677
@@ -168,22 +199,50 @@ await lib.XInputEnable(1);
168199
169200#### ` const types: object `
170201
171- The FFI Library's primitive types as well as corresponding alias such as Windows specific types (DWORD,...) are exposed for convenience.
202+ The FFI Library's primitive types as well as corresponding alias are exposed for convenience.
203+ Such as Deno types (rust) and Windows specific types (DWORD,...).
204+
205+ 💡 Windows specific types are grouped together under ` win32 ` .
172206
173207``` js
174- import { types } from " @xan105/ffi/napi" ;
175- // or
176- import { types } from " @xan105/ffi/koffi" ;
208+ import { types } from " @xan105/ffi/[ napi | koffi ]" ;
209+ const { DWORD , LPCSTR } = types .win32 ;
177210```
178211
179- When using ` koffi ` alias are also set with ` koffi.alias() ` .
212+ 💡 When using ` koffi ` alias are also set with ` koffi.alias() ` so you can use them as string .
180213
181214``` js
182215import { load } from " @xan105/ffi/koffi" ;
183216const call = load (" user32.dll" , { abi: " stdcall" });
184217const MessageBoxA = call (" MessageBoxA" , " int" , [" void *" , " LPCSTR" , " LPCSTR" , " uint" ]);
185218```
186219
220+ ⚠️ Types are not exposed under their own namespace because some words are illegal or already in use in JavaScript.
221+ You can still use destructuring if needed as long as the name is "allowed".
222+
223+ ❌ No
224+
225+ ```
226+ import { i32 } from "@xan105/ffi/koffi/types"
227+ ```
228+
229+ ✔️ Yes
230+ ```
231+ import { types } from "@xan105/ffi/koffi"
232+ const { i32 } = types;
233+ ```
234+
235+ 🚫 Forbidden
236+
237+ ```
238+ import { function } from "@xan105/ffi/napi/types"
239+ ```
240+
241+ ```
242+ import { types } from "@xan105/ffi/napi"
243+ const { function } = types;
244+ ```
245+
187246#### ` class Callback `
188247
189248Create a callback to be called at a later time (registered callback).
@@ -200,6 +259,10 @@ This is a class wrapper to the FFI library's callback function(s) inspired by De
200259
201260 The pointer to the callback.
202261
262+ - ` address: number | BigInt | null `
263+
264+ The memory address of the pointer.
265+
203266 - ` type: unknown `
204267
205268 The type of the callback.
@@ -217,14 +280,11 @@ This is a class wrapper to the FFI library's callback function(s) inspired by De
217280##### Example
218281
219282``` js
220- const callback = new Callback (
221- { parameters: [], result: " void" },
222- () => {},
223- );
283+ import { dlopen , types , Callback } from " @xan105/ffi/..." ;
224284
225285const library = dlopen (" ./callback.so" , {
226286 setCallback: {
227- parameters: [callback . type ],
287+ parameters: [types . function ],
228288 result: " void" ,
229289 },
230290 doSomething (): {
@@ -233,6 +293,11 @@ const library = dlopen("./callback.so", {
233293 },
234294});
235295
296+ const callback = new Callback (
297+ { parameters: [], result: " void" },
298+ () => {},
299+ );
300+
236301library .setCallback (callback .pointer );
237302library .doSomething ();
238303
@@ -243,6 +308,8 @@ callback.close();
243308You can also register the callback at a later time:
244309
245310``` js
311+ import { dlopen , Callback } from " @xan105/ffi/..." ;
312+
246313const callback = new Callback (
247314 { parameters: [], result: " void" }
248315);
0 commit comments