ruby-gnome / yard-gobject-introspection

YARD plugin to retrieve documentation from GObject Introspection data
GNU Lesser General Public License v2.1
5 stars 4 forks source link

How Interface are loaded with GObjectIntrospection ? #2

Closed cedlemo closed 7 years ago

cedlemo commented 7 years ago

@kou,

I am trying to figure out how you map interface with GObject-Introspection in order to create the related documentation with this yard plugin.

For example this irb session :

irb -r gtk3
irb(main):001:0>  Gtk::Actionable.class
=> Module

But if I look at the code of GObjectIntrospection, I have this :

    def load_interface_info(info)
      interface_module =
        self.class.define_interface(info.gtype,
                                    rubyish_class_name(info),
                                    @base_module)
      load_methods(info, interface_module)
    end

Which if I look at the ruby and C code, create a class.

kou commented 7 years ago

Do you want to know where is Loader.define_interface defined? Right?

It's defined at gobject-introspection/ext/gobject-introspection/rb-gi-loader.c:

static VALUE
rg_s_define_interface(G_GNUC_UNUSED VALUE klass,
                      VALUE rb_gtype, VALUE rb_name, VALUE rb_module)
{
    GType gtype;

    gtype = NUM2ULONG(rb_to_int(rb_gtype));
    return G_DEF_INTERFACE(gtype, RVAL2CSTR(rb_name), rb_module);
}

G_DEF_INTERFACE is defined at [glib2/ext/glib2/rbgobject.h[(https://github.com/ruby-gnome2/ruby-gnome2/blob/master/glib2/ext/glib2/rbgobject.h#L53-L54):

#define G_DEF_INTERFACE(gtype, name, module)\
    (rbgobj_define_class(gtype, name, module, 0, 0, Qnil))

rbgobj_define_class() is defined at glib2/ext/glib2/rbobj_type.c:

VALUE
rbgobj_define_class(GType gtype, const gchar *name, VALUE module,
                    RGMarkFunc mark, RGFreeFunc free, VALUE parent)
{
    RGObjClassInfo* cinfo;
    if (gtype == 0)
        rb_bug("rbgobj_define_class: Invalid gtype [%s]\n", name);

    cinfo = (RGObjClassInfo*)rbgobj_lookup_class_by_gtype(gtype, parent);
    cinfo->mark = mark;
    cinfo->free = free;
    rb_define_const(module, name, cinfo->klass);
    return cinfo->klass;
}
cedlemo commented 7 years ago

Sorry @kou, it seems that my question was not clear enought. I have found the C code of this function but what is interesting me is the fact that this code create a class from the interface info while in my irb session, I found that the interface is implemented as a module.

irb -r gtk3
irb(main):001:0>  Gtk::Actionable.class
=> Module

In order to be able to create the right documentation with the yard plugin, should I consider that all the C interface are mapped to Ruby module ? If so why in the C code the C interface is mapped to a class with rbobj_define_class

kou commented 7 years ago

should I consider that all the C interface are mapped to Ruby module ?

Yes.

If so why in the C code the C interface is mapped to a class with rbobj_define_class

The function name is not good. :< It defines a module when the GType is G_TYPE_INTERFACE.

cedlemo commented 7 years ago

Ok thanks.