我正在学习Roberto Ierusalimschy的“Lua编程”,我发现在书中,Sandboxing的例子使用函数setfenv()来改变给定函数的环境,但是在lua 5.2中,这个函数不再可用了. 我试图将一些文件(配置文件)中的值加
我试图将一些文件(配置文件)中的值加载到表中的字段中,但是在lua 5.2中我不能使用setfenv(所以我可以在给定的环境中加载值).在阅读了一些关于lua 5.2的文章之后,我发现每个函数可能有(或没有)一个名为_ENV的upvalue作为环境,因此,我尝试了以下代码:
function sandbox(sb_func, sb_env)
if not sb_func then return nil, "sandbox function not valid" end
sb_orig_env = _ENV
_ENV = sb_env -- yes, replaces the global _ENV
pcall_res, message = pcall( sb_func )
local modified_env = _ENV -- gets the environment that was used in the pcall( sb_func )
_ENV = sb_orig_env
return true, modified_env
end
function readFile(filename)
code = loadfile(filename)
res, table = sandbox(code, {})
if res then
--[[ Use table (modified_env) ]]--
else
print("Code not valid")
end
替换’sandbox’函数中的_ENV效果很好(无法访问常规字段),但是,当执行’code’时,它似乎忽略了我替换了_ENV,它仍然可以访问常规字段(print,loadfile, dofile等).
再读一点,我发现lua 5.2为此提供了一个函数,这个函数是loadin(env,chunk),它在给定的环境中运行给定的块,但是,当我尝试将这个函数添加到我的代码中时,该函数不存在(全局_G字段中不存在).
一些帮助将不胜感激.
从沙箱中分配_ENV时,您不会覆盖全局环境 – 您正在替换当前正在运行的代码的_ENV upvalue.添加print(_ENV)调用可以帮助您更好地理解所涉及的表的身份.例如:
function print_env()
print(_ENV)
end
function sandbox()
print(_ENV) -- prints: "table: 0x100100610"
-- need to keep access to a few globals:
_ENV = { print = print, print_env = print_env, debug = debug, load = load }
print(_ENV) -- prints: "table: 0x100105140"
print_env() -- prints: "table: 0x100105140"
local code1 = load('print(_ENV)')
code1() -- prints: "table: 0x100100610"
debug.setupvalue(code1, 1, _ENV) -- set our modified env
code1() -- prints: "table: 0x100105140"
local code2 = load('print(_ENV)', nil, nil, _ENV) -- pass 'env' arg
code2() -- prints: "table: 0x100105140"
end
loadin函数存在于Lua 5.2的一些预发布版本中,但在最终版本发布之前已被删除.相反,Lua 5.2 load and loadfile functions采用env参数.您也可以使用debug.setupvalue修改另一个函数的_ENV.
