rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethod.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 291 vm/src/test/java/org/apidesign/vm4brwsr/StaticMethod.java@c6f21b56a6cf
child 1466 39d26d3686d9
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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 org.apidesign.bck2brwsr.core.JavaScriptBody;
    21 
    22 /**
    23  *
    24  * @author Jaroslav Tulach <jtulach@netbeans.org>
    25  */
    26 public class StaticMethod {
    27     private static int cnt;
    28     private static Object NULL;
    29 
    30     public static int minusOne() {
    31         return -1;
    32     }
    33     
    34     public static Object none(int x, int y) {
    35         Object toRet = null;
    36         for (int i = x; i < y; i++) {
    37             if (i == 2) {
    38                 toRet = null;
    39             } else {
    40                 toRet = new Object();
    41             }
    42         }
    43         return toRet;
    44     }
    45     
    46     public static boolean isNull() {
    47         return NULL == null;
    48     }
    49     
    50     public static int sum(int x, int y) {
    51         return x + y;
    52     }
    53     public static float power(float x) {
    54         return x * x;
    55     }
    56     public static double minus(double x, long y) {
    57         return x - y;
    58     }
    59     public static int div(byte c, double d) {
    60         return (int)(d / c);
    61     }
    62     public static int mix(int a, long b, byte c, double d) {
    63         return (int)((b / a + c) * d);
    64     }
    65     public static long xor(int a, long b) {
    66         return a ^ b;
    67     }
    68     public static long orOrAnd(boolean doOr, int a, int b) {
    69         return doOr ? a | b : a & b;
    70     }
    71     public static int shiftLeft(int what, int much) {
    72         return what << much;
    73     }
    74     public static int shiftArithmRight(int what, int much, boolean signed) {
    75         if (signed) {
    76             return what >> much;
    77         } else {
    78             return what >>> much;
    79         }
    80     }
    81     public static long factRec(int n) {
    82         if (n <= 1) {
    83             return 1;
    84         } else {
    85             return n * factRec(n - 1);
    86         }
    87     }
    88     public static long factIter(int n) {
    89         long res = 1;
    90         for (int i = 2; i <= n; i++) {
    91             res *= i;
    92         }
    93         return res;
    94     }
    95     public static int inc4() {
    96         cnt++;
    97         cnt+=2;
    98         cnt++;
    99         return cnt;
   100     }
   101     
   102     @JavaScriptBody(
   103         args={"i","j"}, body="\n\r\treturn (i + j).toString();"
   104     )
   105     public static String i2s(int i, int j) {
   106         throw new IllegalStateException();
   107     }
   108     
   109     public static String castNull(boolean n) {
   110         Object value = n ? null : "Ahoj";
   111         return (String)value;
   112     }
   113     
   114     public static String swtch(int what) {
   115         switch (what) {
   116             case 0: return "Jarda";
   117             case 1: return "Darda";
   118             case 2: return "Parda";
   119             default: return "Marda";
   120         }
   121     }
   122     public static String swtch2(int what) {
   123         switch (what) {
   124             case 0: return "Jarda";
   125             case 11: return "Darda";
   126             case 22: return "Parda";
   127             default: return "Marda";
   128         }
   129     }
   130     
   131     static {
   132         // check order of initializers
   133         StaticUse.NON_NULL.equals(new Object());
   134     }
   135 }