51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2036|回复: 0
打印 上一主题 下一主题

[原创] 深入lua栈交互—cpp调用lua数据

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-3-13 17:12:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

用原生API来交互

lua是通过lua_state这个栈来和c 交互的

  1. 1.....lua栈 index 下往上增长 如: 1 2 3 4 5 6
  2. 2.....lua栈 index 是循环的 如下 index 上到下 是 3 2 1 0 -1 -2 -3 ,栈对应的值为
  3.     1
  4.     2
  5.     3
  6.     x
  7.     1
  8.     2
  9.     3
  10.    
  11. 3......lua函数多个返回值如果上面是function返回了3个返回值,那么return a ,b,c  中 a=3 b=2 c=1 第一个返回值先入栈
复制代码

栈pop问题:lua_pop(x) ;x 为 pop的个数 ,一般调用函数后 pop(1) 因为一般返回值只有一个

ps:调用函数的时候 不会堆栈平衡,返回时已经平衡了,值需要对返回的值  占用的栈清理


c++使用lua的数据 例子

  1. extern "C"{
  2. #include "src/lualib.h"
  3. #include "src/lauxlib.h"
  4. #include "src/lua.h"

  5. }

  6. #include "iostream"
  7. using namespace std;

  8. lua_State*l;


  9. int get_sum(int x, int y)
  10. {
  11.         int sum=0;

  12.         lua_getglobal(l, "get_sum");/*调用函数*/

  13.         lua_pushnumber(l, x);
  14.         lua_pushnumber(l, y);

  15.         lua_call(l, 2, 3);/*参数2个,返回值3个*/

  16.         cout << "top is  " << lua_gettop(l) << endl;
  17.         cout << lua_tonumber(l, lua_gettop(l) - 0) << endl;
  18.         cout << lua_tonumber(l, lua_gettop(l) - 1) << endl;
  19.         cout << lua_tonumber(l, lua_gettop(l) - 2) << endl;
  20.         cout << lua_tonumber(l, lua_gettop(l) - 2) << endl;
  21.         cout << lua_tonumber(l, lua_gettop(l) - 3) << endl;
  22.         cout << lua_tonumber(l, lua_gettop(l) - 4) << endl;
  23.         cout << lua_tonumber(l, lua_gettop(l) - 5) << endl;
  24.         cout << lua_tonumber(l, lua_gettop(l) - 6) << endl;
  25.         cout << lua_tonumber(l, lua_gettop(l) - 7) << endl;
  26.         cout << lua_tonumber(l, lua_gettop(l) - 8) << endl;
  27.         cout << lua_tonumber(l, lua_gettop(l) - 9) << endl;


  28.         lua_pop(l, 3);/*function返回了3个值*/


  29.         cout << "\n\n" << endl;

  30.         lua_getglobal(l, "b");/*获取变量 压入栈中*/
  31.         cout <<"b=" <<lua_tonumber(l, lua_gettop(l)/*1*/ ) << endl;

  32.         lua_getglobal(l, "a");/*获取变量 压入栈中*/
  33.         cout << "a=" << lua_tonumber(l, lua_gettop(l)/*1*/) << endl;

  34.         lua_getglobal(l, "c");/*获取变量 压入栈中*/
  35.         cout << "c=" << lua_tonumber(l, lua_gettop(l)/*1*/) << endl;

  36.         lua_pop(l, 3);/*清除栈*/
  37.         cout << "top is" << lua_gettop(l) << endl;


  38.         return sum;
  39. }





  40. int main()
  41. {
  42.         l = lua_open();

  43.         luaL_openlibs(l);


  44.         luaL_dofile(l, "a.lua");

  45.         //cout << get_sum(1, 2) << endl;

  46.         get_sum(1, 2);

  47.         lua_close(l);

  48.         system("pause");
  49.         return 0;
  50. }
复制代码

a=10;

b=11;

c=12;


function get_sum(arg_1,arg_2)

return arg_1+arg_2,"100","200";

end



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /2 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-6-7 11:00 , Processed in 0.061986 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表