We call the keys in a metatable events and the values metamethods. In the previous example, the event is "add"
and the metamethod is the function that performs the addition.
You can query the metatable of any value through the function.
You can replace the metatable of tables through the setmetatable
function. You cannot change the metatable of other types from Lua (except by using the debug library); you must use the C API for that.
Tables and full userdata have individual metatables (although multiple tables and userdata can share their metatables). Values of all other types share one single metatable per type; that is, there is one single metatable for all numbers, one for all strings, etc.
A metatable controls how an object behaves in arithmetic operations, order comparisons, concatenation, length operation, and indexing. A metatable also can define a function to be called when a userdata is garbage collected. For each of these operations Lua associates a specific key called an event. When Lua performs one of these operations over a value, it checks whether this value has a metatable with the corresponding event. If so, the value associated with that key (the metamethod) controls how Lua will perform the operation.
Metatables control the operations listed next. Each operation is identified by its corresponding name. The key for each operation is a string with its name prefixed by two underscores, ‘__
‘; for instance, the key for operation “add” is the string "__add"
. The semantics of these operations is better explained by a Lua function describing how the interpreter executes the operation.
The code shown here in Lua is only illustrative; the real behavior is hard coded in the interpreter and it is much more efficient than this simulation. All functions used in these descriptions (, tonumber
, etc.) are described in . In particular, to retrieve the metamethod of a given object, we use the expression
This should be read as
- rawget(getmetatable(obj) or {}, event)
“add”: the
+
operation.The function
getbinhandler
below defines how Lua chooses a handler for a binary operation. First, Lua tries the first operand. If its type does not define a handler for the operation, then Lua tries the second operand.- function getbinhandler (op1, op2, event)
- return metatable(op1)[event] or metatable(op2)[event]
- end
By using this function, the behavior of the
op1 + op2
is- function add_event (op1, op2)
- local o1, o2 = tonumber(op1), tonumber(op2)
- if o1 and o2 then -- both operands are numeric?
- return o1 + o2 -- '+' here is the primitive 'add'
- else -- at least one of the operands is not numeric
- local h = getbinhandler(op1, op2, "__add")
- if h then
- -- call the handler with both operands
- return (h(op1, op2))
- else -- no handler available: default behavior
- error(···)
- end
- end
- end
“sub”: the operation. Behavior similar to the “add” operation.
- “mul”: the
*
operation. Behavior similar to the “add” operation. - “div”: the
/
operation. Behavior similar to the “add” operation. - “mod”: the
%
operation. Behavior similar to the “add” operation, with the operationo1 - floor(o1/o2)*o2
as the primitive operation. - “pow”: the
^
(exponentiation) operation. Behavior similar to the “add” operation, with the functionpow
(from the C math library) as the primitive operation. “unm”: the unary
-
operation.“concat”: the
..
(concatenation) operation.- function concat_event (op1, op2)
- if (type(op1) == "string" or type(op1) == "number") and
- (type(op2) == "string" or type(op2) == "number") then
- return op1 .. op2 -- primitive string concatenation
- else
- local h = getbinhandler(op1, op2, "__concat")
- if h then
- return (h(op1, op2))
- else
- error(···)
- end
- end
- end
“len”: the
#
operation.- function len_event (op)
- if type(op) == "string" then
- elseif type(op) == "table" then
- return #op -- primitive table length
- else
- local h = metatable(op).__len
- if h then
- -- call the handler with the operand
- return (h(op))
- else -- no handler available: default behavior
- error(···)
- end
- end
- end
See §2.5.5 for a description of the length of a table.
-
- function getcomphandler (op1, op2, event)
- if type(op1) ~= type(op2) then return nil end
- local mm1 = metatable(op1)[event]
- local mm2 = metatable(op2)[event]
- if mm1 == mm2 then return mm1 else return nil end
- end
The “eq” event is defined as follows:
a ~= b
is equivalent tonot (a == b)
. “lt”: the
<
operation.- function lt_event (op1, op2)
- if type(op1) == "number" and type(op2) == "number" then
- return op1 < op2 -- numeric comparison
- elseif type(op1) == "string" and type(op2) == "string" then
- return op1 < op2 -- lexicographic comparison
- else
- local h = getcomphandler(op1, op2, "__lt")
- if h then
- return (h(op1, op2))
- else
- error(···)
- end
- end
- end
a > b
is equivalent tob < a
.“le”: the
<=
operation.- function le_event (op1, op2)
- if type(op1) == "number" and type(op2) == "number" then
- return op1 <= op2 -- numeric comparison
- elseif type(op1) == "string" and type(op2) == "string" then
- return op1 <= op2 -- lexicographic comparison
- else
- local h = getcomphandler(op1, op2, "__le")
- return (h(op1, op2))
- else
- h = getcomphandler(op1, op2, "__lt")
- if h then
- return not h(op2, op1)
- else
- error(···)
- end
- end
- end
- end
a >= b
is equivalent tob <= a
. Note that, in the absence of a “le” metamethod, Lua tries the “lt”, assuming thata <= b
is equivalent tonot (b < a)
.“index”: The indexing access .
- function gettable_event (table, key)
- local h
- if type(table) == "table" then
- local v = rawget(table, key)
- if v ~= nil then return v end
- h = metatable(table).__index
- if h == nil then return nil end
- else
- h = metatable(table).__index
- if h == nil then
- error(···)
- end
- end
- if type(h) == "function" then
- return (h(table, key)) -- call the handler
- else return h[key] -- or repeat operation on it
- end
- end
“newindex”: The indexing assignment
table[key] = value
.-
- function function_event (func, ...)
- if type(func) == "function" then
- return func(...) -- primitive call
- else
- local h = metatable(func).__call
- if h then
- return h(func, ...)
- else
- error(···)
- end
- end