lua-users home
lua-l archive

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


On Feb 21, 2018, at 6:43 AM, Chris Jones <cmsj@tenshu.net> wrote:

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:


--
Cheers,

Chris

For my case, A were originally a simple lua function that call B, C, ...

But, A should really belong inside the lib, so when I do 
lib = require 'lib', A can be called as lib.A

Had not expected calling B shared the same lua stack.
Is there a way to force calling B, C, ... with a new stack ? (like in lua ?)