[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Poping C function arguments from the Lua stack
- From: Sean Conner <sean@...>
- Date: Thu, 25 Nov 2021 04:28:58 -0500
It was thus said that the Great Meir Shpilraien once stated:
> I was wondering whether or not it's OK to pop out from the stack the
> arguments given to a native C function from Lua code. I am asking
> because users might send a lot of arguments to the C function (using unpack
> for example) and acquire all the room on the stack (which is set by default
> to 8K), leaving no place to push replies or perform other operations that
> require the stack.
> I am using Lua 5.1.5 (if it makes any difference).
It depends on what you are "popping" out. Values like floats or integers
are fine if you have them cached in some C variable, but strings are another
char const *s = lua_tostring(L,idx);
may cause an issue as a garbage collection could occur and invalidate the
address in s . Similar issues arrise with userdata, and there's no way to
reference a table *except* through the Lua stack.
You might also want to check out lua_checkstack() to see if you have
enough stack space to do all you need to do and if not, handle the situation
appropriate to what you are trying to do.
 Unless you know a reference to the string (or userdata) exists
elsewhere, but in general, you shouldn't rely upon this.