Luwra Documentation

Luwra

A header-only C++ library which provides a Lua wrapper with minimal overhead.

Usage

Refer to the wiki pages or the documentation. In order to use the library you must clone this repository and include lib/luwra.hpp.

Have a question? Simply ask or open an issue.

Examples

In the following examples lua refers to an instance of lua_State*.

Easily push values onto the stack:

1 {c++}
2 // Push an integer
3 luwra::push(lua, 1338);
4 
5 // Push a number
6 luwra::push(lua, 13.37);
7 
8 // Push a boolean
9 luwra::push(lua, false);
10 
11 // Push a string
12 luwra::push(lua, "Hello World");

Or retrieve them:

1 {c++}
2 // Your function
3 int my_fun(int a, int b) {
4  return a + b;
5 }
6 
7 // Prepare stack
8 luwra::push(lua, 13);
9 luwra::push(lua, 37);
10 
11 // Apply your function
12 int result = luwra::apply(lua, my_fun);
13 
14 // which is equivalent to
15 int result = luwra::apply(lua, 1, my_fun);
16 
17 // and equivalent to
18 int result = luwra::apply(lua, -2, my_fun);
19 
20 // All of this is essentially syntactic sugar for
21 int result = my_fun(luwra::read<int>(lua, 1), luwra::read<int>(lua, 2));

Generate a C function which can be used by Lua:

1 {c++}
2 // Assuming your function looks something like this
3 int my_function(const char* a, int b) {
4  // ...
5 }
6 
7 // Convert to lua_CFunction
8 lua_CFunction cfun = LUWRA_WRAP(my_function);
9 
10 // Do something with it, for example set it as a Lua global function
11 luwra::setGlobal(lua, "my_function", cfun);
1 -- Invoke the registered function
2 local my_result = my_function("Hello World", 1337)
3 print(my_result)

Or register your own class:

1 {c++}
2 struct Point {
3  double x, y;
4 
5  Point(double x, double y):
6  x(x), y(y)
7  {
8  std::cout << "Construct Point(" << x << ", " << y << ")" << std::endl;
9  }
10 
11  ~Point() {
12  std::cout << "Destruct Point(" << x << ", " << y << ")" << std::endl;
13  }
14 
15  void scale(double f) {
16  x *= f;
17  y *= f;
18  }
19 
20  std::string __tostring() {
21  return "<Point(" + std::to_string(x) + ", " + std::to_string(y) + ")>";
22  }
23 };
24 
25 // Register the metatable and constructor
26 luwra::registerUserType<Point(double, double)>(
27  lua,
28 
29  // Constructor name
30  "Point",
31 
32  // Methods need to be declared here
33  {
34  LUWRA_MEMBER(Point, scale),
35  LUWRA_MEMBER(Point, x),
36  LUWRA_MEMBER(Point, y)
37  },
38 
39  // Meta methods may be registered aswell
40  {
41  LUWRA_MEMBER(Point, __tostring)
42  }
43 );
1 -- Instantiate 'Point'
2 local point = Point(13, 37)
3 
4 -- Invoke 'scale' method
5 point:scale(1.5)
6 
7 -- Convert to string via '__tostring' meta method
8 print(point)
9 
10 -- Read properties 'x' and 'y'
11 print(point:x(), point:y())
12 
13 -- Set property 'x'
14 point:x(4.2)

Requirements

You need a C++11-compliant compiler and at least Lua 5.1 to get this library to work. I recommend using Lua 5.3 or later, to avoid the messy lua_Integer situation. LuaJIT 2.0 seems to work, apart from user types, which fail for yet unknown reasons.

Tests

The attached GNU Makefile allows you to run both examples and tests using make examples and make test respectively. You might need to adjust LUA_* variables, so Luwra finds the Lua headers and library.

Assuming all headers are located in /usr/include/lua5.3 and the shared object name is liblua5.3.so, you need to invoke this:

1 make LUA_INCDIR=/usr/include/lua5.3 LUA_LIBNAME=lua5.3 test