lua-users home
lua-l archive

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

On Wednesday, December 19, 2007 Norman Ramsey wrote: 

> On page 254 of the Blue PIL, there's a nice example of using 
> a fresh environment to share information among functions in a library.
> One thing I'm not clear on: whose responsibility is it to restore the
> previous environment?  The example doesn't show the environment
> being restored.  Is it somehow restored magically?  Does 'require'
> reset the environment when it's finished?  Or is the example wrong,
> and is it up to me to restore the environment in my own code?

My Blue PiL is at home (I'm at work) not sure exactly what example
you're citing, but...

My conclusions after some code spelunking for my Lua gem on using
environments in libraries for thread local storage:

- require doesn't restore or reset the environment

- uses of lua_replace(L, LUA_ENVIRONINDEX) weren't designed to be
nested, so if you want that you need to save and restore it yourself

- use lua_replace(L, LUA_ENVIRONINDEX) followed by luaL_register()
and then, if you are paranoid about leaking your environment, as I am,
use lua_pushnil(L); lua_replace(L, LUA_ENVIRONINDEX) to hide it


Doug Currie
Londonderry, NH, USA