如何将 Lua 函数传递给 C 函数并多次执行 Lua 函数? [英] How do you pass a Lua Function to a C Function and execute the Lua Function Several Times?

查看:39
本文介绍了如何将 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,name)",它就可以很好地将 lua 函数放在堆栈中,但你如何从参数中获取它?

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()返回的值来使用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.

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 )

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

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