How to bind a C++ class to Lua?
Lunar wraps this common pattern:
#include "lunar.h"
class Account {
lua_Number m_balance;
public:
static const char className[];
static Lunar<Account>::RegType methods[];
Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); }
int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; }
int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; }
int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; }
~Account() { printf("deleted Account (%p)\n", this); }
};
const char Account::className[] = "Account";
Lunar<Account>::RegType Account::methods[] = {
LUNAR_DECLARE_METHOD(Account, deposit),
LUNAR_DECLARE_METHOD(Account, withdraw),
LUNAR_DECLARE_METHOD(Account, balance),
{0,0}
};
....
Lunar<Account>::Register(L);
This does all the userdata and metatable creation for you; each method of your class iwill be a normal Lua C function that gets its arguments from the stack as usual.
tolua++ is an attractive solution because it can parse 'cleaned' C/C++ headers and generate the bindings.
A very simple example: say we have a burning need for the C function strstr
; given this file lib.pkg
:
const char *strstr(const char*, const char*);
then tolua -o lib.c lib.pkg
will generate a binding file lib.c
which can be compiled and linked against the tolua
library.
require 'lib'
may result in a surprise, since there is no table lib
generated, only a global function strstr
. To put the function into a tble lib
, lib.pkg
must look like this:
module lib
{
const char *strstr(const char*, const char*);
}
So .pkg files are not really C/C++ headers; however, it is not difficult to convert headers into .pkg files by cleaning them up.
An alternative to tolua is luabind
SWIG is a popular binding generator which can target Lua as well. If a SWIG binding exists for a library, then it is relatively easy to generate Lua bindings from it.