Skip to Content
Skip to Content

Foreign Function Interface

zune.ffi


A library to interact with foreign functions and libraries.

Properties

prefix

The recommended prefix for the library file.

Platforms:

  • Windows: none
  • Linux: lib
  • macOS: lib
prefix: string

suffix

The recommended suffix for the library file.

Platforms:

  • Windows: dll
  • Linux: so
  • macOS: dylib
suffix: string

types

types: <<table> = { void: FFIDataType, i8: FFIDataType, u8: FFIDataType, i16: FFIDataType, u16: FFIDataType, i32: FFIDataType, u32: FFIDataType, i64: FFIDataType, u64: FFIDataType, float: FFIDataType, double: FFIDataType, pointer: FFIPointerType, vector2_32: FFIVectorType, vector3_32: FFIVectorType, }table<table> = { void: FFIDataType, i8: FFIDataType, u8: FFIDataType, i16: FFIDataType, u16: FFIDataType, i32: FFIDataType, u32: FFIDataType, i64: FFIDataType, u64: FFIDataType, float: FFIDataType, double: FFIDataType, pointer: FFIPointerType, vector2_32: FFIVectorType, vector3_32: FFIVectorType, }>

null

Functions

ptr

Parameters

  • src: FFIPointer - The pointer to reference.

Throws

  • Memory Error

ptrFromAddress

ptrFromAddress(src: buffer): FFIPointer

Parameters

  • src: buffer - The address of the pointer.

Throws

  • Memory Error

getLuaState

Returns the lua state.

getLuaState(mainthread: boolean?): FFIPointer

Parameters

  • mainthread: boolean? - Whether to get the main thread or the current thread.
    • default: false

Throws

  • Memory Error

alloc

alloc(size: number, alignment: number?): FFIPointer

Parameters

  • size: number - The size to allocate.
  • alignment: number? - The alignment to allocate.
    • default: 1

Throws

  • FFI Error
  • Memory Error

create

create(type: -- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerTypeFFIAnyDataType-- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType): FFIPointer

Parameters

  • type: FFIAnyDataType - The data type to allocate.

Throws

  • FFI Error
  • Memory Error

free

free(src: FFIPointer): ()

Parameters

  • src: FFIPointer - The pointer to free. If the pointer is static, it must have a known size.

Throws

  • FFI Error
  • Memory Error

dupe

dupe(src: buffer | FFIPointer | vector): FFIPointer

Parameters

  • src: buffer | FFIPointer | vector - The pointer to duplicate. If the source is a static pointer, it must have a known size.

Throws

  • Memory Error

tagName

tagName(tag: number): string?

Parameters

  • tag: number - The tag id to get the name of.

struct

Creates a structure data type.

  • The order of the fields is the number index of the field table.
  • There cannot be multiple fields in the same field table.
  • Multiple fields with the same field name is not allowed.
struct(fields: {{[string]: -- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerTypeFFIAnyDataType-- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType}}): FFIStructureType

Parameters

  • fields: {{ [string]: FFIAnyDataType }} - The fields of the structure.

Throws

  • FFI Error
  • Memory Error

fn

fn(definition: -- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType export type FFIFunctionDefintion = { returns: FFIAnyDataType, args: {FFIAnyDataType} }FFIFunctionDefintion-- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType export type FFIFunctionDefintion = { returns: FFIAnyDataType, args: {FFIAnyDataType} }, srcPtr: FFIPointer): ((...any) -> any)

Parameters

  • definition: FFIFunctionDefintion - The function definition.
    • returns: FFIAnyDataType - The return type of the function.
    • args: {FFIAnyDataType} - The argument types of the function.
  • srcPtr: FFIPointer - The pointer to the function.

Throws

  • FFI Error
  • Memory Error

closure

closure(definition: -- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType export type FFIFunctionDefintion = { returns: FFIAnyDataType, args: {FFIAnyDataType} }FFIFunctionDefintion-- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType export type FFIFunctionDefintion = { returns: FFIAnyDataType, args: {FFIAnyDataType} }, handler: (...any) -> any): FFIPointer

Parameters

  • definition: FFIFunctionDefintion - The function definition.
    • returns: FFIAnyDataType - The return type of the function.
    • args: {FFIAnyDataType} - The argument types of the function.
  • handler: (...any) -> any - The lua function to call.

Throws

  • FFI Error
  • Memory Error

dlopen

dlopen(path: string, functions: {[string]: -- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType export type FFIFunctionDefintion = { returns: FFIAnyDataType, args: {FFIAnyDataType} }FFIFunctionDefintion-- references: -- (1) FFIDataType -- (2) FFIStructureType -- (3) FFIArrayType -- (4) FFIPointerType export type FFIAnyDataType = FFIDataType | FFIStructureType | FFIArrayType | FFIPointerType export type FFIFunctionDefintion = { returns: FFIAnyDataType, args: {FFIAnyDataType} }}): -- references: -- (1) FFIPointer export type FFILibrary = { getSymbol: (self: FFILibrary, symbol: string) -> FFIPointer?, [string]: (...any) -> any, }FFILibrary-- references: -- (1) FFIPointer export type FFILibrary = { getSymbol: (self: FFILibrary, symbol: string) -> FFIPointer?, [string]: (...any) -> any, }

Parameters

  • path: string - The path to the library.
  • functions: { [string]: FFIFunctionDefintion } - The symbols to load from the library.

Throws

  • FFI Error
  • Memory Error

Namespaces

c


FUNCTIONS

compile

c.compile(src: string, opts: -- references: -- (1) FFIPointer export type FFICompileOptions = { options: string?, files: {string}?, libraries: {string}?, library_paths: {string}?, includes: {string}?, sysincludes: {string}?, symbols: { [string]: FFIPointer }?, }FFICompileOptions-- references: -- (1) FFIPointer export type FFICompileOptions = { options: string?, files: {string}?, libraries: {string}?, library_paths: {string}?, includes: {string}?, sysincludes: {string}?, symbols: { [string]: FFIPointer }?, }?): FFICompiled

Parameters

  • src: string - The C source code to compile.
  • opts: FFICompileOptions? - The options for the compilation.
    • options: string? - The compiler options to use.
    • files: {string}? - The files to include in the compilation.
    • libraries: {string}? - The libraries to link against.
    • library_paths: {string}? - The library paths to use.
    • includes: {string}? - The include directories to use.
    • sysincludes: {string}? - The system include directories to use.
    • symbols: {[string]: FFIPointer}? - The symbols to define.
Last updated on