blitz-research / monkey

Blitz Research Monkey Source
226 stars 59 forks source link

Typecasting in a while loop (Java, CSharp) #31

Closed aglitchman closed 11 years ago

aglitchman commented 11 years ago

Example:

Interface TestForCast
End Interface

Class SampleApp Extends App
    Field test1:Object[] = New Object[100]
    Field test2:Object

    Method OnRender:Int()
        Cls(200, 200, 200)

        Local i:Int = 0
        While TestForCast(test1[i]) <> Null And i < test1.Length
            i += 1  
        End While

        If TouchHit(0) Error("")

        Return 0
    End Method
End Class

Android target generates the following code:

class c_SampleApp extends c_App{
    public c_SampleApp m_new(){
        super.m_new();
        return this;
    }
    Object[] m_test1=new Object[100];
    public int p_OnRender(){
        bb_graphics.g_Cls(200.0f,200.0f,200.0f);
        int t_i=0;
        Object t_=m_test1[t_i]; // ???
        while((t_ instanceof c_TestForCast ? (c_TestForCast)t_ : null)!=null && t_i<bb_std_lang.arrayLength(m_test1)){
            t_i+=1;
        }
        if((bb_input.g_TouchHit(0))!=0){
            bb_std_lang.error("");
        }
        return 0;
    }
}

XNA target:

class c_SampleApp : c_App{
    public c_SampleApp m_SampleApp_new(){
        base.m_App_new();
        return this;
    }
    public Object[] m_test1=new Object[100];
    public override sealed int p_OnRender(){
        bb_graphics.g_Cls(200.0f,200.0f,200.0f);
        int t_i=0;
        Object t_=m_test1[t_i]; // ???
        while((t_ is c_TestForCast ? (c_TestForCast)t_ : null)!=null && t_i<bb_std_lang.length(m_test1)){
            t_i+=1;
        }
        if((bb_input.g_TouchHit(0))!=0){
            bb_std_lang.Error("");
        }
        return 0;
    }
}

Obviously while loop doesn't work as it is supposed to.

blitz-research commented 11 years ago

Thanks!

Fix will be in next release.

On Sat, Aug 3, 2013 at 10:59 AM, programmerby notifications@github.comwrote:

Example:

Interface TestForCastEnd Interface Class SampleApp Extends App Field test1:Object[] = New Object[100] Field test2:Object

Method OnRender:Int()
    Cls(200, 200, 200)

    Local i:Int = 0
    While TestForCast(test1[i]) <> Null And i < test1.Length
        i += 1
    End While
    If TouchHit(0) Error("")

    Return 0
End MethodEnd Class

Android target generates the following code:

class c_SampleApp extends c_App{ public c_SampleApp m_new(){ super.m_new(); return this; } Object[] m_test1=new Object[100]; public int p_OnRender(){ bb_graphics.g_Cls(200.0f,200.0f,200.0f); int ti=0; Object t=m_test1[ti]; // ??? while((t instanceof c_TestForCast ? (cTestForCast)t : null)!=null && t_i<bb_std_lang.arrayLength(m_test1)){ t_i+=1; } if((bb_input.g_TouchHit(0))!=0){ bb_std_lang.error(""); } return 0; }}

XNA target:

class c_SampleApp : c_App{ public c_SampleApp m_SampleApp_new(){ base.m_App_new(); return this; } public Object[] m_test1=new Object[100]; public override sealed int p_OnRender(){ bb_graphics.g_Cls(200.0f,200.0f,200.0f); int ti=0; Object t=m_test1[ti]; // ??? while((t is c_TestForCast ? (cTestForCast)t : null)!=null && t_i<bb_std_lang.length(m_test1)){ t_i+=1; } if((bb_input.g_TouchHit(0))!=0){ bb_std_lang.Error(""); } return 0; }}

Obviously while loop doesn't work as it is supposed to.

— Reply to this email directly or view it on GitHubhttps://github.com/blitz-research/monkey/issues/31 .

aglitchman commented 11 years ago

Thank you!

Yet another example:

Class TestForCast2
    Field dummy:Object
End Class

...
Field test2:Object
...

If TestForCast2(test2) And TestForCast(TestForCast2(test2).dummy)
    ...
Endif

Android (Java) code:

Object t_2=m_test2;
Object t_3=m_test2;
Object t_5=m_test2;
Object t_4=(t_5 instanceof c_TestForCast2 ? (c_TestForCast2)t_5 : null).m_dummy; // <-- !!!!!
if(((t_2 instanceof c_TestForCast2 ? (c_TestForCast2)t_2 : null)!=null) && ((t_4 instanceof c_TestForCast ? (c_TestForCast)t_4 : null)!=null)){
    ...
}

i.e. when "test2" isn't "TestForCast2", Java throws NullPointerException before reaching if-else statement.