当前位置: 首页 > news >正文

C++和Lua混和调用

为什么要C/C++

  • 流行的语言,学习人员多
  • 高性能,对于嵌入式设备则是省电
  • 大量的第三方库

为什么要Lua

  • C++缺点:编译慢,调试难,学习难度大
  • Lua优点:
    • 最快的脚本语言
    • 可以编译调试
    • 与C/C++结合容易
    • Lua是对性能有要求的必备脚本

lua基本语法

lua基础数据类型和变量

  • 全局变量
b = 10
  • 局部变量:尽量使用局部变量,保证变量控制
local b = 10
  • 数据类型

    • NIL

      • 用于区分具有一些数据或者没有数据的值
      • 全局变量设置为nil会交给垃圾回收
      local a = nil
      print(type(a)) --> nil
      
    • Booleans

      • Lua中所有的值都可以作为条件
      • 除了false和nil为假以外,其他的值都为真,0为真
    • Numbers

      • Lua中没有整数,都是用浮点数进行运算
      • 对应的c中的double类型
      • 新版中有基于64位的整形
      • tonumber()转换格式
    • Strings

      • tostring()格式转换
      • [[]]多行字符串赋值
      • 与C一样转义\
      • …字符串拼接
      • String 处理
        • 字符串长度string.len
        • 字符串子串string.sub(str, 3, 5)
        • 字符串查找local b,e = string.find(str, “HEAD”) 支持正则
        • 字符串替换string.gsub(str, "HEAD, “XCJ”)

lua控制结构语句

条件判断

  • if 条件语句
if conditions thentehn-part
elseif condition thenelseif-part
else else-part
end
  • 逻辑运算
    • and or not
    • < > <= >= ~= ==

循环语句

  • while循环语句
while condition dostatements
end

break 退出循环

  • repeat循环语句
repeatstatements
until conditions

break 退出循环

  • for 循环语句
/// 1
for var=from, to, step doloop-part
end
/// 2
for i,v in ipairs(a) do print(v)
end

break 退出循环

lua表和函数

lua表

  • 表的大小 table.getn(t1)
  • 插入 table.insert(a, pos, line)
    • 不传pos相当于push_back
  • 删除table.remove(a, pos)返回这次删除的值
    • 不传pos相当于pop_back
local tab1 = {"001", "002", "003"}
for i, v in ipairs(tab1) doprint(i..":"..v)
endprint("======= insert =======")
table.insert(tab1, 3, "002-2")table.insert(tab1, "004")
for i, v in ipairs(tab1) doprint(i..":"..v)
endprint("======= remove =======")
table.remove(tab1, 3)
table.remove(tab1)
for i, v in ipairs(tab1) doprint(i..":"..v)
endlocal tab1 = { id = 123, age = 20}
tab1["name"] = "aaa"
print("====== insert ======")
for k, v in pairs(tab1) doprint(k..":"..v)
endprint("====== remove ======")
tab1["id"] = nil
for k, v in pairs(tab1) doprint(k..":"..v)
endprint("====== tab3 ======")
local tab3 = {}tab3[1] = {"1", "2"}tab3[2] = {"3", "4"}
for k, v in pairs(tab3) dofor k2, v2 in pairs(v) doprint(k.."::"..k2..":"..v2)end
end

lua函数

  • 函数语法
function func_name(args)statement-list;
end
function test1(args)print(args)
end
function test2(args)return 1
end
test1(11)
print(test2(11))

lua调用C++

函数调用

#include <iostream>extern "C"
{#include "lua.h"#include "lauxlib.h"#include "lualib.h"
}int test(lua_State* L)
{printf("int test");return 0;
}int main()
{lua_State *L = lua_open();luaopen_base(L);luaopen_string(L);luaopen_table(L);lua_register(L, "test", test);luaL_loadfile(L, "main.lua");lua_pcall(L, 0, 0, 0);return 0;
}
test()

参数传递

  • 传递普通参数
#include <iostream>extern "C"
{#include "lua.h"#include "lauxlib.h"#include "lualib.h"
}int test(lua_State* L)
{printf("int test\n");size_t len;const char* str = lua_tolstring(L, 1, &len);printf("lua args %s\n", str);int age = lua_tointeger(L, 2);printf("lua args %d\n", age);return 0;
}int main()
{lua_State *L = lua_open();luaopen_base(L);luaopen_string(L);luaopen_table(L);lua_register(L, "test", test);luaL_loadfile(L, "main.lua");lua_pcall(L, 0, 0, 0);return 0;
}
test("hello lua", 123)
  • 传递数组
#include <iostream>extern "C"
{#include "lua.h"#include "lauxlib.h"#include "lualib.h"
}int test(lua_State* L)
{printf("int test\n");size_t len;const char* str = lua_tolstring(L, 1, &len);printf("lua args %s\n", str);int age = lua_tointeger(L, 2);printf("lua args %d\n", age);return 0;
}
int test_array(lua_State *L)
{printf("init test_array\n");int len = luaL_getn(L, 1);for (int i = 0; i < len; i++){lua_pushnumber(L, i + 1);lua_gettable(L, 1); // pop idx  push 压入tablesize_t len;printf("%s\n", lua_tolstring(L, -1, &len));lua_pop(L, 1);}return 0;
}int main()
{lua_State *L = lua_open();luaopen_base(L);luaopen_string(L);luaopen_table(L);lua_register(L, "test", test);lua_register(L, "test_array", test_array);luaL_loadfile(L, "main.lua");lua_pcall(L, 0, 0, 0);return 0;
}
local tab = {"001", "002", "003"}
test_array(tab)
  • 传递kv表
int test_array2(lua_State *L)
{printf("init test_array2\n");
//    lua_pushnil(L);
//    while (lua_next(L, 1) != 0)
//    {
//        printf("key = %s\n", lua_tostring(L, -2));
//        printf("value = %s\n", lua_tostring(L, -1));
//        lua_pop(L, 1);
//    }lua_getfield(L, 1, "age");printf("age = %s\n", lua_tostring(L, -1));return 0;
}
local tab = {name="xiaoming", age="22", id="007"}
test_array2(tab)
  • C++参数类型检查
int test_array2(lua_State *L)
{luaL_checktype(L, 1, LUA_TTABLE);if (lua_type(L, 2) != LUA_TNUMBER){printf("arg2 is not number\n");}printf("init test_array2\n");lua_getfield(L, 1, "age");printf("age = %s\n", lua_tostring(L, -1));return 0;
}
local tab = {name="xiaoming", age="22", id="007"}
local size = "108"
test_array2(tab, size)

返回值获取

  • C++返回值普通类型
int test_ret(lua_State *L)
{lua_pushstring(L, "test_ret");return 1;
}
print(test_ret())
  • 返回对象
int test_ret(lua_State *L)
{lua_newtable(L);lua_pushstring(L, "name");lua_pushstring(L, "zhangsan");lua_settable(L, -3);lua_pushstring(L, "age");lua_pushnumber(L, 21);lua_settable(L, -3);return 1;
}
tab = test_ret()
print(tab["name"])
print(tab["age"])

C++调用lua

全局变量访问(普通、表)

int main()
{lua_State *L = lua_open();luaopen_base(L);luaopen_string(L);luaopen_table(L);lua_register(L, "test", test);lua_register(L, "test_array", test_array);lua_register(L, "test_array2", test_array2);lua_register(L, "test_ret", test_ret);lua_pushstring(L, "hello");lua_setglobal(L, "test1_hello");lua_newtable(L);lua_pushstring(L, "name");lua_pushstring(L, "lisi");lua_settable(L, -3);lua_setglobal(L, "test1_table");if (luaL_loadfile(L, "main.lua")){const char *error = lua_tostring(L, -1);printf("lua call error: %s\n", error);return -1;}if (lua_pcall(L, 0, 0, 0)){const char *error = lua_tostring(L, -1);printf("lua call error: %s\n", error);return -1;}lua_getglobal(L, "width");int width = lua_tonumber(L, -1);lua_pop(L, 1);printf("width = %d\n", width);lua_getglobal(L, "tab1");lua_getfield(L, -1, "name");printf("%s\n", lua_tostring(L, -1));lua_getfield(L, -2, "age");printf("%d\n", (int)lua_tonumber(L, -1));lua_pop(L, 3);lua_close(L);return 0;
}
width = 20
tab1 = {name="zhangsan", age=20}
print(test1_hello)for i, v in pairs(test1_table) doprint(i..":"..v)
end

函数调用(参数,返回值)

int main()
{lua_State *L = lua_open();luaopen_base(L);luaopen_string(L);luaopen_table(L);lua_register(L, "test", test);lua_register(L, "test_array", test_array);lua_register(L, "test_array2", test_array2);lua_register(L, "test_ret", test_ret);lua_pushstring(L, "hello");lua_setglobal(L, "test1_hello");lua_newtable(L);lua_pushstring(L, "name");lua_pushstring(L, "lisi");lua_settable(L, -3);lua_setglobal(L, "test1_table");if (luaL_loadfile(L, "main.lua")){const char *error = lua_tostring(L, -1);printf("lua call error: %s\n", error);lua_pop(L, 1);}if (lua_pcall(L, 0, 0, 0)){const char *error = lua_tostring(L, -1);printf("lua call error: %s\n", error);lua_pop(L, 1);}lua_getglobal(L, "width");int width = lua_tonumber(L, -1);lua_pop(L, 1);printf("width = %d\n", width);lua_getglobal(L, "tab1");lua_getfield(L, -1, "name");printf("%s\n", lua_tostring(L, -1));lua_getfield(L, -2, "age");printf("%d\n", (int)lua_tonumber(L, -1));lua_pop(L, 3);// 调用函数lua_getglobal(L, "event");lua_pushstring(L, "key");lua_pushstring(L, "value");if (lua_pcall(L, 2, 1, 0) != 0){const char *error = lua_tostring(L, -1);printf("lua call error: %s\n", error);lua_pop(L, 1);}else{printf("lua call error: %s\n", lua_tostring(L, -1));lua_pop(L, 1);}printf("top is %d\n", lua_gettop(L));lua_close(L);return 0;
}
width = 20
tab1 = {name="zhangsan", age=20}
print(test1_hello)for i, v in pairs(test1_table) doprint(i..":"..v)
endfunction event(key, value)print("key:"..key.."  value:"..value)return "aaaaa"
endfunction event(args)for i,v in ipairs(args) doprint("key:"..i.."  value:"..v)end
end

备注: 注意栈空间清理,防止内存泄露, 防止多线程互斥问题。

相关文章:

  • 编译原理期末重点-个人总结——2 文法与语言
  • 相同IP和端口的服务器ssh连接时出现异常
  • 36-校园反诈系统(小程序)
  • JS DAY4 日期对象与节点
  • JAVA简单走进AI世界~Spring AI
  • Ubuntu K8S(1.28.2) 节点/etc/kubernetes/manifests 不存在
  • 二、【LLaMA-Factory实战】数据工程全流程:从格式规范到高质量数据集构建
  • 虚幻引擎5-Unreal Engine笔记之显卡环境设置使开发流畅
  • springboot+mysql+element-plus+vue完整实现汽车租赁系统
  • Vue3携手Echarts,打造炫酷数据可视化大屏
  • Flutter——数据库Drift开发详细教程(四)
  • GZ人博会自然资源系统(测绘)备考笔记
  • 享元模式(Flyweight Pattern)详解
  • 小米刷新率 2.4 | 突破屏幕刷新率限制,享受更流畅视觉体验的应用程序
  • 内存碎片深度剖析
  • 十大排序算法全面解析(Java实现)及优化策略
  • Java SE(8)——继承
  • 残差网络实战:基于MNIST数据集的手写数字识别
  • 主机漏洞扫描:如何保障网络安全及扫描原理与类型介绍?
  • JVM 内存结构全解析
  • 金融监管总局:正在修订并购贷款管理办法,将进一步释放并购贷款的潜力
  • 共生与伴生:关于人工智能时代艺术评论的对象与主体的思考
  • 日本儿童人数已连续44年减少,少子化问题越发严重
  • 巴菲特股东大会4.5万字问答实录:股神60年穿越牛熊的最新心得和人生思考
  • 世锦赛决赛今夜打响,斯诺克运动需要赵心童创造历史
  • 英国传统两党受挫地方选举后反思,改革党异军突起“突破想象”