lua-users home
lua-l archive

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


What does the param name mean in luaL_loadbuffer?

"Peter Shook" <pshook@sympatico.ca> wrote in message
3F070C80.7010605@sympatico.ca">news:3F070C80.7010605@sympatico.ca...
>
> anderson wrote:
> > I have some scripts that may be ran many times, in one program
execution.
> > I'd like to keep them in memory (RAM) in the lua complied state. How do
I do
> > that?
>
> If you are using Lua 5.0 then:
>
> Short answer: use luaL_loadfile or luaL_loadbuffer to load it as a
> function, then use lua_pcall to evaluate it later.
>
> Long answer:
>
>    const char *filename = "x.lua";
>    if (luaL_loadfile(L, filename)) {
>      fprintf(stderr, "%s\n", lua_tostring(L, -1));
>      lua_pop(L, 1);
>    }
>    /* take func from top of stack and store it in the Registry */
>    int func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
>    /* now store func_ref some place safe in C */
>
> Later on, you can get it back and call it.
>
>    lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref);
>    if (lua_pcall(L, 0, 0, 0)) {
>      fprintf(stderr, "%s\n", lua_tostring(L, -1));
>      lua_pop(L, 1);
>    }
>
> - Peter Shook
>
>