2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
20 import org.apidesign.bck2brwsr.core.JavaScriptBody;
24 * @author Jaroslav Tulach <jtulach@netbeans.org>
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' };
56 static double[][] dbls = new double[1][2];
57 public static double twoDoubles() {
58 return dbls[0][0] + dbls[0][0];
61 static int[][] tints = new int[1][2];
62 public static int twoInts() {
63 return tints[0][0] + tints[0][0];
66 private static final Array[] ARR = { new Array(), new Array(), new Array() };
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];
75 private static <T> T[] filter(T[] in) {
79 public static double sum() {
80 disableClassForName();
82 for (Array[] row : arr()) {
93 sum += filter(row)[indx].doubles();
98 private static final int[] arr = { 0, 1, 2, 3, 4, 5 };
99 public static int simple(boolean clone) {
114 public static String objectArrayClass() {
115 enableClassForName(prevClassForName);
116 prevClassForName = null;
117 String s = Object[].class.getName();
118 disableClassForName();
122 public static boolean instanceOfArray(Object obj) {
123 if ("string-array".equals(obj)) {
124 obj = new String[] { "Ahoj" };
126 return obj instanceof Object[];
129 public static boolean castArray(int type) {
131 Object orig = new Object();
134 Object[] arr = new Integer[1];
135 String[] str = (String[]) arr;
140 String[] str = (String[]) arr;
144 Object[] arr = new String[1];
145 String[] str = (String[]) arr;
149 Object[] arr = new String[1];
150 CharSequence[] str = (CharSequence[]) arr;
154 } catch (ClassCastException ex) {
159 public static int sum(int size) {
160 int[] arr = new int[size];
161 return arr[0] + arr[1];
164 static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
165 while (count-- > 0) {
166 dst[dstBegin++] = value[srcBegin++];
170 public static char copyArray() {
171 char[] arr = { '0' };
172 arraycopy(arr()[0][0].chars, 0, arr, 0, 1);
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"
183 private static Object disableClassForNameImpl() {
187 private static void disableClassForName() {
188 if (prevClassForName == null) {
189 prevClassForName = disableClassForNameImpl();
193 @JavaScriptBody(args = { "fn" }, body =
194 "if (fn !== null) vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2 = fn;"
196 private static void enableClassForName(Object fn) {
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();
207 public static int multiLen() {
208 return new int[1][0].length;