lua-users home
lua-l archive

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


On Wed, Apr 2, 2008 at 6:58 PM, Eugen-Andrei Gavriloaie wrote:
>
>  Here is what I know so far:
*snip*

>  3. Adding custom made functionality represented by c++ functions
>  lua_pushcfunction(pLuaState, w_cfunc_call);
>  lua_setglobal(pLuaState, "cfunc");
*snip*

>  6. Now, the cloudy part for me is what is happening when I do a
>  luaL_loadstring(...).

You understand what lua_pushcfuntion does, right? luaL_loadstring()
does the same thing, except with the contents of a lua function.

When luaL_loadstring() is done, you can lua_pcall() the function and
it will run and then be garbage collected[1]. Or you can
lua_setglobal() to give it a name so it won't be garbage collected. Or
you can do anything else that you can normally do with a function
sitting on the lua stack.

>  What would happen if we do a second luaL_loadfile(...) on the
>  same lua_State? replacing the previous one or appending? Same question
>  with the luaL_loadstring(...)

luaL_loadfile() does the same thing as luaL_loadstring(), except with
a file instead of a string literal.

Peter Harris

[1] the function itself will be garbage collected, not any variables
that the function may set when you lua_pcall() it. In your
luaL_loadfile() case, that means the tables will still be there when
the file (function) is garbage collected. In other cases, it might
include creating other functions, and other things.