lua-users home
lua-l archive

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


On 05/23/2013 01:28 PM, Satz Klauer wrote:
Hi,

I have a pointer to some user data available during initialisation,
means before the LUA-script is started. This pointer has to be
available during execution of the script, since it is a multithreaded,
explicitely linked shared library environment I can't use just a
global variable that stores these data.

So when a C-function is called out of a running LUA-script I need to
access these user data. Since the C-function is always defined as

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!


Is this pointer once per lua_State? If this is the case you can use a dirty trick that I do and store it in the *ud of the Lua state.

main() {
    sometype* mypointer;
    lua_State L = lua_newstate(someallocfunction, mypointer);

    lua_pushcfunction(L, my_lua_called_c_fct);
    lua_setglobal(L, "my_c_function");

    lua_loadbuffer(L, "my_c_function()", 15, "");
    lua_call(L, 0, 0);
}

int my_lua_called_c_fct(lua_State *L) {
   sometype* mypointer;
   lua_getallocf(L, (void**)&mypointer);
   DoSomeStuff(mypointer);
}

It's a trick but its as I see it the fastest and easiest way to store a pointer in L.

--
Thomas