lua-users home
lua-l archive

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


Hey

On 18 February 2018 at 17:37, albertmcchan <albertmcchan@yahoo.com> wrote:
And this actualy happens to me recently. I had to fix B to leave the stack alone.

IMHO it's generally better to only touch the stack in A and isolate B from Lua entirely. I tend to separate concerns like this:

Function A:
 * Validate argument types
 * Fetch argument values from the stack
 * Call function B with no lua_State access
 * Collect results from B, push whatever appropriate value(s) onto the stack
 * Return

I actually like this - unless the purpose of the function is really really simple, I always prefer to have the actual work happening in a separate function. It nicely separates the Lua glue from the actual functionality, and it's easier to test.

It might be a bit hard to follow, since we've abstracted a ton of useful Lua glue work into something called LuaSkin, but I consider this to be pretty much my ideal Lua C function:

https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/streamdeck/internal.m#L130

--
Cheers,

Chris