Skip to main content

FFI

The Yock authorsLess than 1 minuteAPIsync

ffi allows yock open and call dynamic library.

ffiLib

  • lib table

ffi.library()

  • Prototype
---@param lib string
---@param funcs table
function ffi.library(lib, funcs) end
  • Introduce

library declares function prototype (arguments + return value type) and creates its at running. Then, you can call its as function through ffi.lib.'lib_name'.'function_name' format.

  • Type Mapping
LuaC
void
integeruint8
integerint8
integerint8
integeruint16
integerint16
integeruint32
integerint32
integeruint64
integerint64
integerint (int32)
integerlong (int64)
numberfloat
numberdouble
booleanbool
stringstr (ptr type alias)
userdataptr # pointer type
  • Example
-- declares function prototype, and it's worth noting filename extension isn't required,
-- and yock will refer extension's name to add it automatically
-- according to platform (windows: .dll, linux: .so, darwin: .dylib).
-- Of course, obvious to add it is allowed, but not recommended because it's considered
-- difficult to cross platform.
ffi.library("./lib/mylib", {
     hello = { "void", { "str" } }
})

-- calls function, no matter how tortuous the library path is, and yock only extracts filename
-- in the end of path, meanwhile to split filename extension (.dll, .so, .dylib) to ensure cross platform.
ffi.lib.mylib.hello("yock")