c和lua进行的一些基础交互和处理,这里留用lua5.1
2017-11-15 20:50
337 查看
#include <iostream> extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } static int my_add(lua_State *L) { int a = luaL_checknumber(L, 1); int b = luaL_checknumber(L, 2); lua_pushnumber(L, a + b); return 1; } static const struct luaL_Reg mylib[] = { {"madd", my_add }, {NULL, NULL} }; int main(int argc, char** argv) { lua_State *L = luaL_newstate(); luaL_openlibs(L); //============================================= //c和lua交互部分 lua_pushcfunction(L, my_add); lua_setglobal(L, "madd"); // lua中调用时 print(madd(10,20)) luaL_register(L, "mylib", mylib); // lua中调用时 local lib require "mylib" print(lib.madd(10,20)) //============================================ //lua调用c函数 // 参考上面 //============================================ //c调用lua函数 lua_getglobal(L, "math"); //lua_pushstring(L, "min"); lua_getfield(L, -1, "pow"); if (lua_isfunction(L, -1)) { lua_pushinteger(L, 3); lua_pushinteger(L, 2); lua_call(L, 2, 1); printf("result:%d\n", lua_tointeger(L, -1)); lua_pop(L, 1); } //============================================ //c中操作lua的表, 索引和kv操作 lua_newtable(L); lua_pushstring(L, "key"); lua_pushstring(L, "mm"); //由于上面两次压栈,现在table元素排在栈顶往下数第三的位置 lua_settable(L, -3); //lua_settable会自己弹出上面压入的key和value for (int i = 1; i < 10; i++) { lua_pushinteger(L, i); //lua_pushstring(L, "gogoxxxx" + i); lua_pushfstring(L, "gogoxxxx%d", i); lua_settable(L, -3); } lua_setglobal(L, "mk"); //lua_settable会自己弹出上面压入的key和value //============================================ //c中遍历lua的表 lua_getglobal(L, "mk"); size_t n = lua_objlen(L, -1); for (int i = 1; i <= n; ++i) { lua_pushinteger(L, i); lua_gettable(L, -2); printf("mk[%d] = %s\n", i, lua_tostring(L, -1)); lua_pop(L, 1); } printf("========================================\n"); int nIndex = lua_gettop(L); // 取 table 索引值 lua_pushnil(L); // nil 入栈作为初始 key while (0 != lua_next(L, nIndex)) { printf("mk = %s\n", lua_tostring(L, -1)); lua_pop(L, 1); // 弹出 value,让 key 留在栈顶 } // 现在栈顶是 table lua_pushstring(L, "=====>"); lua_setglobal(L, "kkm"); luaL_dofile(L, "test.lua"); lua_close(L); system("pause"); return 0; }
lua脚本部分
local lib = require "mylib" print("gogo"); print(mk.key); print(mk[1]); print(kkm); print(madd(100, 20)) print(lib.madd(120, 20))
相关文章推荐
- Lua5.1编程4:Lua与C交互基础
- Lua5.1编程四:Lua与C交互基础
- 使用Java进行图像处理的一些基础操作
- 在线Word编辑的jQuery插件时间:2010-12-29 09:15点击:122 次 【大 中 小】 在做OA或者工作流程的网站中,常常能够看到一些在线Word编辑进行文档处理的功能,这里我开发了
- Lua5.1编程四:Lua与C交互基础
- Lua基础 编译、运行、错误处理
- LUA学习笔记--泛型for进行迭代处理的细节
- 视频图像处理基础知识0(双线性插值算法进行图像缩放)
- css一些东西(这里主要有一个方法就是背景渐变,一般我们处理的时候是用图片)
- 开始进行C语言学习的一些基础东西,关键字”代码行“,”空行的使用“,”注释“,”代码行内的空格“等等
- 一些滤波操作1403:图像处理基础部分
- 【笔记】想学机器学习?这里有你需要知道的一些基础知识!
- ios 对于网络图片进行的一些处理
- 在开发过程中,数据的输入和输出全部用string类型进行交互,是否会更好一些?
- c++与lua的交互--表的处理
- 【Android】Glide 实现图片再处理(比如在右下角添加 GIF 标识、圆角处理、添加水印等,通过在原 bitmap 的基础上进行再处理)
- C/C++与Lua之间进行数据函数交互以及解决“PANIC: unprotected error in call to Lua API
- 利用Python进行数据分析(10) pandas基础: 处理缺失数据
- 为用户节省流量,app第一次安装或升级启动时,本地进行一些必要基础数据的存储,避免多余的下载
- Python学习入门基础教程(learning Python)--5.1 Python下文件处理基本过程