lua-users home
lua-l archive

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


Unfortunately, there's nothing sol2 can do about `longjmp`. Unless I were to take over malloc for the duration of any user-supplied functions to ensure memory safety (but no execution safety: for example, typical handle resource cleanup), there's no way for me to prevent it from happening. If you build Lua as C++, you can call `luaL_error()` and -- as mentioned before -- Lua will trigger stack unwinding. LuaJIT also has made significant strides in allowing stack unwinding on all platforms (including x86) in LuaJIT-2.1.0-beta3.

There's really no getting around longjmp. The best thing you can do is use as many safety features as humanly possible to prevent resource-critical code from exploding. In sol2, that's sol::optional<type> and sol::protected_function. Also see `SOL_CHECK_ARGUMENTS` and all the safety macros defined here: http://sol2.readthedocs.io/en/latest/safety.html#config

It's not ideal, but it's the best I can do. I can only hope it helps...!


On Wed, Feb 21, 2018 at 10:28 AM, Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "albertmcchan" == albertmcchan  <albertmcchan@yahoo.com> writes:

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

Of course there is, you use lua_pushcfuncfion and lua_call, as I pointed
out to you before.

int B(lua_State *L)
{
    lua_settop(L, 0);
    lua_pushstring(L, "foo!");
    return 1;
}

int A(lua_State *L)
{
    /* stack currently has args of A if any */
    lua_pushstring(L, "bar!");
    /* this calls B in A's stack frame, so it will clobber the
     * string pushed above and any args, leaving A's stack with
     * just B's "foo!" on it
     */
    B(L);
    /* but this calls B on a brand new stack frame of its own,
     * so the existing "foo!" remains on the stack and another
     * one is pushed on top
     */
    lua_pushcfunction(L, B);
    lua_call(L, 0, 1);  /* 0 args, 1 result */
    /* stack now has just "foo!", "foo!" */
    return 2;
}

This is how you do it if B and A both need to be independently callable
from Lua but A needs to call B.

--
Andrew.