如何存储在Lua中初始化的数据?

时间:2013-05-08 11:05:49

标签: c lua

我编写一些调用Lua的C代码。有三个Lua文件: init.lua redis_pool.lua run.lua 。首先,我在 redis_pool.lua 中初始化了redis池(调用 init.lua init.lua 调用 redis_pool.lua ), redis_pool.lua 就像这样:

    -- init.lua
    local redis_pool = require('redis_pool')
    redis_pool.init_pools()
    ...

    -- redis_pool.lua
    local redis_pool = {}

    function init_pools()
            -- init data in redis_pool
    end

    function redis_pool.get_pool(pool_name)
            -- return one of redis in @redis_pool
            return redis_pool[pool_name]
    end

在init之后,表redis_pool似乎是这样的:

    redis_pool = {
            ['pool1'] = {pool_sz, pool = {...}}
            ['pool2'] = {pool_sz, pool = {...}}
            ['pool3'] = {pool_sz, pool = {...}}
            ['pool4'] = {pool_sz, pool = {...}}

            -- some other functions...
    }

现在,我认为表redis_pool已准备好,然后我在C中调用 run.lua

    -- run.lua
    local redis_pool = require('redis_pool')

    function run_func
            -- error, redis_pool['pool1'] is nil!!
            local pool = redis_pool.get_pool('pool1')
    end

我已经初始化了表redis_pool,但为什么当C调用另一个Lua来访问它时它变为nil? 我是否必须将redis_pool返回到C堆栈,并将其传递给连续的Lua访问函数?


更新

其中一些C代码:

    /* C code to call init.lua */
    int init_redis_pool(void) {
            int ret = 0;
            lua_State *ls = luaL_newstate();
            luaL_openlibs(ls);
            ret = luaL_loadfile(ls, "init.lua");
            const char *err;
            (void)err;

            if (ret) {
                    err = lua_tostring(ls, -1);
                    return -1;
            }

            /* preload */
            ret = lua_pcall(ls, 0, 0, 0);
            if (ret) {
                    err = lua_tostring(ls, -1);
                    return -1;
            }

            lua_getglobal(ls, "init_pools");
            ret = lua_pcall(ls, 0, 0, 0);
            if (ret) {
                    err = lua_tostring(ls, -1);
                    return -1
            }

            lua_close(ls);

            return 0;
    }

    /* calling run.lua from C */
    int some_func() {
            ...
            ret = luaL_loadfile(ls, "run.lua");

            ...
            lua_getglobal(ls, "run_func")
            ret = lua_pcall(ls, 0, 0, 0)
            if (ret) {
                    /* error here */
                    err = lua_tostring(ls, -1);
                    return -1;
            }

            ...
            return 0;
    }

1 个答案:

答案 0 :(得分:3)

您有两个独立的Lua状态用于初始化和使用:

/* C code to call init.lua */
int init_redis_pool(void) {
        int ret = 0;
        lua_State *ls = luaL_newstate(); // ls is a local variable
        luaL_openlibs(ls);
        ret = luaL_loadfile(ls, "init.lua");


/* calling run.lua from C */
int some_func() {
        ...
        ret = luaL_loadfile(ls, "run.lua"); // ls is another local variable

加载init.lua并初始化池时,更改仅适用于本地 ls变量。当您在另一个函数中访问run.lua时,之前的Lua状态已经关闭并被销毁。

您需要在函数之间共享Lua状态变量。一种方法是在两个函数之外创建状态并将其传递给每个函数:

/* C code to call init.lua */
int init_redis_pool(lua_State *ls) {

/* calling run.lua from C */
int some_func(lua_State *ls) {
        ...