lua_settable 函数详解

本文通过示例展示了如何使用C/C++在Lua中设置函数和变量,并调用lua_pcall执行lua脚本。在示例中,创建了一个全局表`COM`,注册了名为`lua_func`的C函数,设置了`lua_var`变量,之后在lua脚本中调用这些设置并修改变量值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        上一篇文章讲到 lua_gettable() 函数,是获取table项的函数,而 lua_settable() 则是设置的函数,而操作的 key 和 value 在理解上应该是一样的:作一个等价于 t[k] = v 的操作,这里 t 是一个给定有效索引index处的值,v 指栈顶的值,而 k 是栈顶之下的那个值。这个函数会把键和值都从栈中弹出。

        如下用 lua_settable() 来设置一个函数操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>    
}
#endif

/* 将要注册给Lua 的函数, 定义Lua 传回三个数值 */
int LuaFunc(lua_State *L)
{
    if(lua_gettop(L) != 3)
    {
        printf("lua send back para is not correct.\n");
        return 1;
    }
    int ret1 = (int)lua_tonumber(L, -3);
    int ret2 = (int)lua_tonumber(L, -2);
    int ret3 = (int)lua_tonumber(L, -1);
    printf("Line: %d, Lua send back  %d, %d, %d\n", __LINE__, ret1, ret2, ret3);

    return 0;
}

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    if(luaL_loadfile(L, "parse.lua"))
    {
        printf("load file: parse.lua faild...\n");
        lua_close(L);
        return 1;
    }
    lua_pcall(L, 0, 0, 0); /* 前面这几步都基本固定 */
    
    lua_getglobal(L, "COM");         /* 先找到表 */
    if(lua_type(L, -1) != LUA_TTABLE)    
    {
        printf("can not find table: COM \n");
        lua_close(L);
        return 1;
    }

    lua_pushstring(L, "lua_func");    /* 注册函数 */
    lua_pushcfunction(L, LuaFunc);    /* 往表 COM 里设置了COM[key] = value, 即 COM["lua_func"] = LuaFunc */
    lua_settable(L, -3);

    lua_getglobal(L, "parseData");
    if(lua_type(L, -1) != LUA_TFUNCTION)
    {
        printf("can not find function: parseData.\n");
        lua_close(L);
        return 1;
    }
    lua_pushstring(L, "C Call Lua function");
    lua_pcall(L, 1, 0, 0);
    
    lua_close(L);
    return 0;
}

parse.lua 脚本为:

--定义了一个全局表
COM = {}

function parseData(str)
    print("From c/c++ str: "..str);
    COM.lua_func(12, 23, 45);
    COM.lua_var = "changed in lua";
end

结果为:

 而如果我只设置一个变量,也是可以的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>    
}
#endif

/* 将要注册给Lua 的函数, 定义Lua 传回三个数值 */
int LuaFunc(lua_State *L)
{
    if(lua_gettop(L) != 3)
    {
        printf("lua send back para is not correct.\n");
        return 1;
    }
    int ret1 = (int)lua_tonumber(L, -3);
    int ret2 = (int)lua_tonumber(L, -2);
    int ret3 = (int)lua_tonumber(L, -1);
    printf("Line: %d, Lua send back  %d, %d, %d\n", __LINE__, ret1, ret2, ret3);

    return 0;
}

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    if(luaL_loadfile(L, "parse.lua"))
    {
        printf("load file: parse.lua faild...\n");
        lua_close(L);
        return 1;
    }
    lua_pcall(L, 0, 0, 0); /* 前面这几步都基本固定 */
    
    lua_getglobal(L, "COM");         /* 先找到表 */
    if(lua_type(L, -1) != LUA_TTABLE)    
    {
        printf("can not find table: COM \n");
        lua_close(L);
        return 1;
    }

    lua_pushstring(L, "lua_var");    /* 设置变量 */
    lua_pushstring(L, "c/c++ var");    /* 往表 COM 里设置了COM[key] = value, 即 COM["lua_func"] = LuaFunc */
    lua_settable(L, -3); /* 这步后弹出 key(lua_var) 和 value(c/c++ var) , 所以top 位置为 COM */

    // lua_getglobal(L, "COM");
    // if(lua_type(L, -1) != LUA_TTABLE)
    // {
    //     printf("can not find table: COM\n");
    //     lua_close(L);
    //     return 1;
    // }

    lua_pushstring(L, "lua_var");
    lua_gettable(L, -2);
    if(lua_type(L, -1) != LUA_TSTRING)
    {
        printf("can not find string: lua_var\n");
        lua_close(L);
        return 1;
    }
    
    char buf[32] = {0};
    memcpy(buf, lua_tostring(L, -1), sizeof(buf));
    printf("get lua_var = \"%s\" from lua\n", buf);
    lua_close(L);

    return 0;
}

其实我只是设置了 lua_var="c/c++ var",然后再取出来:

 以下则是既设置函数,也设置变量,且在 lua 函数里把原变量的值修改掉:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>    
}
#endif

/* 将要注册给Lua 的函数, 定义Lua 传回三个数值 */
int LuaFunc(lua_State *L)
{
    if(lua_gettop(L) != 3)
    {
        printf("lua send back para is not correct.\n");
        return 1;
    }
    int ret1 = (int)lua_tonumber(L, -3);
    int ret2 = (int)lua_tonumber(L, -2);
    int ret3 = (int)lua_tonumber(L, -1);
    printf("Line: %d, Lua send back  %d, %d, %d\n", __LINE__, ret1, ret2, ret3);

    return 0;
}

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    if(luaL_loadfile(L, "parse.lua"))
    {
        printf("load file: parse.lua faild...\n");
        lua_close(L);
        return 1;
    }
    lua_pcall(L, 0, 0, 0); /* 前面这几步都基本固定 */
    
    lua_getglobal(L, "COM");         /* 先找到表 */
    if(lua_type(L, -1) != LUA_TTABLE)    
    {
        printf("can not find table: COM \n");
        lua_close(L);
        return 1;
    }

    lua_pushstring(L, "lua_func");    /* 注册函数 */
    lua_pushcfunction(L, LuaFunc);    /* 往表 COM 里设置了COM[key] = value, 即 COM["lua_func"] = LuaFunc */
    lua_settable(L, -3);

    lua_pushstring(L, "lua_var"); /* 设置变量COM["lua_var"] = "c/c++ var" */
    lua_pushstring(L, "c/c++ var");
    lua_settable(L, -3);


    lua_getglobal(L, "parseData"); /* 查找函数进行调用 */
    if(lua_type(L, -1) != LUA_TFUNCTION)
    {
        printf("can not find the function of parseData\n");
        lua_close(L);
        return 1;
    }
    lua_pushstring(L, "call function in c/c++"); /* lua 函数调用参数 */
    lua_pcall(L, 1, 0, 0);

    char buf[32] = {0};
    lua_pushstring(L, "lua_var"); /* lua 函数调用结束后,再获取变量 lua_var,因为我在那个函数里修改了 lua_var变量的值 */
    lua_gettable(L, -2);
    if(lua_type(L, -1) != LUA_TSTRING)
    {
        printf("can not find the string of lua_var\n");
        lua_close(L);
        return 1;
    }
    memcpy(buf, lua_tostring(L, -1), sizeof(buf));
    printf("get lua_var = \"%s\" from lua\n", buf);
    
    lua_close(L);
    return 0;
}

函数调用后,lua_var 变量值已经被修改。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值