Skip to content
FFI package for Lua 5.1 and Lua 5.2
C Lua Other
Branch: master
Clone or download
soumith Merge pull request #35 from afonso360/master
Support newer versions of lua
Latest commit 532c757 Jul 21, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis
dynasm Adding LONG action to dynasm arm so .long can use runtime values Jul 25, 2011
msvc Add ARM CE support. Jul 25, 2011
.gitignore Add luarocks spec file for package. Apr 28, 2015
.travis.yml Add support for Lua 5.3 Jan 2, 2016
CONTRIBUTING.md Add Facebook LICENSE, PATENTS, and CONTRIBUTING files Aug 3, 2015
LICENSE Fix LICENSE and PATENTS file Sep 16, 2015
Makefile Add ARM header files Jan 19, 2016
PATENTS Fix LICENSE and PATENTS file Sep 16, 2015
README.md Update version requirements in README.md Jun 17, 2016
call.c
call_arm.dasc Add ARM header files Jan 19, 2016
call_arm.h Add ARM header files Jan 19, 2016
call_x64.h Fix invalid >2GB relative jumps to shared global functions. Oct 27, 2016
call_x64win.h Fix invalid >2GB relative jumps to shared global functions. Oct 27, 2016
call_x86.dasc Fix invalid >2GB relative jumps to shared global functions. Oct 27, 2016
call_x86.h Fix invalid >2GB relative jumps to shared global functions. Oct 27, 2016
ctype.c Fix LICENSE and PATENTS file Sep 16, 2015
ffi.c Fixing memleak: jit->pages not free'd Jun 27, 2016
ffi.h Support newer versions of lua Jul 21, 2017
generate_call_h.bat Add ARM CE support. Jul 25, 2011
luaffi-scm-1.rockspec Add Travis CI build for Lua 5.1 and 5.2 Nov 15, 2015
msvcbuild.bat Add support for complex numbers Apr 10, 2012
parser.c Port to PPC64el Apr 11, 2016
pretty.lua Add table pretty-printer to help with debugging Apr 25, 2012
test.c Add Travis CI build for Lua 5.1 and 5.2 Nov 15, 2015
test.lua Changed cdata to native Lua 5.3 numbers Jun 17, 2016
test_includes.sh Make default target when running test_includes.sh Apr 14, 2016

README.md

Build Status

About

This is a library for calling C function and manipulating C types from lua. It is designed to be interface compatible with the FFI library in LuaJIT (see http://luajit.org/ext_ffi.html). It can parse C function declarations and struct definitions that have been directly copied out of C header files and into lua source as a string.

This is a fork of https://github.com/jmckaskill/luaffi

Source

https://github.com/facebook/luaffifb

Platforms

Currently supported:

  • Linux x86/x64
  • OS X x86/x64

Runs with Lua 5.1, 5.2, and 5.3

Build

In a terminal:

git clone https://github.com/facebook/luaffifb
cd luaffifb
luarocks make

Documentation

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.

Pointer Comparison

Use ffi.NULL instead of nil when checking for NULL pointers.

  ffi.new('void *', 0) == ffi.NULL -- true

Known Issues

  • Comparing a ctype pointer to nil doesn't work the same as in LuaJIT (see above). This is unfixable with the current metamethod semantics.
  • Constant expressions can't handle non integer intermediate values (eg offsetof won't work because it manipulates pointers)
  • Not all metamethods work with Lua 5.1 (eg char* + number). This is due to the way metamethods are looked up with mixed types in Lua 5.1. If you need this upgrade to Lua 5.2 or use boxed numbers (uint64_t and uintptr_t).
  • All bitfields are treated as unsigned (does anyone even use signed bitfields?). Note that "int s:8" is unsigned on unix x86/x64, but signed on windows.

How it works

Types are represented by a struct ctype structure and an associated user value table. The table is shared between all related types for structs, unions, and functions. It's members have the types of struct members, function argument types, etc. The struct ctype structure then contains the modifications from the base type (eg number of pointers, array size, etc).

Types are pushed into lua as a userdata containing the struct ctype with a user value (or fenv in 5.1) set to the shared type table.

Boxed cdata types are pushed into lua as a userdata containing the struct cdata structure (which contains the struct ctype of the data as its header) followed by the boxed data.

The functions in ffi.C provide the cdata and ctype metatables and ffi.* functions which manipulate these two types.

C functions (and function pointers) are pushed into lua as a lua c function with the function pointer cdata as the first upvalue. The actual code is JITed using dynasm (see call_x86.dasc). The JITed code does the following in order:

  1. Calls the needed unpack functions in ffi.C placing each argument on the HW stack
  2. Updates errno
  3. Performs the C call
  4. Retrieves errno
  5. Pushes the result back into lua from the HW register or stack
You can’t perform that action at this time.