lua和C++相互交换数据

下面的代码演示了在C++和lua脚本之间传递数据。

首先在C++中创建一个table,添加元素,然后放置到lua的全局表中。在lua脚本中可以使用C++创建的这个表。

然后在脚本中创建一个表,以脚本返回值的方式返回给C++,在C++中可以读取表中的值。

 

例子代码需要一个args.lua的lua文件,要手工创建,我把它放到了C盘根目录下。

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // cpplua.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6. extern "C"  
  7. {  
  8. #include <lua.h>  
  9. #include <lauxlib.h>  
  10. #include <lualib.h>  
  11. }  
  12. #include <iostream>  
  13.   
  14. /* args.lua文件的内容  
  15. io.write( "[lua] These args were passed into the script from C/n" );  
  16.   
  17. for i=1,table.getn(arg) do  
  18. print(i,arg[i])  
  19. end  
  20.   
  21. io.write("[lua] Script returning data back to C/n")  
  22.   
  23. local temp = {}  
  24. temp[1]=9  
  25. temp[2]=8  
  26. temp[3]=7  
  27. temp[4]=6  
  28. temp[5]=5  
  29. temp["test1 key"]="test1 value"  
  30. temp[6]="test 6"  
  31. temp["test 99"]=99  
  32.   
  33. for i,n in pairs(temp)  
  34. do   
  35. print (i,n)  
  36. end  
  37.   
  38. return temp,9,1  
  39.   
  40. */  
  41. int _tmain(int argc, _TCHAR* argv[])  
  42. {  
  43.     int status;  
  44.   
  45.     // lua_open: 创建一个新的lua环境  
  46.     lua_State* state = lua_open();  
  47.   
  48.     // 在state环境上打开标准库,  
  49.     // 标准库包括:  
  50.     // luaopen_base  
  51.     // luaopen_package  
  52.     // luaopen_table  
  53.     // luaopen_io  
  54.     // luaopen_os  
  55.     // luaopen_string  
  56.     // luaopen_math  
  57.     // luaopen_debug  
  58.     luaL_openlibs(state);  /* open libraries */  
  59.   
  60.     status = luaL_loadfile( state, "c://args.lua" );  
  61.   
  62.     std::cout << "[C++] Passing 'arg' array to script" << std::endl;  
  63.   
  64.     // 创建一个新的表  
  65.     lua_newtable( state );  
  66.   
  67.     //  
  68.     // set first element "1" to value 45  
  69.     //  
  70.     // 调用lua的函数,都是通过压栈出栈来完成的  
  71.     // 为表执行一个t[k]=v的操作,则需要先将k压栈,再将v压栈,再调用操作函数  
  72.     // lua_rawset直接赋值(不触发metamethods方法)。   
  73.       
  74.     // lua_rawset/lua_settable使用:  
  75.     // 它从栈中获取参数。以table在栈中的索引作为参数,  
  76.     // 并将栈中的key和value出栈。  
  77.     // lua_pushnumber函数调用之前,  
  78.     // table是在栈顶位置(索引为-1)。index和value入栈之后,  
  79.     // table索引变为-3。  
  80.     lua_pushnumber( state, 1 );  
  81.     lua_pushnumber( state, 45 );  
  82.     lua_rawset( state, -3 );  
  83.   
  84.     // set second element "2" to value 99  
  85.     lua_pushnumber( state, 2 );  
  86.     lua_pushnumber( state, 99 );  
  87.     lua_rawset( state, -3 );  
  88.   
  89.     // set the number of elements (index to the last array element)  
  90.     // lua_pushliteral压入一个字符串,不需要指定长度  
  91.     // 如果lua_pushlstring,则需要指定长度  
  92.     lua_pushliteral( state, "n" );  
  93.     lua_pushnumber( state, 2 );  
  94.     lua_rawset( state, -3 );  
  95.   
  96.     // set the name of the array that the script will access  
  97.     // Pops a value from the stack and sets it as the new value of global name.  
  98.     // 从栈顶弹出一个值,并将其设置全局变量"arg"的新值。  
  99.     lua_setglobal( state, "arg" );  
  100.   
  101.   
  102.     std::cout << "[C++] Running script" << std::endl;  
  103.   
  104.     int result = 0;  
  105.     if (status == 0)  
  106.     {  
  107.         result = lua_pcall( state, 0, LUA_MULTRET, 0 );  
  108.     }  
  109.     else  
  110.     {  
  111.         std::cout << "bad" << std::endl;  
  112.     }  
  113.   
  114.     if (result != 0)  
  115.     {  
  116.         std::cerr << "[C++] script failed" << std::endl;  
  117.     }  
  118.   
  119.     std::cout << "[C++] These values were returned from the script" << std::endl;  
  120.   
  121.     // lua_gettop返回栈顶的索引  
  122.     // 如果索引为0,则表示栈为空  
  123.     while (lua_gettop( state ))  
  124.     {  
  125.         switch (lua_type( state, lua_gettop( state ) ))  
  126.         {  
  127.         case LUA_TNUMBER:  
  128.             {  
  129.                 std::cout << "script returned " << lua_tonumber( state, lua_gettop( state ) ) << std::endl;   
  130.                 break;  
  131.             }  
  132.         case LUA_TTABLE:    
  133.             {  
  134.                 std::cout << "script returned a table" << std::endl;   
  135.                   
  136.                 // 简单的遍历表的功能  
  137.                 // ***好像lua不保存表的元素的添加顺序***  
  138.   
  139.                 // 压入第一个键  
  140.                 lua_pushnil(state);  /* 第一个 key */  
  141.                 int t = -2;  
  142.                 while (lua_next(state, t) != 0)  
  143.                 {  
  144.                     /* 'key' (索引-2) 和 'value' (索引-1) */  
  145.                     const char* key = "unknown";  
  146.                     const char* value;  
  147.                     if(lua_type(state, -2) == LUA_TSTRING)  
  148.                     {  
  149.                         key = lua_tostring(state, -2);  
  150.                         value = lua_tostring(state, -1);  
  151.                     }  
  152.                     else if(lua_type(state, -2) == LUA_TNUMBER)  
  153.                     {  
  154.                         // 因为lua_tostring会更改栈上的元素,  
  155.                         // 所以不能直接在key上进行lua_tostring  
  156.                         // 因此,复制一个key,压入栈顶,进行lua_tostring  
  157.                         lua_pushvalue(state, -2);  
  158.                         key = lua_tostring(state, -1);  
  159.                         lua_pop(state, 1);  
  160.                         value = lua_tostring(state, -1);  
  161.                     }  
  162.                     else  
  163.                     {  
  164.                         value = lua_tostring(state, -1);  
  165.                     }  
  166.   
  167.                     std::cout   <<"key="<< key   
  168.                                 << ", value=" << value << std::endl;  
  169.   
  170.                     /* 移除 'value' ;保留 'key' 做下一次迭代 */  
  171.                     lua_pop(state, 1);  
  172.                 }  
  173.   
  174.                 break;  
  175.             }  
  176.         case LUA_TSTRING:   
  177.             {  
  178.                 std::cout << "script returned " << lua_tostring( state, lua_gettop( state ) ) << std::endl;  
  179.                 break;  
  180.             }  
  181.         case LUA_TBOOLEAN:  
  182.             {  
  183.                 std::cout << "script returned " << lua_toboolean( state, lua_gettop( state ) ) << std::endl;  
  184.                 break;  
  185.             }  
  186.         default:   
  187.             std::cout << "script returned unknown param" << std::endl;   
  188.             break;  
  189.         }  
  190.         lua_pop( state, 1 );  
  191.     }  
  192.     lua_close( state );  
  193.     return 0;  
  194. }  

本例用了一个控制台工程,输出如下:

[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
1       9
2       8
3       7
4       6
5       5
6       test 6
test 99 99
test1 key       test1 value
[C++] These values were returned from the script
script returned 1
script returned 9
script returned a table
key=1, value=9
key=2, value=8
key=3, value=7
key=4, value=6
key=5, value=5
key=6, value=test 6
key=test 99, value=99
key=test1 key, value=test1 value

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值