# tolua - accessing C/C++ code from Lua

News: tolua 4.0 alpha version available.

tolua is a tool that greatly simplifies the integration of C/C++ code with Lua. Based on a cleaned header file, tolua automatically generates the binding code to access C/C++ features from Lua. Using Lua API and tag method facilities, tolua maps C/C++ constants, external variables, functions, classes, and methods to Lua.

tolua current version is 3.2 and is implemented upon Lua 3.2. Several improvements have been made since version 3.0 (see Changes since v3.0 for details).

The sections below describe how to use tolua. Please send me bug reports, suggestions, and comments.

- Shortcuts:
  - How toLua works
  - How to use toLua
  - Basic Concepts
  - Binding constants
  - Binding external variables
  - Binding functions
  - Binding struct fields
  - Binding classes and methods
  - Module definition
  - Renaming constants, variables and functions
  - Storing additional fields
  - Exported utility functions
  - Changes since v2.*
  - Changes since v1.*
  - Credits
  - Availability

### How tolua works

To use totua, we create a package file, a C/C++ cleaned header file, listing the constants, variables, functions, classes, and methods we want to export to the Lua environment. Then tolua parses this file and creates a C/C++ file that automatically binds the C/C++ code to Lua. If we link the created file with our application, the specified C/C++ code can be accessed from Lua.

Let's start with some examples. If we specify as input the following C-like header file to tolua:

    #define FALSE 0
    #define TRUE 1

    enum {
     POINT = 100,
     LINE,
     POLYGON
    }

    Object* createObejct (int type);
    void drawObject (Object* obj, double red, double green, double blue);
    int isSelected (Object* obj);

A C file that binds such a code to Lua is automatically generated. Therefore, in Lua code, we can access the C code, writing, for instance:

    ...
    myLine = createObject(LINE)
    ...
    if isSelected(myLine) == TRUE then
      drawObject(myLine, 1.0, 0.0, 0.0);
    else
      drawObject(myLine, 1.0, 1.0, 1.0);
    end
    ...

Also, consider a C++-like header file:

    #define FALSE 0
    #define TRUE 1

    class Shape
    {
      void draw (void);
      void draw (double red, double green, double blue);
      int isSelected (void);
    };

    class Line : public Shape
    {
     Line (double x1, double y1, double x2, double y2);
     ~Line (void);
    };

If this file is used as input to tolua, a C++ file is automatically generated proving access to such a code from Lua. Therefore, it would be valid to write Lua statements like:

    ...
    myLine = Line:new (0,0,1,1)
    ...
    if myLine:isSelected() == TRUE then
     myLine:draw(1.0,0.0,0.0)
    else
     myLine:draw()
    end
    ...
    myLine:delete()
    ...

The package file (usually with extension `.pkg`) passed to tolua is not the real C/C++ header file, but a cleaned version of it. For instance, the C++ methods should be public since we are accessing them from external code, but no `public:` keyword appears in it. tolua does not implement a complete parse to interpret C/C++ code, but it understands a few declarations that are used to describe the features that are to be exported to Lua. However, obtaining a cleaned header file should be a matter of rearraging existing declaration of the real header file.

### How to use toLua

tolua is composed by two pieces of code: an executable and a library. The executable represents the parser that reads a package file and output a C/C++ code that implements the binding to access the C/C++ features from Lua. If the package file is a C++ like code (i.e., includes class definitions), a C++ code is generated. If the cleaned header file is a C like code (i.e., without classes), a C code is generated. tolua accepts a set of options. Running `"tolua -h"` displays the current accepted options. For instance, to parse a file called `myfile.pkg` genereating the binding code in `myfile.c`, we do:

`tolua -o myfile.c myfile.pkg`

The generated code must be compiled and linked with the application to provide the desired access from Lua. Each parsed file represents a package being exported to Lua. By default, the package name is the input file root name (`myfile` in the example). The user can specify a different name for the package:

`tolua -n pkgname -o myfile.c myfile.pkg`

The package should also be explicitaly initialized. To initialize the package from our C/C++ code, we must declare and call the initialization function. The initialization function is defined as

`int tolua_pkgname_open (void);`

where `pkgname` represents the name of the package being bound. If we are using C++, we can opt for automatic initialization:

