首页 / LUA / 深入lua栈交互—cpp调用lua数据
深入lua栈交互—cpp调用lua数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了深入lua栈交互—cpp调用lua数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2231字,纯文字阅读大概需要4分钟。
内容图文
lua是通过lua_state这个栈来和c 交互的
1.....lua栈 index 下往上增长 如: 1 2 3 4 5 6 2.....lua栈 index 是循环的 如下 index 上到下 是 3 2 1 0 -1 -2 -3 ,栈对应的值为 1 2 3 x 1 2 3 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的数据 例子
extern "C"{ #include "src/lualib.h" #include "src/lauxlib.h" #include "src/lua.h" } #include "iostream" using namespace std; lua_State*l; int get_sum(int x, int y) { int sum=0; lua_getglobal(l, "get_sum");/*调用函数*/ lua_pushnumber(l, x); lua_pushnumber(l, y); lua_call(l, 2, 3);/*参数2个,返回值3个*/ cout << "top is " << lua_gettop(l) << endl; cout << lua_tonumber(l, lua_gettop(l) - 0) << endl; cout << lua_tonumber(l, lua_gettop(l) - 1) << endl; cout << lua_tonumber(l, lua_gettop(l) - 2) << endl; cout << lua_tonumber(l, lua_gettop(l) - 2) << endl; cout << lua_tonumber(l, lua_gettop(l) - 3) << endl; cout << lua_tonumber(l, lua_gettop(l) - 4) << endl; cout << lua_tonumber(l, lua_gettop(l) - 5) << endl; cout << lua_tonumber(l, lua_gettop(l) - 6) << endl; cout << lua_tonumber(l, lua_gettop(l) - 7) << endl; cout << lua_tonumber(l, lua_gettop(l) - 8) << endl; cout << lua_tonumber(l, lua_gettop(l) - 9) << endl; lua_pop(l, 3);/*function返回了3个值*/ cout << "\n\n" << endl; lua_getglobal(l, "b");/*获取变量 压入栈中*/ cout <<"b=" <<lua_tonumber(l, lua_gettop(l)/*1*/ ) << endl; lua_getglobal(l, "a");/*获取变量 压入栈中*/ cout << "a=" << lua_tonumber(l, lua_gettop(l)/*1*/) << endl; lua_getglobal(l, "c");/*获取变量 压入栈中*/ cout << "c=" << lua_tonumber(l, lua_gettop(l)/*1*/) << endl; lua_pop(l, 3);/*清除栈*/ cout << "top is" << lua_gettop(l) << endl; return sum; } int main() { l = lua_open(); luaL_openlibs(l); luaL_dofile(l, "a.lua"); //cout << get_sum(1, 2) << endl; get_sum(1, 2); lua_close(l); system("pause"); return 0; }
a=10;
b=11;
c=12;
function get_sum(arg_1,arg_2)
return arg_1+arg_2,"100","200";
end
原文:http://my.oschina.net/kkkkkkkkkkkkk/blog/518154
内容总结
以上是互联网集市为您收集整理的深入lua栈交互—cpp调用lua数据全部内容,希望文章能够帮你解决深入lua栈交互—cpp调用lua数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。