instanceof works on null emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Feb 2013 15:19:16 +0100
branchemul
changeset 639960ecf7cea5d
parent 638 f203b54b3d33
child 640 693745d01b55
instanceof works on null
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/test/java/org/apidesign/vm4brwsr/Instance.java
vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Fri Feb 01 14:16:26 2013 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Fri Feb 01 15:19:16 2013 +0100
     1.3 @@ -1269,7 +1269,7 @@
     1.4                      int indx = readIntArg(byteCodes, i);
     1.5                      final String type = jc.getClassName(indx);
     1.6                      if (!type.startsWith("[")) {
     1.7 -                        emit(out, "var @2 = @1.$instOf_@3 ? 1 : 0;",
     1.8 +                        emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
     1.9                               smapper.popA(), smapper.pushI(),
    1.10                               type.replace('/', '_'));
    1.11                      } else {
     2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Instance.java	Fri Feb 01 14:16:26 2013 +0100
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Instance.java	Fri Feb 01 15:19:16 2013 +0100
     2.3 @@ -68,12 +68,12 @@
     2.4          GetByte i = new InstanceSub(7, 2.2d);
     2.5          return i.getByte();
     2.6      }
     2.7 -    public static boolean instanceOf(boolean sub) {
     2.8 +    public static boolean instanceOf(int sub) {
     2.9          Instance i = createInstance(sub);
    2.10          return isInstanceSubOf(i);
    2.11      }
    2.12      public static int castsWork(boolean interfc) {
    2.13 -        Instance i = createInstance(true);
    2.14 +        Instance i = createInstance(2);
    2.15          if (interfc) {
    2.16              GetByte b = (GetByte)i;
    2.17          } else {
    2.18 @@ -85,11 +85,16 @@
    2.19      private static boolean isInstanceSubOf(Instance instance) {
    2.20          return instance instanceof InstanceSub;
    2.21      }
    2.22 -    private static Instance createInstance(boolean sub) {
    2.23 -        return sub ? new InstanceSub(3, 0) : new Instance();
    2.24 +    private static Instance createInstance(int type) {
    2.25 +        switch (type) {
    2.26 +            case 0: return null;
    2.27 +            case 1: return new Instance();
    2.28 +            case 2: return new InstanceSub(3, 0);
    2.29 +        }
    2.30 +        throw new IllegalArgumentException();
    2.31      }
    2.32      private static boolean isNull() {
    2.33 -        return createInstance(true) == null;
    2.34 +        return createInstance(2) == null;
    2.35      }
    2.36      
    2.37      @JavaScriptBody(args = "obj", body = "return obj.constructor;")
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java	Fri Feb 01 14:16:26 2013 +0100
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java	Fri Feb 01 15:19:16 2013 +0100
     3.3 @@ -80,16 +80,23 @@
     3.4      @Test public void isInstanceOf() throws Exception {
     3.5          assertExec(
     3.6              "Yes, we are instance",
     3.7 -            Instance.class, "instanceOf__ZZ",
     3.8 -            Double.valueOf(1.0), true
     3.9 +            Instance.class, "instanceOf__ZI",
    3.10 +            Double.valueOf(1.0), 2
    3.11          );
    3.12      }
    3.13  
    3.14      @Test public void notInstanceOf() throws Exception {
    3.15          assertExec(
    3.16              "No, we are not an instance",
    3.17 -            Instance.class, "instanceOf__ZZ",
    3.18 -            Double.valueOf(0.0), false
    3.19 +            Instance.class, "instanceOf__ZI",
    3.20 +            Double.valueOf(0.0), 1
    3.21 +        );
    3.22 +    }
    3.23 +    @Test public void nullInstanceOf() throws Exception {
    3.24 +        assertExec(
    3.25 +            "No, null is not an instance",
    3.26 +            Instance.class, "instanceOf__ZI",
    3.27 +            Double.valueOf(0.0), 0
    3.28          );
    3.29      }
    3.30