如何从C(异步)执行Lua回调?
我想从C库中调用Lua中的函数,但要异步完成 我有一些Lua代码,如下所示:如何从C(异步)执行Lua回调?,c,lua,C,Lua,我想从C库中调用Lua中的函数,但要异步完成 我有一些Lua代码,如下所示: l = require "mylib" l.subscribe(function () print("Called") error("This is an error") end) mylib是使用luaapi实现的。看起来有点像这样 struct cb { int ref; lua_State *state; } int number_callbacks; struct **cb callbac
l = require "mylib"
l.subscribe(function ()
print("Called")
error("This is an error")
end)
mylib
是使用luaapi实现的。看起来有点像这样
struct cb {
int ref;
lua_State *state;
}
int number_callbacks;
struct **cb callback_functions;
static int subscribe(lua_State *L) {
lua_settop(L, 1);
luaL_checktype(L, 1, LUA_TFUNCTION);
struct cb c;
c.ref = luaL_ref(L, LUA_REGISTRYINDEX);
c.state = L;
// store c in callback_functions
}
// foo is registered as a callback to another C lib and
// is called occasionaly
static void foo(int callback_code) {
for (int i = 0; i < number_callbacks; i++) {
struct cb c = callback_functions[i];
lua_rawgeti(c->state, LUA_REGISTRYINDEX, c->ref);
lua_pushinteger(c->state, callback_code);
int ret = lua_pcall(c->state, 1, 0, 0);
// Check ret...
}
}
struct cb{
int-ref;
卢厄州*州;
}
int-number_回调;
结构**cb回调函数;
静态int订阅(lua_State*L){
lua_机顶盒(L,1);
LUA_checktype(L,1,LUA_函数);
结构cbc;
c、 ref=luaL\u ref(L,LUA\u注册表索引);
c、 状态=L;
//在回调函数中存储c
}
//foo被注册为对另一个C库的回调,并且
//有时被称为
静态void foo(int回调_代码){
for(int i=0;istate,lua_registeryindex,c->ref);
lua\u pushinteger(c->state,回调\u代码);
int ret=lua_pcall(c->state,1,0,0);
//检查ret。。。
}
}
回调中的消息会被打印出来,但是错误
显示在错误的位置(它说这是由一些不相关的函数引起的)。我想这是因为Lua状态发生了变化
正确的方法是什么?传递给error的参数在堆栈上传递,您检查了吗?@MarcBalmer Yes-read正在使用
lua\u-tostring
然后传递给luaL\u-error
。