Skip to content

Commit e84df05

Browse files
committed
reorganize types
1 parent 474e3c5 commit e84df05

11 files changed

Lines changed: 173 additions & 91 deletions

File tree

README.md

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { dlopen } from "@xan105/ffi/napi";
1919
import { dlopen } from "@xan105/ffi/koffi";
2020

2121
const lib = dlopen("libm", {
22-
"ceil": {
22+
ceil: {
2323
result: "double",
2424
parameters: [ "double" ]
2525
}
@@ -41,6 +41,37 @@ const MB_ICONINFORMATION = 0x40;
4141
MessageBoxA(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+
4475
Install
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
182215
import { load } from "@xan105/ffi/koffi";
183216
const call = load("user32.dll", { abi: "stdcall" });
184217
const 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

189248
Create 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

225285
const 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+
236301
library.setCallback(callback.pointer);
237302
library.doSomething();
238303

@@ -243,6 +308,8 @@ callback.close();
243308
You can also register the callback at a later time:
244309

245310
```js
311+
import { dlopen, Callback } from "@xan105/ffi/...";
312+
246313
const callback = new Callback(
247314
{ parameters: [], result: "void" }
248315
);

lib/ffi-napi/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ found in the LICENSE file in the root directory of this source tree.
55
*/
66

77
import ref from "ref-napi";
8-
import * as windows from "./types/windows.js";
9-
import * as rust from "./types/rust.js";
8+
import * as win32 from "./types/windows.js";
9+
import * as deno from "./types/deno.js";
1010

1111
export * from "./open.js";
1212
export * from "./helper.js";
13-
export const types = {
13+
export const types = Object.assign(Object.create(null), {
14+
pointer: ref.refType(ref.types.void),
1415
...ref.types,
15-
...windows,
16-
...rust
17-
};
16+
...deno.types,
17+
win32
18+
});

lib/ffi-napi/types/deno.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright (c) Anthony Beaumont
3+
This source code is licensed under the MIT License
4+
found in the LICENSE file in the root directory of this source tree.
5+
*/
6+
7+
import ref from "ref-napi";
8+
9+
export const types = Object.assign(Object.create(null), {
10+
i8 : ref.types.int8,
11+
u8 : ref.types.uint8,
12+
i16 : ref.types.int16,
13+
u16 : ref.types.uint16,
14+
i32 : ref.types.int32,
15+
u32 : ref.types.uint32,
16+
i64 : ref.types.int64,
17+
u64 : ref.types.uint64,
18+
usize : ref.types.size_t,
19+
f32 : ref.types.float,
20+
f64 : ref.types.double,
21+
"function" : ref.refType(ref.types.void)
22+
});

lib/ffi-napi/types/rust.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/ffi-napi/types/windows.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const ULONG_PTR = _WIN64 ? ref.refType(ref.types.uint64) : ref.refType(re
3333

3434
export const BOOL = ref.types.int;
3535
export const BOOLEAN = ref.types.bool;
36-
export const CALLBACK = ref.types.pointer;
36+
export const CALLBACK = ref.refType(ref.types.void);
3737
export const CCHAR = ref.types.uint8;
3838
export const CHAR = ref.types.uint8;
3939
export const COLORREF = DWORD;
@@ -107,8 +107,8 @@ export const LPDWORD = ref.refType(ref.types.uint16);
107107
export const LPHANDLE = _WIN64 ? ref.refType(ref.types.int64) : ref.refType(ref.types.int32);
108108
export const LPINT = ref.refType(ref.types.int);
109109
export const LPLONG = ref.refType(ref.types.int32);
110-
export const LPMSG = ref.types.pointer;
111-
export const LPPOINT = ref.types.pointer;
110+
export const LPMSG = ref.refType(ref.types.void);
111+
export const LPPOINT = ref.refType(ref.types.void);
112112
export const LPSTR = ref.refType(ref.types.char);
113113
export const LPWSTR = ref.refType(ref.types.uint16);
114114
export const LPTSTR = ref.refType(ref.types.uint16);
@@ -127,7 +127,7 @@ export const PDWORD_PTR = DWORD_PTR;
127127
export const PDWORD32 = ref.refType(ref.types.uint32);
128128
export const PDWORD64 = ref.refType(ref.types.uint64);
129129
export const PFLOAT = ref.refType(ref.types.float);
130-
export const PHALF_PTR = ref.types.pointer;
130+
export const PHALF_PTR = ref.refType(ref.types.void);
131131
export const PHANDLE = _WIN64 ? ref.refType(ref.refType(ref.types.uint64)) : ref.refType(ref.refType(ref.types.uint32));
132132
export const PHKEY = _WIN64 ? ref.refType(ref.refType(ref.types.uint64)) : ref.refType(ref.refType(ref.types.uint32));
133133
export const PINT = ref.refType(ref.types.int);
@@ -139,22 +139,22 @@ export const PINT64 = ref.refType(ref.types.int64);
139139
export const PLCID = ref.refType(ref.types.uint32);
140140
export const PLONG = ref.refType(ref.types.long);
141141
export const PLONGLONG = ref.refType(ref.types.int64);
142-
export const PLONG_PTR = ref.types.pointer;
142+
export const PLONG_PTR = ref.refType(ref.types.void);
143143
export const PLONG32 = ref.refType(ref.types.int32);
144144
export const PLONG64 = ref.refType(ref.types.int64);
145145
export const POINTER_32 = ref.refType(ref.types.int32);
146146
export const POINTER_64 = _WIN64 ? ref.refType(ref.types.int64) : ref.refType(ref.types.int32);
147-
export const POINTER_SIGNED = ref.types.pointer;
148-
export const POINTER_UNSIGNED = ref.types.pointer;
147+
export const POINTER_SIGNED = ref.refType(ref.types.void);
148+
export const POINTER_UNSIGNED = ref.refType(ref.types.void);
149149
export const PSHORT = ref.refType(ref.types.int16);
150150
export const PSIZE_T = ULONG_PTR;
151-
export const PSSIZE_T = ref.types.pointer;
151+
export const PSSIZE_T = ref.refType(ref.types.void);
152152
export const PSTR = ref.refType(ref.types.char);
153153
export const PTBYTE = ref.refType(ref.types.int16);
154154
export const PTCHAR = ref.refType(ref.types.uint16);
155155
export const PTSTR = ref.refType(ref.types.uint16);
156-
export const PUCHAR = ref.types.pointer;
157-
export const PUHALF_PTR = ref.types.pointer;
156+
export const PUCHAR = ref.refType(ref.types.void);
157+
export const PUHALF_PTR = ref.refType(ref.types.void);
158158
export const PUINT = ref.refType(ref.types.uint);
159159
export const PUINT_PTR = ref.refType(ref.refType(ref.types.uint));
160160
export const PUINT8 = ref.refType(ref.types.uint8);

lib/koffi/index.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@ found in the LICENSE file in the root directory of this source tree.
55
*/
66

77
import koffi from "koffi";
8-
import * as windows from "./types/windows.js";
9-
import * as rust from "./types/rust.js";
8+
import * as win32 from "./types/windows.js";
9+
import * as deno from "./types/deno.js";
1010
import { isWindows } from "@xan105/is";
1111
import { shouldObj } from "@xan105/is/assert";
1212

13-
function setAlias(list){
14-
shouldObj(list);
15-
for (const [name, type] of Object.entries(list))
13+
const setAlias = function(types){
14+
shouldObj(types);
15+
for (const [name, type] of Object.entries(types))
1616
if (koffi.types[name] === undefined)
1717
koffi.alias(name, type);
18-
}
18+
};
1919

20-
if (isWindows()) setAlias(windows);
21-
setAlias(rust);
20+
if (isWindows()) setAlias(win32);
21+
setAlias({
22+
pointer: koffi.pointer(koffi.types.void),
23+
...deno.types
24+
});
2225

2326
export * from "./open.js";
2427
export * from "./helper.js";
25-
export const types = {
28+
export const types = Object.assign(Object.create(null), {
29+
pointer: koffi.pointer(koffi.types.void),
2630
...koffi.types,
27-
...windows,
28-
...rust
29-
};
31+
...deno.types,
32+
win32
33+
});

lib/koffi/types/deno.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright (c) Anthony Beaumont
3+
This source code is licensed under the MIT License
4+
found in the LICENSE file in the root directory of this source tree.
5+
*/
6+
7+
import koffi from "koffi";
8+
9+
export const types = Object.assign(Object.create(null), {
10+
i8 : koffi.types.int8,
11+
u8 : koffi.types.uint8,
12+
i16 : koffi.types.int16,
13+
u16 : koffi.types.uint16,
14+
i32 : koffi.types.int32,
15+
u32 : koffi.types.uint32,
16+
i64 : koffi.types.int64,
17+
u64 : koffi.types.uint64,
18+
usize : koffi.types.size_t,
19+
f32 : koffi.types.float,
20+
f64 : koffi.types.double,
21+
"function" : koffi.pointer(koffi.types.void)
22+
});

lib/koffi/types/rust.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)