Tomasu / LuaGlue

C++11 Lua 5.2 Binding Library
zlib License
79 stars 22 forks source link

Static method and callback invocation #67

Closed ehrhart closed 10 years ago

ehrhart commented 10 years ago

Hello, I have a class with a static function used to register a lua callback.

When I call luaCallback it prints an error: "LuaGlueLuaFuncRef expected a function, got a table"

Is there any way to make it work?

Thanks

main.cpp:
class StaticInvoke
{
public:
    static void luaCallback(std::function<void(int)> f) { printf("calling std::func\n"); f(123); }
};

int main(int, char **)
{
    LuaGlue state;

    auto &Class = state.Class<StaticInvoke>("StaticInvoke").
    method("luaCallback", &StaticInvoke::luaCallback);

    state.open().glue();

    if(!state.doFile("static_invoke.lua"))
    {
        printf("failed to dofile: invoke.lua\n");
        printf("err: %s\n", state.lastError().c_str());
    }

    return 0;
}
static_invoke.lua:
function callback(i)
    io.write("in lua callback! got: "..i.."\n");
end

StaticInvoke.luaCallback(callback);
Error:
err: LuaGlueLuaFuncRef expected a function, got a table
Tomasu commented 10 years ago

Hmm, just a guess StaticInvoke is getting passed to it, even though it shouldn't be. I'll take a look at it "soon".

Tomasu commented 10 years ago

For kicks, what happens if you call it like so: StaticInvoke:luaCallback(callback); ?

ehrhart commented 10 years ago

Thanks for your reply Tomasu. Same thing happens if I call StaticInvoke:luaCallback(callback);