我們有時需要限制lua代碼的運行環境,或者是讓使用者不能訪問到lua的一些全局函數.lua語言本身沒有類似於C++, C#, Java那樣的成員訪問控制. 但lua提供了setfenv函數可以很靈活的處理各類權限問題
廢話不多說, 看代碼
1: -- 創建沙盒
2: function SpawnSandBox( )
3:
4: local SandBoxGlobals = {}
5:
6: -- 基礎函數添加
7: SandBoxGlobals.print = print
8: SandBoxGlobals.table = table
9: SandBoxGlobals.string = string
10: SandBoxGlobals.math = math
11: SandBoxGlobals.assert = assert
12: SandBoxGlobals.getmetatable = getmetatable
13: SandBoxGlobals.ipairs = ipairs
14: SandBoxGlobals.pairs = pairs
15: SandBoxGlobals.pcall = pcall
16: SandBoxGlobals.setmetatable = setmetatable
17: SandBoxGlobals.tostring = tostring
18: SandBoxGlobals.tonumber = tonumber
19: SandBoxGlobals.type = type
20: SandBoxGlobals.unpack = unpack
21: SandBoxGlobals.collectgarbage = collectgarbage
22: SandBoxGlobals._G = SandBoxGlobals
23:
24: return SandBoxGlobals
25: end
26:
27: -- 在沙盒內執行腳本, 出錯時返回錯誤, nil表示正確
28: function ExecuteInSandBox( SandBox, Script )
29:
30: local ScriptFunc, CompileError = loadstring( Script )
31:
32: if CompileError then
33: return CompileError
34: end
35:
36: setfenv( ScriptFunc, SandBox )
37:
38: local Result, RuntimeError = pcall( ScriptFunc )
39: if RuntimeError then
40: return RuntimeError
41: end
42:
43: return nil
44: end
45:
46: function ProtectedFunction( )
47: print("protected func")
48: end
49:
50:
51: local SandBox = SpawnSandBox( )
52:
53:
54: print ( "Response=", ExecuteInSandBox( SandBox, "table.foreach( _G, print )" ) )
55:
56: print ( "Response=", ExecuteInSandBox( SandBox, "ProtectedFunction()" ) )
57:
58: SandBox.ProtectedFunction = ProtectedFunction
59:
60: print ( "Response=", ExecuteInSandBox( SandBox, "ProtectedFunction()" ) )
54行執行結果是
1: _G table: 00421258
2: string table: 00421050
3: pairs function: 00567F58
4: collectgarbage function: 005675F0
5: unpack function: 004217E8
6: assert function: 005675B0
7: print function: 00567830
8: ipairs function: 00567F28
9: type function: 004217A8
10: tonumber function: 00421768
11: tostring function: 00421788
12: table table: 00420DA8
13: math table: 004210C8
14: setmetatable function: 00421748
15: getmetatable function: 00567710
16: pcall function: 005677F0
17: Response= nil
54行由於沒有注冊這個全局函數, 因此無法訪問
Response= [string "ProtectedFunction()"]:1: attempt to call global 'ProtectedFunction' (a nil value)
58行在全局環境中加上了這個函數,因此在60行訪問正常
protected func
Response= nil
摘自 戰魂小築