ffi
Description
The FFI (foreign function interface) allows calling external C functions and using C data structures from pure Lua code.
The FFI library largely obviates the need to write tedious manual Lua/C bindings in C. No need to learn a separate binding language — it parses plain C declarations! These can be cut-n-pasted from C header files or reference manuals. It's up to the task of binding large libraries without the need for dealing with fragile binding generators.
The FFI library is tightly integrated into LuaJIT (it's not available as a separate module). The code generated by the JIT-compiler for accesses to C data structures from Lua code is on par with the code a C compiler would generate. Calls to C functions can be inlined in JIT-compiled code, unlike calls to functions bound via the classic Lua/C API.
No hand-holding!
The FFI library has been designed as a low-level library. The goal is to interface with C code and C data types with a minimum of overhead. This means you can do anything you can do from C: access all memory, overwrite anything in memory, call machine code at any memory address and so on.
The FFI library provides no memory safety, unlike regular Lua code. It will happily allow you to dereference a NULL
pointer, to access arrays out of bounds or to misdeclare C functions. If you make a mistake, the game might crash, just like equivalent C code would. It needs to be used with care, but it's flexibility and performance often outweigh this concern.