lua-users home
lua-l archive

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


On Apr 1, 2009, at 5:51 AM, Tom Miles wrote:

I'm getting a little confused here again as well, if another state were to call getglobal() on our first state, would it be able to see it using
this methood?

This is where the definition of globals in C v Lua gets a little bit complicated and others can almost certainly explain it better than I can. On the Lua side, the function/chunk has an environment table and global lookups go to that table. There needn't be any other links to that table so beyond code that calls getfenv, that environment isn't going to escape. (You can protect yourself from Lua code doing this through appropriate metatable construction.)

On the C side, you have the table at LUA_GLOBASINDEX and the table at LUA_ENVIRONINDEX. My recollection is that the former is bound to the Lua state and is accessible to Lua code via getfenv. The latter is bound to the C code that is executing. Since you don't need to adjust either of these to set the environment for the script chunk, there shouldn't be an issue of the environment escaping.

Mark