lua-users home
lua-l archive

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


Yeah i am using it for various scripts, so probably not.

Now, what if this script includes functions; I imagine those are added to the registry when loadstring is called?

it is possible that subsequent loadings could overwrite them; i assume just calling the function returned from loadstring wont restore them?

-Raymond

On Thu, Jan 8, 2009 at 1:41 PM, Duncan Cross <duncan.cross@gmail.com> wrote:


On Thu, Jan 8, 2009 at 6:20 PM, Raymond Jacobs <raymondj@gmail.com> wrote:
Correct, the script doesn't change.

As for storing it; what does loadstring actually do; does it push the code and functions onto the stack?
or is it all wrapped up as a single function?

in such a case I guess I could use lua_ref, to create a ref to it for later use?
or what else would you suggest?

-Raymond

It compiles the string into a single Lua function and pushes that function onto the stack. If the only thing you're doing with this lua_State is running this single function over and over again, after it has been added to the stack by loadstring you can run it by doing this:

 lua_pushvalue(L,-1); // get a copy of the function from the top of the stack (as calling it will remove it from the stack)
 lua_call(L,0,0); // call the function (no parameters, no return value)

Repeating this fragment of code later will run the function again. If you are using the lua_State for other things, interleaved with calls to this function, it will probably not work.

-Duncan