[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Eval a Lua expression
- From: "Peter Harris" <lua@...>
- Date: Wed, 2 Apr 2008 19:58:16 -0400
On Wed, Apr 2, 2008 at 6:58 PM, Eugen-Andrei Gavriloaie wrote:
> Here is what I know so far:
> 3. Adding custom made functionality represented by c++ functions
> lua_pushcfunction(pLuaState, w_cfunc_call);
> lua_setglobal(pLuaState, "cfunc");
> 6. Now, the cloudy part for me is what is happening when I do a
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. 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.
 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.