加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua变量范围与setfenv

发布时间:2020-12-15 00:16:17 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用原始Lua文件进行配置,但不希望配置文件污染全局命名空间. 我遇到的问题是dofile似乎总是在真实的全局环境中执行,因此外部文件只是将所有声明都抛入_G. 这是一个示例主文件,注释表明我的一厢情愿. function myFunc() print("In the sandbox:")
我正在尝试使用原始Lua文件进行配置,但不希望配置文件污染全局命名空间.

我遇到的问题是dofile似乎总是在真实的全局环境中执行,因此外部文件只是将所有声明都抛入_G.

这是一个示例主文件,注释表明我的一厢情愿.

function myFunc()
    print("In the sandbox:")
    print("Should be 1:",a)  -- falls back to _G for lookup
    a = 2  -- instantiating new global for sandbox
    print("Should be 2:",a)  -- from sandbox
    print("Should still be 1:",_G.a)  -- from host environment

    dofile("loading.lua")  -- here's where things go wrong

    print "nBack in the sandbox:"
    print("Should be 3:",a)  -- changed by loadfile
    print("Should STILL be 1:",_G.a)  -- unchanged
end

a = 1
local newgt = {} -- new environment
setmetatable(newgt,{__index = _G})
setfenv(myFunc,newgt)
myFunc()

print("nOutside of the sandbox:")
print("Should be 1: ",a)  -- in theory,has never changed

它正在加载的文件(loading.lua:

print ("nLoading file...")

print("Should be 2: ",a) -- coming from the sandbox environment
a = 3
print("Should be 3: ",a) -- made a change to the environment

最后我看到的输出:

In the sandbox:
Should be 1: 1
Should be 2: 2
Should still be 1: 1

Loading file...
Should be 2:  1
Should be 3:  3

Back in the sandbox:
Should be 3: 2
Should STILL be 1: 3

Outside of the sandbox:
Should be 1:  3

解决方法

您描述的问题也在此页面 Dofile Namespace Proposal中讨论.解决方案似乎是dofile的以下替代:
function myapp.import(name)
  local f,e = loadfile(name)
  if not f then error(e,2) end
  setfenv(f,getfenv(2))
  return f()
end

另见:Sand Boxes

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读