Skip to content

codegen: update to methodcallmodule because g_source_new expects source_funcs not to be const

Al Thomas requested to merge astavale/vala:gsource-sourcefuncs into master

When attempting to write a GSource this Vala code:

void main () {
    Source a = new QuitApplicationSource ();
}

class QuitApplicationSource:Source {

    public override bool prepare (out int timeout) {
        return false;
    }

    public override bool check () {
        return false;
    }

    public override bool dispatch (SourceFunc callback) {
        return false;
    }
}

produces a C warning:

quit_as_gsource.vala.c: In function ‘quit_application_source_new’:
quit_as_gsource.vala.c:95:48: warning: passing argument 1 of ‘g_source_new’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  self = (QuitApplicationSource*) g_source_new (&_source_funcs, sizeof (QuitApplicationSource));
                                                ^
In file included from /usr/include/glib-2.0/glib/giochannel.h:33:0,
                 from /usr/include/glib-2.0/glib.h:54,
                 from quit_as_gsource.vala.c:6:
/usr/include/glib-2.0/glib/gmain.h:418:10: note: expected ‘GSourceFuncs * {aka struct _GSourceFuncs *}’ but argument is of type ‘const GSourceFuncs * {aka const struct _GSourceFuncs *}’
 GSource *g_source_new             (GSourceFuncs   *source_funcs,
          ^

This merge request simply removes the const from the line

var funcs = new CCodeDeclaration ("const GSourceFuncs");

in codegen/valamethodcallmodule.vala. Vala has some code in there to support the binding to GSource.

At present codegen uses the C static modifier to create the SourceFuncs struct passed to g_source_new. I note from the Stack Overflow answer to What does “static” mean in C? that using static like this 'should be used very sparingly - it makes your code not thread-safe and harder to understand'. I'm wondering if longer term the SourceFuncs struct should be heap allocated and removed in the GSource finalize method. Not sure how that would interact with g_source_set_funcs.

Merge request reports