`tolua -a -n pkgname -o myfile.c myfile.pkg`

In that case, the initialization function is automatically called. However, if we are planning to use multiple Lua states, automatic initialization does not work, because the order static variables are initialized in C++ is not defined.

The binding code generated by tolua uses a set of functions defined in the tolua library. Thus, this library also has to be linked with the application. The file `tolua.h` is also necessary to compile the generated code.

An application can use tolua object-oriented framework (see exported utility functions) without binding any package. In that case, the application must call tolua initialization function (this function is called by any package file initialization function):

`int tolua_open (void);`

If multiple Lua states are to be used, after setting a Lua state, we need to call a function to restore tolua internal state:

`void tolua_restorestate (void);`

### Basic Concepts

The first step in using tolua is to create the package file. Starting with the real header files, we clean them by declaring the features we want to access from Lua in a format that tolua can understand. The format tolua understands is simple C/C++ declarations as described below.

#### Basic types

tolua automatically maps C/C++ basic types to Lua basic types. Thus, `char`, `int`, `float`, and `double` are mapped to the Lua type `number`;`char*` is mapped to `string`; and`void*` is mapped to `userdata`. Types may be preceeded by modifiers (`unsigned`, `static`, `short`, `const`, etc.); however, be aware that tolua ignores the modifier `const` if applied to basic types. Thus, if we pass a constant basic type to Lua and then pass it back to C/C++ code where a non-constant is expected, the constant to non-constant conversion will be silently done.

Functions in C/C++ can also manipulate Lua objects explicitly. Thus `lua_Object` is also considered a basic type. In this case, any Lua value matches it.

#### User defined types

All other types that appear in the package file being processed are considered user defined types. These are mapped to tagged userdata type in Lua. Lua can only store pointers to user defined types; although, tolua automatically makes the necessary arrangement to deal with references and values. For instance, if a function or method returns a value of user defined type, tolua allocates a clone object when returning it to Lua and sets the garbage collection tag method to automatically free the allocated object when no longer in use by Lua.

For user defined types, constness if preserved. Thus passing a non-constant user defined type to a function that expects constant type generates an type mismatching error.

#### `NULL` and `nil`

C/C++ `NULL` or `0` pointers are mapped to Lua `nil` type; conversely, `nil` may be specified wherever a C/C++ pointer is expected. This is valid for any type: `char*`, `void*`, and pointers to user defined types.

#### Typedefs

tolua also accepts simple typedef's inside the package files. Any occurrence of a type after its definition is mapped by tolua to the base type. They are useful because several packages redefine the basic C/C++ types to their own types. For instance, one can define the type `real` to represent a `double`. In that case, `real` can be used to specify the variable types inside the package file interpreted by tolua, but only if we include the following definition before any use of the type `real`.

`typedef double real;`

Otherwise, `real` would be interpreted as a user defined type and would not be mapped to Lua numbers.

#### Including real header files

In the package file, we must specify which are the real header files that should be included so that the generated code can access the constants, variables, functions, and classes we are binding. Any line in the package file beginning with a $ is insert into the generated binding C/C++ code without any change, but the elimination of the $ itself. We use this feature to include the real header files. So, our package files will usually start with a set of $ beginning lines specifying the files that must be included, that is, the files the package file is based on.

    /* specify the files to be included */

    $#include "header1.h"                 // include first header
    $#include "header2.h"                 // include second header

As illustrated, tolua also accepts comments, using C or C++ convention, inside the package file. Nested C-like comments can also be used.

In the following sections, we describe how to specify the C/C++ code we want to bind to Lua. The formats are simplified valid C/C++ statments. It is very simple to convert an real C/C++ header file into a package file.

### Binding constants

To bind constants, tolua accepts both define's and enum's. For define's the general format is:

    #define NAME [ VALUE ]

The value, as showed above, is optional. If such a code is inserted inside the file being processed, tolua generates a code that allows the use of `NAME` as a Lua global variable that has the corresponding C/C++ constant value. Only numeric constants are accepted.

For enum's, the general format is:

    enum {
      NAME1 [ = VALUE1 ] ,
      NAME2 [ = VALUE2 ] ,
      ...
      NAMEn [ = VALUEn ]
    };

