vm/src/test/java/org/apidesign/vm4brwsr/Instance.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 02 Dec 2012 21:00:12 +0100
changeset 239 8ceee38f5840
parent 226 907a52ed10e3
child 442 b107ed66f2e7
permissions -rw-r--r--
Ability to control prototypes. Making sure any JavaScript Object is instance of Java object
     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(boolean sub) {
    72         Instance i = createInstance(sub);
    73         return isInstanceSubOf(i);
    74     }
    75     public static int castsWork(boolean interfc) {
    76         Instance i = createInstance(true);
    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(boolean sub) {
    89         return sub ? new InstanceSub(3, 0) : new Instance();
    90     }
    91     private static boolean isNull() {
    92         return createInstance(true) == null;
    93     }
    94     
    95     @JavaScriptBody(args = "obj", body = "return obj.constructor;")
    96     static Object constructor(Object obj) {
    97         return obj;
    98     }
    99     
   100     public static boolean sharedConstructor() {
   101         class X {
   102         }
   103         
   104         X x1 = new X();
   105         X x2 = new X();
   106         
   107         return constructor(x1) == constructor(x2);
   108     }
   109     public static boolean differentConstructor() {
   110         class X {
   111         }
   112         class Y {
   113         }
   114         
   115         X x = new X();
   116         Y y = new Y();
   117         
   118         return constructor(x) == constructor(y);
   119     }
   120     @JavaScriptBody(args = {}, body = "return {};")
   121     private static Object jsObj() {
   122         return null;
   123     }
   124     
   125     public static boolean iofObject() {
   126         return jsObj() instanceof Object;
   127     }
   128 }