Lua __metatable bypass?

1.8k Views Asked by At

I was wondering if there is any way to escape the __metatable metamethod. I know there isn't one, but I'm trying to do something like this, but obviously __metatable blocks that from happening:

    -- 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)

What I am trying to do here is simply escape past the __metatable field and simply allow myself to set a metatable on _G, which already has one. I know it is impossible to do this, but I'd like to ask if there is still a chance to bypass?

2

There are 2 best solutions below

1
On

The only way to do this from Lua is to use debug.setmetatable.

The whole point of __metatable is protection. The debug library bypasses this protection (and others) and should be used with care, of course.

1
On

I would just override the metables.

You can do that by using synapse x overridemetables().