【发布时间】:2012-08-27 07:20:24
【问题描述】:
那么,将 C 函数作为函数成员或将 C 函数注册为 lua 函数没有问题 lua_register(L, lua_func_name, c_func);
但是如何告诉 lua 我想从 C 传递 luaFoo() 作为“foober”的函数回调参数? lua_pushcfunction - 推送 C 函数,lua_pushstring 只推送纯字符串,所以回调字段变成了字符串,而不是函数。
Lua 代码:
CALLBACKS = {};
FOO = 0;
function luaFoo()
FOO = FOO + 1;
end;
function addCallback(_name, _callback)
CALLBACKS[_name] = _callback;
end;
function doCallback(_name)
CALLBACKS[_name]();
end;
C 代码:
static int c_foo(lua_State* l)
{
printf("FOO\n");
return 0;
}
/*load lua script*/;
lua_State* l = /*get lua state*/;
lua_getglobal(l, "addCallback");
lua_pushstring(l, "foober");
//What push for luaFoo()
lua_pushcfunction(l, c_foo);
lua_call(l, 2, 0);
lua_getglobal(l, "doCallback");
lua_pushstring(l, "foober");
lua_call(l, 1, 0);
类似 - 如果我得到已经注册到 lua_register 的 C 函数,如何将它们作为回调参数从 C 传递。所以我们注册 c_foo => c_foo 作为 lua 函数存在, 如何判断我们想要传递 "c_foo" 作为回调函数参数。
【问题讨论】: