lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

 

I want to know whether the registry is suitable for what I am trying to do.

 

I have a C thread function which initializes the Lua state, opens the standard libraries and registers the C Lua functions to Lua using lua_register. I want to store an integer value when I spawn the script in this C thread function (lua_execute) and access the value in the C Lua API function.

 

Below is the code snippet of what I am trying to do.

 

For e.g. void *lua_execute (void *arg) {

      /*lua_State *L;

      /*opens standard libraries*/

      L = lua_open();

      luaopen_base(L);

      /* and so on */

      lua_register(L,"getval", lgetval);

      /*do thread related work */

      /*This is what I want to be able to do*/

      /* In this C function push an integer mynumber and access it in lgetval */

      static const char key = 'k';

      mynumber = 5;

      lua_pushlightuserdata(L,(void *)&key);

      lua_pushnumber(L,mynumber);

      lua_settable(L,LUA_REGISTRYINDEX);

      /*load a lua script file execute it */

      /*close thread and exit */

}

 

int lgetval(lua_State *L) {

      /*In this function I want to access the value of mynumber can I access it? */

      lua_pushlightuserdata (L, (void *)&key);

      lua_gettable (L, LUA_REGISTRYINDEX);

      mynumber = lua_tonumber (L,-1);

 

      }

 

I look forward to your reply.

 

Thanks