侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

技术  /  管理员 发布于 6年前   343

项目中部分只读表易被人误改写,故决定在非线上环境里对这些表附加只读属性,方便在出现误改写的时候抛出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=t1local t3 = read_only(t1)print(t3.d, t3.c.e, t3.c.c.b.t2.fdsf)function q1() t3.d = 4555 endfunction q2() t3.c.d = 90 endfunction q3() t3.c.c.b.t2.fdsf =90 endfunction q4() table.remove(t3.a) endfunction q5() t3.b[ddss] = nil endfunction q6() t3[f] = 89 endfunction q7() table.insert(t3.a, 999) endprint(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)endfor k,v in pairs(t3.a) do print("===pairs t3.a:",k,v)endfor k,v in ipairs(t3) do print("===ipairs t3:",k,v)endfor k,v in ipairs(t3.a) do print("===ipair t3.a",k,v)endprint("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: 0x20d4ba0false input:17: error write to a read-only table with key = dfalse input:17: error write to a read-only table with key = dfalse input:17: error write to a read-only table with key = fdsffalse input:17: error write to a read-only table with key = 2false input:17: error write to a read-only table with key = nilfalse input:17: error write to a read-only table with key = nilfalse 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 89len t3: 0len t3.a: 2table: 0x20d4870 table: 0x20d4870table: 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 C 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 C 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,可以直接访问获得

总结

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


  • 上一条:
    mysql自动化安装脚本(ubuntu and centos64)
    下一条:
    Lua中类的实现原理探讨(Lua中实现类的方法)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    • 2025-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客