3.9 – Metatables

The following functions allow you to manipulate the metatables of an object:

  1. int lua_getmetatable (lua_State *L, int index);
  2. int lua_setmetatable (lua_State *L, int index);

lua_getmetatable pushes on the stack the metatable of a given object. If the index is not valid, or if the object does not have a metatable, lua_getmetatable returns 0 and pushes nothing on the stack.

lua_setmetatable pops a table from the stack and sets it as the new metatable for the given object. lua_setmetatable returns 0 when it cannot set the metatable of the given object (that is, when the object is neither a userdata nor a table); even then it pops the table from the stack.