vm/src/test/java/org/apidesign/vm4brwsr/StaticMethod.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 30 Oct 2012 22:59:31 +0100
changeset 131 dbfbcd718146
parent 115 3d5597011af0
child 161 978301fdb4ec
permissions -rw-r--r--
One license is enough
     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 
    29     public static int minusOne() {
    30         return -1;
    31     }
    32     
    33     public static Object none(int x, int y) {
    34         Object toRet = null;
    35         for (int i = x; i < y; i++) {
    36             if (i == 2) {
    37                 toRet = null;
    38             } else {
    39                 toRet = new Object();
    40             }
    41         }
    42         return toRet;
    43     }
    44     
    45     public static int sum(int x, int y) {
    46         return x + y;
    47     }
    48     public static float power(float x) {
    49         return x * x;
    50     }
    51     public static double minus(double x, long y) {
    52         return x - y;
    53     }
    54     public static int div(byte c, double d) {
    55         return (int)(d / c);
    56     }
    57     public static int mix(int a, long b, byte c, double d) {
    58         return (int)((b / a + c) * d);
    59     }
    60     public static long xor(int a, long b) {
    61         return a ^ b;
    62     }
    63     public static long orOrAnd(boolean doOr, int a, int b) {
    64         return doOr ? a | b : a & b;
    65     }
    66     public static int shiftLeft(int what, int much) {
    67         return what << much;
    68     }
    69     public static int shiftArithmRight(int what, int much, boolean signed) {
    70         if (signed) {
    71             return what >> much;
    72         } else {
    73             return what >>> much;
    74         }
    75     }
    76     public static long factRec(int n) {
    77         if (n <= 1) {
    78             return 1;
    79         } else {
    80             return n * factRec(n - 1);
    81         }
    82     }
    83     public static long factIter(int n) {
    84         long res = 1;
    85         for (int i = 2; i <= n; i++) {
    86             res *= i;
    87         }
    88         return res;
    89     }
    90     public static int inc4() {
    91         cnt++;
    92         cnt+=2;
    93         cnt++;
    94         return cnt;
    95     }
    96     
    97     @JavaScriptBody(
    98         args={"i","j"}, body="return (i + j).toString();"
    99     )
   100     public static String i2s(int i, int j) {
   101         throw new IllegalStateException();
   102     }
   103     
   104     public static String swtch(int what) {
   105         switch (what) {
   106             case 0: return "Jarda";
   107             case 1: return "Darda";
   108             case 2: return "Parda";
   109             default: return "Marda";
   110         }
   111     }
   112     public static String swtch2(int what) {
   113         switch (what) {
   114             case 0: return "Jarda";
   115             case 11: return "Darda";
   116             case 22: return "Parda";
   117             default: return "Marda";
   118         }
   119     }
   120     
   121     static {
   122         // check order of initializers
   123         StaticUse.NON_NULL.equals(new Object());
   124     }
   125 }