| 1 | **This is a maintained copy of fork from https://github.com/facebook/luaffifb** |
| 2 | |
| 3 | About |
| 4 | ----- |
| 5 | This is a library for calling C function and manipulating C types from lua. It |
| 6 | is designed to be interface compatible with the FFI library in LuaJIT (see |
| 7 | http://luajit.org/ext_ffi.html). It can parse C function declarations and |
| 8 | struct definitions that have been directly copied out of C header files and |
| 9 | into lua source as a string. |
| 10 | |
| 11 | This is a fork of https://github.com/jmckaskill/luaffi |
| 12 | |
| 13 | Source |
| 14 | ------ |
| 15 | https://github.com/facebook/luaffifb |
| 16 | |
| 17 | Platforms |
| 18 | --------- |
| 19 | Currently supported: |
| 20 | - Linux x86/x64 |
| 21 | - Linux ARM Aarch64 |
| 22 | - OS X x86/x64 |
| 23 | - OS X M1 (ARM64) |
| 24 | |
| 25 | Runs with Lua 5.1, 5.2, and 5.3 |
| 26 | |
| 27 | Build |
| 28 | ----- |
| 29 | In a terminal: |
| 30 | |
| 31 | ```bash |
| 32 | git clone https://github.com/facebook/luaffifb |
| 33 | cd luaffifb |
| 34 | luarocks make |
| 35 | ``` |
| 36 | |
| 37 | Documentation |
| 38 | ------------- |
| 39 | This library is designed to be source compatible with LuaJIT's FFI extension. The documentation at http://luajit.org/ext_ffi.html describes the API and semantics. |
| 40 | |
| 41 | Pointer Comparison |
| 42 | ------------ |
| 43 | Use `ffi.NULL` instead of `nil` when checking for `NULL` pointers. |
| 44 | ```lua |
| 45 | ffi.new('void *', 0) == ffi.NULL -- true |
| 46 | ``` |
| 47 | |
| 48 | Known Issues |
| 49 | ------------ |
| 50 | - Comparing a ctype pointer to `nil` doesn't work the same as in LuaJIT (see above). |
| 51 | This is unfixable with the current metamethod semantics. |
| 52 | - Constant expressions can't handle non integer intermediate values (eg |
| 53 | offsetof won't work because it manipulates pointers) |
| 54 | - Not all metamethods work with Lua 5.1 (eg char\* + number). This is due to |
| 55 | the way metamethods are looked up with mixed types in Lua 5.1. If you need |
| 56 | this upgrade to Lua 5.2 or use boxed numbers (uint64\_t and uintptr\_t). |
| 57 | - All bitfields are treated as unsigned (does anyone even use signed |
| 58 | bitfields?). Note that "int s:8" is unsigned on unix x86/x64, but signed on |
| 59 | windows. |
| 60 | |
| 61 | |
| 62 | How it works |
| 63 | ------------ |
| 64 | Types are represented by a struct ctype structure and an associated user value |
| 65 | table. The table is shared between all related types for structs, unions, and |
| 66 | functions. It's members have the types of struct members, function argument |
| 67 | types, etc. The struct ctype structure then contains the modifications from |
| 68 | the base type (eg number of pointers, array size, etc). |
| 69 | |
| 70 | Types are pushed into lua as a userdata containing the struct ctype with a |
| 71 | user value (or fenv in 5.1) set to the shared type table. |
| 72 | |
| 73 | Boxed cdata types are pushed into lua as a userdata containing the struct |
| 74 | cdata structure (which contains the struct ctype of the data as its header) |
| 75 | followed by the boxed data. |
| 76 | |
| 77 | The functions in `ffi.C` provide the `cdata` and `ctype` metatables and ffi.\* |
| 78 | functions which manipulate these two types. |
| 79 | |
| 80 | C functions (and function pointers) are pushed into lua as a lua c function |
| 81 | with the function pointer cdata as the first upvalue. The actual code is JITed |
| 82 | using dynasm (see call\_x86.dasc). The JITed code does the following in order: |
| 83 | |
| 84 | 1. Calls the needed unpack functions in `ffi.C` placing each argument on the HW stack |
| 85 | 2. Updates `errno` |
| 86 | 3. Performs the C call |
| 87 | 4. Retrieves `errno` |
| 88 | 5. Pushes the result back into lua from the HW register or stack |