5

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.

3
  • 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. Commented Dec 11, 2013 at 14:48
  • 1
    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). Commented Dec 11, 2013 at 14:58
  • 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. Commented Dec 11, 2013 at 15:40

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Alec, thanks for the answer - I am familiar with how Lua works in general; communication via the stack and tables and metatables for storing/organising data. I think maybe the problem is my terminology about binding. I want from Lua to call a function on a C++ instance. 'you want Lua to call some function of yours with a "self" argument, that "self" should tell you what ojbect you are working with.' Yes, exactly - so how do I achieve this, should I create C++ static functions which accept a void* and dereference it, then call an equivalent method on a C++ instance?
@user1654794 that's up to you, remember if you do dare the void* way, use dynamic_cast, you'd be surprised at how often bad-casts don't cause crashes. Why not look at tutorials or the code of other libraries. That's how I learnt.
OK great - thanks Alec. My main concern was that this approach wasn't good, or was frowned upon. Since your reply I'm a bit more confident in simply running the code and seeing what happens :) thanks for the dynamic_cast tip by the way. Great idea for testing the pointers coming out of Lua are correct types. :) Thanks again!
Hi Alec, I upvoted yesterday (and also accepted your first response as a valid answer) - sorry it didn't appear that way. It's showing as upvoted within my browser - is it now resolved your end also? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.