lua-users home
lua-l archive

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


Perhaps you are looking for something like:

-- begin of code

-- userCode should be the user script, loaded from somewhere
local userCode = [[
a = 5
b = 3
someFunction()
for i=1,10 do anotherFunction(i) end
math.sin = 'my own evil string'
string.find = 'another one'
]]

local protectedEnv = {
    anotherFunction = print,
    someFunction = function() print 'Yeah' end,
math = {sin = math.sin, log = math.log, min = math.min, max = math.max},
    string = {find = string.find, sub=string.sub, ....},
    ....
}

local f,err = loadstring(userCode)
if not f then -- syntax error
	error(err)
end
setfenv(f, protectedEnv)
f()
> yeah
> 1
> 2
> 3
> 4
> ...
> 10

print(protectedEnv.a,protectedEnv.b, protectedEnv.math.sin, protectedEnv.string.find, math.sin, string.find)
> 5	3	my own evil string	another one	function: ...	function: ...

-- end of code

PS: This code is untested ;)

--rb