2013-05-08 2 views
1

나는 Lua를 호출하는 C 코드를 작성한다. 세 개의 루아 파일이 있습니다 : init.lua, redis_pool.luarun.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; 
    } 
+0

파일을 실행하는 C 코드의 부분을 게시하십시오. –

+0

@llmo Euro : C 코드를 업데이트했습니다. – coanor

답변

3

당신은 독립적 인 루아가 초기화 및 사용에 대한 언급이 가지고 :

/* 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 변수에 pply하십시오. 다른 함수에서 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) { 
     ... 
+0

다른 새로운'Lua_state'의 전역 (전달 된)'Lua_State'에서 이러한 redis 풀을 가져 오는 방법은 무엇입니까? – coanor

+0

다른 주를 무엇이 필요로합니까? 어떻게 든 개체를 C에 저장 한 다음 다른 Lua 상태로 전달할 수는 있지만 그 이유는 알 수 없습니다. –

+0

@llmo 유로 http://stackoverflow.com/questions/16453654/how-to-pass-data-between-multiple-lua-statemulti-thread – coanor

관련 문제