当前位置: 首页 > 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

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

http://www.xdnf.cn/news/289153.html

相关文章:

  • 什么是加油站专用可燃气体磁吸无线检测探测器
  • 小米刷新率 2.4 | 突破屏幕刷新率限制,享受更流畅视觉体验的应用程序
  • 《类和对象(上)》
  • 架构思维:构建高并发读服务_基于流量回放实现读服务的自动化测试回归方案
  • 直方图比较
  • SecureCRT 使用指南:安装、设置与高效操作
  • springboot+mysql+element-plus+vue完整实现汽车租赁系统
  • 文本三剑客试题
  • 【Elasticsearch入门到落地】12、索引库删除判断以及文档增删改查
  • 【Leetcode 每日一题 - 补卡】1128. 等价多米诺骨牌对的数量
  • 【Unity】AssetBundle热更新
  • Java中线程间数据共享:ThreadLocal与ScopedValue
  • 二、【LLaMA-Factory实战】数据工程全流程:从格式规范到高质量数据集构建
  • Qt 显示QRegExp 和 QtXml 不存在问题
  • 线程池配置不合理:系统性能的隐形杀手(深度解析版)
  • Python基本环境搭配
  • 代码随想录第32天:动态规划5(组合、排列、最小方法数)
  • 二、Python变量基础(2)
  • STM32 PulseSensor心跳传感器驱动代码
  • 常用非对称加密算法的Python实现及详解
  • simulink使能子系统的四种配置
  • uniapp开发06-视频组件video的使用注意事项
  • 大数据分析在视频监视方面的应用综述
  • ROS2 开发踩坑记录(持续更新...)
  • Serverless
  • 机器学习项目流程极简入门:从数据到部署的完整指南
  • 物联网mqtt和互联网http协议区别
  • 硬件工程师面试常见问题(14)
  • [学习] RTKlib详解:功能、工具与源码结构解析
  • 基于MATLAB的图像色彩识别项目,彩色图像矩阵识别