Sign in to follow this  
Wipe

funcdef inside shared interface; "interface already implement" warning

Recommended Posts

1st thing--

// module 1
funcdef void Func();

shared interface ielement
{
    Func@ f { get; set; }
}

// module 2
funcdef void Func();

shared interface ielement
{
    Func@ f { get; set; }
}

class celement : ielement
{
    Func@ fdef;

    Func@ get_f()
    {
       return( this.fdef ); 
    }
    void set_f( Func@ newF )
    {
       @this.fdef = newF;
    }
}

Used that way, makes  "Missing implementation of ..." error for both, getter and setter. So i tried to use "normal" functions.
 

// module 1
 
funcdef void Func();

shared interface ielement
{
     Func@ fGet();
     void fSet( Func@ );
}

// module 2

funcdef void Func();

shared interface ielement
{
    Func@ fGet();
    void fSet( Func@ );
}

class celement : ielement
{
    Func@ fdef;

    Func@ fGet()
    {
       return( this.fdef ); 
    }

    void fSet( Func@ newF )
    {
       @this.fdef = newF;
    }
}

Error changed to " Shared type 'ielement' doesn't match the original declaration in other module"
 
----

2nd thing--

shared interface ielement
{
   void dummy1();
}

shared interface isprite : ielement
{
    void dummy2();
}

class celement : ielement
{
   void dummy1() {}
}

class csprite : celement, isprite
{
    csprite()
    {
        super();
    }

    void dummy2() {}
}

Makes warning "The interface 'ielement' is already implemented" - is that possible to make this warning go away? smile.png

EDIT: everything was tested with r1563

Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites

Makes warning "The interface 'ielement' is already implemented" - is that possible to make this warning go away?

 

I am wondering why this warning is even needed. We have:

1. Multiple inheritance for interfaces, i.e. abstract classes.

2. Single inheritance for regular classes.

3. All inheritance is virtual anyway.

 

This is a nice setup, and we can't ever suffer from the diamond problem. However,

4. It's impossible to decouple class declaration from its implementation (notwithstanding the shared keyword).

 

Because of 4. it's still reasonable to create otherwise superfluous interfaces, if only to make script headers more readable. For now I don't see a reason to report that an interface is already implemented as it manifests itself in typical and practical situations. But maybe I don't see the entire picture (how do mixin classes come into this?).

 

edit: this might be interesting:

 

interface I
{
    void f();
}

interface J : I
{
}

mixin class M1 : I
{
    void f() { x = 1; }
};

mixin class M2 : I
{
    void f() { x = 2; }
};

class T : M1, M2
{
    X() { x = 0; }
    int x;
};

This does not generate any warning, but it does if I change "mixin class M : I" to "mixin class M : J". But in both cases T inherits I along two paths.

 

edit2: Another issue:

 

mixin class M1
{
    void f() { x = 1; }
};

mixin class M2
{
    void f() { x = 2; }
};

class T : M1, M2
{
    T() { x = 0; }
    void g()
    {
        f(); // cannot use qualified names: M1::f() or M2::f()
    }
    int x;
};

There is no warning that T inherits f() twice (but g() calls f() in M1, the closest base class, I assume this is correct) and maybe this actually should warrant a warning, since it's actually a real function being inherited, not just an an interface method. Especially since there appers to be no way to explicitly call f() from the other base class, M2 (is this at all intended?).

Edited by TheAtom

Share this post


Link to post
Share on other sites

I'll look into the problems reported.

 

As for mixin classes; they are not really inherited. When including a mixin class only the parts that are not already in the class will be included, for example, if the class already has a property that is also in the mixin class, the version from the mixin class won't be included again. The same goes for methods and implemented interfaces.

 

You can think of mixin classes as macros for providing default implementations. 

Share this post


Link to post
Share on other sites

Hm, looks like there are still problems when loading from bytecode (shared type doesn't match the original declaration).

Edited by Wipe

Share this post


Link to post
Share on other sites

That's strange. I fixed that too yesterday. Loading from bytecode had the same problem as compiling the script in the first place, in that the funcdef didn't get the same type in both modules.

 

What script is failing to load from bytecode? I'll have to do some further investigation on it.

Share this post


Link to post
Share on other sites

You were right (of course). My previous test didn't catch this problem because I loaded the bytecode right after having compiled the script from source. Only when loading the bytecode into a fresh engine did the problem appear.

 

The bug was a bit more complex than I had imagined, but it should now be fixed in revision 1574.

Share this post


Link to post
Share on other sites
Another dark secrets of funcdefs found, sorry! ;) We call start() function and expect to reach end().
 
Let's start from crash in cfuncdef1_1::crashme().
[spoiler]
 
funcdef void funcdef1( ifuncdef1_1& i );

shared interface ifuncdef1_1
{
    ifuncdef1_2@ events { get; set; }
    void crashme();
}

shared interface ifuncdef1_2
{
    funcdef1@ f { get; set; }
}

class cfuncdef1_1 : ifuncdef1_1
{
    ifuncdef1_2@ _events_;

    cfuncdef1_1() { @this._events_ = cfuncdef1_2(); }

    ifuncdef1_2@ get_events() { return( this._events_ ); }
    void set_events( ifuncdef1_2@ events ) { @this._events_ = events; }

    void crashme()
    {
         if( @this._events_ != null && @this._events_.f != null )
            this.events.f( this );

    }
}

class cfuncdef1_2 : ifuncdef1_2
{
    funcdef1@ ff;

    cfuncdef1_2() { @ff = null; }

    funcdef1@ get_f() { return( @this.ff ); }
    void set_f( funcdef1@ _f ) { @this.ff = _f; }
}

void start()
{
    ifuncdef1_1@ i = cfuncdef1_1();
    i.events.f = end;
    i.crashme();
}

void end( ifuncdef1_1& i  )
{
    Log( "you win!" );
}
[/spoiler]
 
If we change crashme() a bit, everything looks like it works without any problems (we can reach end()), but generated bytecode cannot be loaded (LoadByteCode() returns -1).
 
    void crashme()
    {
        if( @this._events_ != null && @this._events_.f != null )
        {
            funcdef1@ crash = this.events.f;
            crash( this );
        }
    }
 
And finally, different edit makes "GC cannot free an object of type '_builtin_function_', it is kept alive by the application." error show up.
cfuncdef1_1() { @this._events_ = null; }
Edited by Wipe

Share this post


Link to post
Share on other sites

I believe the problem is actually with virtual property accessors, and not function pointers here.

 

By manually calling the get functions everything works.

 

 
                void crashme() 
                { 
                     if( @this._events_ != null && @this._events_.f != null ) 
                     { 
//                      this.events.f( this ); 
                        this.get_events().get_f()( this ); 
                     } 
                }

Share this post


Link to post
Share on other sites

The crash has been fixed in revision 1575.

 

The memory leak upon the null pointer access exception is still there. I'm still investigating that, but it looks like a problem in the clean up of the callstack after an exception.

Share this post


Link to post
Share on other sites

Indeed. The memory leak is a bug with the logic for cleaning up the callstack after an exception. The problem happens when attempting to call a class method on a null pointer and the method takes a handle as argument. The cleanup of the callstack doesn't release that handle in this situation, thus causing the leak.

 

I know what is causing the leak, but I'll have to work on the fix at a later time as it's getting late and this won't be a trivial change with just a couple of lines that needs to change.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this