lua-users home
lua-l archive

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


Hi!

I'm trying to implement a simple sandbox to call a user-specified
function in a restricted env. I've played with _G/setfenv but still
didn't find out a solution. Searching lua-l-archive and Wiki didn't help
either. 

Here is my prototype.


x = 10

function sandbox(env, f, ...)
	local args = unpack(arg)
	setfenv(2, env)
	local retval = f(args)
	setfenv(2, _G)
	return retval
end

function sandbox2(env, f, ...)
	local g = _G
	_G = env
	local args = unpack(arg)
	local retval = f(args)
	_G = g
	return retval
end

function add(a,b) io.write('!'); return a+b+x end

x = sandbox({x=0}, add, 2, 3)
print(x)


I'm trying to get 5 in print(x) output, but got 15 or errors like this:
x.lua:16: attempt to perform arithmetic on local `b' (a nil value)
stack traceback:
        x.lua:16: in function `f'
        x.lua:10: in function `sandbox'
        x.lua:18: in main chunk
        [C]: ?

-- 
Regards, max.