Cpp Lua 数据传递

lua-users home
wiki

此示例代码演示了如何在 C++ 和 lua 脚本之间传递数据。阅读以下两段代码中的注释和文本输出(cout 和 print 语句)。

运行程序后

这是运行下面列出的代码后的输出。
[C++] Passing 'arg' array to script
[C++] Running script
[lua] These args were passed into the script from C
1       45
2       99
[lua] Script returning data back to C
[C++] These values were returned from the script
script returned 1
script returned 9
script returned a table

C++ 代码

extern "C"
{
   #include <lua.h>
   #include <lauxlib.h>
   #include <lualib.h>
}
#include <iostream>

// a list of libs to expose for use in scripts
static const luaL_reg lualibs[] = 
{
  {"base", luaopen_base},
  {"table", luaopen_table},
  {"io", luaopen_io},
  {"string", luaopen_string},
  {"math", luaopen_math},
  {"debug", luaopen_debug},
  {"loadlib", luaopen_loadlib}, 
  /* add your libraries here */
  {NULL, NULL}
};

// load the list of libs
static void openstdlibs( lua_State *l ) 
{
  const luaL_reg *lib = lualibs;
  for (; lib->func; lib++) 
  {
    lib->func(l);  /* open library */
    lua_settop(l, 0);  /* discard any results */
  }
}

int main()
{
   int status;
   lua_State* state = lua_open();

  
   openstdlibs( state );
   status = luaL_loadfile( state, "args.lua" );

   std::cout << "[C++] Passing 'arg' array to script" << std::endl;

   // start array structure
   lua_newtable( state );

   // set first element "1" to value 45
   lua_pushnumber( state, 1 );
   lua_pushnumber( state, 45 );
   lua_rawset( state, -3 );

   // set second element "2" to value 99
   lua_pushnumber( state, 2 );
   lua_pushnumber( state, 99 );
   lua_rawset( state, -3 );
  
   // set the number of elements (index to the last array element)
   lua_pushliteral( state, "n" );
   lua_pushnumber( state, 2 );
   lua_rawset( state, -3 );

   // set the name of the array that the script will access
   lua_setglobal( state, "arg" );


   std::cout << "[C++] Running script" << std::endl;
   
   int result = 0;
   if (status == 0)
      result = lua_pcall( state, 0, LUA_MULTRET, 0 );
   else
      std::cout << "bad" << std::endl;
   
   if (result != 0)
      std::cerr << "[C++] script failed" << std::endl;

   std::cout << "[C++] These values were returned from the script" << std::endl;
   while (lua_gettop( state ))
   {
      switch (lua_type( state, lua_gettop( state ) ))
      {
         case LUA_TNUMBER: std::cout << "script returned " << lua_tonumber( state, lua_gettop( state ) ) << std::endl; break;
         case LUA_TTABLE:  std::cout << "script returned a table" << std::endl; break;
         case LUA_TSTRING: std::cout << "script returned " << lua_tostring( state, lua_gettop( state ) ) << std::endl; break;
         case LUA_TBOOLEAN:std::cout << "script returned " << lua_toboolean( state, lua_gettop( state ) ) << std::endl; break;
         default: std::cout << "script returned unknown param" << std::endl; break;
      }
      lua_pop( state, 1 );
   }
   lua_close( state );
   return 0;
}

Lua 脚本“args.lua”

io.write( "[lua] These args were passed into the script from C\n" );

for i=1,table.getn(arg) do
   print(i,arg[i])
end

io.write("[lua] Script returning data back to C\n")

local temp = {}
temp[1]=9
temp[2]=8
temp[3]=7
temp[4]=6
temp[5]=5

return temp,9,1

最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2020 年 6 月 2 日下午 3:26 GMT (差异)