Hiroaki Nakamura wrote: > Now I am trying to get the callback function defined in another Lua state > and call pthread_create with the callback address. You must cast the Lua function ('hello') to a C function pointer and then to a number in the other state and then convert it back to a function pointer in the main state. Patch attached. --Mike
--- thread2.lua.orig +++ thread2.lua @@ -39,7 +39,7 @@ static const int LUA_GLOBALSINDEX = -10002; void lua_getfield(lua_State *L, int index, const char *k); -const void *lua_topointer(lua_State *L, int index); +ptrdiff_t lua_tointeger(lua_State *L, int index); void lua_settop(lua_State *L, int index); ]] @@ -49,16 +49,18 @@ assert(L ~= nil) C.luaL_openlibs(L) assert(C.luaL_loadstring(L, [[ -function hello() +local ffi = require("ffi") +local function hello() print("Hello from another Lua state!") end print(hello) +cb_hello = tonumber(ffi.cast('intptr_t', ffi.cast('void *(*)(void *)', hello))) ]]) == 0) assert(C.lua_pcall(L, 0, 1, 0) == 0) -C.lua_getfield(L, C.LUA_GLOBALSINDEX, 'hello') -local func_ptr = C.lua_topointer(L, -1); +C.lua_getfield(L, C.LUA_GLOBALSINDEX, 'cb_hello') +local func_ptr = C.lua_tointeger(L, -1); print('func_ptr', func_ptr) C.lua_settop(L, -2);