lua-users home
lua-l archive

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


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