Re: how to send lua table to c/c++ by luajit.ffi ?

  • From: cosnis zhang <cosnis@xxxxxxxxx>
  • To: Ryan Gonzalez <rymg19@xxxxxxxxx>, luajit@xxxxxxxxxxxxx
  • Date: Wed, 27 Nov 2013 11:09:44 +0800

thanks, but if i don’t know how much char’s in the struct, and how to do…
-- 
cosnis zhang

On 2013年11月27日 at 上午11:07:02, Ryan Gonzalez (rymg19@xxxxxxxxx) wrote:

I think you need this:

ffi.cdef[[
typedef struct { char* a, b, c; } mystruct;
]]

mystruct = ffi.new("mystruct[?]", 3)
mystruct[0] = "a";
mystruct[1] = "b";
mystruct[2] = "c";

Note that I quickly hacked this together, and it is UNTESTED!!!


On Tue, Nov 26, 2013 at 8:58 PM, cosnis zhang <cosnis@xxxxxxxxx> wrote:
ok.. 

now, i have a dynamic length lua table… may i convert to FFI structs?

like this

local var = {“a”, “b”, “c"}
local var1 = {“a”, “b”, “c"}
local var2 = {“a”, “b”, “c”}

// how to convert to FFI structs.

ffi.C.call(var)
ffi.C.call(var1)
ffi.C.call(var2)

-- 
cosnis zhang

On 2013年11月27日 at 上午10:50:01, Coda Highland (chighland@xxxxxxxxx) wrote:

On Tue, Nov 26, 2013 at 6:38 PM, cosnis zhang <cosnis@xxxxxxxxx> wrote:
> like the subject, i want send a lua table like {“a”,”b”,”c”} to c/c++
>
> and how to read that value by c/c++?
>
> and how to get that count by c/c++?

The short answer to your question is "you can't." Lua tables are
purely Lua constructs and can't be sent to C via the FFI. You can use
FFI structs to assemble data structures that you can send to C
functions through the FFI, but if you need more generic table
manipulation you should simply write the code as Lua code.

Also note that you shouldn't access the Lua state from within a
function called by the FFI. This is unsupported and can cause crashes.

/s/ Adam




--
Ryan
When your hammer is C++, everything begins to look like a thumb.

Other related posts: