当前位置 : 主页 > 网络编程 > lua >

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

来源:互联网 收集:自由互联 发布时间:2021-06-23
我想要做的是创建一个函数,它将迭代一些对象并为每个函数调用一个函数.我使用的是BlitzMax,而不是C,但除此之外,因为它有一个完整的Lua C函数包装器. Lua有一个lua_pushcfunction()命令,但是
我想要做的是创建一个函数,它将迭代一些对象并为每个函数调用一个函数.我使用的是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函数很好地放在堆栈上,但是如何从一个参数中得到它?

编辑

我回去并且实际上尝试使用this question I found earlier中的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;
}

callback.lua

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

RegisterCallback( MyCallback )
网友评论