Gal*_*613 4 lua stack arguments push function
我想要做的是创建一个函数,它将迭代一些对象并为每个函数调用一个函数.我使用的是BlitzMax,而不是C,但除此之外,因为它有一个完整的Lua C函数包装器.Lua有一个lua_pushcfunction()命令,但是它的lua_pushfunction()命令在哪里?调用具有名称的函数非常容易,但是如何调用作为参数传递的函数?
就像是:
ForEach( PlanetList, function (planet)
if(planet.exists == true) then
Planet_Count = Planet_Count + 1
end
end )
Run Code Online (Sandbox Code Playgroud)
通常你只是说"lua_getglobal(L,name)"并且它将lua函数很好地放在堆栈上,但是如何从一个参数中得到它?
编辑
我回去,并使用真实目的是试图luaL_ref()从这个问题,我发现较早.使用luaL_ref()弹出从堆栈的顶部的函数值,并把它变成一个临时寄存器什么我做的是,我用从返回的值luaL_ref()使用lua_rawgeti()的列表中的每个项目.然后luaL_unref()在列表完成后使用以释放该寄存器.
因为我自己是Lua的新手,所以我有同样的问题.因为在我看来,没有令人满意的答案,我想我会写一个,即使这个问题可能永远不会被关闭.希望这会在这种情况下帮助其他人.
main.c中
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
/* this keeps our Lua reference to the Lua function */
int callback_reference = 0;
/* this is called by Lua to register its function */
int lua_registerCallback( lua_State *L ) {
/* store the reference to the Lua function in a variable to be used later */
callback_reference = luaL_ref( L, LUA_REGISTRYINDEX );
return 0;
}
/* calls our Lua callback function and resets the callback reference */
void call_callback( lua_State *L ) {
/* push the callback onto the stack using the Lua reference we */
/* stored in the registry */
lua_rawgeti( L, LUA_REGISTRYINDEX, callback_reference );
/* duplicate the value on the stack */
/* NOTE: This is unnecessary, but it shows how you keep the */
/* callback for later */
lua_pushvalue( L, 1 );
/* call the callback */
/* NOTE: This is using the one we duplicated with lua_pushvalue */
if ( 0 != lua_pcall( L, 0, 0, 0 ) ) {
printf("Failed to call the callback!\n %s\n", lua_tostring( L, -1 ) );
return;
}
/* get a new reference to the Lua function and store it again */
/* NOTE: This is only used in conjunction with the lua_pushvalue */
/* above and can be removed if you remove that */
callback_reference = luaL_ref( L, LUA_REGISTRYINDEX );
}
int main( void ) {
/* set up Lua */
lua_State *L = lua_open();
luaL_openlibs( L );
/* register the lua_registerCallback function as */
/* "RegisterCallback" so it can be called by Lua */
lua_pushcfunction( L, lua_registerCallback );
lua_setglobal( L, "RegisterCallback" );
/* run our Lua file */
if ( 0 != luaL_dofile( L, "callback.lua" ) ) {
printf("Failed to load calback.lua!\n %s",
lua_tostring( L, -1 ) );
lua_close( L );
return 1;
}
/* call the callback */
call_callback( L );
/* call the callback again if you want (because we restored */
/* the Lua function reference) */
call_callback( L );
/* remove the reference to the callback */
/* NOTE: This is also unnecessary if you didn't re-add the */
/* function to the registry */
luaL_unref( L, LUA_REGISTRYINDEX, callback_reference );
/* uninitialize Lua */
lua_close( L );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
callback.lua
function MyCallback()
print("Hello World!")
end
RegisterCallback( MyCallback )
Run Code Online (Sandbox Code Playgroud)
lua_pushvalue一旦函数在堆栈上,就可以使用它来复制它。
更新:每次要调用该函数时,您都需要调用 lua_pushvalue(),因为实际使用 lua_pcall() 或 lua_call() 调用该函数将从堆栈中弹出该函数。
| 归档时间: |
|
| 查看次数: |
3567 次 |
| 最近记录: |