4
votes

I'm new to Lua, and trying to understand some of the fundamentals. Something I want to understand is binding Lua to C++ instances.

I am not interested in third party libraries, I want to understand this at a more fundamental level - thanks :)

Here are my questions:

  1. My assumption based on what I have read, is that Lua can only bind to static C functions. Is this correct?
  2. Does that mean that to bind an instance of a C++ class, I'd first need to write static functions for each method and property getter/setter I want, accepting an instance pointer as a paramter.
  3. I'd register these functions with Lua.
  4. I'd pass Lua a pointer to the instance of the C++ class.
  5. From Lua I'd call one of the registered functions, passing the C++ instance pointer.
  6. The static function dereferences the pointer, calling the equivalent method.

Does this make sense? Or have I gotten something wrong?

Thanks for reading this far.

1
Binding different languages is usually done through a C interface (the ABI is better defined for C). I am not familiar enough with lua to know the details, but my advice is to opt for a library solution. You are going to reinvent the wheel, and it might not be trivial to get it to be as round as existing solutions. At the very least, take a look at what those libraries do. - David Rodríguez - dribeas
Thanks, but I'm specifically trying to understand the principles of Lua for a use case where I don't want to (am not allowed to) use third party libraries. I have looked into a few libraries, but I'm only interested in the core principles of how this works, and couldn't grasp that from the libraries (highly abstracted, heavy reading). - user1654794
Many third party libraries that utilize Lua-C++ bindings are opensource. You can always look how they do stuff in order to understand them. Caveat emptor though. - Bartek Banachewicz

1 Answers

4
votes

This is right up my ally.

1) Lua ... it doesn't really bind to stuff, what you need to do is "play nice with Lua" and that requires knowing a bit about how Lua works.

I REALLY suggest reading http://luaforge.net/docman/83/98/ANoFrillsIntroToLua51VMInstructions.pdf that.

That tells you about EVERYTHING Lua is actually able to do. So the functions Lua gives you let you manipulate just those structures.

After that everything makes a lot more sense.

Why this answer should end here

Your questions after 1 are all wrong. and 1 is semantically wrong, a static function just has internal/weak linkage. I guess you mean "not a method"

2) Not really, remember you have that nice "self"/"this" identity with objects (and lua with tables/meta-tables) - you don't bind to methods.

You want Lua to call some function of yours with a "self" argument, that "self" (whatever it may be, a simple integer ID, or a void* if you're feeling dangerous) should tell you what ojbect you are working with.

3/4/5/6 don't really make sense, read that document :) Comment in reply to this if you need more or have something more specific, it's not a bad question btw it's just naive