Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File Lua 5.2中的沙箱_File_Lua_External_Sandbox_Environment - Fatal编程技术网

File Lua 5.2中的沙箱

File Lua 5.2中的沙箱,file,lua,external,sandbox,environment,File,Lua,External,Sandbox,Environment,我正在学习Roberto Ierusalimschy的“Lua编程”,我发现在这本书中,沙箱示例使用函数setfenv()来更改给定函数的环境,但在Lua 5.2中,此函数不再可用 我试图将一些值从文件(配置文件)加载到表中的字段中,但在Lua5.2中,我不能使用setfenv(因此我可以在给定的环境中加载值)。在阅读了一些关于Lua5.2的文章后,我发现每个函数可能(或不)都有一个名为_ENV的upvalue,它充当环境,因此,我尝试了以下代码: function sandbox(sb_fun

我正在学习Roberto Ierusalimschy的“Lua编程”,我发现在这本书中,沙箱示例使用函数
setfenv()
来更改给定函数的环境,但在Lua 5.2中,此函数不再可用

我试图将一些值从文件(配置文件)加载到表中的字段中,但在Lua5.2中,我不能使用setfenv(因此我可以在给定的环境中加载值)。在阅读了一些关于Lua5.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”函数中替换
\u ENV
效果很好(无法访问常规字段),但是,当执行“code”时,它似乎忽略了我替换的
\u ENV
,它仍然可以访问常规字段(打印、加载文件、dofile等)

再看一点,我发现Lua5.2为此提供了一个函数,这个函数是
loadin(env,chunk)
,它在给定的环境中运行给定的chunk,但是,当我尝试将这个函数添加到代码中时,这个函数不存在(在全局
\u G
字段中不存在)


一些帮助将不胜感激。

当您从
沙盒
中分配到
\u ENV
时,您并没有覆盖全局环境--您正在替换当前运行代码的
\u ENV
上限值。添加对
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

在Lua5.2的一些预发布版本中,出现了
loadin
功能,但在最终发布之前被删除。相反,Lua5.2采用
env
参数。您还可以使用修改另一个函数的
\u ENV

谢谢!解决办法很简单。我认为这种在
load
loadfile
中更改环境的方法更好,因为
setfenv
主要用于加载的代码中,因此。。。非常感谢。我按预期设置了沙盒。@定义print_env()时,它不接收全局_env作为upvalue吗?那么,当在sandbox()内部调用print_env()时,它是如何改变的呢?@tiagovalues是外部局部变量<代码>\u ENV
在这种情况下是块本地的;对
\u ENV
的赋值会影响该区块局部变量,该变量是两个闭包的共享值。