Jump to content
Double Fine Action Forums
Sign in to follow this  
DF Du Bois

Structure of MOAILuaObjects

Recommended Posts

I just sent this to an internal mailing list and thought you guys might find it interesting too.


I thought my notes on MOAILuaObject might be interesting/useful. Here's a picture that I'll be referring to:

see attachment 1

There are 5 pieces to a MOAILuaObject:

The C++ instance

The Lua userdata, which is what you pass around in Lua. The userdata only

contains a back-pointer (opaque to lua) to the C++ object

The "member table", which serves 2 purposes

As the userdata's metatable, it tells the userdata that users are allowed to

look up fields (__index) and assign new fields (__newindex).

__index and __newindex point back at the member table, so it also serves as

the storage for those fields. This is how code like "rViewport.sizeX = 300" works.

The "ref table", which also serves 2 purposes:

As the member table's metatable, it tells the member table that failed lookups

should look in the "vtbl" described later (__index points to the "vtbl").

Completely separate from this, it's used as a place for C++ to store references

to lua objects -- kind of an additional, private storage location for the C++

instance. For example, if lua code sets a collision callback on a MOAIBox2D

instance, the C++ code stuffs that lua function pointer in here.

Its metatable also points at the vtbl, but I have no idea why. Not only does

the vtbl contain no "__xxx" methods, only raw, non-metatable-invoking lookups

occur on the ref table.

The "vtbl" (my own word), which is a plain table of functions. This is the

only shared piece. This is where lookups like "rProp:setLoc()" terminate.

Here's a snapshot from 2HB for a concrete example.

see attachment 2

The yellow-highlighted fields show the userdata pointing to the member table,

and the member table pointing back at itself. Those two red fields were set

by DFCommon.Graphics.

Going one layer down, we see that the member table has a metatable to inherit

methods like getClass, getClassName, etc. The innermost metatable is that thing

which has no known reason for existing. The field marked in blue is an example

of what it looks like when C++ code uses this table to store references to lua

objects (thus its name: the "ref table"). I poked that value in there myself

because this feature isn't used much by the C++ code, but that's what it would

look like.

Share this post

Link to post
Share on other sites

I really should read up on that lua stuff. I tried to watch the stream back but it seems the recorded footage is of too low quality to actually see what's going on. still it's great that we got to see some of the guts today.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Create New...