lua-users home
lua-l archive

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


On Tue, Oct 23, 2012 at 4:25 PM, D. Matt Placek <atomicsuntan@gmail.com> wrote:
> I'm trying to update a codebase from 5.1 to 5.2, and I'm wondering what to
> do with uses of setfenv().  I have code like:
>
>      chunk = loadfile( filename )
>      setfenv( chunk, envtable )
>
> I don't see a way to replace the _ENV table for the loaded chunk unless _ENV
> is specifically declared in the source of the file I'm loading.  Is there
> something obvious I'm missing?  I don't like the idea of having to go into
> every file that could possibly be loaded and adding hooks to replace the
> local environment.  Could I maybe change _ENV using debug.setupvalue, or is
> there a different mechanism that is recommended for replacing the old
> setfenv() calls?
>

In this case, the short answer is:

     chunk = loadfile( filename, "bt", envtable )

Internally, loadfile with three arguments will load the file as before
and then call debug.setupvalue to change _ENV.