Rapptz / sol

A C++11 Lua wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

new get method

DominikMS opened this issue · comments

Please add std::function in get method:

inline std::function<void()> get(typesstd::function<void()>, lua_State* L, int index = -1)
{
if(!lua_isfunction(L, index))
{
return {}; // maybe error?
};

lua_pushvalue(L, index);
int func = luaL_ref(L, LUA_REGISTRYINDEX);

return [func, L]()
{
    lua_rawgeti(L, LUA_REGISTRYINDEX, func);

    if(lua_isfunction(L, -1))
    {
        if(lua_pcall(L, 0, 0, 0))
        {} // maybe error?
}

    luaL_unref(L, LUA_REGISTRYINDEX, func);
};

}

example:
void testFunc(std::function<void()> f)
{
f();
}

A:testFunc(function() print("hello std::function") end)

Your function should take a sol::function instead, and this will work as desired. However, I do see the appeal in making it play nice with std::function -- looking into seeing if there's a nice way to have this work out.

This is fixed up and ready to roll in this pull request. Thanks for pointing it out.

std::function<> still not working well, engine i crashed before called c code.

        void testFunc(std::function<bool(int)> f)
        {
            std::cout << "[testFunc]" << std::endl;
            f(5);
        }
A:testFunc(function(x) print("hello std::function") end)

Known bug at the moment.

This has been fixed as of this pull request. The problem was with two-phase lookup and g++, which did not consider an overload defined later than was available in the list of overloads provided to stack::get (free functions must be defined before they can be used). In this manner, since the std::function overload was not a specialization, it was ignored for consideration with stack::get's internal mechanisms. Forward-declaring and then defining the function later fixes the problem and has GCC picking the right overload.