[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: loadstring and accessing local variables
- From: Gert <lua@...>
- Date: Fri, 11 Dec 2009 21:56:42 +0100
* On 2009-12-11 Petite Abeille <email@example.com> wrote :
> On Dec 11, 2009, at 9:46 PM, Gert wrote:
> > I've been playing around with that but no luck yet.
> > my first try was
> > setfenv(chunk, getfenv(1))
> > But this fails.
> Right. The chunk doesn't have any environment at this point. You need to create one.
> > My guess would be that getfenv() would get the
> > environment for the current function, but the environment does not
> > contain the local variables of the function itself, only the functions
> > in, well, it's environment.
> Right. You can use the environment to build an appropriate context for the chunk to execute in. What an "appropriate context" means, is up to you.
In my application, the chunk is some generated lua code from some kind
of domain specific language. In this case, 'Appropriate context' would
everything reachable from the current function, just as if the loaded
chunk was running at the same location. Unfortunately things just don't
work that way, so I'll either have to use the debug library, or just
live with it.
I think I prefer the second options :)