lua-users home
lua-l archive

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

(oops I accidentally hit send before finishing)

On 27 March 2017 at 12:43, Daurnimator <> wrote:
> You mean like the (now deprecated) lua_cpcall?
> This can be replicated in lua 5.3 with:

lua_pushcfunction(L, f);
lua_pushlightuserdata(L, p);
lua_pcall(L, 1, 0, 0);

(This above pattern isn't safe in 5.1, as back then lua_pushcfunction
could throw)
However this new form is more powerful e.g. you can pass multiple
arguments instead of one, and you can actually get return values.
Consider these benefits as you make your request:

On 27 March 2017 at 11:59, Tim Hill <> wrote:
> If func() returns normally, the return value is the return value of func(), otherwise it is the same as for lua_pcall(). Note that no new Lua stack frame is setup; the called function will see the same Lua stack as the original C function (and can push return values if it desires).

It should be simple enough to write this function ourselves:

typedef int (ProtCFunc)(lua_State *L, void *p);
static int helper(lua_State *L) {
    ProtCFunc *f = lua_touserdata(L, -2);
    void *p = lua_touserdata(L, -1);
    lua_pop(L, 2);
    return f(L, p);

int lua_pcallc(lua_State *L, ProtCFunc f, void *p) {
    int status;
    int n = lua_gettop(L);
    lua_pushcfunction(L, helper);
    lua_insert(L, 1);
    lua_pushlightuserdata(L, f);
    lua_pushlightuserdata(L, p);
    status = lua_pcall(L, 2+n, LUA_MULTRET, 0);
    if (status < 0)
        return status;
        return lua_gettop(L);