2

I would like to know how I can pass a parameter table from a lua function to a C function. I found some example but they didn't explain how we can get back our table in our C function. If we pass a number from lua to C, we use luaL_checkint it's simple. But we can not doing the same thing with table.

For example in my lua function I call a C function like that :

local a, b= library.executeDiagRequestOnChannel(test,test2) 

where library is my new library for my embedded system functions, test and test 2 are two tables, and executeDiagRequestOnChannel is a function of my library.

In my C file, I have the function :

static long executeDiagRequestOnChannel(lua State *L){} 

and I want in this fonction do operation on my tables test and test2 and sent back after to lua function the result.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Subas
  • 65
  • 6
  • You use the table inspection functions in the api. You don't pull the table off the stack into a C variable. – Etan Reisner Aug 21 '14 at 15:55
  • @EtanReisner I need to get back the table from lua function parameter in a C function, because I want to do some operation with this table. For example in my lua function I call a C function like that : local a, b= library.executeDiagRequestOnChannel(test,test2) where library is my new library for my embedded system functions, test and test 2 are two tables, and executeDiagRequestOnChannel is a function of my library. – Subas Aug 22 '14 at 07:22
  • @EtanReisner ... In my C file, I have the function : static long executeDiagRequestOnChannel(lua State *L){} and I want in this fonction do operation on my table and sent back after to lua function the result. – Subas Aug 22 '14 at 07:31
  • 2
    Yes, but that's not the point. The point is that a lua table isn't a C primitive so you can't get a C variable with the table in it. So what you do instead is operate on the table (which lives on the lua stack) by using the lua table manipulation functions in the API. Functions like `lua_gettable`, `lua_getfield`, etc. – Etan Reisner Aug 22 '14 at 12:01
  • @EtanReisner So can you say me that function permit to : have the table in the stack and how I can check if my table is in the stack. At this moment I do a luaL_checktype(L,1,LUA_TTABLE) and after I do lua_gettable(L,1) to get the first value of the first table, when lua_gettable(L,2) to have the second value, etc Is it correct or why can I do that ? – Subas Aug 22 '14 at 12:13
  • 1
    Yes, that's the idea. The `checktype` function is checking the item at that stack index. `lua_gettable` is operating on the table at the given index and using the value from the top of the stack as the key, etc. – Etan Reisner Aug 22 '14 at 12:21
  • @EtanReisner Can you give me a short example ? – Subas Aug 22 '14 at 12:27
  • There are examples of `lua_gettable`/etc. all over SO and the internet. As well as in the Programming in Lua books (and Lua reference manual). – Etan Reisner Aug 22 '14 at 13:13

0 Answers0