Search code examples
capilua

Lua C API: pushing function with lua_pushcfunction isn't working


How can I define such a Lua function using C API?

function Caller(f)
    f()
end

This is what I've tried but it crashes:

typedef lua_CFunction luaFunc;

int Caller(lua_State* luaVM)
    luaFunc Func = lua_tocfunction(luaVM, 1);

    lua_pushcfunction(luaVM, Func);
    lua_call(luaVM, 0, 0);
    return 0;
}

Solution

  • The equivalent of the Lua code

    function Caller(f)
        f()
    end
    

    is

    int Caller(lua_State* luaVM)    
        lua_call(luaVM, 0, 0);
        return 0;
    }