如何传递一个Lua函数C函数和执行Lua的功能几次? [英] How do you pass a Lua Function to a C Function and execute the Lua Function Several Times?

查看:438
本文介绍了如何传递一个Lua函数C函数和执行Lua的功能几次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的就是创建一个将通过一些对象进行迭代,并调用每个函数的函数的函数。我使用BlitzMax,而不是C,但这是除了点,因为它有Lua的C函数一个完整的包装。 lua有一个lua_pushcfunction()命令,哪来的这lua_pushfunction()命令?这是很容易调用具有名字的功能,但你怎么骂被作为参数传递的功能?

What I want to do is create a function that will iterate through some objects and call a function for each function. I'm using BlitzMax, not C, but that is besides the point because it has a full wrapper of Lua's C functions. Lua has a lua_pushcfunction() command, but where's it's lua_pushfunction() command? It is very easy to call functions that have a name, but how do you call a function that was passed as a argument?

是这样的:

ForEach( PlanetList, function (planet)
    if(planet.exists == true) then
        Planet_Count = Planet_Count + 1
    end
end )

通常你只是说lua_getglobal(L,姓名),它会将卢阿功能很好地在栈中,但你如何从一个参数得到它?

Usually you just say "lua_getglobal(L,name)" and it puts the lua function nicely on the stack, but how do you get it from an argument?

修改

我回去,并使用真实目的是试图 luaL_ref()来自的这个问题,我发现前面。我在做什么用luaL_ref()弹出从堆栈顶部的函数值,并把它变成一个临时寄存器,我用从 luaL_ref()返回的值使用 lua_rawgeti()对列表中的每个项目。然后使用 luaL_unref()

I went back and and actually tried using luaL_ref() from this question I found earlier. What I am doing is using luaL_ref() to pop the function value from the top of the stack and put it into a temporary register, I used the value returned from luaL_ref() to use lua_rawgeti() for each of the items in the list. And then used luaL_unref() after the list was finished to release that register.

推荐答案

我有同样的问题,因为我是新来LUA自己。因为,在我看来,没有满意的答案我想我会写一个,虽然这个问题可能永远不会被关闭。希望这将有助于其他一些在这种情况下。

I was having the same question since I'm new to Lua myself. Since, in my opinion, there was no satisfactory answer I figured I would write one, even though this question may not ever be closed. Hopefully this will help some else in this situation.

的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 )

这篇关于如何传递一个Lua函数C函数和执行Lua的功能几次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