如何在 Lua/LuaJ 中使全局变量“不可变”?

Ste*_*ong 1 lua luaj readonly-variable

描述

我正在做一个 LuaJ 程序,这是一个像这样的 lib 脚本:

function foo()
    print("foo");
end
Run Code Online (Sandbox Code Playgroud)

我希望该函数可以直接foo在其他脚本中调用(否),但在不同脚本中执行不可变。(即使一个脚本覆盖它,它也会像其他脚本中的原始方式一样执行。)require

例如,这是脚本 1:

foo = function()
    print("bar");
end
Run Code Online (Sandbox Code Playgroud)

这是脚本 2:

foo();
Run Code Online (Sandbox Code Playgroud)

做了什么

我看到了这两个问题。它们确实有效,但不是解决这个问题的方法。

LuaJ 如何避免覆盖全局表中的现有条目

防止 Lua 表中的函数重写

使全局环境仅可访问(Lua)


我尝试在每次 exec 脚本或 set 时加载 lib local _ENV,但由于可能会有从 Java 到 Lua 的进一步回调,因此它无法正常工作。

Globals我现在通过每次在 Java 中加载脚本时创建一个并加载 lib 脚本来处理它,如下所示:

    public static void main(String[] args) {
        loadAndCallViaDifferentEnv(libPath, script1);
        loadAndCallViaDifferentEnv(libPath, script2);
    }

    static void loadAndCallViaDifferentEnv(String libPath, String scriptPath) {
        Globals globals = JsePlatform.standardGlobals();
        globals.loadfile(libPath).call();
        globals.loadfile(scriptPath).call();
    }
Run Code Online (Sandbox Code Playgroud)

效果很好,但成本很高。有没有更好的办法?

Ego*_*off 5

我假设您想保护三个函数不被覆盖:foo1foo2print

-- define foo1 and foo2 inside protected table instead of as usual globals
local protected = {}

function protected.foo1()  
   print("foo1");
end

function protected.foo2()
   print("foo2");
end

-- if a function to be protected already exists, remove it from globals:
protected.print = print
print = nil

-- Now set the metatable on globals
setmetatable(_G, {
   __index = protected,
   __newindex =
      function(t, k, v)
         if not protected[k] then
            rawset(t, k, v)
         end
      end
})
Run Code Online (Sandbox Code Playgroud)

现在您可以在没有 的情况下从其他模块调用foo1,foo2和,但不能覆盖它们:printrequire

-- the script 1:
foo1 = function()
   print("bar");
end
foo1();   -- original protected.foo1() is invoked
Run Code Online (Sandbox Code Playgroud)
-- the script 2:
foo1();   -- original protected.foo1() is invoked
Run Code Online (Sandbox Code Playgroud)