lua-users home
lua-l archive

Re: Hand over global userdata to LUA-called C-function

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


Satz Klauer wrote:

int my_lua_called_c_fct(lua_State *L)

there seems to be "L" where I could get these user data from. But how
can I put the data to "L" during initialisation and how can I retrieve
them within that function?

These are some configuration-data that are required to execute
my_lua_called_c_fct()...

Thanks!


Normally you would stash the userdata in the registry table. Two options exist for this; the first is to use a string key and retrieve it with code along the lines of:
lua_pushliteral(L, "mykey");
lua_gettable(L, LUA_REGISTRYINDEX);
MyType* data = "" lua_touserdata(L, -1);

Another alternative, slightly faster (because you don't have to intern the string each time) is to use a light user data pointed at a glo bal constant for the key:
static const char* mykey = "This string is completely ignored";
...
lua_pushlightuserdata(L, mykey);
lua_gettable(L, LUA_REGISTRYINDEX);
MyType* data = "" lua_touserdata(L, -1);


The registry table is a table that is global to the interpreter (and not normally visible to script)

AltStyle によって変換されたページ (->オリジナル) /