rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 May 2014 14:33:11 +0200
branchclosure
changeset 1595 19d0484c1916
parent 1558 0c5a8b83182a
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Need to flush the stack before destroying it with return value of a function
     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     public 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     public 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     public 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     public static long deserLong(byte[] arr, int shift) throws IOException {
    59         return deserLong(arr) >> shift;
    60     }
    61     public static int deserInt() throws IOException {
    62         byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
    63         ByteArrayInputStream is = new ByteArrayInputStream(arr);
    64         DataInputStream dis = new DataInputStream(is);
    65         return dis.readInt();
    66     }
    67     public static long bytesToLong(byte b1, byte b2, int shift) {
    68         return (((long)b1 << 56) +
    69                 ((long)b2 & 255) << 48) >> shift;
    70     }
    71 
    72     public static String intToString() {
    73         return new Integer(5).toString().toString();
    74     }
    75     public static String floatToString() {
    76         return new Float(7.0).toString().toString();
    77     }
    78     
    79     public static double seven(int todo) {
    80         switch (todo) {
    81             case 0: return sevenNew().doubleValue();
    82             case 1: return sevenNew().intValue();
    83             case 2: return sevenNew().longValue();
    84             case 3: return sevenNew().shortValue();
    85             case 4: return sevenNew().byteValue();
    86             case 8: return valueOf(Double.valueOf(7.0));
    87             case 9: return valueOf(Long.valueOf(Long.MAX_VALUE / 5));
    88             case 30: return valueOf(Boolean.FALSE);
    89             case 31: return valueOf(Boolean.TRUE);
    90             case 65: return valueOf(Character.valueOf('A'));
    91             default: throw new IllegalStateException();
    92         }
    93     }
    94     public static boolean bseven(int todo) {
    95         switch (todo) {
    96             case 30: return bvalueOf(Boolean.FALSE);
    97             case 31: return bvalueOf(Boolean.TRUE);
    98             default: throw new IllegalStateException();
    99         }
   100     }
   101     
   102     @JavaScriptBody(args = {}, body = "return 7;")
   103     private static native Number sevenNew();
   104 
   105     @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
   106     private static native double valueOf(Object o);
   107     
   108     @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
   109     private static native boolean bvalueOf(Object o);
   110     
   111     public static int around(Object model, int x, int y) {
   112         return minesAt(model, x - 1, y - 1)
   113             + minesAt(model, x - 1, y)
   114             + minesAt(model, x - 1, y + 1)
   115             + minesAt(model, x, y - 1)
   116             + minesAt(model, x, y + 1)
   117             + minesAt(model, x + 1, y - 1)
   118             + minesAt(model, x + 1, y)
   119             + minesAt(model, x + 1, y + 1);
   120     }
   121 
   122     private static int minesAt(Object model, int x, int y) {
   123         return x + y;
   124     }    
   125 }