lua-users home
lua-l archive

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


Hi,

Huang Lin wrote:
Hi there,
I use lua in this way in my C program. int getPrompt (char* output)
{
        lua_State *L = lua_open();  /* create state */

        if (luaL_loadfile(L, "test.lua") || lua_pcall(L, 0, 0, 0))
        {

        }
        /// other lua logic to get output
        lua_close(L);
}
By using a local lua_State, the C program can begin the LUA logic from a fresh environment. This is also helpful when the C function, getPrompt, was run in multithread environment that doesn't require LUA related data to be shared/exchanged between threads. But it is expensive to load the LUA instructions each time when I just want a local and fresh LUA runtime environment. Is it possible to load/compile the LUA scripts once, put the result in the memory, and let the local lua_State(an independent lua universes) use it? Thanks,
Huang Lin

Since luaL_load() pushes the main chunk to the stack, you can store it in some global variable or in the registry, along with the environment (using perhaps getfenv()/setfenv() ) and call it several times later, restoring the env each time.

I realise that if you can't reuse the lua_State then this is probably all useless.


Cheers!
Bogdan