나는 Lua를 호출하는 C 코드를 작성한다. 세 개의 루아 파일이 있습니다 : init.lua, redis_pool.lua 및 run.lua. 첫째, redis_pool.lua ( 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
초기화 후 는 테이블 redis_pool
그렇게 보인다
redis_pool = {
['pool1'] = {pool_sz, pool = {...}}
['pool2'] = {pool_sz, pool = {...}}
['pool3'] = {pool_sz, pool = {...}}
['pool4'] = {pool_sz, pool = {...}}
-- some other functions...
}
을 지금, 나는 그때적인 CAL 테이블 redis_pool
준비가 생각
-- 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
C
에서 링 run.lua이 나는 테이블redis_pool
초기화, 그러나 그것은 nil
이 된 이유는 C 호출은 다른 루아는 액세스하는 동안? 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;
}
파일을 실행하는 C 코드의 부분을 게시하십시오. –
@llmo Euro : C 코드를 업데이트했습니다. – coanor