satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

std::function and self #90

Open joeroback opened 5 years ago

joeroback commented 5 years ago

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!

joeroback commented 5 years ago

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()
joeroback commented 5 years ago

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

joeroback commented 5 years ago

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...