vm/src/test/java/org/apidesign/vm4brwsr/Instance.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Feb 2013 15:19:16 +0100
branchemul
changeset 639 960ecf7cea5d
parent 442 b107ed66f2e7
permissions -rw-r--r--
instanceof works on null
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    21 
    22 /**
    23  *
    24  * @author Jaroslav Tulach <jtulach@netbeans.org>
    25  */
    26 public class Instance {
    27     private int in;
    28     protected short s;
    29     public double d;
    30     private float f;
    31     protected byte b = (byte)31;
    32     
    33     private Instance() {
    34     }
    35 
    36     public Instance(int i, double d) {
    37         this.in = i;
    38         this.d = d;
    39     }
    40     public byte getByte() {
    41         return b;
    42     }
    43     
    44     public void setByte(byte b) {
    45         this.b = b;
    46     }
    47     public static double defaultDblValue() {
    48         Instance create = new Instance();
    49         return create.d;
    50     }
    51     
    52     public static byte assignedByteValue() {
    53         return new Instance().b;
    54     }
    55     public static double magicOne() {
    56         Instance i = new Instance(10, 3.3d);
    57         i.b = (byte)0x09;
    58         return (i.in - i.b) * i.d;
    59     }
    60     public static int virtualBytes() {
    61         Instance i = new InstanceSub(7, 2.2d);
    62         i.setByte((byte)0x0a);
    63         Instance i2 = new Instance(3, 333.0d);
    64         i2.setByte((byte)44);
    65         return i.getByte() + i2.getByte();
    66     }
    67     public static float interfaceBytes() {
    68         GetByte i = new InstanceSub(7, 2.2d);
    69         return i.getByte();
    70     }
    71     public static boolean instanceOf(int sub) {
    72         Instance i = createInstance(sub);
    73         return isInstanceSubOf(i);
    74     }
    75     public static int castsWork(boolean interfc) {
    76         Instance i = createInstance(2);
    77         if (interfc) {
    78             GetByte b = (GetByte)i;
    79         } else {
    80             InstanceSub s = (InstanceSub)i;
    81         }
    82         return 5;
    83     }
    84     
    85     private static boolean isInstanceSubOf(Instance instance) {
    86         return instance instanceof InstanceSub;
    87     }
    88     private static Instance createInstance(int type) {
    89         switch (type) {
    90             case 0: return null;
    91             case 1: return new Instance();
    92             case 2: return new InstanceSub(3, 0);
    93         }
    94         throw new IllegalArgumentException();
    95     }
    96     private static boolean isNull() {
    97         return createInstance(2) == null;
    98     }
    99     
   100     @JavaScriptBody(args = "obj", body = "return obj.constructor;")
   101     static Object constructor(Object obj) {
   102         return obj;
   103     }
   104     
   105     public static boolean sharedConstructor() {
   106         class X {
   107         }
   108         
   109         X x1 = new X();
   110         X x2 = new X();
   111         
   112         return constructor(x1) == constructor(x2);
   113     }
   114     public static boolean differentConstructor() {
   115         class X {
   116         }
   117         class Y {
   118         }
   119         
   120         X x = new X();
   121         Y y = new Y();
   122         
   123         return constructor(x) == constructor(y);
   124     }
   125     @JavaScriptBody(args = {}, body = "return {};")
   126     private static Object jsObj() {
   127         return null;
   128     }
   129     
   130     public static boolean iofObject() {
   131         return jsObj() instanceof Object;
   132     }
   133     
   134     public static int jscall() {
   135         return jsgetbytes(new Instance());
   136     }
   137     
   138     @JavaScriptBody(args = { "instance" }, body = "return instance.getByte__B();")
   139     private static native int jsgetbytes(Instance instance);
   140 }