Lua中设置table为只读属性的方法详解

2019-10-28 18:48:19 来源:易采站长站 作者:王振洲

项目中部分只读表易被人误改写,故决定在非线上环境里对这些表附加只读属性,方便在出现误改写的时候抛出lua错误,最终版代码如下:

--[[------------------------------------------------------------------------------
-** 设置table只读 出现改写会抛出lua error
-- 用法 local cfg_proxy = read_only(cfg) retur cfg_proxy
-- 增加了防重置设置read_only的机制
-- lua5.3支持 1)table库支持调用元方法,所以table.remove table.insert 也会抛出错误,
--  2)不用定义__ipairs 5.3 ipairs迭代器支持访问元方法__index,pairs迭代器next不支持故需要元方法__pairs
-- 低版本lua此函数不能完全按照预期工作
*]]
function read_only(inputTable)
 local travelled_tables = {}
 local function __read_only(tbl)
 if not travelled_tables[tbl] then
  local tbl_mt = getmetatable(tbl)
  if not tbl_mt then
  tbl_mt = {}
  setmetatable(tbl, tbl_mt)
  end

  local proxy = tbl_mt.__read_only_proxy
  if not proxy then
  proxy = {}
  tbl_mt.__read_only_proxy = proxy
  local proxy_mt = {
   __index = tbl,
   __newindex = function (t, k, v) error("error write to a read-only table with key = " .. tostring(k)) end,
   __pairs = function (t) return pairs(tbl) end,
   -- __ipairs = function (t) return ipairs(tbl) end, 5.3版本不需要此方法
   __len = function (t) return #tbl end,
   __read_only_proxy = proxy
  }
  setmetatable(proxy, proxy_mt)
  end
  travelled_tables[tbl] = proxy
  for k, v in pairs(tbl) do
  if type(v) == "table" then
   tbl[k] = __read_only(v)
  end
  end
 end
 return travelled_tables[tbl]
 end
 return __read_only(inputTable)
end

测试代码如下:

local t0 = {k = 1}
local t2 = {
 fdsf = {456}
}
local t1 = {
  a = {456, 89},
  b = {456,ddss = 9, t2 = t2},
  d = 45,
  e = "string",
}
t1.c=t1

local t3 = read_only(t1)

print(t3.d, t3.c.e, t3.c.c.b.t2.fdsf)
function q1() t3.d = 4555 end
function q2() t3.c.d = 90 end
function q3() t3.c.c.b.t2.fdsf =90 end
function q4() table.remove(t3.a) end
function q5() t3.b[ddss] = nil end
function q6() t3[f] = 89 end
function q7() table.insert(t3.a, 999) end

print(pcall(q1))
print(pcall(q2))
print(pcall(q3))
print(pcall(q4))
print(pcall(q5))
print(pcall(q6))
print(pcall(q7))
print(t3.a[1])
for k,v in pairs(t3) do
 print("===pairs t3:",k,v)
end
for k,v in pairs(t3.a) do
 print("===pairs t3.a:",k,v)
end
for k,v in ipairs(t3) do
 print("===ipairs t3:",k,v)
end
for k,v in ipairs(t3.a) do
 print("===ipair t3.a",k,v)
end
print("len t3:",#t3)
print("len t3.a:", #t3.a)

local t4 = read_only(t2)

local t5 = read_only(t0)
local t6 = read_only(t0)

print(t3.b.t2, read_only(t2))
print(t5, t6, t0)

测试环境https://www.lua.org/cgi-bin/demo  lua5.3.4:

string table: 0x20d4ba0
false input:17: error write to a read-only table with key = d
false input:17: error write to a read-only table with key = d
false input:17: error write to a read-only table with key = fdsf
false input:17: error write to a read-only table with key = 2
false input:17: error write to a read-only table with key = nil
false input:17: error write to a read-only table with key = nil
false input:17: error write to a read-only table with key = 3
===pairs t3: e string
===pairs t3: b table: 0x20ccd60
===pairs t3: a table: 0x20d4e70
===pairs t3: d 45
===pairs t3: c table: 0x20ca700
===pairs t3.a: 1 456
===pairs t3.a: 2 89
===ipair t3.a 1 456
===ipair t3.a 2 89
len t3: 0
len t3.a: 2
table: 0x20d4870 table: 0x20d4870
table: 0x20d5690 table: 0x20d5690 table: 0x20d1140
            




              

微信扫一扫

易采站长站微信账号