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

 更新时间:2017年07月07日 08:42:07   作者:vanishfan  
这篇文章主要给大家介绍了关于Lua中设置table为只读属性的方法,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

项目中部分只读表易被人误改写,故决定在非线上环境里对这些表附加只读属性,方便在出现误改写的时候抛出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

代码思路设计:

1.使用proxy={}空表而不是目标表tbl来设置__newindex是因为__newindex必须在原表里面不存在才会调用,这样就依然可以对已存在的字段进行改写

__newindex: The indexing assignment table[key] = value. Like the index event, this event happens when table is not a table or when key is not present in table. The metamethod is looked up in table.

Like with indexing, the metamethod for this event can be either a function or a table. If it is a function, it is called with table, key, and value as arguments. If it is a table, Lua does an indexing assignment to this table with the same key and value. (This assignment is regular, not raw, and therefore can trigger another metamethod.)

Whenever there is a __newindex metamethod, Lua does not perform the primitive assignment. (If necessary, the metamethod itself can call rawset to do the assignment.)

2.避免出现table的互相引用,加入travelled_tables存储已经设置过proxy的table的映射

3.对于原表tbl的访问使用__index=tbl

4.对于表查长度使用__len= function () return #tbl end

5.对于遍历pairs,查到lua5.3的pairs默认迭代器next不支持访问元表__index,故直接__pairs = function () return pairs(tbl) end,以此来生成对目标表的迭代遍历

6.对于ipairs,查到lua5.3 ipairs函数生成的迭代器默认就支持访问元表__index,故不需要添加__ipairs

     8.2 – Changes in the Libraries

     •The ipairs iterator now respects metamethods and its __ipairs metamethod has been deprecated.

7.对于table.insert , table.remove不用特殊处理,lua5.3的table lib支持元表操作,故依然会抛错

      8.2 – Changes in the Libraries

      •The Table library now respects metamethods for setting and getting elements.

8.避免重复创建read_only,每个tbl只创建一个proxy代理,在tbl的metatable里和proxy的metatable里都设置属性__read_only_proxy,可以直接访问获得

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对程序员之家的支持。

相关文章

  • Lua中的变量和流控制入门学习

    Lua中的变量和流控制入门学习

    这篇文章主要介绍了Lua中的变量和流控制入门学习,其中--两个横线开始单行的注释,--[[加上两个[和]表示多行的注释--]],需要的朋友可以参考下
    2015-07-07
  • Lua中使用table实现的其它5种数据结构

    Lua中使用table实现的其它5种数据结构

    这篇文章主要介绍了Lua中使用table实现的其它5种数据结构,本文用table为基础,实现了数组、链表、队列、集合等数据类型,需要的朋友可以参考下
    2014-09-09
  • Lua中string.lower()使用指南

    Lua中string.lower()使用指南

    这篇文章主要介绍了Lua中操作字符串的基本方法整理,是Lua入门学习中的基础知识,需要的朋友可以参考下
    2016-08-08
  • Lua table中安全移除元素的方法

    Lua table中安全移除元素的方法

    这篇文章主要介绍了Lua table中安全移除元素的方法,本文给出3种实现方法,分别是从后往前删除和while删除,需要的朋友可以参考下
    2015-06-06
  • 详解Lua中的数据类型

    详解Lua中的数据类型

    这篇文章主要介绍了Lua中的数据类型,是Lua入门学习中的基础知识,需要的朋友可以参考下
    2015-05-05
  • Lua教程(六):绑定一个简单的C++类

    Lua教程(六):绑定一个简单的C++类

    这篇文章主要介绍了Lua教程(六):绑定一个简单的C++类,本文是最后一篇C/C++与Lua交互的教程,其他教程请参阅本文下方的相关文章,需要的朋友可以参考下
    2014-09-09
  • Lua协程(coroutine)程序运行分析

    Lua协程(coroutine)程序运行分析

    这篇文章主要介绍了Lua协程(coroutine)程序运行分析,本文讲解分析了一段lua 协程代码是如何运行的,需要的朋友可以参考下
    2015-05-05
  • Lua教程之弱引用table

    Lua教程之弱引用table

    这篇文章主要介绍了Lua教程之弱引用table,本文讲解了自动内存管理的缺陷、弱引用table、三种形式的弱引用等内容,需要的朋友可以参考下
    2014-09-09
  • Lua脚本自动生成APK包

    Lua脚本自动生成APK包

    这篇文章主要介绍了Lua脚本自动生成APK包,本文脚本适用于cocos2dx+lua的项目,需要的朋友可以参考下
    2015-04-04
  • Lua中table的遍历详解

    Lua中table的遍历详解

    这篇文章主要介绍了Lua中table的遍历详解,本文讲解了4种遍历方法,并详细分析了这4种方法的特点、使用环境、相关知识等,需要的朋友可以参考下
    2015-04-04

最新评论

?


http://www.vxiaotou.com