umegaya/ffiex — pull request #5 by umegaya (2014-05-26 12:08:38+00:00)
Title: Umegaya/feature/selective cdef

## implement selective cdef
as planned in #2
``` lua
-- importer = ffi.import(symbols):from(source_code)

-- eg)
ffi.import({
"pthread_t",
"pthread_create"
}):from[[
#include
]]
```
note that ffi parse pre-processed header file and cache result as dependency tree of each function and type definition, so you can call ffi.import to different symbols in same header file.

also you can know all variable name and type of the type or function which you interested in.
``` lua
-- source = ffi.src_of(symbol)

-- eg)
print(ffi.src_of("pthread_t"))
```

## major refactoring
#### enable multiple "parse" state for ffi
from initial release of ffiex, ffiex cache macro information in it to enabling old-fashioned style include guard like
```
#if !defined(__HOGE_H__)
#define __HOGE_H__
#endif
```
its useful, rather essential to parse most of practical C header files, but cause problem when someone try to write module which internally depends on ffiex,

because ffi.cdef in module code *pollutes* macro definition cache in your ffiex module.

if macro which has same name already declared in module which you require, your attempt to include your header file may fails.

to solve this problem, I rewrite entire module.
- create ffi_state object, which has separated cache of macro and dependency tree (of ffi.import)
- actual method calls of ffiex are delegated to method of ffi_state

now module writer which internally uses ffiex should use ffi.newstate() like following
``` c
...
//bar.h
extern int barfunc(void);
...
````
``` lua
local state = ffi.newstate()
state:cdef[[
#define FOO(x) (x + 1)
#include "bar.h"
]]
local lib = state:load("bar")
print(state.FOO(1)) --> 2

-- main ffiex still doen't know macro FOO
print(ffiex.defs.FOO) --> nil
-- able to define another macro FOO
ffiex.cdef[[
#define FOO(x) (x + 10)
]]
print(ffiex.defs.FOO(1)) --> 11
-- but cdef'ed symbol is shared among all ffi_state
local lib2 = ffi.load("bar")
```