lua-users home
lua-l archive

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


Hi José,

One trick I picked up in the IRC channel (don't remember who told me, sorry) is to use function arguments instead of globals.

So if you have arguments a, b, c that you want to provide to the user provided snippet, you do like this:

0. snippet is user provided script
1. snippetFunction = "return function(a, b, c) " + snippet + " end"
2. load buffer with snippetFunction and pcall to make the function end up on the top of the stack
3. push the arguments a,b,c to the stack
4. pcall the function which will execute the user provided snippet

Hope that helps!

Cheers,
 Jonas

Zé Pedro wrote:
I've seen a similar question already answered in the list but I'm not sure it applies to my problem. In my program I'm executing lua code made by users and I have no control over it. What I'm doing now is setting the variables the code needs by doing:
                     lua_pushinteger(pLuaState, (int)atoi(value.c_str()));
                     lua_setglobal(pLuaState, name.c_str());

and then executing the script doing:
                     luaL_dostring (pLuaState, script.c_str())

the thing is that the script has no functions or anything, just calls to methods and declaration of variables. Once a script is executed the variables should be cleared but this must be made in C after the luaL_dostring call. Is there any way to do this or do I have to create a new LuaState for each time I execute a script?

thanks,
José Tavares

--
Believe nothing, no matter where you read it, or who said it,
no matter if I have said it, unless it agrees with your own reason
and your common sense.
- Buda

Para quê ter olhos azuis, se a Natureza deixa os meus  vermelhos?
- Bob Marley

"Don't try to use what you learn from Buddhism to be a Buddhist;
use it to be a better whatever-you-already-are."
- His Holiness the 14th Dalai Lama

Idealism is what precedes experience; cynicism is what follows.
  - David T. Wolf

"Please don't download, because I want to get a pool in my second home."
  - Kanye West