🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Happy birthday to me!

Published August 11, 2009
Advertisement
I turned 17 today! I won't technically be 17 for maybe three more hours, but for all intents and purposes...

I've been working a lot on that Nucleus engine I mentioned previously. Right now I'm getting to know the Lua/C API so I can write a C module that Nucleus can load as a native extension to Java, as a Lua interop module. At the moment it's standalone for testing purposes, while I struggle to learn how to bind C++ classes/objects into Lua to my tastes. I'm happy with what I have so far, but right now it can only bind functions, not other kinds of values. It's probably worth writing down what I've learned so far though.


The goal: Access instance methods of a C++ object from Lua.
The problem: Lua can only easily bind static methods. There needs to be some way to tell a function what instance it should act on.
The solution (simple in hindsight): store the object instance as a full userdatum, and call its methods in Lua using the colon operator (object:method(params)). This is really just syntactic sugar for object.method(object, params). Given that object is the instance, object is really just 'this'.

The above is the keystone to the solution. Obviously it won't do much without some support. For example, we still have to bind a static function for Lua to call. Furthermore, userdata does nothing useful without a metatable. So we have to register a metatable to our new userdatum, with a (static) method registered as its __index at the very least. This function will handle lookups into the userdatum; if I do "object.foo", __index will recieve obj and "foo", and return an appropriate value, even if it's just nil. This is done with a static array of name/pointer pairs to allow lookups based on name.

We expect "object.method" to call __index and return that particular method. But remember, these methods are instance methods, and Lua can't call those directly. So instead of returning the method directly, we'll create a static 'thunk' function to resolve the access and call our instance method. We'll register this thunking function multiple times, once for each instance method, and registering a pointer to a data structure as an upvalue each time. The data structure just holds a pointer-to-member; even though pointers-to-member can't be casted to void*, a pointer to a vanilla struct instance sure can. This is basically just a Lua version of std::bind1st, creating a new object (closure) each time.

Now, "object.method" will call into __index and get back our simple thunking method, with a unique upvalue representing the method to call. So, calling this method with parameters (and using the colon syntax to pass a hidden 'this') should successfully resolve to a call to an object's instance method: "object:method(1,2,3)".


The code is... a bit ugly right now. I want to try to clean it up a bit before I make it public, and hopefully add support for __index returning things other than a function. This is a hobby project, so while I know I could just go the C route and go with static functions in separate .c files, I like working with objects, and it's fun to figure out how this is all supposed to work. [smile]

Thanks for reading!
~Jonathan


EDIT: May as well post what my goal code is on the Lua end. This is what I want to be able to do when the interop module is finished:

local server = atom.request("server/telnet")server.handlers["connect"] = function(self, client)  local db = atom.request("database/mysql")  db:connect("localhost", "foologin", "barpasS")  db:use("blacklist")  local result = db:query("SELECT * FROM tbl WHERE IP='" .. client.IP .. "'")  if #result > 0 then    self:disconnect(client, result[1].reason)  endend


Ideally, the server.handlers table/udata would use a __newindex metamethod to tell when something's being assigned, and register the hooks appropriately. I think it's pretty intuitive, and I'm almost positive I can do everything in this snippet. Give me some time, people... [grin]
Previous Entry Short update on Nucleus
Next Entry Ah, me
0 likes 5 comments

Comments

Prinz Eugn
Happy Birthday!
I'm no expert, but why aren't you using the [ source lang="c"]
[/source ] code? All the cool kids do it, as I understand.

So... when are we gonna see a demo?
August 11, 2009 01:26 PM
Twisol
Ah, I used [ code] instead of [ source]. It's a short snippet and it fits the way it is now, but thanks for reminding me. [grin]

As to a demo... I'm not entirely sure. It won't be at least until I finish this Lua/Java layer, and then I'll still need to write some dummy modules in Java to show it off. At that point, I think I'll be opening the floor for third-party module submissions anyways.

Thanks for commenting!
August 11, 2009 01:36 PM
Aardvajk
Happy Birthday, mate. [smile]
August 11, 2009 03:15 PM
Programmer16
I'm a little late, but happy birthday Jon!
August 12, 2009 12:22 AM
Twisol
Still an hour and 20 minutes before the end of the day where I live. [wink] Thanks!
August 12, 2009 12:27 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement