chore(types): updated ffi types

This commit is contained in:
Erica Marigold 2024-10-16 12:34:59 +01:00 committed by GitHub
parent 95258e1b51
commit f27bed578f
Signed by: DevComp
GPG key ID: B5690EEEBB952194

View file

@ -1,79 +1,140 @@
--[=[ export type CType<T> = {
@interface Box size: number,
@within FFI signedness: boolean,
Box is an untyped, sized memory area that Lua can manage. ptr: <K>(self: CType<T>) -> CPtr<K>,
This area is safe within Lua. Operations have their boundaries checked. box: <K>(self: CType<T>, val: K) -> Box,
from: <K>(self: CType<T>, ud: any, offset: number?) -> K,
into: <K>(self: CType<T>, ud: any, value: K, offset: number?) -> (),
arr: <K>(self: CType<T>, len: number) -> CArr<K>,
-- FIXME: intoType is of type `CTypes`, but that leads to recursive types
cast: <F, I>(self: CType<T>, intoType: any, from: F, into: I) -> ()
} & { ["__phantom"]: T }
You can passing box as raw arguments or as pointer to outside. export type CPtr<T> = {
It also helps you handle data that Lua cannot handle. or you can reuse box to save cost from convertsion. size: number,
Depending on the type, operations such as sum, mul, and mod may be implemented. See Types inner: T?,
}
```lua export type CArr<T> = {
ffi.box(size) size: number,
``` length: number,
This is a dictionary that will contain the following values: inner: {T}?,
* `readOnly` - If the target path is read-only or not offset: (self: CArr<T>, offset: number) -> number,
]=] ptr: <K>(self: CArr<T>) -> CPtr<K>,
box: <K>(self: CArr<T>, table: { K }) -> Box,
from: <K>(self: CArr<T>, ud: any, offset: number?) -> { K },
into: <K>(self: CArr<T>, ud: any, value: { K }, offset: number?) -> (),
}
-- Numeric types --
export type u8 = CType<"u8">
export type u16 = CType<"u16">
export type u32 = CType<"u32">
export type u64 = CType<"u64">
export type u128 = CType<"u128">
export type i8 = CType<"i8">
export type i16 = CType<"i16">
export type i32 = CType<"i32">
export type i64 = CType<"i64">
export type i128 = CType<"i128">
export type f32 = CType<"f32">
export type f64 = CType<"f64">
export type usize = CType<"usize">
export type isize = CType<"isize">
-- C types --
export type char = CType<"char">
export type float = CType<"float">
export type double = CType<"double">
export type uchar = CType<"uchar">
export type schar = CType<"schar">
export type short = CType<"short">
export type ushort = CType<"ushort">
export type int = CType<"int">
export type uint = CType<"uint">
export type long = CType<"long">
export type ulong = CType<"ulong">
export type longlong = CType<"longlong">
export type ulonglong = CType<"ulonglong">
export type CFn = {
caller: () -> Callable
}
export type CTypes =
| u8
| u16
| u32
| u64
| u128
| i8
| i16
| i32
| i64
| i128
| f32
| f64
| usize
| isize
| char
| float
| double
| uchar
| schar
| short
| ushort
| int
| uint
| long
| ulong
| longlong
| ulonglong
export type Ref = {
deref: (self: Ref) -> Ref,
offset: (self: Ref, offset: number) -> Ref,
ref: (self: Ref) -> Ref,
isNullptr: (self: Ref) -> boolean,
}
export type Box = { export type Box = {
size: number,
ref: (self: Box)->Ref,
}
export type BoxConstructor = (size: number)->Box
export type Type = {}
---! FIXME: better typing for PointerSize
export type PointerSize = number -- typeof(5) | typeof(8)
export type Arr<T> = {
inner: T,
size: number, size: number,
ptr: (self: Arr<T>) -> any,
zero: (self: Box) -> Box,
leak: (self: Box, offset: number?) -> Ref,
ref: (self: Box, offset: number?) -> Ref,
} }
--[=[ export type Library = {
@interface Ptr find: (sym: string) -> Ref,
@within FFI
]=]
---! FIXME: due to recursive type limition. hardcoded 6 depth. better idea?
export type Ptr<T> = {
inner: T,
size: PointerSize,
ptr: (self: Ptr<T>)->PtrPtr<Ptr<T>>,
arr: (self: Ptr<T>, size: number) -> Arr<Ptr<T>>,
}
export type PtrPtr<T> = {
inner: T,
size: PointerSize,
ptr: (self: PtrPtr<T>)->PtrPtrPtr<PtrPtr<T>>,
arr: (self: PtrPtr<T>, size: number) -> Arr<PtrPtr<T>>,
}
export type PtrPtrPtr<T> = {
inner: T,
size: PointerSize,
ptr: (self: PtrPtrPtr<T>)->PtrPtrPtrPtr<PtrPtrPtr<T>>,
arr: (self: PtrPtrPtr<T>, size: number) -> Arr<PtrPtrPtr<T>>,
}
export type PtrPtrPtrPtr<T> = {
inner: T,
size: PointerSize,
ptr: (self: PtrPtrPtrPtr<T>)->PtrPtrPtrPtrPtr<PtrPtrPtrPtr<T>>,
arr: (self: PtrPtrPtrPtr<T>, size: number) -> Arr<PtrPtrPtrPtr<T>>,
}
export type PtrPtrPtrPtrPtr<T> = {
inner: T,
size: PointerSize,
ptr: (self: PtrPtrPtrPtrPtr<T>)->PtrPtrPtrPtrPtrPtr<PtrPtrPtrPtrPtr<T>>,
arr: (self: PtrPtrPtrPtrPtr<T>, size: number) -> Arr<PtrPtrPtrPtrPtr<T>>,
}
export type PtrPtrPtrPtrPtrPtr<T> = {
inner: T,
size: PointerSize,
ptr: (self: PtrPtrPtrPtrPtrPtr<T>)->any, -- Yes. At this point. more type is useless.
arr: (self: PtrPtrPtrPtrPtrPtr<T>, size: number) -> Arr<PtrPtrPtrPtrPtrPtr<T>>,
} }
export type Callable = {
call: (...any) -> (),
}
local ffi = {}
ffi.u8 = {} :: u8
ffi.nullptr = {} :: Ref
function ffi.box(size: number): Box
return nil :: any
end
function ffi.open(path: string): Library
return nil :: any
end
function ffi.ref(): Ref
return nil :: any
end
function ffi.isInteger<T>(val: T): boolean
return nil :: any
end
function ffi.fn<T>(args: { any }, ret: CPtr<T>): CFn
return nil :: any
end