rt/vm/src/test/java/org/apidesign/vm4brwsr/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 23 Sep 2014 21:52:27 +0200
changeset 1702 228f26fc1159
parent 1535 c02c6d409461
child 1787 ea12a3bb4b33
permissions -rw-r--r--
for (var x in array) should return only expected values
     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         disableClassForName();
    81         double sum = 0.0;
    82         for (Array[] row : arr()) {
    83             int indx = -1;
    84             for (Array a : row) {
    85                 indx++;
    86                 if (a == null) {
    87                     continue;
    88                 }
    89                 sum += a.bytes();
    90                 sum += a.shorts();
    91                 sum += a.ints()[2];
    92                 sum += a.floats();
    93                 sum += filter(row)[indx].doubles();
    94             }
    95         }
    96         return sum;
    97     }
    98     private static final int[] arr = { 0, 1, 2, 3, 4, 5 };
    99     public static int simple(boolean clone) {
   100         int[] ar;
   101         if (clone) {
   102             ar = arr.clone();
   103         } else {
   104             ar = arr;
   105         }
   106         
   107         int sum = 0;
   108         for (int a : ar) {
   109             sum += a;
   110         }
   111         return sum;
   112     }
   113     
   114     public static String objectArrayClass() {
   115         enableClassForName(prevClassForName);
   116         prevClassForName = null;
   117         String s = Object[].class.getName();
   118         disableClassForName();
   119         return s;
   120     }
   121     
   122     public static boolean instanceOfArray(Object obj) {
   123         if ("string-array".equals(obj)) {
   124             obj = new String[] { "Ahoj" };
   125         }
   126         return obj instanceof Object[];
   127     }
   128     
   129     public static boolean castArray(int type) {
   130         try {
   131             Object orig = new Object();
   132             Object res = orig;
   133             if (type == 0) {
   134                 Object[] arr = new Integer[1];
   135                 String[] str = (String[]) arr;
   136                 res = str;
   137             }
   138             if (type == 1) {
   139                 Object[] arr = null;
   140                 String[] str = (String[]) arr;
   141                 res = str;
   142             }
   143             if (type == 2) {
   144                 Object[] arr = new String[1];
   145                 String[] str = (String[]) arr;
   146                 res = str;
   147             }
   148             if (type == 3) {
   149                 Object[] arr = new String[1];
   150                 CharSequence[] str = (CharSequence[]) arr;
   151                 res = str;
   152             }
   153             return res != orig;
   154         } catch (ClassCastException ex) {
   155             return false;
   156         }
   157     }
   158     
   159     public static int sum(int size) {
   160         int[] arr = new int[size];
   161         return arr[0] + arr[1];
   162     }
   163     
   164     static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
   165         while (count-- > 0) {
   166             dst[dstBegin++] = value[srcBegin++];
   167         }
   168     }
   169 
   170     public static char copyArray() {
   171         char[] arr = { '0' };
   172         arraycopy(arr()[0][0].chars, 0, arr, 0, 1);
   173         return arr[0];
   174     }
   175     
   176     @JavaScriptBody(args = {  }, body = 
   177         "var prev = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2;\n"
   178       + "if (!prev) throw 'forName not defined';\n"
   179       + "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2 = function(s) {\n"
   180       + "  throw 'Do not call me: ' + s;\n"
   181       + "};\n"
   182       + "return prev;\n")
   183     private static Object disableClassForNameImpl() {
   184         return null;
   185     }
   186     
   187     private static void disableClassForName() {
   188         if (prevClassForName == null) {
   189             prevClassForName = disableClassForNameImpl();
   190         }
   191     }
   192     
   193     @JavaScriptBody(args = { "fn" }, body = 
   194         "if (fn !== null) vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2 = fn;"
   195     )
   196     private static void enableClassForName(Object fn) {
   197     }
   198     
   199     private static Object prevClassForName;
   200     public static String nameOfClonedComponent() {
   201         disableClassForName();
   202         Object[] intArr = new Integer[10];
   203         intArr = intArr.clone();
   204         return intArr.getClass().getComponentType().getName();
   205     }
   206     
   207     public static int multiLen() {
   208         return new int[1][0].length;
   209     }
   210     
   211     @JavaScriptBody(args = { "arr" }, body = 
   212         "var cnt = '';\n" +
   213         "if (arr === null) arr = [];\n" +
   214         "for (var i in arr) { cnt += i; }\n" +
   215         "return cnt;\n"
   216     )
   217     private static native String iterateArray(Object[] arr);
   218     
   219     public static String iterateArray(boolean javaArray) {
   220         return iterateArray(javaArray ? new String[0] : null);
   221     }
   222 }