Lua - 我将如何破坏元表或使它们无法使用?

Lua - How would I break meta tables or make them unuseable?

所以在我的游戏中,大多数剥削者都使用元表来阻止禁令,我想打破它们,我不使用它们,这只会伤害剥削者。即使这破坏了 lua 的其他部分,我可以并且会修复它,但这需要停止,这是我最好的选择。

来自 Programming in Lua,第 4 版,第 20 章:

The functions setmetatable and getmetatable also use a metafield, in this case to protect metatables. Suppose we want to protect our sets, so that users can neither see nor change their metatables. If we set a __metatable field in the metatable, getmetatable will return the value of this field, whereas setmetatable will raise an error:

mt.__metatable = "not your business"

s1 = Set.new{}
print(getmetatable(s1))     --> not your business
setmetatable(s1, {})
    stdin:1: cannot change protected metatable

那么,你的答案是什么?设置 __metatable 字段以防止访问 table 的元数据 table。对您需要保护的所有 table 执行此操作。