rt/vm/src/test/java/org/apidesign/vm4brwsr/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 06 May 2014 10:08:42 +0200
branchclosure
changeset 1534 ca538fb33f48
parent 1532 10d26626c426
child 1535 c02c6d409461
permissions -rw-r--r--
2nd step in eliminating need for Class.forName when dealing with arrays
     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 Array {
    27     byte[] bytes = { 1 };
    28     short[] shorts = { 2, 3 };
    29     int[] ints = { 4, 5, 6 };
    30     float[] floats = { 7, 8, 9, 10 };
    31     double[][] doubles = { {11}, {12}, {13}, {14}, {15} };
    32     char[] chars = { 'a', 'b' };
    33     
    34     private Array() {
    35     }
    36     
    37     byte bytes() {
    38         return bytes[0];
    39     }
    40     short shorts() {
    41         return shorts[1];
    42     }
    43     
    44     int[] ints() {
    45         return ints;
    46     }
    47     
    48     float floats() {
    49         return floats[3];
    50     }
    51     
    52     double doubles() {
    53         return doubles[4][0];
    54     }
    55     
    56     static double[][] dbls = new double[1][2];
    57     public static double twoDoubles() {
    58         return dbls[0][0] + dbls[0][0];
    59     }
    60 
    61     static int[][] tints = new int[1][2];
    62     public static int twoInts() {
    63         return tints[0][0] + tints[0][0];
    64     }
    65     
    66     private static final Array[] ARR = { new Array(), new Array(), new Array() };
    67     
    68     private static Array[][] arr() {
    69         Array[][] matrix = new Array[3][3];
    70         for (int i = 0; i < ARR.length; i++) {
    71             matrix[i][i] = ARR[i];
    72         }
    73         return matrix;
    74     }
    75     private static <T> T[] filter(T[] in) {
    76         return in;
    77     }
    78     
    79     public static double sum() {
    80         double sum = 0.0;
    81         for (Array[] row : arr()) {
    82             int indx = -1;
    83             for (Array a : row) {
    84                 indx++;
    85                 if (a == null) {
    86                     continue;
    87                 }
    88                 sum += a.bytes();
    89                 sum += a.shorts();
    90                 sum += a.ints()[2];
    91                 sum += a.floats();
    92                 sum += filter(row)[indx].doubles();
    93             }
    94         }
    95         return sum;
    96     }
    97     private static final int[] arr = { 0, 1, 2, 3, 4, 5 };
    98     public static int simple(boolean clone) {
    99         int[] ar;
   100         if (clone) {
   101             ar = arr.clone();
   102         } else {
   103             ar = arr;
   104         }
   105         
   106         int sum = 0;
   107         for (int a : ar) {
   108             sum += a;
   109         }
   110         return sum;
   111     }
   112     
   113     public static String objectArrayClass() {
   114         return Object[].class.getName();
   115     }
   116     
   117     public static boolean instanceOfArray(Object obj) {
   118         if ("string-array".equals(obj)) {
   119             obj = new String[] { "Ahoj" };
   120         }
   121         return obj instanceof Object[];
   122     }
   123     
   124     public static boolean castArray(int type) {
   125         try {
   126             Object orig = new Object();
   127             Object res = orig;
   128             if (type == 0) {
   129                 Object[] arr = new Integer[1];
   130                 String[] str = (String[]) arr;
   131                 res = str;
   132             }
   133             if (type == 1) {
   134                 Object[] arr = null;
   135                 String[] str = (String[]) arr;
   136                 res = str;
   137             }
   138             if (type == 2) {
   139                 Object[] arr = new String[1];
   140                 String[] str = (String[]) arr;
   141                 res = str;
   142             }
   143             if (type == 3) {
   144                 Object[] arr = new String[1];
   145                 CharSequence[] str = (CharSequence[]) arr;
   146                 res = str;
   147             }
   148             return res != orig;
   149         } catch (ClassCastException ex) {
   150             return false;
   151         }
   152     }
   153     
   154     public static int sum(int size) {
   155         int[] arr = new int[size];
   156         return arr[0] + arr[1];
   157     }
   158     
   159     static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
   160         while (count-- > 0) {
   161             dst[dstBegin++] = value[srcBegin++];
   162         }
   163     }
   164 
   165     public static char copyArray() {
   166         char[] arr = { '0' };
   167         arraycopy(arr()[0][0].chars, 0, arr, 0, 1);
   168         return arr[0];
   169     }
   170     
   171     @JavaScriptBody(args = {  }, body = 
   172         "if (!vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2) throw 'forName not defined';\n"
   173       + "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2 = function(s) {\n"
   174       + "  throw 'Do not call me: ' + s;\n"
   175       + "};\n")
   176     private static void disableClassForName() {
   177     }
   178     
   179     public static String nameOfClonedComponent() {
   180         disableClassForName();
   181         Object[] intArr = new Integer[10];
   182         intArr = intArr.clone();
   183         return intArr.getClass().getComponentType().getName();
   184     }
   185     
   186     public static int multiLen() {
   187         return new int[1][0].length;
   188     }
   189 }