vm/src/test/java/org/apidesign/vm4brwsr/StaticMethod.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 12:42:00 +0200
changeset 106 346633cd13d6
parent 99 67e892757752
child 115 3d5597011af0
permissions -rw-r--r--
Fixing license in all files
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@0
    18
/*
jaroslav@0
    19
Java 4 Browser Bytecode Translator
jaroslav@0
    20
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@0
    21
jaroslav@0
    22
This program is free software: you can redistribute it and/or modify
jaroslav@0
    23
it under the terms of the GNU General Public License as published by
jaroslav@0
    24
the Free Software Foundation, version 2 of the License.
jaroslav@0
    25
jaroslav@0
    26
This program is distributed in the hope that it will be useful,
jaroslav@0
    27
but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@0
    28
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@0
    29
GNU General Public License for more details.
jaroslav@0
    30
jaroslav@0
    31
You should have received a copy of the GNU General Public License
jaroslav@0
    32
along with this program. Look for COPYING file in the top folder.
jaroslav@0
    33
If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@0
    34
*/
jaroslav@22
    35
package org.apidesign.vm4brwsr;
jaroslav@0
    36
jaroslav@94
    37
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@94
    38
jaroslav@0
    39
/**
jaroslav@0
    40
 *
jaroslav@0
    41
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@0
    42
 */
jaroslav@0
    43
public class StaticMethod {
jaroslav@9
    44
    private static int cnt;
jaroslav@48
    45
jaroslav@48
    46
    public static int minusOne() {
jaroslav@48
    47
        return -1;
jaroslav@48
    48
    }
jaroslav@9
    49
    
jaroslav@46
    50
    public static Object none(int x, int y) {
jaroslav@46
    51
        Object toRet = null;
jaroslav@46
    52
        for (int i = x; i < y; i++) {
jaroslav@46
    53
            if (i == 2) {
jaroslav@46
    54
                toRet = null;
jaroslav@46
    55
            } else {
jaroslav@46
    56
                toRet = new Object();
jaroslav@46
    57
            }
jaroslav@46
    58
        }
jaroslav@46
    59
        return toRet;
jaroslav@46
    60
    }
jaroslav@46
    61
    
jaroslav@0
    62
    public static int sum(int x, int y) {
jaroslav@0
    63
        return x + y;
jaroslav@0
    64
    }
jaroslav@1
    65
    public static float power(float x) {
jaroslav@1
    66
        return x * x;
jaroslav@1
    67
    }
jaroslav@2
    68
    public static double minus(double x, long y) {
jaroslav@2
    69
        return x - y;
jaroslav@2
    70
    }
jaroslav@3
    71
    public static int div(byte c, double d) {
jaroslav@3
    72
        return (int)(d / c);
jaroslav@3
    73
    }
jaroslav@3
    74
    public static int mix(int a, long b, byte c, double d) {
jaroslav@3
    75
        return (int)((b / a + c) * d);
jaroslav@3
    76
    }
jaroslav@6
    77
    public static long xor(int a, long b) {
jaroslav@6
    78
        return a ^ b;
jaroslav@6
    79
    }
jaroslav@7
    80
    public static long orOrAnd(boolean doOr, int a, int b) {
jaroslav@7
    81
        return doOr ? a | b : a & b;
jaroslav@7
    82
    }
jaroslav@93
    83
    public static int shiftLeft(int what, int much) {
jaroslav@93
    84
        return what << much;
jaroslav@93
    85
    }
jaroslav@93
    86
    public static int shiftArithmRight(int what, int much, boolean signed) {
jaroslav@93
    87
        if (signed) {
jaroslav@93
    88
            return what >> much;
jaroslav@93
    89
        } else {
jaroslav@93
    90
            return what >>> much;
jaroslav@93
    91
        }
jaroslav@93
    92
    }
jaroslav@4
    93
    public static long factRec(int n) {
jaroslav@4
    94
        if (n <= 1) {
jaroslav@4
    95
            return 1;
jaroslav@4
    96
        } else {
jaroslav@4
    97
            return n * factRec(n - 1);
jaroslav@4
    98
        }
jaroslav@4
    99
    }
jaroslav@5
   100
    public static long factIter(int n) {
jaroslav@5
   101
        long res = 1;
jaroslav@5
   102
        for (int i = 2; i <= n; i++) {
jaroslav@5
   103
            res *= i;
jaroslav@5
   104
        }
jaroslav@5
   105
        return res;
jaroslav@5
   106
    }
jaroslav@9
   107
    public static int inc4() {
jaroslav@9
   108
        cnt++;
jaroslav@9
   109
        cnt+=2;
jaroslav@9
   110
        cnt++;
jaroslav@9
   111
        return cnt;
jaroslav@9
   112
    }
jaroslav@94
   113
    
jaroslav@94
   114
    @JavaScriptBody(
jaroslav@99
   115
        args={"i","j"}, body="return (i + j).toString();"
jaroslav@94
   116
    )
jaroslav@99
   117
    public static String i2s(int i, int j) {
jaroslav@94
   118
        throw new IllegalStateException();
jaroslav@94
   119
    }
jaroslav@97
   120
    
jaroslav@97
   121
    static {
jaroslav@97
   122
        // check order of initializers
jaroslav@97
   123
        StaticUse.NON_NULL.equals(new Object());
jaroslav@97
   124
    }
jaroslav@0
   125
}