This isn't actually a question. I finally managed to figure out the lua5.2 lib properly.
I thought I'd share what I came up with:
Maybe it'll help someone here trying to embed lua in their own programs, who also want to use objects from C++.
append:
Forgot the actual lua script:
local test = Test.new(); test:it();
Fjellstrom! This is...you are....nice one man!
Fjellstrom! This is...you are....nice one man!
I take it you've been fighting with lua 5.2 as well? Most of my frustration was due to a typo/thinko in luaL_checkudata, passed 0 instead of 1 as the second argument. tried changing everything but that, thinking it was ok
Ahh...err...well I've been applying the 'copy & paste' approach (because I'm a lazy bastard), and have had not much luck so far with 5.2, I've hacked? together a solution, but compared to what you've just shared it looks like...well, a mess.
I.. Yeah, I tried the copy & paste method hoping it would get me at least part way, but almost none of the available code is for 5.2.
I may change up the api later to use templates and other magic to make things a bit easier and cleaner. Maybe..
Right now thats just ripped out of a project I started trying to integrate lua into while I was at my moms. I spent days if not a week (or two) trying to get it to work properly.
It's much appreciated! And 'hi' to your mum! (and Jasper!)
That stuff looks way over my head. I've got to break out of C++ one of these days
lua's api really could have been done better.
So it's just a C api? I haven't looked at LUA in ages.
LUA's api isn't designed to be used in object-oriented fashion, provided LUA don't support objects. So it's as if you were complaining that OO-coding with plain assembly was hard and it could've done it better.
Nobody is complaining. The only one (again!) that is bitching and moaning is... You. 
EDIT: And while the API for Lua is in C, the language itself supports OOP.
Hey, I'm not moaning. Also, that's not really an OOP, I mean you could do just the same with C, but that doesn't make C an object-oriented language.
I did something quite similar, very much inspired by this wrapper solution here, which also provides some nice templates and ugly macros (
) for passing arguments : Embedding Lua for Fun and Profit
Had to adapt it to 5.2, too, though. As far as I remember, it wasn't more than replacing luaL_openlib with luaL_setfuncs. (edit: There was another thing, in Lua 5.2, tables can have a __gc metamethod while in 5.1 they can't, so I had to allow its execution for the userdata only, as this wrapper uses an userdata object wrapped inside a table for its c++-objects.)
Also, this wrapper doesn't require separate wrapper classes, as yours.
But I like the separation of class-methods and meta-methods, which you do, very much.
The one thing I found quite complicated to do about binding lua and c++ was to track userdata objects which contain other user-data objects, when the container is managed on the C side. You have to keep around a lua reference in your container object on the C-side in order to prevent the contained objects from being garbage-collected...
Have you thought about how do deal with that already? I'd be interested in how you do it.
I also hacked the Lua core so I had to declare globals. I don't like the concept of treating everything that isn't anything else as globals implicitly (I mean, I don't think global by default is bad, but you should be required to declare them in order to prevent typos from crashing your program, just as an extra-check).
I mean you could do just the same with C
Last time I checked, C didn't provide anything like meta-methods, which allow you to do constructors, destructors, operator overloading, ...
I was comparing it to LUA's "OO". Seriously, that ain't OO. It's just LUA's loose internal structure allows a lot of things to work, which makes it possible to run sort of OO there. But LUA by itself has no support for it so you'd have to kludge up some kludgingly kludgy kludges to make it work right, just like you'd have to with C.
That's sort of right, but I'd still argue that it works better with Lua than with C because you have metatables and metamethods. You simply can't do this implicit- function-calling-OOP-magic with C!
If you need native-true-OOP support out of the box, you should use a different embeddable scripting language, not Lua.
I like the concept of Lua's general purpose tables which is why I use it.
It's about as good as C++'s OO support. It has some convenient syntax sugar to allow some OO concepts to work, just like lua does \o/
But yes, please keep this as on topic as possible, winging about random crap just because you can is looked down upon.
Also, this wrapper doesn't require separate wrapper classes, as yours.
I started off without the wrapper classes. I added them thinking not using them was causing issues somehow? I tried a lot of things to get my code to work. But it turns out I want to keep the wrappers anyhow, so I can let lua properly dispose of stuff.
The wrapper classes in some instances will be aloud to dispose of their wrapped objects, and you can't really do that by storing a single pointer in the udata.
It actually wouldn't take much to rip out the need for the wrapper class in those macros..
Something like:
The only issue with that, is if you really want lua calling your destructor? Probably not, so that should probably be removed.
Have you thought about how do deal with that already? I'd be interested in how you do it.
I haven't considered that. Even slightly. I'm sure you can do it simply enough, possibly with a nice little "smart_lua_ptr" class to wrap those lua objects?
Interesting.
You could use placement new directly on your C++-object/the userdata with (sizeof(YourClass) and call the destructor explicitly in the __gc method, can't you? Is there a difference between Lua - TestDestroy - ~LuaTest (explicitly) - ~Test (implicitly) and Lua - TestDestroy - ~Test (explicitly)? Isn't it just shorter?
At least that's how I do it. I found it to become really tedious to write the glue code, so I'd like to minimize that and avoid extra wrapper classes when possible. (Of course you can emit the __gc glue-function (e. g. TestDestroy) in one line using a macro).
But: In what situation would you want the Lua wrapper class to let go of the wrapped C++ object? Wouldn't that be insecure? 
While a smart pointer is a good idea (of which I haven't thought
), I think it doesn't solve the problem with the references on its own. You'd still need a place to store your lua_smart_ptrs; chances are that you don't want your C++-container-object to store them, because then, you couldn't use it with plain C++ objects anymore (?). Besides, I'd prefer to not have my C++ objects "contaminated" with Lua glue code. You definitively need those references though, because it will inevitably crash when you don't have them (speaking from experience here
). [As soon as Lua runs a GC-cycle, it will collect your CppObjects, because it is unable to "see" that they are "referenced" on the C++ side in your CppContainer.]
While I think about it, this container-object-reference-thing is actually the only case for which you definitively need a wrapper class, no?
Well, whatever, I just wanted to draw your attention to this problem, because it was the part which I found the most difficult. 
I've also spent quite some time in order to get it to work; I guess I spent even more time searching for premade solutions - which either didn't work, were made for Lua 5.1 or were overly complicated and took ages to compile because of all the templates involved.
(By the way, is there a reason you don't use lua_dofile in the example? )
The main reason I use pointers (or the wrapper object) rather than explicit objects is, at least in the project I wrote the original code for, most objects will be created in the C++ code, and exposed to lua. And I don't want lua destroying my objects on me.
With the wrapper class, I have an extra step where I can put some smarter code to handle the lua destruction event. It could potentially do smarter things. Maybe.
But: In what situation would you want the Lua wrapper class to let go of the wrapped C++ object? Wouldn't that be insecure?
Yeah, it could be. But its nice to allow it, say the lua code creates an object, and doesn't attach it to anything, and then exits? I'd like the orphans to go away if at all possible.
(By the way, is there a reason you don't use lua_dofile in the example? )
Nope. I yanked the loadstring call from another example that loads from an array of strings. I just forgot about dofile and friends.
most objects will be created in the C++ code, and exposed to lua. And I don't want lua destroying my objects on me.
Ah, okay, understand now.
In case you're interested, this is the other lua test I made:
It loads each script into its own table/namespace in the same lua_State. I intend(ed) to use it to load a bunch of scripts that do certain things, and I can then just run them all in sequence without the overhead of separate states and the like. Oh, and they could potentially share some data if needed. They are partially sandboxed, so a bit safer than just loading a bunch of scripts into the same state, but not quite locked down, which you can do, but I didn't bother with the added complexity.