Lua 绕过元表

Lua 绕过元表,直接访问 table 的字段。

绕过元表

rawset(table, index, value),在不触发元方法的情况下,设置 table[index] 的值为 value。

rawget(table, index),在不触发元方法的情况下,获取 table[index] 的值。

现有一个 hero 表,设置了元表和元方法。

lua 复制代码
local hero = {
    level = 1,
    exp = 0
}

local mt = {
    __index = function (table, key)
        print("trigger __index")
    end,
    __newindex = function (table, key, value)
        print("trigger __newindex")
    end
}
setmetatable(hero, mt)

如果去访问一个不存在的 key,则会进入元表的元方法。

因为 __newindex 被赋值为一个函数,只有打印,没有把值存到 table 里,所以 hero 里还是没有 star 字段。

lua 复制代码
hero.star = 5
print(hero.star)

-- trigger __newindex
-- trigger __index
-- nil

现在使用 rawset 为 hero 添加一个新字段 star,再使用 rawget 从 hero 中获取 star 字段。

可以发现,没有触发元方法的打印,hero 表中也添加了 star 字段。

lua 复制代码
rawset(hero, "star", 5)
local star = rawget(hero, "star")
print(star)

-- 5

防止死循环

修改一下元方法,在 __index 中返回 table[key] 的值,在 __newindex 中设置 table[key] 为 value。

lua 复制代码
local hero = {
    level = 1,
    exp = 0
}

local mt = {
    __index = function (table, key)
        print("trigger __index")
        return table[key]
    end,
    __newindex = function (table, key, value)
        print("trigger __newindex")
        table[key] = value
    end
}
setmetatable(hero, mt)

看上去似乎没有什么问题,但是如果触发了这两个元方法,它们内部的逻辑(也就是 table[key])又会触发元方法,进入死循环,最后栈溢出。

lua 复制代码
hero.star = 5

-- trigger __newindex
-- trigger __newindex
-- trigger __newindex
-- ...
-- C stack overflow

print(hero.star)

-- trigger __index
-- trigger __index
-- trigger __index
-- ...
-- C stack overflow

要防止这种死循环,可以利用 rawset 和 rawget,避免再次触发元方法。

lua 复制代码
local hero = {
    level = 1,
    exp = 0
}

local mt = {
    __index = function (table, key)
        print("trigger __index")
        return rawget(table, key)
    end,
    __newindex = function (table, key, value)
        print("trigger __newindex")
        rawset(table, key, value)
    end
}
setmetatable(hero, mt)

再次访问 star 字段,就不会栈溢出了。

lua 复制代码
hero.star = 5
-- trigger __newindex

print(hero.star)
-- 5
相关推荐
代码狂人8 小时前
Lua中使用module时踩过的坑
开发语言·lua
莱茵不哈哈2 天前
OpenResty 深度解析:构建高性能 Web 服务的终极方案
nginx·lua·kong·openresty·conf
技术宝哥2 天前
Redis(2):Redis + Lua为什么可以实现原子性
数据库·redis·lua
kaixin_learn_qt_ing3 天前
脚本语言Lua
lua
搞不懂语言的程序员4 天前
Redis的Pipeline和Lua脚本适用场景是什么?使用时需要注意什么?
数据库·redis·lua
莱茵不哈哈4 天前
初探 Skynet:轻量级分布式游戏服务器框架实战
lua·c·skynet
·云扬·6 天前
【PmHub后端篇】PmHub中基于Redis加Lua脚本的计数器算法限流实现
redis·算法·lua
Aric_Jones6 天前
lua入门语法,包含安装,注释,变量,循环等
java·开发语言·git·elasticsearch·junit·lua
Petrichorzncu7 天前
Lua再学习
开发语言·学习·lua
mikey棒棒棒7 天前
lua脚本+Redission实现分布式锁
redis·分布式·lua·看门狗·redission