lua-users home
lua-l archive

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


ThePhD,

As you now already know, leaving arguments on the stack with return values is generally not a problem. However, if this function manipulates the stack then their is the potential for a problem to arise. This is because the stack space given to each function is a fixed size (20 IIRC, with an additional five for play). For functions that are going to use so much stack, you have to call lua_checkstack[1]

[1] http://www.lua.org/manual/5.3/manual.html#lua_checkstack

Regards,

Liam


On 09/07/2016 23:08, ThePhD wrote:
Dear Niccolo,

     You're right. When I looked back I misunderstood the examples provided in both PIL and the Lua Manual. Embarrassing, on my part!

     Thanks for the "lua_settop(L, 0)" advice; I had not considered using that function for when I needed to clear the stack.

Sincerely,
ThePhD

On Sat, Jul 9, 2016 at 5:57 PM, Niccolo Medici <niccolomedici@gmail.com> wrote:
On 7/9/16, ThePhD <jm3689@columbia.edu> wrote:
>      Thank you for the clarification! I was unsure, since almost every
> example I had seen did this
> [...]
> examples I have seen in PIL and other places.

It's likely that you misunderstood the examples. Can you quote two examples?

> On Sat, Jul 9, 2016 at 12:02 PM, Alysson Cunha <alyssonrpg@gmail.com>
> wrote:
>
>> You dont need to pop the arguments from stack to rerurn values....
>>
>> Em 09/07/2016 12:37, "ThePhD" <jm3689@columbia.edu> escreveu:
>>
>> Hello!
>>
>>      I have a question regarding the implementation details of the
>> returns
>> for C functions into Lua. As of right now, I use the C API to return one
>> or
>> more values back to Lua, but only after popping all of the arguments
>> given
>> to Lua just before I do this. This seems to be what is necessary, given
>> the
>> examples I have seen in PIL and other places.
[...]
>>      Lua (5.1, 5.2, and 5.3), when I pop more than what's on the stack
>> from the stack, seem to protect against me ruining the stack by
>> "overpopping". LuaJIT, however, crashes because the stack is not
>> protected
>> from these over-pops.

Even if this were true, that you need to pop all values before your
function returns, you could simply do "lua_settop(L, 0)". No need to
calculate anything.