Assuming there is following C code:
struct Foo { int dummy; }
int tryToAllocateFoo(Foo ** dest);
...How to do following in LuaJIT?
Foo * pFoo = NULL;
tryToAllocateFoo(&pFoo);
The LuaJIT FFI automatically generates special callback functions whenever a Lua function is converted to a C function pointer. This associates the generated callback function pointer with the C type of the function pointer and the Lua function object (closure).
The FFI library allows you to create and access C data structures. Of course, the main use for this is for interfacing with C functions. But they can be used stand-alone, too. Lua is built upon high-level data types. They are flexible, extensible and dynamic.
local ffi = require 'ffi'
ffi.cdef [[
struct Foo { int dummy; };
int tryToAllocateFoo(Foo ** dest);
]]
local theDll = ffi.load(dllName)
local pFoo = ffi.new 'struct Foo *[1]'
local ok = theDll.tryToAllocateFoo(pFoo)
if ok == 0 then -- Assuming it returns 0 on success
print('dummy ==', pFoo[0].dummy)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With