C++和Lua混和调用

[复制链接]
发表于 2025-9-18 03:19:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
为什么要C/C++


  • 盛行的语言,学习职员多
  • 性能,对于嵌入式装备则是省电
  • 大量的第三方库
为什么要Lua


  • C++缺点:编译慢,调试难,学习难度大
  • Lua长处:

    • 最快的脚本语言
    • 可以编译调试
    • 与C/C++联合轻易
    • Lua是对性能有要求的必备脚本

lua基本语法

lua基础数据类型和变量


  • 全局变量
  1. b = 10
复制代码

  • 局部变量:尽量利用局部变量,包管变量控制
  1. local b = 10
复制代码

  • 数据类型

    • NIL

      • 用于区分具有一些数据大概没有数据的值
      • 全局变量设置为nil会交给垃圾接纳
      1. local a = nil
      2. 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 条件语句
  1. if conditions then
  2.         tehn-part
  3. elseif condition then
  4.         elseif-part
  5. else
  6.         else-part
  7. end
复制代码

  • 逻辑运算

    • and      or       not
    • <     >     <=      >=      ~=     ==

循环语句


  • while循环语句
  1. while condition do
  2.     statements
  3. end
复制代码
break 退出循环

  • repeat循环语句
  1. repeat
  2.     statements
  3. until conditions
复制代码
break 退出循环

  • for 循环语句
  1. /// 1
  2. for var=from, to, step do
  3.     loop-part
  4. end
  5. /// 2
  6. for i,v in ipairs(a) do
  7.     print(v)
  8. end
复制代码
break 退出循环
lua表和函数

lua表


  • 表的巨细 table.getn(t1)
  • 插入 table.insert(a, pos, line)

    • 不传pos相称于push_back

  • 删除table.remove(a, pos)返回这次删除的值

    • 不传pos相称于pop_back

  1. local tab1 = {"001", "002", "003"}
  2. for i, v in ipairs(tab1) do
  3.     print(i..":"..v)
  4. end
  5. print("======= insert =======")
  6. table.insert(tab1, 3, "002-2")
  7. table.insert(tab1, "004")
  8. for i, v in ipairs(tab1) do
  9.     print(i..":"..v)
  10. end
  11. print("======= remove =======")
  12. table.remove(tab1, 3)
  13. table.remove(tab1)
  14. for i, v in ipairs(tab1) do
  15.     print(i..":"..v)
  16. end
  17. local tab1 = { id = 123, age = 20}
  18. tab1["name"] = "aaa"
  19. print("====== insert ======")
  20. for k, v in pairs(tab1) do
  21.     print(k..":"..v)
  22. end
  23. print("====== remove ======")
  24. tab1["id"] = nil
  25. for k, v in pairs(tab1) do
  26.     print(k..":"..v)
  27. end
  28. print("====== tab3 ======")
  29. local tab3 = {}
  30. tab3[1] = {"1", "2"}
  31. tab3[2] = {"3", "4"}
  32. for k, v in pairs(tab3) do
  33.     for k2, v2 in pairs(v) do
  34.         print(k.."::"..k2..":"..v2)
  35.     end
  36. end
复制代码
lua函数


  • 函数语法
  1. function func_name(args)
  2.         statement-list;
  3. end
复制代码
  1. function test1(args)
  2.         print(args)
  3. end
  4. function test2(args)
  5.         return 1
  6. end
  7. test1(11)
  8. print(test2(11))
复制代码
lua调用C++

函数调用
  1. #include <iostream>
  2. extern "C"
  3. {
  4.    #include "lua.h"
  5.    #include "lauxlib.h"
  6.    #include "lualib.h"
  7. }
  8. int test(lua_State* L)
  9. {
  10.     printf("int test");
  11.     return 0;
  12. }
  13. int main()
  14. {
  15.     lua_State *L = lua_open();
  16.     luaopen_base(L);
  17.     luaopen_string(L);
  18.     luaopen_table(L);
  19.     lua_register(L, "test", test);
  20.     luaL_loadfile(L, "main.lua");
  21.     lua_pcall(L, 0, 0, 0);
  22.     return 0;
  23. }
复制代码
  1. test()
