項目客戶端腳本全面升級lua5.2 這是自06年后最大的一次主干更新, 帶來的機制, 函數變化也是非常不錯的
1. 去掉了全局性質的setfenv/getfenv系列函數, 語言層面提供_ENV語法糖, 這東西跟:操作符一樣, 只存在于編譯期.
官方建議自己實現module/require/package機制, 畢竟原生這套機制實現太弱智了..
2. 提供了無符號訪問函數
3. 更多的lua api變化. 如果想兼容lua5.1的寫法, 可以參考luabridge內LuaHelpers.h的實現
以下是本人使用lua5.2實現的一套package機制, 供大家參考
package = {}
-- 讀取標記
package.loaded = {}
-- 搜索路徑數組
package.path = {}
package.access =
{
["string"] = string,
["print"] = print,
["table"] = table,
["assert"] = assert,
["error"] = error,
["pairs"] = pairs,
["ipairs"] = ipairs,
["tonumber"] = tonumber,
["tostring"] = tostring,
["type"] = type,
["math"] = math,
}
local function getreadablepath( name, pathlist )
for _, path in ipairs(pathlist) do
local fullpath = path .. "/" .. name .. ".lua"
local f = io.open( fullpath )
if f then
f:close()
return fullpath
end
end
return nil
end
function package.import( name )
-- 已經讀取的直接返回
local existedenv = package.loaded[name]
if existedenv then
return existedenv
end
local access = package.access
-- 設置訪問控制權限
local meta =
{
__index = function( tab, key )
-- 優先取包的空間
local v = rawget( tab, key )
if v then
return v
end
-- 找不到時,從可訪問的權限表中查找
return access[key]
end,
}
-- 初始化一個包的全局環境, 并設置訪問方法
local env = setmetatable( {} , meta )
--
local readablepath = getreadablepath( name, package.path )
if not readablepath then
error(string.format("package '%s' not found \n%s", name, table.concat( package.path, "\n" ) ) )
end
local func = loadfile( readablepath, "bt", env )
if type(func) ~= "function" then
return nil
end
-- 設置標記
package.loaded[name] = env
-- 執行全局空間
func( )
return env
end
package.path = {"E:/Download/t"}
local m = package.import "m"
m.foo()
以下是m模塊(m.lua)的內容
function foo( )
print("轉載注明: 戰魂小筑 http://www.shnenglu.com/sunicdavy", io )
end
測試結果中, io打印出nil, 顯示權限已經被控制住
本package設計比原生package更靈活, 更強大