Advertisement

How to get a table from the stack in Lua?

Started by September 01, 2004 05:54 PM
14 comments, last by trolocsis 20 years ago
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.
Ok mate. I have never do scripting, so I´m a bit concerned about performance.


HexDump.
Advertisement
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.
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.
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.
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);		

This topic is closed to new replies.

Advertisement