vm/src/test/java/org/apidesign/vm4brwsr/Instance.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 08 Nov 2012 18:34:12 +0100
changeset 137 45184b2f9697
parent 106 346633cd13d6
child 226 907a52ed10e3
permissions -rw-r--r--
There can be fields named "in" and string constants with \t, \r, \n and \\
jaroslav@24
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@24
     4
 *
jaroslav@24
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@24
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@24
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@24
     8
 *
jaroslav@24
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@24
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@24
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@24
    12
 * GNU General Public License for more details.
jaroslav@24
    13
 *
jaroslav@24
    14
 * You should have received a copy of the GNU General Public License
jaroslav@24
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@24
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@8
    17
 */
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@8
    19
jaroslav@8
    20
/**
jaroslav@8
    21
 *
jaroslav@8
    22
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@8
    23
 */
jaroslav@8
    24
public class Instance {
jaroslav@137
    25
    private int in;
jaroslav@8
    26
    protected short s;
jaroslav@8
    27
    public double d;
jaroslav@8
    28
    private float f;
jaroslav@10
    29
    protected byte b = (byte)31;
jaroslav@10
    30
    
jaroslav@10
    31
    private Instance() {
jaroslav@10
    32
    }
jaroslav@8
    33
jaroslav@8
    34
    public Instance(int i, double d) {
jaroslav@137
    35
        this.in = i;
jaroslav@8
    36
        this.d = d;
jaroslav@8
    37
    }
jaroslav@8
    38
    public byte getByte() {
jaroslav@8
    39
        return b;
jaroslav@8
    40
    }
jaroslav@8
    41
    
jaroslav@8
    42
    public void setByte(byte b) {
jaroslav@8
    43
        this.b = b;
jaroslav@8
    44
    }
jaroslav@10
    45
    public static double defaultDblValue() {
jaroslav@10
    46
        Instance create = new Instance();
jaroslav@10
    47
        return create.d;
jaroslav@10
    48
    }
jaroslav@8
    49
    
jaroslav@10
    50
    public static byte assignedByteValue() {
jaroslav@10
    51
        return new Instance().b;
jaroslav@10
    52
    }
jaroslav@8
    53
    public static double magicOne() {
jaroslav@8
    54
        Instance i = new Instance(10, 3.3d);
jaroslav@10
    55
        i.b = (byte)0x09;
jaroslav@137
    56
        return (i.in - i.b) * i.d;
jaroslav@8
    57
    }
jaroslav@14
    58
    public static int virtualBytes() {
jaroslav@13
    59
        Instance i = new InstanceSub(7, 2.2d);
jaroslav@12
    60
        i.setByte((byte)0x0a);
jaroslav@14
    61
        Instance i2 = new Instance(3, 333.0d);
jaroslav@14
    62
        i2.setByte((byte)44);
jaroslav@14
    63
        return i.getByte() + i2.getByte();
jaroslav@12
    64
    }
jaroslav@15
    65
    public static float interfaceBytes() {
jaroslav@15
    66
        GetByte i = new InstanceSub(7, 2.2d);
jaroslav@15
    67
        return i.getByte();
jaroslav@15
    68
    }
jaroslav@16
    69
    public static boolean instanceOf(boolean sub) {
jaroslav@16
    70
        Instance i = createInstance(sub);
jaroslav@16
    71
        return isInstanceSubOf(i);
jaroslav@16
    72
    }
jaroslav@30
    73
    public static int castsWork(boolean interfc) {
jaroslav@30
    74
        Instance i = createInstance(true);
jaroslav@30
    75
        if (interfc) {
jaroslav@30
    76
            GetByte b = (GetByte)i;
jaroslav@30
    77
        } else {
jaroslav@30
    78
            InstanceSub s = (InstanceSub)i;
jaroslav@30
    79
        }
jaroslav@30
    80
        return 5;
jaroslav@30
    81
    }
jaroslav@30
    82
    
jaroslav@16
    83
    private static boolean isInstanceSubOf(Instance instance) {
jaroslav@16
    84
        return instance instanceof InstanceSub;
jaroslav@16
    85
    }
jaroslav@16
    86
    private static Instance createInstance(boolean sub) {
jaroslav@16
    87
        return sub ? new InstanceSub(3, 0) : new Instance();
jaroslav@16
    88
    }
jaroslav@16
    89
    private static boolean isNull() {
jaroslav@16
    90
        return createInstance(true) == null;
jaroslav@16
    91
    }
jaroslav@8
    92
}