[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: setfenv/getfenv
- From: Petite Abeille <petite.abeille@...>
- Date: Tue, 12 Jan 2010 22:42:45 +0100
On Jan 12, 2010, at 4:25 PM, Roberto Ierusalimschy wrote:
> code = "string with the code to be loaded"
> f = loadstring("in ... do " .. code .. " end")
Hmmm... not sure if all that serialization to string is a step forward... e.g.:
-- using setfenv
local debug = require( 'debug' )
local function Test( anIndex )
print( anIndex, foo )
end
debug.setfenv( Test, { print = print } )
Test( 1 )
debug.setfenv( Test, { print = print, foo = _VERSION } )
Test( 2 )
-- vs. using string.dump + loadin
local aChunk = string.dump( Test )
loadin( { print = print }, aChunk )( 3 )
loadin( { print = print, foo = _VERSION }, aChunk )( 4 )
> 1 nil
> 2 Lua 5.2
> 3 nil
> 4 Lua 5.2
The second version seems a rather whereabout way of doing things, no?