Re: [Vala] wrong struct code generated



On Mon, 17 Aug 2009 18:03:17 +0100, Spencer, Matthew wrote:
Using the following code:

vapi:

namespace Test {

    [SimpleType]

I don't think you want a [SimpleType] annotation here. IIRC that
annotation means the type is not a struct, but a typedef of int, float
or something. Passing by value is specified by the fact it is a struct.

(I did some .vapi last week and anything declared struct was passed by
value)

    [CCode (cheader_filename="testStruct.h")]
    public struct InitParams {
         public unowned char SourceName;

It contains a single char? Shouldn't it say public unowned string
SourceName? (and it would be source_name in Vala convention)

    }
}

vala:
using Test;

namespace TestRun {
    public static void main(string[] args) {
        InitParams params=InitParams();
      params=params;
    }
}

I get the following c code generated:
<snip>
void test_run_main (char** args, int args_length1) {
        TestInitParams params;
        params = memset (0, sizeof (TestInitParams));
        params = params;
}
</snip>

Independent of whether your .vapi is correct, vala should not generate
bogus code. I have already hit this with GLib.Pid (which is, by the way,
real SimpleType -- it is declared as struct in glib-2.0.vapi, but as
typedef to int in glib.h) and been pointed at bug
http://bugzilla.gnome.org/show_bug.cgi?id=578968

This is surely incorrect as the wrong number of arguments are being passed to memset.  I would have thought 
that 
        params = memset (&params, 0, sizeof (TestInitParams));

No, it should not. The result of memset is irrelevant, so it should have
been just:
        memset (&params, 0, sizeof (TestInitParams));

should have been generated.  Is this a bug, or is my vapi file incorrect?

Please, try without the [SimpleType] if it really is defined as struct
in C. If it still won't work then, please comment on the above bug, that
it does not work for real structs either.

If it won't work for you, give the struct a constructor. I think in vala
(unlike C#) can have a parameterless constructor, but you have to verify
that by trying.


Hm, does anybody know whether Vala supports struct initializer
expression? I.e. would it work to:

  TestInitParams params = { 'c', };

or

  TestInitParams params = TestInitParams () { 'c', };

(or some other combination with or without 'new' and with or without '()')

--------------------------------------------------------------------------------
                                                - Jan Hudec `Bulb' <bulb ucw cz>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]