Metatables

Read and modify metatables, including locked ones.

12 functionssUNC 100%Lynx 0.747

Metatable functions let you inspect protected metatables, change table write access and work with namecall methods. A table's readonly flag and its environment flag are independent.

getrawmetatable

getrawmetatable(value: any): table?

getrawmetatable retrieves a value's actual metatable, even when its __metatable field hides it from ordinary access.

Also available as get_raw_metatable.

Parameters

valueany
Any supplied value, including nil.

Returns

table?

Metatable or nil when no metatable is attached.

Example

local t = setmetatable({}, { __metatable = "locked" })
print(getmetatable(t), getrawmetatable(t))

setrawmetatable

setrawmetatable(value: any, metatable: table | nil): any

setrawmetatable assigns or removes a value's metatable, including a metatable protected by __metatable.

Also available as set_raw_metatable.

Parameters

valueany
Value to update.
metatabletable | nil
New metatable, or nil to remove it. This argument is required.

Returns

any

The original value.

Example

local t = {}
setrawmetatable(t, { __index = { Answer = 42 } })
print(t.Answer)
setrawmetatable(t, nil)

isreadonly

isreadonly(value: table): boolean

isreadonly checks whether a table is readonly.

Also available as is_readonly.

Parameters

valuetable
Table to inspect.

Returns

boolean

True when the table rejects ordinary writes.

Example

print(isreadonly(table.freeze({})))

iswritable

iswritable(value: table): boolean

iswritable queries whether a table can be written to. See the note below for the current return-value behavior.

Also available as iswriteable.

Parameters

valuetable
Table to inspect.

Returns

boolean

Currently true for either table state.

Example

local t = table.freeze({})
print(iswritable(t))
print(not isreadonly(t)) -- reliable writable-state test

setreadonly

setreadonly(value: table, readonly: boolean): ()

setreadonly makes a table readonly or writable according to the supplied boolean.

Also available as set_readonly.

Parameters

valuetable
Table to update.
readonlyboolean
True freezes ordinary writes; false enables them.

Returns

()

No return values.

Example

local t = {}
setreadonly(t, true)
print(isreadonly(t))
setreadonly(t, false)

makereadonly

makereadonly(value: table): ()

makereadonly makes a table readonly so ordinary writes are rejected.

Also available as make_readonly.

Parameters

valuetable
Table to update.

Returns

()

No return values.

Example

local t = {}
makereadonly(t)
print(isreadonly(t))

makewritable

makewritable(value: table): ()

makewritable makes a table writable again.

Also available as make_writable, make_writeable and makewriteable.

Parameters

valuetable
Table to update.

Returns

()

No return values.

Example

local t = {}
makewritable(t)
print(isreadonly(t))

getnamecallmethod

getnamecallmethod(): string?

getnamecallmethod returns the method name associated with the current namecall.

Also available as get_namecallmethod and get_namecall_method.

Returns

string?

Current method name or nil.

Example

print(getnamecallmethod())

setnamecallmethod

setnamecallmethod(method: string): ()

setnamecallmethod sets the method name used by the current namecall.

Also available as set_namecallmethod and set_namecall_method.

Parameters

methodstring
New method string.

Returns

()

No return values.

Example

local old = getnamecallmethod()
if old then setnamecallmethod(old) end

isprotected

isprotected(value: table): boolean

isprotected checks whether a table has a metatable that hides itself through __metatable.

Parameters

valuetable
Table to inspect.

Returns

boolean

True when a non-nil __metatable field is found.

Example

print(isprotected(setmetatable({}, { __metatable = false })))

setuntouched

setuntouched(value: function | thread | table, untouched: boolean): ()

setuntouched changes the untouched-environment flag of a table or of the environment belonging to a function or thread.

Parameters

valuefunction | thread | table
The table, function or thread whose environment flag to change.
untouchedboolean
New safe-environment flag.

Returns

()

No return values.

Example

local environment = {}
setuntouched(environment, false)
print(isuntouched(environment))

isuntouched

isuntouched(value: function | thread | table): boolean

isuntouched checks the untouched-environment flag of a table, function environment or thread environment.

Parameters

valuefunction | thread | table
The table, function or thread whose environment flag to check.

Returns

boolean

true when the selected environment has its untouched flag set.

Example

print(isuntouched(getgenv()))