Foreign Function Interface
Must be enabled in zune.toml
to use.
[experimental]
ffi=true
Properties
Functions
- sizeOf
- alignOf
- eqlPtr
- intFromPtr
- valueFromPtr
- readFromPtr
- spanFromPtr
- writeIntoPtr
- struct
- call
- closure
- dlopen
Namespaces
Properties
supported
ffi.supported: boolean
Whether the FFI is supported on the platform.
prefix
ffi.prefix: string
The recommended prefix for the library file. Platforms:
- Windows: none
- Linux:
lib
- macOS:
lib
suffix
ffi.suffix: string
The recommended suffix for the library file. Platforms:
- Windows:
.dll
- Linux:
.so
- macOS:
.dylib
Functions
sizeOf
ffi.sizeOf(type: Structure | number): number
Gets the size of a type.
Parameters
type:
Structure
|number
- The type to get the size of.
Returns
number
- The size of the type.
alignOf
ffi.alignOf(type: Structure | number): number
Gets the alignment of a type.
Parameters
type:
Structure
|number
- The type to get the alignment of.
Returns
number
- The alignment of the type.
eqlPtr
ffi.eqlPtr(a: buffer, b: buffer?): boolean
Checks if two underlying pointers are equal from a buffer.
Parameters
a: buffer
- The first pointer.b: buffer?
- The second pointer.
Returns
boolean
- Whether the pointers are equal.
intFromPtr
ffi.intFromPtr(ptr: buffer, src: buffer): buffer
Gets then integer from of the source pointer and writes the value to the destination pointer.
Parameters
ptr: buffer
- The destination pointer.src: buffer
- The source pointer.
Returns
buffer
- The destination pointer.
valueFromPtr
ffi.valueFromPtr(ptr: buffer, type: Structure | number): any
Gets the value from a pointer.
Parameters
ptr: buffer
- The buffer holding the pointer.type:
Structure
|number
- The type of the value.
Returns
any
- The value.
readFromPtr
ffi.readFromPtr(srcPtr: buffer, srcOffset: number, dest: buffer, destOffset: number, size: number): ()
Reads data from a pointer to a buffer.
Parameters
srcPtr: buffer
- The source pointer.srcOffset: number
- The source offset.dest: buffer
- The destination buffer.destOffset: number
- The destination offset.size: number
- The size to read.
spanFromPtr
ffi.spanFromPtr(srcPtr: buffer, srcOffset: number): buffer
Takes a pointer and loops through it until it finds a null terminator.
Parameters
srcPtr: buffer
- The source pointer.srcOffset: number
- The source offset.
Returns
buffer
- The data.
writeIntoPtr
ffi.writeIntoPtr(destPtr: buffer, destOffset: number, src: buffer, srcOffset: number, size: number): ()
Writes data from a buffer to a pointer.
Parameters
destPtr: buffer
- The destination pointer.destOffset: number
- The destination offset.src: buffer
- The source buffer.srcOffset: number
- The source offset.size: number
- The size to write.
struct
ffi.struct(name: string, fields: {{ [string]: Structure | number }}): Structure
Creates a structure.
Parameters
name: string
- The name of the structure.fields: {{ [string]:
Structure
|number
}}
- The fields of the structure.
Returns
Structure
- The structure.
call
ffi.call(definition: FunctionDefinition, srcPtr: buffer, ...any): any
Calls a function at the pointer.
Parameters
definition:
FunctionDefinition
- The function definition.srcPtr: buffer
- The pointer to the function....any
- The arguments to pass to the function.
Returns
any
- The return value.
closure
ffi.closure(definition: FunctionDefinition, handler: (...any) -> any): Closure
Creates a closure for C functions to use, calling the lua handler.
Parameters
definition:
FunctionDefinition
- The function definition.handler: (...any) -> any
- The lua function.
Returns
Closure
- The closure.
dlopen
ffi.dlopen(path: string, { [string] : FunctionDefinition }): Library
Opens a dynamic library.
Parameters
path: string
- The path to the library.{ [string] :
FunctionDefinition
}
- The functions to load.
Returns
Library
- The library.
Namespaces
types
Properties
- types.void
- types.i8
- types.u8
- types.i16
- types.u16
- types.i32
- types.u32
- types.i64
- types.u64
- types.float
- types.double
- types.pointer
types.void
ffi.types.void: number
The void type.
types.i8
ffi.types.i8: number
The signed 8-bit integer type.
types.u8
ffi.types.u8: number
The unsigned 8-bit integer type.
types.i16
ffi.types.i16: number
The signed 16-bit integer type.
types.u16
ffi.types.u16: number
The unsigned 16-bit integer type.
types.i32
ffi.types.i32: number
The signed 32-bit integer type.
types.u32
ffi.types.u32: number
The unsigned 32-bit integer type.
types.i64
ffi.types.i64: number
The signed 64-bit integer type.
types.u64
ffi.types.u64: number
The unsigned 64-bit integer type.
types.float
ffi.types.float: number
The float (f32) type.
types.double
ffi.types.double: number
The double (f64) type.
types.pointer
ffi.types.pointer: number
The pointer type.
Types
FunctionDefinition
export type FunctionDefinition = {
returns : Structure | number,
args : { Structure | number }
}
Closure
export type Closure = {
ptr : buffer
}
Library
export type Library = {
close : (self: Library) -> (),
[string]: (...any) -> any,
}
Structure
export type Structure = {
size : (self: Structure) -> number,
alignment : (self: Structure) -> number,
offset : (self: Structure, field: string) -> number,
new : (self: Structure, fields: {{ [string]: buffer | number }}) -> buffer,
}