我正在尝试使用 LuaInterface 2.0.3 在我的 C# 应用程序中嵌入一些 Lua 脚本功能。到目前为止,这工作得很好,但我不知道如何限制对少数指定的 .Net 类的访问。默认情况下,所有.Net库都可以通过“luanet”直接访问,并且Lua脚本可以自由打开新窗口或访问文件系统。
例如这个 Lua 脚本将打开一个新窗口:
Form = luanet.System.Windows.Forms.Form
mainForm = Form()
mainForm:ShowDialog()
脚本自由固然很棒,但这可能会干扰托管应用程序,并且会产生一些我不太喜欢的与安全相关的影响。有什么办法可以禁用这个吗?
--make a table for all the classes you want to expose
safeClasses = {}
--store all the ones you want
safeClasses.Form = luanet.System.Windows.Forms.Form
--etc...
--remove access to LuaInterface
luanet = nil
package.loaded.luanet = nil
--prevent future packages from being loaded
require = nil
package.loadlib = nil
您也可以反向执行此操作,首先删除 LuaInterface 的全局实例和存储实例,然后通过本地引用完成所有工作(所有代码都在 LuaInterface 的其余部分中)block http://www.lua.org/manual/5.1/manual.html#2.4.2可以使用):
--get a local reference to LuaInterface without clobbering the name
local luainterface = luanet
--delete the global reference to it
luanet = nil
--also delete it from the package store and disable package loading
package.loaded.luanet = nil
require = nil
package.loadlib = nil
--put luanet back locally at its original name (for convenience)
local luanet = luainterface
--make a table for all the classes you want to expose
safeClasses = {}
--store all the ones you want
safeClasses.Form = luanet.System.Windows.Forms.Form
--etc...
(你可以避免上面的三步保名舞蹈(local luainterface=luanet; luanet=nil; local luanet=luainterface
)通过直接本地化到luanet
然后通过删除全局_G
引用全局表:
local luanet=_G.luanet
_G.luanet = nil
我只是出于个人喜好而选择不这样做。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)