【C++/Lua联合开发】 (三) C++调用Lua

C++调用Lua

  • 全局变量访问(普通,表),函数调用(参数,返回值)

  • 注意栈空间清理,防止内存泄漏

1. C++调用Lua全局变量(普通类型)

lua_getglobal(L, "width");   //取出全局变量"width"并压栈
int width = lua_tointeger(L, -1);
lua_pop(L, 1);                    //栈顶出栈
cout << "width: " << width << endl;
width = 1920

2. C++给Lua传递全局变量

luaL_loadfile 调用前执行,设置全局变量cname

lua_pushstring(lua, "c name value");   //压栈
lua_setglobal(lua, "cname");   

lua_setglobal(L, "cname") 把栈顶的值弹出并设置为全局变量 cname(在 Lua中相当于 _G[“cname”] = value)

3. C++调用Lua全局变量(表)

-- main.lua
conf = {
    title = "My Title",
    height = 200
}
// main.cpp
lua_getglobal(L, "conf");  // [conf]
lua_getfield(L, -1, "title");  // [conf  "My Title"]
cout << "title: " << lua_tostring(L, -1) << endl;  
lua_pop(L, 1);            // [conf]

lua_getfield(L, -1, "height");    [conf  200]
cout << "height: " << lua_tointeger(L, -1) << endl;
lua_pop(L, 1);     // [conf]

4. C++给Lua传递全局变量表

lua_newtable(L);      //[..., table]
lua_pushstring(L, "skan"); // [..., table, "skan"]
//把栈顶值作为value,弹出它并设置索引-2处表的字段name,等价于table["name"]=value
lua_setfield(L, -2, "name");  // [..., table]

lua_pushinteger(L, 42);
lua_setfield(L, -2, "age");

lua_setglobal(L, "person");
-- main.lua
print("person name: " .. person.name)
print("person age: " .. person.age)

5. C++调用Lua函数

注意点:

  1. 堆栈
  2. 多线程调用(dispatch的方式)

5.1 基础调用

function lua_event() 
    print("Event triggered")
end
lua_getglobal(L, "lua_event");
if (!lua_isfunction(L, -1)) {
    // 不是函数:弹出并忽略
    lua_pop(L, 1); // 栈恢复
}
else {
    int ret = lua_pcall(L, 0, 0, 0); // 弹出函数并调用
    if (ret != LUA_OK) {
        const char* err = lua_tostring(L, -1); // 栈顶是错误信息
        printf("call event error: %s\n", err);
        lua_pop(L, 1); // 弹出错误信息
    }
}
  • lua_getglobal(L, "lua_event"):把全局变量 lua_event的值压入栈顶(不弹出任何东西)。

    • 调用前(假设栈为空):[]
    • 调用后:[ lua_event_value ](栈顶索引 -1)
  • lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)的行为:

    • 要求栈顶是一个可调用值(function)。lua_pcall会弹出该函数和传入的参数(此处 nargs=0),在保护模式下调用它。
    • 成功返回(ret == LUA_OK):根据 nresults=0,不会把函数的返回值压回栈,最终栈回到调用前的状态(即如果之前只有函数,则变为空栈)。
    • 调用前:[ function ] → 调用后(成功):[]
    • 失败返回(ret != LUA_OK):lua_pcall在栈顶压入错误信息(error message),并返回错误码。
      • 调用后(出错):[ error_msg ]
  • 如果 lua_event不是函数(例如 niltable),直接 lua_pcall会失败并在栈上留下错误信息("attempt to call a … "),应先用 lua_isfunctionlua_type检查

5.2 调用Lua函数传递参数

function lua_event(e1) 
    print("Event triggered")
    print(e1)
    return "test lua_event"
end
lua_getglobal(L, "lua_event");
if (!lua_isfunction(L, -1)) {
    // 不是函数:弹出并忽略
    lua_pop(L, 1); // 栈恢复
}
else {
    lua_pushstring(L, "hello from C");  
    lua_newtable(L);
    lua_pushstring(L, "skan");
    lua_setfield(L, -2, "name");
    int ret = lua_pcall(L, 2, 1, errfunc_pos); // 弹出函数并调用
    if (ret != LUA_OK) {
        const char* err = lua_tostring(L, -1); // 栈顶是错误信息
        printf("call event error: %s\n", err);
        lua_pop(L, 1); // 弹出错误信息
    }
    else {
        printf("lua return: %s\n", lua_tostring(L, -1));
        lua_pop(L, 1);
    }
}

5.3 errfunc

-- main.lua
function my_error_handler(err)
    return "Custom error: " .. tostring(err)
endnd
// ...existing code...
int errfunc_pos = lua_gettop(L) + 1; 
lua_getglobal(L, "my_error_handler");  // 压入错误处理函数,假设索引为 1
// 栈: [..., my_error_handler]
lua_getglobal(L, "lua_event");  // 压入要调用的函数
lua_pushstring(L, "hello from C");  // 参数
// 栈: [..., my_error_handler, lua_event, "hello from C"]

int ret = lua_pcall(L, 1, 1, errfunc_pos);
if (ret != LUA_OK) {
    const char* err = lua_tostring(L, -1);  // 栈顶是 errfunc 返回的字符串
    printf("Handled error: %s\n", err);
    lua_pop(L, 1);
} else {
    printf("Success: %s\n", lua_tostring(L, -1));
    lua_pop(L, 1);
}
lua_pop(L, 1); // errfunc出栈

5.4 返回table

lua_pushstring(L, "hello from C");  
lua_newtable(L);
lua_pushstring(L, "skan");
lua_setfield(L, -2, "name");
int ret = lua_pcall(L, 2, 1, errfunc_pos); // 弹出函数lua_event("hello from C", tab1)并调用
if (ret != LUA_OK) {
    const char* err = lua_tostring(L, -1); // 栈顶是错误信息
    printf("call event error: %s\n", err);
    lua_pop(L, 1); // 弹出错误信息
}
else {
    lua_getfield(L, -1, "result");
    if (lua_isstring(L, -1))
        cout << "result: " << lua_tostring(L, -1) << endl;
    else if (lua_isnumber(L, -1))
        cout << "result: " << lua_tonumber(L, -1) << endl;
    else
        cout << "result: " << lua_typename(L, lua_type(L, -1)) << endl;

    lua_pop(L, 2);
}
function lua_event(e1, tb2) 
    print("Event triggered")
    print(e1)
    print(tb2.name)
    return {result = "success"}
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值