lua-users home
lua-l archive

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

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:

local function Decode( anObject )
    local aChunk = assert( loadstring( anObject ) )
    setfenv( aChunk, { _ = {}, f = DecodeFunction } )
    return getfenv( aChunk )._

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.