lua-users home
lua-l archive

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


Don't reduce your stack "periodically", pop all results after every
lua_call / lua_pcall, after copying them to somewhere else if you need
to keep their values.

If you're in C++, it might be useful to have a RAII-style stack based
object that, in its destructor, asserts that the stack is in the
correct state.  Something like the following untested code:

struct StackCheck
{
  StackCheck (lua_State * lua) : lua(lua), top (lua_gettop(lua)) {}
  ~StackCheck () { if (lua_gettop(lua) != top) /* assert, throw
exception, whatever */ ;}
  const int top;
  lua_State * const lua;
};

// and then later on...
{
  StackCheck s (lua);
  // push function, arguments
  lua_call(lua, ......);
  // examine results
  // pop all results
} // s's destructor checks for stack overflow / underflow.

An exercise for the reader is to adapt StackCheck to have another
constructor argument, which is how many *extra* values are expected to
be on the stack when the destructor runs.