rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 25 Apr 2014 09:59:48 +0200
branchclosure
changeset 1487 84a744941c9f
parent 1097 8e42a376da73
child 1491 4a1398eff4fb
permissions -rw-r--r--
A test to verify the extension mode
     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 @org.apidesign.bck2brwsr.core.Exported 
    30 public class Numbers {
    31     private static Double autoboxDbl() {
    32         return 3.3;
    33     }
    34     public static String autoboxDblToString() {
    35         return autoboxDbl().toString().toString();
    36     }
    37     public static int rem(int a, int b) {
    38         return a % b;
    39     }
    40 
    41     static float deserFloat() throws IOException {
    42         byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
    43         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    44         DataInputStream dis = new DataInputStream(is);
    45         float r = dis.readFloat();
    46         return r;
    47     }
    48     static double deserDouble() throws IOException {
    49         byte[] arr = {(byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
    50         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    51         DataInputStream dis = new DataInputStream(is);
    52         return dis.readDouble();
    53     }
    54     static long deserLong(byte[] arr) throws IOException {
    55         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    56         DataInputStream dis = new DataInputStream(is);
    57         return dis.readLong();
    58     }
    59     static int deserInt() throws IOException {
    60         byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
    61         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    62         DataInputStream dis = new DataInputStream(is);
    63         return dis.readInt();
    64     }
    65 
    66     static String intToString() {
    67         return new Integer(5).toString().toString();
    68     }
    69     static String floatToString() {
    70         return new Float(7.0).toString().toString();
    71     }
    72     
    73     static double seven(int todo) {
    74         switch (todo) {
    75             case 0: return sevenNew().doubleValue();
    76             case 1: return sevenNew().intValue();
    77             case 2: return sevenNew().longValue();
    78             case 3: return sevenNew().shortValue();
    79             case 4: return sevenNew().byteValue();
    80             case 8: return valueOf(Double.valueOf(7.0));
    81             case 9: return valueOf(Long.valueOf(Long.MAX_VALUE / 5));
    82             case 30: return valueOf(Boolean.FALSE);
    83             case 31: return valueOf(Boolean.TRUE);
    84             case 65: return valueOf(Character.valueOf('A'));
    85             default: throw new IllegalStateException();
    86         }
    87     }
    88     static boolean bseven(int todo) {
    89         switch (todo) {
    90             case 30: return bvalueOf(Boolean.FALSE);
    91             case 31: return bvalueOf(Boolean.TRUE);
    92             default: throw new IllegalStateException();
    93         }
    94     }
    95     
    96     @JavaScriptBody(args = {}, body = "return 7;")
    97     private static native Number sevenNew();
    98 
    99     @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
   100     private static native double valueOf(Object o);
   101     
   102     @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
   103     private static native boolean bvalueOf(Object o);
   104 }