-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use the native ffi bindings for the Verifier instead of the rub…
…y bindings
- Loading branch information
1 parent
e5c7b1a
commit 1aea16f
Showing
18 changed files
with
745 additions
and
357 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// https://github.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial | ||
import ffi = require('ffi-napi'); | ||
|
||
// I am so so sorry about these types. They exist | ||
// to infer the returned type of the library | ||
// using the object that we pass in to describe the functions | ||
type AsyncFfiCall<Args extends unknown[], ReturnType> = { | ||
async: ( | ||
...args: [...Args, (err: Error, ret: ReturnType) => void] | ||
) => ReturnType; | ||
}; | ||
|
||
type FfiFunction<T> = T extends (...a: infer Args) => infer ReturnType | ||
? T & AsyncFfiCall<Args, ReturnType> | ||
: never; | ||
|
||
type StringType = 'string' | 'void' | 'int' | 'double' | 'float'; | ||
|
||
type ActualType<T> = [T] extends ['string'] | ||
? string | ||
: [T] extends ['void'] | ||
? void | ||
: [T] extends ['int' | 'double' | 'float'] | ||
? number | ||
: never; | ||
|
||
type ArrayActualType<Tuple extends [...Array<unknown>]> = { | ||
[Index in keyof Tuple]: ActualType<Tuple[Index]>; | ||
} & { length: Tuple['length'] }; | ||
|
||
type TupleType = [StringType, Array<StringType>]; | ||
|
||
type FunctionFromArray<A extends TupleType> = A extends [ | ||
r: infer ReturnTypeString, | ||
args: [...infer ArgArrayType] | ||
] | ||
? (...args: ArrayActualType<ArgArrayType>) => ActualType<ReturnTypeString> | ||
: never; | ||
|
||
type LibDescription<Functions extends string> = { | ||
[k in Functions]: [StringType, Array<StringType>]; | ||
}; | ||
|
||
type FfiBinding<T> = T extends LibDescription<string> | ||
? { | ||
[Key in keyof T]: FfiFunction<FunctionFromArray<T[Key]>>; | ||
} | ||
: never; | ||
|
||
// This function exists to wrap the untyped ffi lib, and return | ||
// the typed version | ||
export const initialiseFfi = <T>( | ||
binaryPath: string, | ||
description: T | ||
): FfiBinding<T> => | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ffi.Library(binaryPath, description as { [k: string]: any }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.