lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Mike,

Well...  that makes perfect sense. I've been using c for a long time and I very rarely use the [] to index arrays. I'm feeling very stupid right about now. A serious case of tunnel vision.

I sincerely appreciate your help and what is obviously a testament to your patience. Your doing some amazing work and I very much appreciate your efforts. LuaJIT/FFI is opening the door to many interesting possibilities ;-) 

Thanks again,
-G   




From: Mike Pall [mailto:mikelu-1104@mike.de]
To: Lua mailing list [mailto:lua-l@lists.lua.org]
Sent: Sun, 17 Apr 2011 19:17:25 -0500
Subject: Re: LuaJIT2 FFI Help

Gerry Weaver wrote:
> I guess I'm basically having issues figuring out how to handle
> function args that set a value, or more specifically, the
> <type>** case. I would really appreciate a nudge in the right
> direction on this.

These are typical C 'outargs'. In C you'd pass the address of a
local variable that receives the result. How to handle this with
the LuaJIT FFI is shown here:
http://luajit.org/ext_ffi_tutorial.html#idioms

Also, check out the zlib tutorial at
http://luajit.org/ext_ffi_tutorial.html#zlib
and pay particular attention to the 'buflen' variable.

For your example that would be (untested):

local mylib = ffi.load("my_lib")

local ctxp = ffi.new("my_ctx_t *[1]")
if mylib.my_init(ctxp) then
local ctx = ctxp[0]

local strp = ffi.new("char *[1]")
if mylib.my_get_string(ctx, strp) then
local str = strp[0]
print("Result:", ffi.tostring(str))
else
error("...")
end

mylib.my_term(ctx)
else
error("...")
end

--Mike