Lua __metatable绕过?

Ber*_*ver 4 lua metatable meta-method

我想知道是否有办法逃避__metatable元方法.我知道没有一个,但我正在尝试做这样的事情,但显然__metatable会阻止这种情况发生:

    -- pretend that there is a __metatable field given to a table
    setmetatable(_G, {__index = {None="No indexes"}})
    -- error: (the string given to the __metatable metamethod)
Run Code Online (Sandbox Code Playgroud)

我在这里尝试做的只是逃避__metatable字段,只是允许自己在_G上设置metatable,它已经有一个.我知道这是不可能做到的,但是我想问一下是否还有机会绕过?

lhf*_*lhf 5

从Lua执行此操作的唯一方法是使用debug.setmetatable.

整点__metatable是保护.调试库绕过了这种保护(和其他),当然应该谨慎使用.

  • 来自C API的`lua_setmetatable`也可以工作.并且一些元表被重用为索引表(类似于`mt .__ index = mt`) - 在这种情况下,你可以简单地设置`aTable .__ index .__ metatable = nil`来删除保护. (5认同)