Events

Tag methods are called in the following events:

  • add
  • Called when a + operation is applied to non-numerical operands. Coercion to numbers is attempted on both operands. If both operands are valid numbers, the primitive add operation is used; otherwise the tag method for the binary operation “add” is retrieved. If the tag method exists, it is called with the original operands; otherwise the error function is called with an error message. Tag method: tm(op1, op2, "add")
  • sub
  • Called for a - operation. Behavior similar to the “add” event.
  • mul
  • Called for a * operation. Behavior similar to the “add” event.
  • div
  • Called for a / operation. Behavior similar to the “add” event.
  • pow
  • Called when a ^ operation is applied, even for numerical opera The tag method for the binary operation “pow” is retrieved. If the tag method exists, it is called with the given operands; otherwise the error function is called with an error message. Tag method: tm(op1, op2, "pow")
  • unm
  • Called when a unary - operation is applied to a non-numerical operand.Coercion to number is attempted on the operand. If the operand is a valid number, the primitive negation operation is used; otherwise the tag method for the unary operation “unm” is retrieved. If the tag method does not exist, get the global tag method (tag 0). If the tag method exists, it is called with the original operand and a nil; otherwise the error function is called with an error message. Tag method: tm(op1, nil, "unm")
  • lt
  • Called when an order operation is applied to non-numerical or non-string operands. Corresponds to the < operator. If both operands are numeric,use the numeric comparison; else if both operands are strings, use the lexicographic comparison; otherwise the tag method for the binary operation “lt” is retrieved. If the tag method exists, it is called with the given operands; otherwise the error function is called with an error message. Tag method: tm(op1, op2, "lt") The other order operators use this tag method according to the usual equivalences: a>b <=> b<a, a<=b <=> not(b<a) and a>=b <=> not(a<b)
  • concat
  • Called when a concatenation is applied to non-string operands. If operands are either numbers or strings, use string concatenation; otherwise the tag method for the binary operation “concat” is retrieved. If the tag method exists, it is called with the given operands; otherwise the error function is called with an error message. Tag method: tm(op1, op2, "concat")
  • getglobal
  • Called whenever Lua needs the value of a global variable. This method can only be set for nil and for tags created by newtag.Note that the tag is that of the current value of the global variable. Access the value of the variable in the global table. Get the tag method using the value’s tag and the event “getglobal”. If the tag method exists, it is called with the variable name and value; otherwise the value of the variable is returned. Tag method: tm(varname, value)
  • setglobal
  • Called whenever Lua assigns to a global variable.Thismethod cannot be set for numbers, strings, and tables and userdata with the default tag. Access the old value of the variable in the global table. Get the tag method using the value’s tag and the event “setglobal”. If the tag method exists, call it with variable name, old value, new value; otherwise the value of the variable is set using the default function. Tag method: tm(varname, oldvalue, newvalue)
  • index
  • Called when Lua tries to retrieve the value of an index not present in a table. See the “gettable” event (below) for its semantics.
  • gettable
  • Called whenever Lua accesses an indexed variable. This method cannot be set for tables with the default tag. Get the tag method using the table’s tag and the event “gettable”. If the tag method exists, it is called with the table name and the index; else if the table is not of type “table”, call the error function; otherwise get the tag method using the table’s tag and event “index”. If the tag method exists, it is called with the table name and the index; otherwise the default function is used. Tag method: tm(table, index)
  • settable
  • Called when Lua assigns to an indexed variable. This method cannot be set for tables with the default tag. Get the tag method using the table’s tag and the event “settable”. If the tag method exists, it is called with the table name, index, value; else if the table is not of type “table”, call the error function; otherwise the default function is used. Tag method: tm(table, index, value)
  • function
  • Called when Lua tries to call a non-function value. If the function is of type “function”, call the default handler function; else get tag method using the function’s tag and the event “function”. If the tag method exists, call it indirectly with the function name inserted into the first element of the argument table; otherwise the error function is called with an error message. Indirect tag method: call(tm, arg)
  • gc
  • Called when Lua is garbage collecting a userdata. Can be set only from C, and cannot be set for a userdata with the default tag. For each userdata to be collected, Lua does the following: Get the tag method using the object’s tag and the event “gc”. If the tag method exists, it is called with object. Tag method: tm(obj) In a garbage-collection cycle, the tag methods for userdata are called in reverse order of tag creation.At the end of the cycle, Lua does the equivalent of the call gc_event(nil).