[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua <-> C++ integration with "unused" stack slots
- From: pan shizhu <pan.shizhu@...>
- Date: Fri, 5 Feb 2010 09:20:35 +0800
> But if the C++ function does not return soon? If the C++ function does use a
> lot of (temporary) C++ objects representing Lua variables (e.g. inside
> a C++ loop body) and does call other C++ functions that are also using slots
> on the stack, the stack might get full of unused stack slots.
What do you mean by "C++ function"? if you mean a "C++ function with
the prototype int foobar(lua_State *L);" then there won't be your
problem:
int function_b(lua_State *L)
{
// do something here
return 0;
}
int function_a(lua_State *L)
{
// do something here
lua_pushcfunction(L, function_b);
lua_pcall(L, 0, 0, 0);
// do something else
return 0;
}
In this way, you call function_b with a clean stack.
Generally, I think using C++ variables to represent lua stack is a bad
idea. At least it is a totally different design. Lua is designed so
that you should never need to represent variables in your host program
language. Since the stack may change after any lua_ function or lua
native function. your representation is invalid after that.
You can use a higher representation in C++ though, i.e. use C++ object
to represent a lua userdata.