lua-users home
lua-l archive

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


Dear Alysson,

     Thank you for the clarification! I was unsure, since almost every example I had seen did this and it was not really specified anywhere exactly. My fear was that if the function called it might "leave" the arguments on the stack below where the results were, and eventually trash the VM with overflows. I'm glad this is not the case, and this will allow me to improve my code.

Sincerely,
ThePhD

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....

If the function rerurns 3, the top 3 elements of the stack will be the returning values of the function.  You dont need to pop the arguments

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.

     My usage gets a bit different when I have optional arguments. I pop a fixed number of arguments from the stack for certain functions after performing the call, but some parameters have provided-defaults if I can't get the item off the stack.

     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.

     Some solutions I imagine to my problem would be to just always pop "int arg_count = lua_gettop(L)" values from the stack. But, I'm trying to get near-zero-overhead, and that's another (even if dirt cheap) API call I have to make.

    My other "solution" is maybe being cautious and only popping arguments I know are "required", but I do not know if the implementation is required to clean up the stack after a C function call and all arguments are returned.

     Are implementations required to clean up the stack if there's more than "return-count" things left on the stack before a C function returns?

Sincerely,
ThePhD