satoren/kaguya

how to bind the function from lua?

owen200008 opened this issue · 5 comments

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

if use this way, it will be error!

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

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

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;
}

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.

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