I have an application that runs Lua scripts. Each Lua script is likely to run several times. Some scripts may even run every time a key is pressed.
I would like these scripts to be "reset" between each run. I.e if a user sets a variable Foo, then Foo should not exist in the script the next time it runs until the user defines it again.
The problem is that, if I want to have such a behaviour, I need to create a new lua_State everytime, then open the libraries into it every time, and then parse the script file everytime, which seems very unoptimized.
Loading the libraries may be a rather lightweight operation (I assume), but parsing the scripts is probably not.
Is there a way to reset the state of a Lua script (i.e clear user-code-defined variables) without creating a new lua_State and reparsing the whole Lua script file ? I would just like the script files to be parsed once on application startup since they are not modifed at run-time.
Thank you. :)
EDIT : I found this topic but it is not detailed about to do that : http://lua-users.org/lists/lua-l/2006-01/msg00493.html
EDIT : lua_setfenv seems to be related to that. I'll dig a bit more.
EDIT : It seems like there is no more lua_setfenv as of LUA 5.2. Since i'm using 5.3 I would have to set the environement (i.e a hidden table nammed _ENV where variables are stored) in order to do that, and thus reload everything, which is what I want not to do...