GObject method calls

Last night I described how I used Gee.HashMap to dispatch to the correct parser for Odysseus’s templating language. This morning I’ll describe how I use GObject’s interfaces to dispatch to the correct method.

I also use it to abstract over different dynamically typed data sources (including GObject’s GValue) for it’s data model and to interpret the AST.

In short I use it very heavily.

To dynamically dispatch method calls, Vala generates wrapper C functions which calls a macro to cast to get at the appropriate “vtable” containing the function to call.

To make those macros work, every object in GObject has a pointer to it’s class as it’s first field. For single-inheritance subclassing that itself is the vtable once, but for interfaces it’s first field is a type ID for GObject to lookup.

That preregistered type descriptor has an array mapping type IDs to interface vtables.

Once correctly type-casted, that gives GObject a structure containing the function pointer to call. All in constant time!


But those type IDs serve another purpose for “Prosody” templates. It supports GObject in defining it’s own dynamic type system (which Prosody’s data model can wrap) called GValue, which even supports auto conversion via functions looked using a binary search.

It’s for this it reserves some type IDs for C builtin types, and from my experience I’m sure it makes language bindings easier.