Sign in to follow this  
HexDump

How to get a table from the stack in Lua?

Recommended Posts

Hello, I have a table in Lua that must be filled in the C side. for example table { a=0, b=0} Now I wnat to call C_function(table) but I don´t know how to get a table from the stack. There's no a lua_totable or something like this like I do with numbers or strings. Could anybody help?. Note: sorry for the bunch of messages I´m posting, but Lua doc isn´t as good as I would. Thanks in advance, HexDump.

Share this post


Link to post
Share on other sites
Have the table on the stack, and have the C function use it from the stack. Most datatypes more complex than numbers and strings are like this in Lua: you don't get them into C variables, you work with them in situ, so to speak.

Share this post


Link to post
Share on other sites
Let's assume there's a table in the Lua universe:
foo = { ["bar"] = 3 }

To get the number three, here's what you'd do:

lua_getglobal(L, "foo"); // puts the table foo on top of the stack
lua_pushstring(L, "bar"); // now the string "bar" is just above the table on the stack
lua_gettable(L, -2); // finds the table foo in stack position -2, pops "bar" off the stack, pushes on foo["bar"]
int n = lua_tonumber(L, -1); // n should now be equal to 3



There's a pretty good example of this sort of twiddling in the lua manual. Read it over and over until you understand it; it's good conceptual practice.

Share this post


Link to post
Share on other sites
ok, I understand now. Thnaks alot.

I´m seeing in the manual that I can push a table into the stack too, this is nice because I wanted to do something like:

--IN LUA

TableFromC= C_Function(Table);

Does Lua get the table without any other work?.


Thanks in advance,
HexDump.

Share this post


Link to post
Share on other sites
Not quite sure what you're asking here. If you want to return a table, just have it as one of the return values (the top n values in the stack, where n is the number the c function returns).

Share this post


Link to post
Share on other sites
I just want to pass a table to C and get a table from C (return value). and I was asking if the assignment in Lua

Table_returned_from_C = C_Fuction(LuaTable)

handles everything to get after the call in Lua the correct Table in Table_returned_from_C, or should I pop anything from Lua stack?. Sorry for the lot of questions :/, I am a bit confussed with LUA.

Thanks in advance,
HexDump.

Share this post


Link to post
Share on other sites
Well, in that case, yes, that looks about correct. The table passed from lua ("Table") will be the only value on the stack when C_Function() starts, and the table passed back and stored in the variable "TableFromC" will be the table in the top of the stack when C_Function() returns (assuming it returns one result).

Share this post


Link to post
Share on other sites
Sneftel a little more thing. I will use this system in order to get input from my engine and do anyting I need from the response I receive. Do you think it will be slow?
I mean, it has to be somthing like this:

1. Lua Pushes the player keys as a table to C
2. C reads the table from the stack and puts keys in a Struct
3. From this struct, engine checks what keys were pressed
4. C Create a new table in the stack, fills it with the response
5. Lua reads the returned Tabel.

I´m asking because I think there's a lot of lua stack reads/writes to do this.

Thanks in advance,
HexDump.

Share this post


Link to post
Share on other sites
Seems okay, depending on what precisely the representation in the table is. But why do this from C in the first place? seems ideal for a Lua function.

Share this post


Link to post
Share on other sites
Generally, what you should do is initially write as much as reasonably possible on the Lua side. Then later, if you find that things are too slow, take the more processor intensive functions and rewrite them in C. Once you get a better handle on what scripting languages are and aren't good for, you can ignore this process somewhat for things you *know* won't go over well in Lua, but when just starting out you should try not to do this sort of premature optimization.

Share this post


Link to post
Share on other sites
I have been reading about LightUserData. Perhaps this is a better way to do what I need. First of all I need to pass the response (table) to C, and I don´t know any easy methods to do it and in the other hand I think that if I push the table from Lua into the stack and get it as a lightuserdata from C, I could map the user data directly into a C struct, am I right?.

Could anybody write a little example about using the LightUserData, I have checked several times the official pdf and there ins´t any.


Thanks in advance,
HexDump.

Share this post


Link to post
Share on other sites
light userdata isn't really what you want. It's primarily for passing data between C functions; in general you should never pass light userdata back to lua.

Share this post


Link to post
Share on other sites
This is all explained within the Lua manual located on the Lua website.

http://www.lua.org/manual/5.0/manual.html#3.11

For example (Passing a Table to lua)

lua_newtable(L);

for(it=info.begin(); it != info.end(); ++it) {
lua_pushstring(L, it.key().latin1()); // key
lua_pushstring(L, it.data().latin1()); // Data
lua_settable(L, -3); // The table location
}

if(lua_pcall(L, 1,2,0) != 0) {

Reversing the process (Loading a table into somesort of C structure)



lua_pushnil(L);
while (lua_next(L, -3) != 0) {
results[lua_tostring(L, -2)] = lua_tostring(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this