复制代码
参数通报


  • 通报普通参数
  1. #include <iostream>
  2. extern "C"
  3. {
  4.    #include "lua.h"
  5.    #include "lauxlib.h"
  6.    #include "lualib.h"
  7. }
  8. int test(lua_State* L)
  9. {
  10.     printf("int test\n");
  11.     size_t len;
  12.     const char* str = lua_tolstring(L, 1, &len);
  13.     printf("lua args %s\n", str);
  14.     int age = lua_tointeger(L, 2);
  15.     printf("lua args %d\n", age);
  16.     return 0;
  17. }
  18. int main()
  19. {
  20.     lua_State *L = lua_open();
  21.     luaopen_base(L);
  22.     luaopen_string(L);
  23.     luaopen_table(L);
  24.     lua_register(L, "test", test);
  25.     luaL_loadfile(L, "main.lua");
  26.     lua_pcall(L, 0, 0, 0);
  27.     return 0;
  28. }
复制代码
  1. test("hello lua", 123)
复制代码

  • 通报数组
  1. #include <iostream>
  2. extern "C"
  3. {
  4.    #include "lua.h"
  5.    #include "lauxlib.h"
  6.    #include "lualib.h"
  7. }
  8. int test(lua_State* L)
  9. {
  10.     printf("int test\n");
  11.     size_t len;
  12.     const char* str = lua_tolstring(L, 1, &len);
  13.     printf("lua args %s\n", str);
  14.     int age = lua_tointeger(L, 2);
  15.     printf("lua args %d\n", age);
  16.     return 0;
  17. }
  18. int test_array(lua_State *L)
  19. {
  20.     printf("init test_array\n");
  21.     int len = luaL_getn(L, 1);
  22.     for (int i = 0; i < len; i++)
  23.     {
  24.         lua_pushnumber(L, i + 1);
  25.         lua_gettable(L, 1); // pop idx  push 压入table
  26.         size_t len;
  27.         printf("%s\n", lua_tolstring(L, -1, &len));
  28.         lua_pop(L, 1);
  29.     }
  30.     return 0;
  31. }
  32. int main()
  33. {
  34.     lua_State *L = lua_open();
  35.     luaopen_base(L);
  36.     luaopen_string(L);
  37.     luaopen_table(L);
  38.     lua_register(L, "test", test);
  39.     lua_register(L, "test_array", test_array);
  40.     luaL_loadfile(L, "main.lua");
  41.     lua_pcall(L, 0, 0, 0);
  42.     return 0;
  43. }
复制代码
  1. local tab = {"001", "002", "003"}
  2. test_array(tab)
复制代码

  • 通报kv表
  1. int test_array2(lua_State *L)
  2. {
  3.     printf("init test_array2\n");
  4. //    lua_pushnil(L);
  5. //    while (lua_next(L, 1) != 0)
  6. //    {
  7. //        printf("key = %s\n", lua_tostring(L, -2));
  8. //        printf("value = %s\n", lua_tostring(L, -1));
  9. //        lua_pop(L, 1);
  10. //    }
  11.     lua_getfield(L, 1, "age");
  12.     printf("age = %s\n", lua_tostring(L, -1));
  13.     return 0;
  14. }
复制代码
  1. local tab = {name="xiaoming", age="22", id="007"}
  2. test_array2(tab)
复制代码

  • C++参数类型查抄
  1. int test_array2(lua_State *L)
  2. {
  3.     luaL_checktype(L, 1, LUA_TTABLE);
  4.     if (lua_type(L, 2) != LUA_TNUMBER)
  5.     {
  6.         printf("arg2 is not number\n");
  7.     }
  8.     printf("init test_array2\n");
  9.     lua_getfield(L, 1, "age");
  10.     printf("age = %s\n", lua_tostring(L, -1));
  11.     return 0;
  12. }
复制代码
  1. local tab = {name="xiaoming", age="22", id="007"}
  2. local size = "108"
  3. test_array2(tab, size)
复制代码
返回值获取


  • C++返回值普通类型
  1. int test_ret(lua_State *L)
  2. {
  3.     lua_pushstring(L, "test_ret");
  4.     return 1;
  5. }
复制代码
  1. print(test_ret())
复制代码

  • 返回对象
  1. int test_ret(lua_State *L)
  2. {
  3.     lua_newtable(L);
  4.     lua_pushstring(L, "name");
  5.     lua_pushstring(L, "zhangsan");
  6.     lua_settable(L, -3);
  7.     lua_pushstring(L, "age");
  8.     lua_pushnumber(L, 21);
  9.     lua_settable(L, -3);
  10.     return 1;
  11. }
复制代码
  1. tab = test_ret()
  2. print(tab["name"])
  3. print(tab["age"])
复制代码
C++调用lua

