rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Apr 2014 21:30:06 +0200
branchclosure
changeset 1491 4a1398eff4fb
parent 1487 84a744941c9f
child 1513 ba912ef24b27
permissions -rw-r--r--
Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
jaroslav@114
     1
/**
jaroslav@114
     2
 * Back 2 Browser Bytecode Translator
jaroslav@114
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@114
     4
 *
jaroslav@114
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@114
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@114
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@114
     8
 *
jaroslav@114
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@114
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@114
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@114
    12
 * GNU General Public License for more details.
jaroslav@114
    13
 *
jaroslav@114
    14
 * You should have received a copy of the GNU General Public License
jaroslav@114
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@114
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@114
    17
 */
jaroslav@114
    18
package org.apidesign.vm4brwsr;
jaroslav@114
    19
jaroslav@181
    20
import java.io.ByteArrayInputStream;
jaroslav@181
    21
import java.io.DataInputStream;
jaroslav@181
    22
import java.io.IOException;
jaroslav@783
    23
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@181
    24
jaroslav@114
    25
/**
jaroslav@114
    26
 *
jaroslav@114
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@114
    28
 */
jaroslav@114
    29
public class Numbers {
jaroslav@114
    30
    private static Double autoboxDbl() {
jaroslav@114
    31
        return 3.3;
jaroslav@114
    32
    }
jaroslav@114
    33
    public static String autoboxDblToString() {
jaroslav@114
    34
        return autoboxDbl().toString().toString();
jaroslav@114
    35
    }
jaroslav@178
    36
    public static int rem(int a, int b) {
jaroslav@178
    37
        return a % b;
jaroslav@178
    38
    }
jaroslav@181
    39
jaroslav@181
    40
    static float deserFloat() throws IOException {
jaroslav@181
    41
        byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
jaroslav@181
    42
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@181
    43
        DataInputStream dis = new DataInputStream(is);
jaroslav@181
    44
        float r = dis.readFloat();
jaroslav@181
    45
        return r;
jaroslav@181
    46
    }
jaroslav@185
    47
    static double deserDouble() throws IOException {
jaroslav@185
    48
        byte[] arr = {(byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
jaroslav@185
    49
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@185
    50
        DataInputStream dis = new DataInputStream(is);
jaroslav@185
    51
        return dis.readDouble();
jaroslav@185
    52
    }
jaroslav@185
    53
    static long deserLong(byte[] arr) throws IOException {
jaroslav@185
    54
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@185
    55
        DataInputStream dis = new DataInputStream(is);
jaroslav@185
    56
        return dis.readLong();
jaroslav@185
    57
    }
jaroslav@181
    58
    static int deserInt() throws IOException {
jaroslav@181
    59
        byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
jaroslav@181
    60
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@181
    61
        DataInputStream dis = new DataInputStream(is);
jaroslav@181
    62
        return dis.readInt();
jaroslav@181
    63
    }
jaroslav@186
    64
jaroslav@186
    65
    static String intToString() {
jaroslav@186
    66
        return new Integer(5).toString().toString();
jaroslav@186
    67
    }
jaroslav@187
    68
    static String floatToString() {
jaroslav@187
    69
        return new Float(7.0).toString().toString();
jaroslav@187
    70
    }
jaroslav@783
    71
    
jaroslav@783
    72
    static double seven(int todo) {
jaroslav@783
    73
        switch (todo) {
jaroslav@783
    74
            case 0: return sevenNew().doubleValue();
jaroslav@783
    75
            case 1: return sevenNew().intValue();
jaroslav@783
    76
            case 2: return sevenNew().longValue();
jaroslav@783
    77
            case 3: return sevenNew().shortValue();
jaroslav@783
    78
            case 4: return sevenNew().byteValue();
jaroslav@783
    79
            case 8: return valueOf(Double.valueOf(7.0));
jaroslav@783
    80
            case 9: return valueOf(Long.valueOf(Long.MAX_VALUE / 5));
jaroslav@791
    81
            case 30: return valueOf(Boolean.FALSE);
jaroslav@791
    82
            case 31: return valueOf(Boolean.TRUE);
jaroslav@791
    83
            case 65: return valueOf(Character.valueOf('A'));
jaroslav@783
    84
            default: throw new IllegalStateException();
jaroslav@783
    85
        }
jaroslav@783
    86
    }
jaroslav@1097
    87
    static boolean bseven(int todo) {
jaroslav@1097
    88
        switch (todo) {
jaroslav@1097
    89
            case 30: return bvalueOf(Boolean.FALSE);
jaroslav@1097
    90
            case 31: return bvalueOf(Boolean.TRUE);
jaroslav@1097
    91
            default: throw new IllegalStateException();
jaroslav@1097
    92
        }
jaroslav@1097
    93
    }
jaroslav@783
    94
    
jaroslav@783
    95
    @JavaScriptBody(args = {}, body = "return 7;")
jaroslav@783
    96
    private static native Number sevenNew();
jaroslav@1097
    97
jaroslav@1097
    98
    @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
jaroslav@1097
    99
    private static native double valueOf(Object o);
jaroslav@783
   100
    
jaroslav@783
   101
    @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
jaroslav@1097
   102
    private static native boolean bvalueOf(Object o);
jaroslav@114
   103
}