satoren/kaguya

std::function and self

joeroback opened this issue · 3 comments

I have a callback that get's bound in lua and from c++ I'd like to invoke that, but self is always nil and I am not sure how to access self as the lua table, not the Client* C++ object...

local client = require 'client'
local view = {}

function view:onConnect()
  self.connected = true
end

function view:create()
  client:onConnect(self.onConnect)
end
m["Client"].setClass(
    UserdataMetatable<Client>()
        .addStaticFunction("onConnect", [](Client* self, const std::function<void(void)>& connectFn)
        {
            connectFn();
        })
);

Is this possible with kaguya?

Thanks!

similar example...

#include <kaguya/kaguya.hpp>

#include <cstdio>

typedef void (*OnConnectFn)(void);

void somefunction(const std::function<void()>& fn)
{
    fn();
}

extern "C" int luaopen_fn(lua_State *L)
{
    kaguya::State state(L);
    kaguya::LuaTable module = state.newTable();
    module["onConnect"] = kaguya::function(somefunction);
    return module.push();
}
local fn = require 'fn'
local view = {}

function view:onConnect()
    print('onConnect', self)
end

function view:main()
    print('main', self)
    print('fn', fn)
    fn.onConnect(self.onConnect)
end

view:main()

I've also tried LuaFunction but no luck having self != nil

this hack seems to work..

#include <kaguya/kaguya.hpp>

#include <cstdio>

typedef void (*OnConnectFn)(void);

void somefunction(kaguya::LuaFunction fn, kaguya::LuaTable self)
{
    fn(self);
}

extern "C" int luaopen_fn(lua_State *L)
{
    kaguya::State state(L);
    kaguya::LuaTable module = state.newTable();
    module["onConnect"] = kaguya::function(somefunction);
    return module.push();
}
local fn = require 'fn'
local view = {}

function view:onConnect()
    print('onConnect', self)
    if self then
        print('onConnect.data', self.data)
    end
end

function view:main()
    self.data = 'foobar'
    print('main', self)
    print('fn', fn)
    fn.onConnect(view.onConnect, self)
end

view:main()
view:onConnect()

my manually passing self lua table around...