Data

Lua is a dynamically typed language. Variables doesn't have any types but the value's does. So a variable can have a string assigned to it and next we can assign a table to it.

Variables

There are three kind of variables, Global Variables, Local variables and table fields.

Global: The default.

Local: Create a local variable by using the local keyword in front of declaration. If no value is assigned it will be initialized to nil.

Field: With a table a variable can be assigned with t[k] = v. Lua handle t.k = v the same as t["k"] = v.

Types

nil

boolean

number

string

function

This can be a lua or a c function.

userdata

This is used to store arbitrary c data in lua variables. It's a raw block of memory. There are two kinds of user data, full userdata and light userdata.

Full userdata

This is a block of memory managed by lua. It can not be created or modified in lua so it must be done through the C API. To create it use lua_newuserdata. It can have metatables and be garbage collected.

Light userdata

This is like a C pointer value. To put it on the stack use lua_pushlightuserdata.

thread

table

A table is a array that can be indexed by any value exept nil. They can also contain any type of value except nil.

Metatables and Metamethods

    • Each value in lua may have a metatable.
    • Tables and userdata have invidual metatable, other types share one single metatable for all values of that type.
    • Any table can be the metatable of any value, including itself.
    • From Lua it is only possible to set metatables on tables. All values of other types it muse be modified from C.

Arithmetic Metamethods

__add

__sub

__mul

__div

__mod

__pow

__unm

__idiv

Bitwise Metamethods

__band

__bor

__bxor

__bnot

__shl

__shr

Relational Metamethods

__eq

__lt

__le

Table-Access Metamethods

__index: One use for this is inheritance. The instance can have the class as a metatable and it can point the index to itself. That way anything one try to access not in the instance will be looked up in the class.

__newindex: The indexing assignment table[key] = value.

__call: One use for this event is to use it on a class like object so you can create a new instance on the class by calling it.

Library Metamethods

__tostring

__metatable

Other

__concat

__len

__gc