Lua can be embedded and extended[1] with code or applications written in other languages. Code and values in another language can be exposed to Lua and vice-versa. The following lists low and high level solutions for binding between Lua and other languages.Lua C API
The most direct method to bind to Lua is with the Lua C API. The C API consists of two parts: the basic API (lua.h) provides the primitive functions for all interactions between C and Lua, while the auxiliary library (lauxlib.h) provides higher-level functions for some common tasks.[2]
Enabling API checking
By default, the Lua C API does almost no sanity checking of arguments passed to it. Passing incorrect stack indexes, for example, can result in segfaults or random data corruption. You should always enable API checking in any debug build. You can do this by compiling with the option -DLUA_USE_APICHECK
. luaconf.h
uses this C macro to define luai_apicheck
to call assert()
in various places (you can also edit this definition to do something possibly more useful).
Example
Some examples of using the C API can be found by examining the source code of Lua's own standard libraries (src/*lib.c
}. For example, the math library (math.*
) is implemented in the file src/lmathlib.c
. The basic form of this file is outlined below. First we import various headers, including the C API (lua.h) and the axillary library (lauxlib.h):
#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"
Then various Lua functions implemented in C are defined. These all have the same signature, and arguments are passed via Lua's own stack. For example, the sin function is defined as follows. luaL_check_number()
is used to check the correct type of the sin
function argument. lua_pushnumber()
is used to return the sine calculated. Note, the return value of the math_sin()
function is the number of values returned (lua functions can return many values).
static int math_sin (lua_State *L) {
lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
return 1;
}
These functions are registered into Lua by building a table of function pointers and names and then calling luaL_register()
. Constants pi
and huge
are set separately. This registration code is placed in a function named luaopen_math()
, which can be called statically (e.g. from linit.c) or dynamically (via Lua's shared library loading mechanism via require
).
static const luaL_reg mathlib[] = {
{"abs", math_abs},
{"cos", math_cos},
{"sin", math_sin},
... etc...rest of table not included, but make sure you finish of with:
{NULL, NULL}
};
/*
** Open math library
*/
LUALIB_API int luaopen_math (lua_State *L) {
luaL_register(L, LUA_MATHLIBNAME, mathlib);
lua_pushnumber(L, PI);
lua_setfield(L, -2, "pi");
lua_pushnumber(L, HUGE_VAL);
lua_setfield(L, -2, "huge");
return 1;
}
Binding C/C++ with Lua
C
- [luapi] (5.0) - a C API over the official Lua API.
- [CaLua] (5.0) - A way to bind C functions and structures to Lua, and work with C pointers, arrays and functions in Lua. (uses x86 assembly)
C Foreign Function Interfaces (FFI)
- [Alien] (5.1) - a foreign function interface (FFI) for Lua. An [FFI] lets Lua code call C functions directly without having to write C "glue", so you can use Alien to write C extensions purely in Lua. (Wraps libffi)
- [C/Invoke for Lua] (5.1) - Use [C/Invoke] from Lua to call C libraries (DLLs, .so files) directly, like Microsoft's P/Invoke and Python's ctypes. (Similar to Alien)
C Inline
- [Luatcc] (5.1) - a Lua binding for libtcc, which is the core library of the [Tiny C Compiler]. It allows compiling and loading C code directly from Lua.
- InlineCee provides a similar approach, invoking other compilers out-of-process.
C++
Various C++ or C++ template bindings have been developed to simplify the process in C++:
- [CppLua] (5.0) - a C++ wrapper of the the Lua API; handles class member functions.
- [Diluculum] (5.1) - A library that intends to make the coexistence of C++ and Lua more harmonious.
- [Luabind] (5.1) - a template-based binding of C++ classes and functions which uses the Boost library.
- [Luabridge] (5.1) - lightweight, dependency-free, template-based library for exporting C++ classes/functions to Lua environments.
- [Luna] (4.0) and LunaWrapper (5.1) - clean, template-based method of binding a C++ class to Lua. See also [LTN5] and SimplerCppBinding.
- [MLuaBind] (5.1) - a template-based binding of C++ classes and functions which uses the Loki library.
- [MultiScript] (5.1) - a simple library that demonstrates Lua independent C++ interface to bind C++ classes and functions.
- [SLB] (5.1) - Simple Lua Binder, template-based library very similar to Luabind but without boost.
- [Sweet Lua] (5.1) - a template-based binding of C++ classes and functions. (MSVC)
See also:
Automatic binding generators
- [toLua] (5.1) - If there is a lot of information to bind then automating the process using a binding generator can help. toLua is one such tool. A package file is prepared, which is a cleaned up version of the C/C++ interface. Another advantage of this technique is that less work may be required between Lua versions if the Lua C API changes significantly.
- [tolua++] (5.1) - an extended version of tolua, with some extra features oriented to c++. See also CompilingToluappWithoutScons (compiling tolua++ without SCons).
- [CPB] (5.0) - A simple, fast, and powerful binding library for the Lua scripting language. It takes the information from the compilers symbols and generates binding code directly from it. (MSVC)
- [SWIG] (5.0/5.1) - Simplified Wrapper and Interface Generator, it can generate bindings between your C/C++ code and a variety of scripting languages (Python, Perl, Tcl/Tk, Ruby, ...), including Lua. A big advantage might be that *one* interface file, similar to that used in tolua/tolua++, is used for bindings to all languages supported by SWIG. See also the SWIG docs pertaining to Lua [3].
Proxy wrapper for DLL
Function information can be exported from dynamically linked libraries. [FuBi] describes a method to call these functions from script (or RPC). This functionality can be used from Lua [4].
Miscellaneous
- [wxScript] (5.0) - A set of abstract classes to add script-interpreter support to your wxWidgets applications/libraries.
Other languages
Ada
- [Lua-Ada] (5.0) - Lua binding to Ada 95. (link broken)
- [lua-ada] (5.1) - Ada bindings to the Lua language (public domain)
Basic
- [FreeBASIC] - A open-source BASIC compiler that comes with the Lua headers and libraries.
- [PBLua] (5.0.2) - a Purebasic library wrapping Lua 5.0.2. (deprecated)
- [PowerBLua] (5.0) - PowerBASIC include & source for wrapping Lua (work in progress).
- [BlitzMax] (5.1.2) - a BlitzMax module wrapping Lua 5.1.2 (note: the link currently points into this wiki (which is usually a bad idea in this list) but this will change as soon as the package has its own web site)
D
- [DLua] (5.1) - Lua bindings for the D programming language.
Haskell
- [HsLua] (5.1) - Scripting.Lua binding for Haskell.
Java
- [LuaJava] (5.1) - allows scripts written in Lua to manipulate components developed in Java (JNI to native Lua) and vice-versa
Objective C
- [LuaObjCBridge] (5.1) - calling Lua from Objective-C and vice-versa (MacOS X Cocoa).
Pascal
- [LuaPascal] (5.1) - Integrates Lua and Object Pascal languages. Allows Lua to manipulate components developed in Pascal/Delphi.
- [Lua4Delphi] (5.1) - Delphi and VCL Lua interface and Util functions.
- LuaDelphi (5.1) - calling Lua from Delphi/Pascal.
- [THlua] (5.0) - Lua distribution for Delphi/Kylix/Free
Pascal. Includes header files, samples, some utils. For Windows, there are also LuaBinaries and project files for Visual Studio 6. - [Delphi-Lua] (5.0) - calling Lua from Delphi/Pascal.
- [DLua] (5.0) - calling Lua from Delphi/Pascal.
Perl
- [LuaPerl] (5.1) - calling Perl (5.8 and 5.10 streams) from Lua.
- [Inline::Lua] (5.1) - calling Lua from Perl (inline).
- [Data::Lua] (5.1) - parse variables out of Lua code. Relies on Inline::Lua.
- [re::engine::Lua] (5.1) - supplies the Lua regular expression dialect for Perl.
PHP
- [phplua] (5.1) - PHP extension which allows embedding the Lua interpreter in a PHP application
Python
- LunaticPython [5] (5.1) - Lua/Python two-way bridge. Allows embedding of Lua interpreter in Python programs, and embedding of Python interpreter in Lua programs.
Ruby
Tcl
Frameworks
CORBA
Windows COM
- LuaCom - (5.1/5.0) Lua interface to Microsoft's Component Object Model (COM). Calling COM objects from Lua and implementing COM objects in Lua.
Windows .NET
- [dotLua] (5.0) - A .NET/CLI library, wrapping the Lua API.
- LuaInterface [6] (5.1) - a library for integration between the Lua language and Microsoft .NET platform's Common Language Runtime (CLR). Lua scripts can use it to instantiate CLR objects, access properties, call methods, and even handle events with Lua functions.
- [LuaNETInterface] [7] (5.1) - a modification of LuaInterface to provide definition of Lua functions using attributes and loaded into the Lua engine using reflection.
See Also
FindPage · RecentChanges · preferences
edit · history
Last edited July 24, 2009 8:30 pm GMT (diff)