gpt4 book ai didi

function - 如何将 Lua 函数传递给 C 函数并多次执行 Lua 函数?

转载 作者:行者123 更新时间:2023-12-02 19:11:26 27 4
gpt4 key购买 nike

我想要做的是创建一个函数,它将迭代一些对象并为每个函数调用一个函数。我使用的是 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 )

通常你只需说“lua_getglobal(L,name)”,它就会把 lua 函数很好地放入堆栈中,但是如何从参数中获取它呢?

编辑

我回去并实际尝试使用 luaL_ref()来自this question I found earlier 。我正在做的是使用 luaL_ref() 从堆栈顶部弹出函数值并将其放入临时寄存器中,我使用从 luaL_ref() 返回的值使用lua_rawgeti()对于列表中的每个项目。然后用luaL_unref()列表完成后释放该寄存器。

最佳答案

由于我自己也是 Lua 新手,所以我也有同样的问题。在我看来,因为没有令人满意的答案,所以我想我会写一个,即使这个问题可能永远不会结束。希望这会对遇到这种情况的其他人有所帮助。

ma​​in.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;
}

callback.lua

function MyCallback()
print("Hello World!")
end

RegisterCallback( MyCallback )

关于function - 如何将 Lua 函数传递给 C 函数并多次执行 Lua 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4928973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com