lua-users home
lua-l archive

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


While not being a requirement, it's a good practice to pop the values you put on the stack. That way you can easily reuse and manipulate blocks of code outside of a lua_call. I don't think that there is any performance penalty in letting the interpreter pop values for you. However, poping values early, as soon as they're not used, can help avoid reordering stack slots, which *may* lead to a performance gain.

For strings you don't have to copy them to be sure to access them later. You can simply reference them in a table and only keep an integer key to access the string later. That way the string will live as long as the table and the Lua state live. The registry table is precisely designed for that kind of uses. For examples just look at luaL_ref in the manual.

-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Beric Holt
Envoyé : 26 octobre 2006 19:30
À : Lua list
Objet : RE: Pop Return results after pcall'ing

Thanks Jerome

The reason that I ask is because I was implementing a Lua Function caller helper (similar to http://www.lua.org/pil/25.3.html in PiL) and I saw it mention that "because the function may return strings, call_va cannot pop the results from the stack. It is up to the caller to pop them, after it finishes using occasional string results (or after copying them to other buffers)."

I realised that I never pop returned variables off the stack after a function call.  I do correctly handle strings:  if I wish to store them beyond the immediate scope of the function call then I copy it into a new string something like this:

char *szLuaString = lua_tostring(luaVM, 1);
char *szNewString = new char[strlen(szLuaString) + 1];
strcpy_s(m_szNewString , strlen(szLuaString) , szLuaString);

So anyway, are you saying that I don't need to pop the return values off the stack; lua will deal with that.  Would anyone say that it is still good practice to pop the returned values after I've used them?  Or should I just not bother?

Sorry for being a little bit dense.  I just want to make sure that I'm getting it right.

Cheers
Beric Holt
http://buzzrick.true.geek.nz 

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Jerome Vuarand
Sent: Friday, 27 October 2006 11:38 a.m.
To: Lua list
Subject: RE: Pop Return results after pcall'ing

All return values are discarded after being used. There is no such problem with the stack size, you don't have to manage anything in your C functions.

And on the other hand you cannot use the stack as a storage place for your objects. If you want them to be preserved after the call to your C function you have to put them in some table.

-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Beric Holt
Envoyé : 26 octobre 2006 18:06
À : Lua list
Objet : Pop Return results after pcall'ing

I've previously asked about whether function parameters are automatically pop'ed off the stack after the function call
(http://lua-users.org/lists/lua-l/2006-03/msg00074.html) but what happens if you don't pop the returned values after you have finished with them.  I assume that the stack will simple grow indefinitely until you run out of memory?

Cheers
Beric Holt
http://buzzrick.true.geek.nz