lua-users home
lua-l archive

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


Begin forwarded message:

From: albertmcchan <albertmcchan@yahoo.com>
Date: February 18, 2018 5:24:07 PM EST
To: Andrew Gierth <andrew@tao11.riddles.org.uk>
Subject: Re: Lua C API can't be used from other languages without C wrappers

On Feb 18, 2018, at 5:06 PM, Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:

"Albert" == Albert Chan <albertmcchan@yahoo.com> writes:

Albert> I have trouble using Lua C stack, even in C

Albert> Say, I have a c function A that uses lua stack, that call
Albert> another c function B that also uses c lua stack. however, B did
Albert> a lua_settop of 1, then return 1.

If A calls B _as a C function_, then B should not be using settop or
making assumptions about the start of the lua stack.

If you want B to be able to use the stack freely without affecting the
caller, then you just call it with lua_pushcfunction and lua_call rather
than directly.

--
Andrew.

confession time:  i wrote B :-(

At the time, I HAD to "clean" the stack and return 1
(i were not sure which side the 1 referred ... stack 1 ? stack -1 ?)

Now, I know return 1 will be enough (if result is at top of stack)

B function work perfectly, however, when call from lua.
I only noticed the bug when it were called from A, another c-function.