rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethod.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 17:49:30 +0100
branchReducedStack
changeset 1473 484248c9c1d6
parent 1472 0a1b4f1bf4d0
child 1545 50991eae72d4
permissions -rw-r--r--
Verify behavior when string constants are used
     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     public static final int MISSING_CONSTANT = 1;
    28     private static int cnt;
    29     private static Object NULL;
    30 
    31     public static int minusOne() {
    32         return -1;
    33     }
    34     
    35     public static Object none(int x, int y) {
    36         Object toRet = null;
    37         for (int i = x; i < y; i++) {
    38             if (i == 2) {
    39                 toRet = null;
    40             } else {
    41                 toRet = new Object();
    42             }
    43         }
    44         return toRet;
    45     }
    46     
    47     public static boolean isNull() {
    48         return NULL == null;
    49     }
    50     
    51     public static int sum(int x, int y) {
    52         return x + y;
    53     }
    54     public static float power(float x) {
    55         return x * x;
    56     }
    57     public static double minus(double x, long y) {
    58         return x - y;
    59     }
    60     public static int div(byte c, double d) {
    61         return (int)(d / c);
    62     }
    63     public static int mix(int a, long b, byte c, double d) {
    64         return (int)((b / a + c) * d);
    65     }
    66     public static long xor(int a, long b) {
    67         return a ^ b;
    68     }
    69     public static long orOrAnd(boolean doOr, int a, int b) {
    70         return doOr ? a | b : a & b;
    71     }
    72     public static int shiftLeft(int what, int much) {
    73         return what << much;
    74     }
    75     public static int shiftArithmRight(int what, int much, boolean signed) {
    76         if (signed) {
    77             return what >> much;
    78         } else {
    79             return what >>> much;
    80         }
    81     }
    82     public static long factRec(int n) {
    83         if (n <= 1) {
    84             return 1;
    85         } else {
    86             return n * factRec(n - MISSING_CONSTANT);
    87         }
    88     }
    89     public static long factIter(int n) {
    90         long res = 1;
    91         for (int i = 2; i <= n; i++) {
    92             res *= i;
    93         }
    94         return res;
    95     }
    96     public static int inc4() {
    97         cnt++;
    98         cnt+=2;
    99         cnt++;
   100         return cnt;
   101     }
   102     
   103     public static int helloWorldLength(String x) {
   104         return (StringSample.HELLO + x).length();
   105     }
   106     
   107     @JavaScriptBody(
   108         args={"i","j"}, body="\n\r\treturn (i + j).toString();"
   109     )
   110     public static String i2s(int i, int j) {
   111         throw new IllegalStateException();
   112     }
   113     
   114     public static String castNull(boolean n) {
   115         Object value = n ? null : "Ahoj";
   116         return (String)value;
   117     }
   118     
   119     public static String swtch(int what) {
   120         switch (what) {
   121             case 0: return "Jarda";
   122             case 1: return "Darda";
   123             case 2: return "Parda";
   124             default: return "Marda";
   125         }
   126     }
   127     public static String swtch2(int what) {
   128         switch (what) {
   129             case 0: return "Jarda";
   130             case 11: return "Darda";
   131             case 22: return "Parda";
   132             default: return "Marda";
   133         }
   134     }
   135     
   136     public static int castString(Object o) {
   137         return ((String)o).length();
   138     }
   139     
   140     public static int initInflater(int w, boolean nowrap) {
   141         Instance i = new Instance(w, 0.0);
   142         return i.sum(nowrap?-w:w, 1);
   143     }
   144     
   145     public static String toStringArr() {
   146         class N implements Next {
   147             int idx = 0;
   148             
   149             @Override
   150             public boolean hasNext() {
   151                 return idx < 5;
   152             }
   153 
   154             @Override
   155             public String next() {
   156                 switch (idx++) {
   157                     case 0: return "Zero";
   158                     case 1: return "One";
   159                     case 2: return "Two";
   160                     case 3: return "Three";
   161                     case 4: return "Four";
   162                 }
   163                 throw new IllegalStateException();
   164             }
   165         }
   166         return toString(null, new N()).toString();
   167     }
   168     
   169     static String toString(Object thiz, Next it) {
   170         if (!it.hasNext()) {
   171             return "[]";
   172         }
   173 
   174         StringBuilder sb = new StringBuilder();
   175         sb.append('[');
   176         for (;;) {
   177             String e = it.next();
   178             sb.append(e == thiz ? "(this Collection)" : e);
   179             if (!it.hasNext()) {
   180                 return sb.append(']').toString();
   181             }
   182             sb.append(',').append(' ');
   183         }
   184     }
   185     
   186     static interface Next {
   187         boolean hasNext();
   188         String next();
   189     }
   190     
   191     
   192     static {
   193         // check order of initializers
   194         StaticUse.NON_NULL.equals(new Object());
   195     }
   196 }