Lua Binding


All the CD functions are available in Lua, with a few exceptions. We call it CDLua. To use them the general application will do require"cdlua", and require"cdluaxxxx" to all other secondary libraries that are needed. The functions and definitions will be available under the table "cd" using the following name rules:

cdXxx  -> cd.Xxx    (for functions)
wdXxx -> cd.wXxx (for WD functions)
CD_XXX -> cd.XXX (for definitions)
cdCanvasXXX(canvas,... => canvas:XXX(... (for methods)

New functions (without equivalents in C) were implemented to create and destroy objects that do not exist in C. For instance functions were developed to create and destroy images, pattern, stipple and palette. All the metatables have the "tostring" metamethod implemented to help debugging. Some functions were modified to receive those objects as parameters.

Also the functions which receive values by reference in C were modified. Generally, the values of parameters that would have their values modified are now returned by the function in the same order.

Notice that, as opposed to C, in which enumeration flags are combined with the bitwise operator OR, in Lua the flags are added arithmetically.

In Lua all parameters are checked and a Lua error is emitted when the check fails.

All the objects are garbage collected by the Lua garbage collector, except the canvas because there can be different Lua canvases pointing to the same C canvas. The "tostring" metamethod of the Lua canvas will print both values, Lua and C. The equal method will compare the C canvas value.


Lua 5.1 "require" can be used for all the CDLua libraries. You can use require"cdlua" and so on, but the LUA_CPATH must also contains the following:

"./lib?;"    [in UNIX]

".\\?51.dll;"     [in Windows]

Also compatible with Lua 5.2 and 5.3, just replace the "51" suffix by "52" or "53".

The LuaBinaries distribution already includes these modifications on the default search path.

If you are using another Lua distribution you can use the environment:

export LUA_CPATH=./\?.so\;./lib\?.so\;./lib\?\;   [in UNIX]

Or you can set it in Lua before loading iup modules:

package.cpath = package.cpath .. "./lib?;"    [in UNIX]

package.cpath = package.cpath .. ".\\?51.dll;"     [in Windows]

The simplest form require"cd" and so on, can not be used because there are CD dynamic libraries with names that will conflict with the names used by require during search.

Additionally you can statically link the CDLua libraries, but you must call the initialization functions manually. The cdlua_open function is declared in the header file cdlua.h, see the example below:

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <cdlua.h>
void main(void)
  lua_State *L = lua_open();





Calling cdlua_close is optional. In Lua it can be called using "cd.Close()". It can be used to avoid a small memory leak. (since 5.8.2)

Exchanging Values between C and Lua

Because of some applications that interchange the use of CD canvases in Lua and C, we build a few C functions that are available in "cdlua.h":

cdCanvas* cdlua_checkcanvas(lua_State* L, int pos);
void cdlua_pushcanvas(lua_State* L, cdCanvas* canvas);