[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Setting the environment for a loaded script
- From: Andrew Starks <andrew.starks@...>
- Date: Thu, 17 Apr 2014 13:47:16 -0500
On Thu, Apr 17, 2014 at 6:51 AM, Milind Gupta <milind.gupta@gmail.com> wrote:
> Hi,
> I have a trusted script that I want to load and execute and I want
> it to run in the current environment. How should I set the script
> environment and also pass all the current upvalues to the script?
>
> Thanks,
> Milind
>
`load` allows you to specify _ENV, but neither load nor require[1]
will read in up values.
One solution is to use the the package.searchpath [2] to find the
file. Read it in as a string and then prepend the locals to the top of
the file. Then `load` that.
Hackery at its finest, but this is what I do and it works. If you'd
like, I can post that code. I won't, in case it would be too
offensive. :)
-Andrew
[1] My guess is that `require` does not allow _ENV to be set is
because it uses `package.loaded` to cache subsequent requests for the
same library. My guess is that it was decided that allowing the first
call to require for a specific module to define the environment for
all that use it was judged as "bad design."
[2] http://www.lua.org/manual/5.2/manual.html#pdf-package.searchpath