lua-users home
lua-l archive

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


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