Similarly, tolua creates a set of global variables, named `NAMEi`, with their corresponding values.

### Binding external variables

Global extern variables can also be exported. In the cleaned header file they are specified as:

    [extern] type var;

tolua binds such declarations to Lua global variables. Thus, in Lua, we can access the C/C++ variable naturally. If the varaible is non-constant, we can also assign the variable a new value from Lua.

### Binding functions

Functions are also specified as conventional C/C++ declarations:

    type funcname (type1 par1[, type2 par2[,...typeN parN]]);

The returned type can be `void`, meaning no value is returned. A function can also have no parameter. In that case, `void` may be specified in the place of the list of parameters. The parameter types must follow the rules already posted. tolua creates a Lua function binding the C/C++ function. When calling a function from Lua, the parameter types must match the corresponding C/C++ types, otherwise, tolua generates an error and reports which parameter is wrongly specified. If a parameter name is omitted, tolua names it automatically, but its type should be a basic type or user type previously used.

#### Arrays

tolua also deals with function or method parameters that represent arrays of values. Arrays can be of any type. The corresponding Lua objects for arrays are Lua tables indexed with numeric values. Be aware that index 1 in Lua is mapped to index 0 in an C/C++ array. The nice thing about arrays is that the corresponding Lua tables have their values updated if the C/C++ function changes the array contents.

The arrays must be predimensioned. For instance:

`void func (double a[3]);`

is a valid function declaration for tolua and calling this function from Lua would be done by, for instance:

`p = {1.0,1.5,8.6}`
`func (p)`

The array dimension need not be a constant expression; the dimension can also be especified by any expresion that can be evaluated in run time. For instance:

`void func (int n, int m, double image[n*m]);`

is also valid since the expression `n*m` is valid in the binding function scope. However, be aware that tolua uses dynamic allocation for binding this function, what can degrade the perfomance.

Despite the dimension specification, it is important to know that all arrays passed to the actual C/C++ function are in the local scope of the binding function. So, if the C/C++ function being called needs to hold the array pointer for later use, the binding code will not work properly.

#### Overloaded functions

Overloaded functions are accepted. Remenber that the distinction between two functions with the same name is made based on the parameter types that are mapped to Lua. So, although

`void func (int a);`
`void func (double a);`

represent two different functions in C++, they are the same function for tolua, because both `int` and `double` are mapped to the same Lua type: `number`.

Another tricky situation occurs when expecting pointers. Suppose:

    void func (char* s);
    void func (void* p);
    void func (Object1* ptr);
    void func (Object2* prt);

Although these four functions represent different functions in C++, a Lua statment like:

    func(nil)

matches all of them.

It is important to know that tolua decides which function will be called in run-time, trying to match each provided function. tolua first tries to call the last specified function; if it fails, tolua then tries the previous one. This proccess is repeated until one function matches the calling code or the first function is reached. For that reason, the mismatching error message, when it occurs, is based on the first function specification. When performance is important, we can specify the most used function as the last one, because it will be tried first.

tolua allows the use of overloaded functions in C, see Renaming for details.

#### Default parameter values

The last function parameters can have associated default values. In that case, if the function is called with fewer parameters, the default values are assumed. The format to specify the default values is the same as the one used in C++ code:

`type funcname (..., typeN-1 parN-1 [= valueN-1], typeN parN [= valueN]);`

toLua implements this feature without using any C++ mechanism; so, it can be used also to bind C functions.

We can also specify default values for the elements of an array (there is no way to specify a default value for the array itself, though). For instance:

`void func (int a[5]=0);`

sets the default element values to zero, thus the function can be called from Lua with an uninitialized table.

For Lua object types (`lua_Object`), tolua defines a constant that can be used to specify `nil` as default value:

`void func (lua_Object lo = TOLUA_NIL);`

#### Multiple returned values

In Lua, a function may return any number of values. tolua uses this feature to simulate values passed by reference. If a function parameter is specified as a pointer to or reference of a basic type or a pointer to or reference of a pointer to an user defined type, tolua accepts the corresponding type as input and returns, besides the conventional function returned value, if any, the updated parameter value.

For instance, consider a C function that swaps two values:

`void swap (double* x, double* y);`

or

`void swap (double& x, double& y);`

