cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 161 forks source link

issue with enum #361

Open isakovarseniy opened 7 years ago

isakovarseniy commented 7 years ago

Hi

package t;

public enum B  implements I{
    Q("q"), W("w");

      private final String value;

      B(final String value) {
        this.value = value;
      }

      public String getValue() {
        return value;
      }

}
package t;

public interface I {

}
package t;

public class C {

    public void method (){
        B b = B.Q;
        int i=0;

        if (b instanceof I){
            i=1;
        }

    }
}

Generated code for class B

namespace t {
    export class C {
        public method() {
            let b : t.B = t.B.Q;
            let i : number = 0;
            if(b != null && (b["__interfaces"] != null && b["__interfaces"].indexOf("t.I") >= 0 || b.constructor != null && b.constructor["__interfaces"] != null && b.constructor["__interfaces"].indexOf("t.I") >= 0)) {
                i = 1;
            }
        }
    }
    C["__class"] = "t.C";

}
  1. instead of
    let b : t.B = t.B.Q;     //   in this case b = 0

    should be class

    let b : t.B =B["_$wrappers"][t.B.Q]   
  2. even you set let b : t.B =B["_$wrappers"][t.B.Q]
    class does not have property "__interfaces"
    if (b instanceof I){
            i=1;        //never happened
        }

BTW Correct me if I am wrong but I would probably put additional brackets

if(
(//add (
    b != null && (b["__interfaces"] != null && b["__interfaces"].indexOf("t.I") >= 0
)//add )
 ||
(   //add (
b.constructor != null && b.constructor["__interfaces"] != null && b.constructor["__interfaces"].indexOf("t.I") >= 0)
) //add )
)
{
                i = 1;

Thanks Arseniy Isakov

renaudpawlak commented 7 years ago

That's a tricky one! Thank you for reporting and I'll keep in touch.