satoren / kaguya

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

how to bind the function from lua? #68

Closed owen200008 closed 7 years ago

owen200008 commented 7 years ago
tbProject["BindNetStateChangeMsg"] = kaguya::function([](const std::function<void(int)>& func){
    g_NetStateChangeFunc = func;
});

if use this way, it will be error!

owen200008 commented 7 years ago

cocos2dx lua5.1 all error in lua_ref_impl.hpp:300 lua_rawgeti(state, LUAREGISTRYINDEX, ref.ref());(push funtion)

owen200008 commented 7 years ago

if the bind function call in coroutine the the coroutine exit. C/C++ code call the lua bind function in main lua state. is it possible create this error?

ok, it is error. how to call the function in main lua state. it contain the state coroutine lua state

owen200008 commented 7 years ago

now the problem is how to to mainthread in lua5.1

inline lua_State toMainThread(lua_State state) {

if LUA_VERSION_NUM >= 502

if (state) { lua_rawgeti(state, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *mainthread = lua_tothread(state, -1); lua_pop(state, 1); if (mainthread) { return mainthread; } }

endif

return state; }

satoren commented 7 years ago

I'm sorry , I don't know how to get main thread from coroutine in lua5.1. How about the following plan? I implement to function for register main thread and use that in toMainThread. If you can call any function from main thread, you register it.

owen200008 commented 7 years ago

i think so. register callbck is better, default is same