lua-users home
lua-l archive

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



On Mar 06, 2006, at 04:35, mr. Aleph wrote:

local aFunction = function( aName )
        return ( "%s! Yes, we can!" ):format( aName )
end
local aFunctionCopy = loadstring( string.dump( aFunction ) )
This trick won't help you with cfunctions. And require is a cfunction.

This is getting a bit convoluted...

local _require = require
local anEnvironment = { require = _require }
setmetatable( anEnvironment, { __index = _G } )

function require( aName )
        print( "my very own require", ( "%q" ):format( aName ) )

        return require( aName )
end

setfenv( require, anEnvironment )

print( require )
print( require( "table" ) )

local aRequireCopy = loadstring( string.dump( require ) )
setfenv( aRequireCopy, anEnvironment )

print( aRequireCopy )
print( aRequireCopy( "table" ) )

> function: 0x10ff60
> my very own require     "table"
> table: 0x101b60
> function: 0x1127d0
> my very own require     "table"
> table: 0x101b60

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/