Skip to content

Commit 8d1ed1e

Browse files
committed
Refactor FFI bindings in integration_tests/ffi_bindings.ts for improved type safety
- Updated the SerialLib type to utilize Deno.DynamicLibrary for better type inference and clarity. - Consolidated symbol definitions into a single object, enhancing organization and maintainability. - Adjusted the loadSerialLib function to reflect the new type structure, ensuring consistency across the codebase.
1 parent e288874 commit 8d1ed1e

1 file changed

Lines changed: 21 additions & 62 deletions

File tree

integration_tests/ffi_bindings.ts

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,26 @@
33
* used by the Deno integration tests.
44
*/
55

6-
// FFI symbol definitions required by the tests
7-
export type SerialLib = {
8-
readonly serialOpen: (
9-
port: Deno.PointerValue,
10-
baudrate: number,
11-
dataBits: number,
12-
parity: number,
13-
stopBits: number,
14-
errorCallback: Deno.PointerValue,
15-
) => bigint;
6+
export type LoadedLibrary = Deno.DynamicLibrary<typeof symbols>;
7+
export type SerialLib = LoadedLibrary["symbols"];
168

17-
readonly serialClose: (
18-
handle: bigint,
19-
errorCallback: Deno.PointerValue,
20-
) => number;
21-
22-
readonly serialRead: (
23-
handle: bigint,
24-
buffer: Deno.PointerValue,
25-
bufferSize: number,
26-
timeoutMs: number,
27-
multiplier: number,
28-
errorCallback: Deno.PointerValue,
29-
) => number;
30-
31-
readonly serialWrite: (
32-
handle: bigint,
33-
buffer: Deno.PointerValue,
34-
bufferSize: number,
35-
timeoutMs: number,
36-
multiplier: number,
37-
errorCallback: Deno.PointerValue,
38-
) => number;
39-
};
40-
41-
// Library object type
42-
export type LoadedLibrary = {
43-
symbols: SerialLib;
44-
close: () => void;
9+
const symbols = {
10+
serialOpen: {
11+
parameters: ["pointer", "i32", "i32", "i32", "i32", "pointer"] as const,
12+
result: "i64" as const,
13+
},
14+
serialClose: {
15+
parameters: ["i64", "pointer"] as const,
16+
result: "i32" as const,
17+
},
18+
serialRead: {
19+
parameters: ["i64", "pointer", "i32", "i32", "i32", "pointer"] as const,
20+
result: "i32" as const,
21+
},
22+
serialWrite: {
23+
parameters: ["i64", "pointer", "i32", "i32", "i32", "pointer"] as const,
24+
result: "i32" as const,
25+
},
4526
};
4627

4728
/**
@@ -66,34 +47,12 @@ export async function loadSerialLib(
6647
"/usr/local/lib/libcpp_bindings_linux.so",
6748
].filter((p): p is string => p !== undefined);
6849

69-
let lib: { symbols: SerialLib; close: () => void } | null = null;
50+
let lib: LoadedLibrary | null = null;
7051
let lastError: Error | null = null;
7152

72-
const symbols = {
73-
serialOpen: {
74-
parameters: ["pointer", "i32", "i32", "i32", "i32", "pointer"] as const,
75-
result: "i64" as const,
76-
},
77-
serialClose: {
78-
parameters: ["i64", "pointer"] as const,
79-
result: "i32" as const,
80-
},
81-
serialRead: {
82-
parameters: ["i64", "pointer", "i32", "i32", "i32", "pointer"] as const,
83-
result: "i32" as const,
84-
},
85-
serialWrite: {
86-
parameters: ["i64", "pointer", "i32", "i32", "i32", "pointer"] as const,
87-
result: "i32" as const,
88-
},
89-
};
90-
9153
for (const path of possiblePaths) {
9254
try {
93-
const loaded = Deno.dlopen(path, symbols) as {
94-
symbols: SerialLib;
95-
close: () => void;
96-
};
55+
const loaded = Deno.dlopen(path, symbols) as LoadedLibrary;
9756
lib = loaded;
9857
break;
9958
} catch (error) {

0 commit comments

Comments
 (0)