If such a function is declared in the package file, tolua binds it as a function receiving two numbers as input and returning two numbers. So, a valid Lua code would be:

`x,y = swap(x,y)`

If the input values are not used, the use of default parameter value allows calling the function from Lua without specifying them:

`void getBox (double* xmin=0, double* xmax=0, double* ymin=0, double* ymax=0);`

In Lua:

`xmin, xmax, ymin, ymax = getBox()`

With user defined types, we would have for instance:

`void update (Point** p);`

or

`void update (Point*& p);`

### Binding struct fields

User defined types are nicely bound by tolua. For each variable or function type that does not correspond to a basic type, tolua automatically creates a tagged userdata to represent the C/C++ type. If the type corresponds to a struct, the struct fields can be directly accessed from Lua, indexing a variable that holds an object of such a type. In C code, these types are commonly defined using typedef's:

    typedef struct [name] {
       type1 fieldname1;
       type2 fieldname2;
       ...
       typeN fieldnameN;
    } typename;

If such a code is inserted in the package file being processed, tolua allows any variable that holds an object of type `typename` to access any listed field indexing the variable by the field name. For instance, if `var` holds a such object, `var.fieldnamei` accesses the field named `fieldnamei`.

### Binding classes and methods

C++ class definitions are also supported by tolua. Actually, the tolua deals with single inheritance and polymorphism in a natural way. The subsections below describe what can be exported by a class definition.

#### Specifying inheritance

If `var` is a Lua variable that holds an object of a derived class, `var` can be used wherever its base class type is expected and `var` can access any method of its base class. For this mechanism to take effect, we must indicate that the derived class actually inherits the base class. This is done in the conventional way:

    class classname : public basename
    {

     /* class definition */

    };

#### Specifying exported members and methods

As for struct fields, class fields, static or not, can be exported. Class methods and class static methods can also be exported. Of course, they must be declared as public in the actual C++ code (although the `public:` keyword does not appear in the package files).

For each bound class, tolua creates a Lua table and stores it at a global variable which name is the name of the C++ class. Static exported fields are accessed by indexing this table with the field names (similar to struct fields). Non-static exported fields are accessed by indexing the variable that holds the object. Class methods follow the format of the function declaration showed above. They can be accessed from Lua code using the conventional way Lua uses to call methods, applied of course to a variable that holds the appropriate object or to the class table, for static methods.

There are a few special methods that are also supported by tolua. Constructors are called as static methods, named `new`. Destructors are called as a conventional method called `delete`.

Note that tolua does support overload. This applies even for constructors. Also note that the `virtual` keyword has no effect in the package file.

The following code exemplifies class definitions that can be interpreted by tolua.

    class Point {
       static int n;    // represents the total number of created Points

       double x;        // represents the x coordinate
       double y;        // represents the y coordinate

       static char* className (void);   // returns the name of the class

       Point (void);                          // constructor 1
       Point (double px, double py);          // constructor 2
       ~Point (void);                         // destructor

       Point add (Point& other);              // add points, returning another one
    };

    class ColorPoint : public Color {
       int red;      // red color component [0 - 255]
       int green;    // green color component [0 - 255]
       int blue;     // blue color component [0 - 255]

       ColorPoint (double px, double py, int r, int g, int b);
    };