全局变量访问(普通、表)
  1. int main()
  2. {
  3.     lua_State *L = lua_open();
  4.     luaopen_base(L);
  5.     luaopen_string(L);
  6.     luaopen_table(L);
  7.     lua_register(L, "test", test);
  8.     lua_register(L, "test_array", test_array);
  9.     lua_register(L, "test_array2", test_array2);
  10.     lua_register(L, "test_ret", test_ret);
  11.     lua_pushstring(L, "hello");
  12.     lua_setglobal(L, "test1_hello");
  13.     lua_newtable(L);
  14.     lua_pushstring(L, "name");
  15.     lua_pushstring(L, "lisi");
  16.     lua_settable(L, -3);
  17.     lua_setglobal(L, "test1_table");
  18.     if (luaL_loadfile(L, "main.lua"))
  19.     {
  20.         const char *error = lua_tostring(L, -1);
  21.         printf("lua call error: %s\n", error);
  22.         return -1;
  23.     }
  24.     if (lua_pcall(L, 0, 0, 0))
  25.     {
  26.         const char *error = lua_tostring(L, -1);
  27.         printf("lua call error: %s\n", error);
  28.         return -1;
  29.     }
  30.     lua_getglobal(L, "width");
  31.     int width = lua_tonumber(L, -1);
  32.     lua_pop(L, 1);
  33.     printf("width = %d\n", width);
  34.     lua_getglobal(L, "tab1");
  35.     lua_getfield(L, -1, "name");
  36.     printf("%s\n", lua_tostring(L, -1));
  37.     lua_getfield(L, -2, "age");
  38.     printf("%d\n", (int)lua_tonumber(L, -1));
  39.     lua_pop(L, 3);
  40.     lua_close(L);
  41.     return 0;
  42. }
复制代码
  1. width = 20
  2. tab1 = {name="zhangsan", age=20}
  3. print(test1_hello)
  4. for i, v in pairs(test1_table) do
  5.     print(i..":"..v)
  6. end
复制代码
函数调用(参数,返回值)
  1. int main()
  2. {
  3.     lua_State *L = lua_open();
  4.     luaopen_base(L);
  5.     luaopen_string(L);
  6.     luaopen_table(L);
  7.     lua_register(L, "test", test);
  8.     lua_register(L, "test_array", test_array);
  9.     lua_register(L, "test_array2", test_array2);
  10.     lua_register(L, "test_ret", test_ret);
  11.     lua_pushstring(L, "hello");
  12.     lua_setglobal(L, "test1_hello");
  13.     lua_newtable(L);
  14.     lua_pushstring(L, "name");
  15.     lua_pushstring(L, "lisi");
  16.     lua_settable(L, -3);
  17.     lua_setglobal(L, "test1_table");
  18.     if (luaL_loadfile(L, "main.lua"))
  19.     {
  20.         const char *error = lua_tostring(L, -1);
  21.         printf("lua call error: %s\n", error);
  22.         lua_pop(L, 1);
  23.     }
  24.     if (lua_pcall(L, 0, 0, 0))
  25.     {
  26.         const char *error = lua_tostring(L, -1);
  27.         printf("lua call error: %s\n", error);
  28.         lua_pop(L, 1);
  29.     }
  30.     lua_getglobal(L, "width");
  31.     int width = lua_tonumber(L, -1);
  32.     lua_pop(L, 1);
  33.     printf("width = %d\n", width);
  34.     lua_getglobal(L, "tab1");
  35.     lua_getfield(L, -1, "name");
  36.     printf("%s\n", lua_tostring(L, -1));
  37.     lua_getfield(L, -2, "age");
  38.     printf("%d\n", (int)lua_tonumber(L, -1));
  39.     lua_pop(L, 3);
  40.     // 调用函数
  41.     lua_getglobal(L, "event");
  42.     lua_pushstring(L, "key");
  43.     lua_pushstring(L, "value");
  44.     if (lua_pcall(L, 2, 1, 0) != 0)
  45.     {
  46.         const char *error = lua_tostring(L, -1);
  47.         printf("lua call error: %s\n", error);
  48.         lua_pop(L, 1);
  49.     }
  50.     else
  51.     {
  52.         printf("lua call error: %s\n", lua_tostring(L, -1));
  53.         lua_pop(L, 1);
  54.     }
  55.     printf("top is %d\n", lua_gettop(L));
  56.     lua_close(L);
  57.     return 0;
  58. }
复制代码
  1. width = 20
  2. tab1 = {name="zhangsan", age=20}
  3. print(test1_hello)
  4. for i, v in pairs(test1_table) do
  5.     print(i..":"..v)
  6. end
  7. function event(key, value)    print("key:"..key.."  value:"..value)    return "aaaaa"endfunction event(args)    for i,v in ipairs(args) do        print("key:"..i.."  value:"..v)    endend
复制代码
备注: 留意栈空间清理,防止内存泄漏, 防止多线程互斥标题。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表