rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Feb 2013 17:50:47 +0100
brancharithmetic
changeset 783 8264f07b1f46
parent 772 d382dacfd73f
child 791 af4001c85438
permissions -rw-r--r--
All JavaScript numbers will have doubleValue__D and co. methods. One can also use valueOf() on any Number to get its primitive value
     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 java.io.ByteArrayInputStream;
    21 import java.io.DataInputStream;
    22 import java.io.IOException;
    23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class Numbers {
    30     private static Double autoboxDbl() {
    31         return 3.3;
    32     }
    33     public static String autoboxDblToString() {
    34         return autoboxDbl().toString().toString();
    35     }
    36     public static int rem(int a, int b) {
    37         return a % b;
    38     }
    39 
    40     static float deserFloat() throws IOException {
    41         byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
    42         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    43         DataInputStream dis = new DataInputStream(is);
    44         float r = dis.readFloat();
    45         return r;
    46     }
    47     static double deserDouble() throws IOException {
    48         byte[] arr = {(byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
    49         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    50         DataInputStream dis = new DataInputStream(is);
    51         return dis.readDouble();
    52     }
    53     static long deserLong(byte[] arr) throws IOException {
    54         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    55         DataInputStream dis = new DataInputStream(is);
    56         return dis.readLong();
    57     }
    58     static int deserInt() throws IOException {
    59         byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
    60         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    61         DataInputStream dis = new DataInputStream(is);
    62         return dis.readInt();
    63     }
    64 
    65     static String intToString() {
    66         return new Integer(5).toString().toString();
    67     }
    68     static String floatToString() {
    69         return new Float(7.0).toString().toString();
    70     }
    71     
    72     static double seven(int todo) {
    73         switch (todo) {
    74             case 0: return sevenNew().doubleValue();
    75             case 1: return sevenNew().intValue();
    76             case 2: return sevenNew().longValue();
    77             case 3: return sevenNew().shortValue();
    78             case 4: return sevenNew().byteValue();
    79             case 8: return valueOf(Double.valueOf(7.0));
    80             case 9: return valueOf(Long.valueOf(Long.MAX_VALUE / 5));
    81             default: throw new IllegalStateException();
    82         }
    83     }
    84     
    85     @JavaScriptBody(args = {}, body = "return 7;")
    86     private static native Number sevenNew();
    87     
    88     @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
    89     private static native double valueOf(Object o);
    90 }