If this segment of code is processed by tolua, we would be able to write the following Lua statements:

    p1 = Point:new(0.0,1.0)
    p2 = ColorPoint:new(1.5,2.2,0,0,255)
    print(Point.n)                     -- would print 2
    p3 = p1:add(p2)
    print(p3.x,p3.y)                   -- would print 1.5 and 3.2
    print(p2.red,p2.green,p2.blue)     -- would print 0, 0, and 255
    print(
    p1:delete()                        -- call destructor
    p2:delete()                        -- call destructor

Note that we can only explicitly delete objects that we explicitly create. In the example above, the point `p3` will be garbage-collected by tolua automatically; we cannot delete it.

Of course, we need to specify only the methods and members we want to access from Lua. Sometimes, it will be necessary to declare a class with no member or method just for the sake of not breaking a chain of inheritances.

#### Overloaded operators

tolua automatically binds the following binary operators:

    operator+   operator-   operator*   operator/
    operator<   operator>   operator<=  operator>=

For the relational operators, toLua also automatically converts a returned `0` value into `nil`, so false in C becomes false in Lua.

As an example, suppose that in the code above, instead of having:

       Point add (Point& other);              // add points, returning another one

we had:

       Point operator+ (Point& other);        // add points, returning another one

In that case, in Lua, we could simply write:

    p3 = p1 + p2

The indexing operator (`operator[]`) when receiving a numeric parameter can also be exported to Lua. In this case, tolua accepts reference as returned value, even for basic types. Then if a reference is returned, from Lua, the programmer can either get or set the value. If the returned value is not a reference, the programmer can only get the value. An example may clarify: suppose we have a vector class and bind the following operator:

       double& operator[] (int index);

In this case, in Lua, we would be able to write: `value = myVector[i]` and also `myVector[i] = value`, what updates the C++ object. However, if the bound operator was:

       double operator[] (int index);

we would only be able to write: `value = myVector[i]`.

Free functions (i.e., not class members) that overload operators are not supported.

### Module definition

tolua allows us to group constants, variables, and functions in a module. The module itself is mapped to a table in Lua, and its constants, variables, and functions are mapped to fields in that table. The general format to specify a module is:

`module name`
`{`
`... // constant, variable, and function declarations`
`}`

Thus, if we bound the following module declaration:

`module mod`
`{`
`#define N`
`extern int var;`
`int func (...):`
`}`

In Lua we would be able to access such features by indexing the module: `mod.N`, `mod.var`, `mod.func`.

### Renaming constants, variables and functions

When exporting constants, variable, and functions (members of a class or not), we can rename them, such that they will be bound with a different name from their C/C++ counterparts. To do that, we write the name they will be referenced in Lua after the character `@`. For instance:

`extern int cvar @ lvar;`

`#define CNAME @ LNAME`

`enum {`
`CITEM1 @ LITEM1,`
`CITEM2 @ LITEM2,`
`...`
`};`

`void cfunc @ lfunc (...);`

`class T`
`{`
`double cfield @ lfield;`
`void cmeth @ lmeth (...);`
`...`
`};`

In such a case, the global variable `cvar` would be identified in Lua by `lvar`, the constant `CNAME` by `LNAME`, and so on. Note that class cannot be renamed, because they represent types in C.

This renaming feature allows function overload in C, because we can choose to export two different C functions with a same Lua name:

`void glVertex3d @ glVertex (double x, double y, double z=0.0);`
`void glVertexdv @ glVertex (double v[3]=0.0);`

### Storing additional fields

Finally, it is important to know that even though the variables that hold C/C++ objects are actually tagged userdata for Lua, tolua creates a mechanism that allows us to store any additional field attached to these objects. That is, these objects can be seen as conventional Lua tables.

    obj = ClassName:new()

    obj.myfield = 1  -- even though "myfield" does not represent a field of ClassName

Such a construction is possible because, if needed, tolua automatically creates a Lua table and associates it with the object. So that, the object can store additional fields not mapped to C/C++, but actually stored in the conjugate table. The Lua programmer accesses the C/C++ features and these additional fields in an uniform way. Note that, in fact, these additional fields overwrite C/C++ fields or methods when the names are the same.

### Exported utility functions

tolua uses itself to export some utility functions to Lua, including its objected-oriented framework. The package file used by tolua is shown below:

`module tolua`
`{`
`void tolua_using @ using (lua_Table module);`
`char* tolua_type @ type (lua_Object lo);`
`void tolua_foreach @ foreach (lua_Object lo, lua_Function f);`
`void tolua_class @ class (lua_Table derived, lua_Table base=TOLUA_NIL);`
`void tolua_instance @ instance (lua_Table instance, lua_Table classobj);`
`lua_Object tolua_base @ base (lua_Object lo);`
`}`

#### tolua.using (table)

This functions receives a table and maps all its fields to the global environment. Thus we can map an entire module and access its features without the module prefix. For instance, if in our Lua code we do:

`tolua.using(tolua)`

all tolua utility functions are mapped to the global environment.

#### tolua.type (var)

Returns a string representing the object type. For instance, `tolua.type(tolua)` returns the string `generic module` and `tolua.type(tolua.type)` returns `cfunction`. Similarly, if `var` is a variable holding a user defined type `T`, `tolua.type(var)` would return `const T` or `T`, depending whether it is a constant reference.

#### tolua.foreach (object)

Allows us to traverse the conjugate table of an user defined instance. If applied to conventional table, it has a similar behavior as the Lua built-in `foreach` function. The difference is that this function filters all fields starting with a dot, not passing them to the provied callback function. This filter is need because tolua adds "hidden" fields to the tables it manipulates, and all its "hidden" fields start with a dot.

#### tolua.class (table, base=nil)

Creates a class by setting the appropriate tag methods to the given table. The created class can inherit from a base class, previously created.

#### tolua.instance (table, class)

Sets the given table to be an instance of the given class. This and the previous utility functions allow object-oriented programming in Lua. As an example consider:

`-- define a Point class`
`classPoint = { x=0, y=0 }`
`tolua.class(classPoint) -- set as a class`

`-- define print method`
`function classPoint:print ()`
`print(self.x,self.y)`
`end`

`-- define add method`
`function classPoint:add (p2)`
`return Point{x=self.x+p2.x,y=self.y+p2.y}`
`end`

`-- define a Point constructor`
`function Point (p)`
`tolua.instance(p,classPoint) -- set as an instance of classPoint`
`return p end`

`-- define a Color Point class`
`classColorPoint = { color = 'black' }`
`tolua.class(classColorPoint,classPoint) -- set as class inheriting from classPoint`

`-- define class methods`
`function classColorPoint:print ()`
`print(self.x,self.y,self.color)`
`end`

`-- define Color Point constructor`
`function ColorPoint (p)`
`tolua.instance(p,classColorPoint) -- set as an instance of classColorPoint`
`return p`
`end`

`-- Some valid codes would then be`
`p = Point{x=1}`
`q = ColorPoint{x=2,y=3,color=2}`
`r = p:add(q)`
`r:print() --> would print "3 3"`

### Changes since v. 3.0

- Support for binding arrays as variables and struct/class field;
- Support for embedding Lua code in generated the binding code;
- New utility functions: cast and takeownership;
- Option to create the corresponding header file of the binding code;
- New "close" package function;
- Fixed bug on cloning objects in C++;
- Fixed bug on enumeration and struct parsing;

  ### Changes since v. 2.0

- There is a new executable parser;
- Support for multiple Lua states is provided;
- Support for module definition is provided;
- Global variables is now directly bound to Lua global variables;
- Constness of user defined types is preserved in Lua;
- Support for multiple returned values from C/C++ is provided (simulating parameters passed by reference);
- Constants, variables, and functions bound to Lua can have different names from their C/C++ counterparts;
- Object-oriented framework (and other utility functions) used in tolua is now exported for Lua programmers;

  #### Incompatibilities

  Lua code based on tolua v2.* should run with no change on tolua v3.0. Although, it may be necessary to change the .pkg file in order to get the same behavior. The following incompatibilities exist:
  - Parameters defined as pointer to basic types are no longer converted to arrays of dimension one; they are now considered parameters based by reference.
  - Automatic initilization for C++ code must be explicitly requested when using the new parser;
  - Global variables are no longer mapped to a table; the definition of a module including the global variables may be used to simulate the old behavior;
  - The initilization function is no longer toLua_package_open but tolua_package_open, without the capital letter (sorry!).

  ### Changes since v. 1.*

- The binding code should run much faster;
- The cleaned header file extension should now be `.pkg` instead of `.L`;
- Type modifiers is now accepted (though the current version ignores `const`'s);
- Returning object by value is accepted and memory allocation is controled by Lua garbage collection;
- Overloaded functions/methods are accepted;
- Parameters with default values are accepted;
- Some overloaded operators are automatically bound.

  ### Credits

  Luiz Henrique de Figueiredo had the idea of creating a tool to automatically bind C code to Lua. L.H.F. wrote the very first version of such a tool (that bound C functions, variables, and constants) in awk. At that time, I was only responsible for the C code that supported the generated binding code.

  ### Availability

  tolua is freely available by ftp. The software provided hereunder is on an "as is" basis, and the author has no obligation to provide maintenance, support, updates, enhancements, or modifications.

  Software and page created and maintained by Waldemar Celes .
  Last update: Ago 1998