|
On 19-Jan-07, at 10:50 PM, Raymond Jacobs wrote:
Scanning the Lua documentation, would it be as easy as creating a new table, and setting it as the environment via lua_setfenv then as you mentioned setting this new table's __index key to the LUA_GLOBALSINDEX table, will make the new environment fall-back on the original globals table (if somthing doesnt exist, for instance?) when new variables are created in a script with this modification will they be saved in the new table?
That's about right. Note that every function has an environment, which is assigned when the function is created (i.e. by executing lua_load or lua_pushcclosure from C, or evaluating a function literal in Lua.) Once a function has been created, changing the global environment table will have no effect on the evaluation of the function.
The precise definition of the __index (and __newindex) metamethods is given in almost Lua code in the reference manual, here:
http://www.lua.org/manual/5.1/manual.html#2.8My suggestion is that you play around with the Lua interpreter, writing stuff in Lua, and then convert it to C if you feel the need. Although the mechanism is quite simple, it's easier to understand through experimentation than reading, at least in my experience.