Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » LuaGlue Binding Lib for C++11 and Lua 5.2.

This thread is locked; no one can reply to it. rss feed Print
LuaGlue Binding Lib for C++11 and Lua 5.2.
Thomas Fjellstrom
Member #476
June 2000
avatar

I'd like to announce a new Lua binding library for C++11.

If you all remember this thread I said I might make a fancier binding library. That is what I ended up doing.

I've posted it on github, and its licensed under the Zlib license.

It is not currently complete, a few things could use some improvement, and I have no idea what the performance or memory use is like.

Patches welcome :)

https://github.com/Tomasu/LuaGlue

Obligitory example:

lua:

local test = Foo.new(333);
test:abc(1,2,3);

print("ONE: "..Foo.ONE);
print("TWO: "..Foo.TWO);
print("THREE: "..Foo.THREE);

C++

#SelectExpand
1#include <LuaGlue/LuaGlue.h> 2 3class Foo 4{ 5 public: 6 Foo(int i) { printf("ctor! %i\n", i); } 7 ~Foo(); 8 9 int abc(int a, int b, int c) { printf("%i:%i:%i\n", a,b,c); return 143; } 10 static void aaa() { printf("aaa!\n"); } 11}; 12 13int main(int, char **) 14{ 15 LuaGlue state; 16 17 state. 18 Class<Foo>("Foo"). 19 ctor<int>("new"). 20 method("abc", &Foo::abc). 21 method("aaa", &Foo::aaa). 22 constants( { { "ONE", 1 }, { "TWO", 2.0 }, { "THREE", "three" } } ). 23 end().open().glue(); 24 25 if(luaL_dofile(state.state(), "foo.lua")) 26 { 27 printf("failed to dofile: foo.lua\n"); 28 const char *err = luaL_checkstring(state.state(), -1); 29 printf("err: %s\n", err); 30 } 31 32 return 0; 33}

Output:

ctor! 333
1:2:3
ONE: 1
TWO: 2
THREE: three

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Erin Maus
Member #7,537
July 2006
avatar

I love the simplicity. Question: does it support interfaces or inheritance? That's something that's bothered me about previous glue libraries...

E.g., say I have a data class/interface (like LOVE does in some form) and then have a generic WriteData method. Can I pass it a BitmapData, or AudioData, or FooData?

I'll take a look at the code soon. Previous libraries aimed at 5.1.x have way too much template trickery going on...

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

Thomas Fjellstrom
Member #476
June 2000
avatar

Question: does it support interfaces or inheritance? That's something that's bothered me about previous glue libraries...

I haven't tested it, but I don't think it'll get in the way[1]. But you can't define any kind of inheritance, and the library currently treats lua as a second class citizen, as in lua cant subclass you classes.

To support a fuller OO experience would probably require something as crazy as luabind.

Quote:

Previous libraries aimed at 5.1.x have way too much template trickery going on...

You are not going to enjoy LuaGlue then. That simplicity is made possible by the template magic. The new C++11 variable argument templates especially so.

References

  1. I can't remember how C++ handles calling calling methods through pointers, if a method which was referenced via a sub class will work on a super class. definitely something to test.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Erin Maus
Member #7,537
July 2006
avatar

You are not going to enjoy LuaGlue then.

Your code is many times cleaner than Luabind (I'm sure it's thanks to C++11 features). The templates and macros and Boost dependency meant only a C++ guru could understand Luabind. So I guess I should've said preprocessor hacks...

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

Thomas Fjellstrom
Member #476
June 2000
avatar

Your code is many times cleaner than Luabind (I'm sure it's thanks to C++11 features).

Thanks... I think? ;)

Quote:

Your code is many times cleaner than Luabind (I'm sure it's thanks to C++11 features). The templates and macros and Boost dependency meant only a C++ guru could understand Luabind. So I guess I should've said preprocessor hacks...

Yeah, I'm not fond of the boost magic they have going on. A lot of it is done with some magic boost preprocessor stuff. It is really hard to follow.

Update

Making some decent progress on the binding, as I try to use it for one of my projects. Last few things that got added are:

  • The ability to bind a destructor method that gets called when lua gc's the userdata, it will not call your object's destructor, but you could do that yourself.

  • Passing glued objects between lua and C++ and have them be properly converted between raw object pointer and lua userdata as needed.

  • Passing unglued objects between lua and C++ as lightuserdata, also being converted as needed.

extended example:

C++

#SelectExpand
1#include <LuaGlue/LuaGlue.h> 2 3class Foo 4{ 5 public: 6 Foo(int i) { printf("ctor! %i\n", i); } 7 ~Foo(); 8 9 int abc(int a, int b, int c) { printf("%i:%i:%i\n", a,b,c); return 143; } 10 static void aaa() { printf("aaa!\n"); } 11 12 void ptrArgTest(Foo *foo) { printf("ptrArgTest: %p abc:%i\n", foo, foo->abc(4,4,4)); } 13 Foo *ptrTest() { return this; } 14 15 void lua_gc() { printf("__gc!\n"); } 16}; 17 18int main(int, char **) 19{ 20 LuaGlue state; 21 22 state. 23 Class<Foo>("Foo"). 24 ctor<int>("new"). 25 dtor(&Foo::lua_gc). 26 method("abc", &Foo::abc). 27 method("aaa", &Foo::aaa). 28 method("ptrTest", &Foo::ptrTest). 29 method("ptrArgTest", &Foo::ptrArgTest). 30 constants( { { "ONE", 1 }, { "TWO", 2.12 }, { "THREE", "three" } } ). 31 end().open().glue(); 32 33 if(luaL_dofile(state.state(), "foo.lua")) 34 { 35 printf("failed to dofile: foo.lua\n"); 36 const char *err = luaL_checkstring(state.state(), -1); 37 printf("err: %s\n", err); 38 } 39 40 return 0; 41}

Lua:

local test = Foo.new(333);
test:abc(1,2,3);

print("ONE: "..Foo.ONE);
print("TWO: "..Foo.TWO);
print("THREE: "..Foo.THREE);

local ptrtest = test:ptrTest();

test:ptrArgTest(test);

test = nil;
collectgarbage();

Output:

ctor! 333
1:2:3
ONE: 1
TWO: 2.12
THREE: three
4:4:4
ptrArgTest: 0x17090d0 abc:143

I seem to have made the dtor/gc method call not work, potentially the test object has been left on the stack at some point. I'll have to ferret that out.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Trezker
Member #1,739
December 2001
avatar

I really should try this. Maybe I could get back on some non web development again...

Go to: