[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: More lua stack operations support?
- From: Tim Hill <drtimhill@...>
- Date: Mon, 3 Mar 2014 18:58:32 -0800
On Mar 3, 2014, at 12:50 AM, Jean-Luc Jumpertz <jean-luc@celedev.eu> wrote:
> From my experience after writing 25K lines of code using intensively the Lua C API, actually lua_remove (range) would be a valuable addition.
>
> In effect, it corresponds to a real and rather common use case where:
> - you want to push several "result" values to the stack,
> - for this you need to compute a number of intermediate stuff like table references and so push these intermediate values in the stack
> - you compute your "result" values that are located at the top of the stack
> - you need to remove the intermediate values from the stack in order to leave it in a clean non-leaking state
>
> IMHO no other additional stack operation is really needed.
>
> Jean-Luc
Well whenever I’ve come across that (one result on the top of the stack and junk underneath) I just flip the problem around, and use lua_insert() to inject the result from the top of the stack down below all the “junk”, and then just reset the stack top down to remove the junk (and the now redundant copy of the result). This is also efficient since it only inserts ONE item in the stack and therefore has pretty optimal performance. Something like this:
// Clean up all junk in Lua stack below top-most item, trimming stack down to nOldTOS+1 and leaving top-most item in place
void CleanJunkUnderTOSResult(lua_State *pvm, int nOldTOS) {
lua_insert(pVM, nOldTOS+1);
lua_settop(pVM, nOldTOS+1);
}
You could of course code up a trivial variant that specified the number of junk items to remove rather than a previously marked stack position.
—Tim