[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: loadstring and accessing local variables
- From: Petite Abeille <petite.abeille@...>
- Date: Fri, 11 Dec 2009 21:52:15 +0100
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.
Here is an usage example:
http://dev.alt.textdrive.com/browser/HTTP/Data.lua#L63
local function Decode( anObject )
local aChunk = assert( loadstring( anObject ) )
setfenv( aChunk, { _ = {}, f = DecodeFunction } )
aChunk()
return getfenv( aChunk )._
end
In other words, provide '_' and 'f' to the chunk to use when executed.
> 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.