Skip to content

setrawmetatable

setrawmetatable forcibly sets the metatable of a value, bypassing the __metatable protection field.

function setrawmetatable<T>(object: T & ({ any } | userdata), metatable: { any }): T

Parameters

Parameter Description
object The value whose metatable will be overwritten.
metatable The new metatable to assign.

Example

Overriding the metatable of a string
1
2
3
4
5
6
7
local dummy_string = "Example"
local string_metatable = setrawmetatable(dummy_string, {
    __index = getgenv()
})

print(string_metatable)          -- Output: Example
print(string_metatable.getgenv)        -- Output: function: 0x...