title: Lua - Reseting the state of a script without reparsing it
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Loading the libraries may be a rather lightweight operation (I assume), but parsing the scripts is probably not.</p>
  
  <p>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.</p>
  
  <p>Thank you. :)</p>
  
  <p><strong>EDIT</strong> : I found this topic but it is not detailed about to do that : <a href="http://lua-users.org/lists/lua-l/2006-01/msg00493.html" rel="nofollow">http://lua-users.org/lists/lua-l/2006-01/msg00493.html</a></p>
  
  <p><strong>EDIT</strong> : lua_setfenv seems to be related to that. I'll dig a bit more.</p>
  
  <p><strong>EDIT</strong> : 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...</p>