# HG changeset patch # User Jaroslav Tulach # Date 1358970731 -3600 # Node ID cde0c2d7794ea44c6970d8cc183fc392e47450fc # Parent 05224402145d224ff35f96590c63f0c89c41dc29 Using specific subpackages rather than generic org.apidesign.bck2brwsr.emul diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/java/lang/Class.java --- a/emul/mini/src/main/java/java/lang/Class.java Wed Jan 23 20:39:23 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/Class.java Wed Jan 23 20:52:11 2013 +0100 @@ -26,14 +26,14 @@ package java.lang; import java.io.ByteArrayInputStream; -import org.apidesign.bck2brwsr.emul.AnnotationImpl; +import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl; import java.io.InputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.TypeVariable; import org.apidesign.bck2brwsr.core.JavaScriptBody; -import org.apidesign.bck2brwsr.emul.MethodImpl; +import org.apidesign.bck2brwsr.emul.reflect.MethodImpl; /** * Instances of the class {@code Class} represent classes and diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/java/lang/Number.java --- a/emul/mini/src/main/java/java/lang/Number.java Wed Jan 23 20:39:23 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/Number.java Wed Jan 23 20:52:11 2013 +0100 @@ -49,7 +49,7 @@ * @since JDK1.0 */ @ExtraJavaScript( - resource="/org/apidesign/vm4brwsr/emul/java_lang_Number.js", + resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js", processByteCode=true ) public abstract class Number implements java.io.Serializable { diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/java/lang/String.java --- a/emul/mini/src/main/java/java/lang/String.java Wed Jan 23 20:39:23 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/String.java Wed Jan 23 20:52:11 2013 +0100 @@ -101,7 +101,7 @@ */ @ExtraJavaScript( - resource="/org/apidesign/vm4brwsr/emul/java_lang_String.js", + resource="/org/apidesign/vm4brwsr/emul/lang/java_lang_String.js", processByteCode=true ) @JavaScriptPrototype(container = "String.prototype", prototype = "new String") diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/java/lang/reflect/Method.java --- a/emul/mini/src/main/java/java/lang/reflect/Method.java Wed Jan 23 20:39:23 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/reflect/Method.java Wed Jan 23 20:52:11 2013 +0100 @@ -28,8 +28,8 @@ import java.lang.annotation.Annotation; import java.util.Enumeration; import org.apidesign.bck2brwsr.core.JavaScriptBody; -import org.apidesign.bck2brwsr.emul.AnnotationImpl; -import org.apidesign.bck2brwsr.emul.MethodImpl; +import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl; +import org.apidesign.bck2brwsr.emul.reflect.MethodImpl; /** * A {@code Method} provides information about, and access to, a single method diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java Wed Jan 23 20:39:23 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.emul; - -import java.lang.annotation.Annotation; -import org.apidesign.bck2brwsr.core.JavaScriptBody; - -/** - * - * @author Jaroslav Tulach - */ -public final class AnnotationImpl implements Annotation { - public Class annotationType() { - return getClass(); - } - - @JavaScriptBody(args = { "a", "n", "values" }, body = "" - + "function f(v, p) {\n" - + " var val = v;\n" - + " var prop = p;\n" - + " return function() {\n" - + " return val[prop];\n" - + " };\n" - + "}\n" - + "var props = Object.getOwnPropertyNames(values);\n" - + "for (var i = 0; i < props.length; i++) {\n" - + " var p = props[i];\n" - + " a[p] = new f(values, p);\n" - + "}\n" - + "a['$instOf_' + n] = true;\n" - + "return a;" - ) - private static T create(AnnotationImpl a, String n, Object values) { - return null; - } - public static T create(Class annoClass, Object values) { - return create(new AnnotationImpl(), annoClass.getName().replace('.', '_'), values); - } - - public static Annotation[] create(Object anno) { - String[] names = findNames(anno); - Annotation[] ret = new Annotation[names.length]; - for (int i = 0; i < names.length; i++) { - String n = names[i].substring(1, names[i].length() - 1).replace('/', '_'); - ret[i] = create(new AnnotationImpl(), n, findData(anno, names[i])); - } - return ret; - } - @JavaScriptBody(args = "anno", body = - "var arr = new Array();" - + "var props = Object.getOwnPropertyNames(anno);\n" - + "for (var i = 0; i < props.length; i++) {\n" - + " var p = props[i];\n" - + " arr.push(p);" - + "}" - + "return arr;" - ) - private static String[] findNames(Object anno) { - throw new UnsupportedOperationException(); - } - - @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];") - private static Object findData(Object anno, String p) { - throw new UnsupportedOperationException(); - } -} diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java --- a/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/MethodImpl.java Wed Jan 23 20:39:23 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,164 +0,0 @@ -/* - * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package org.apidesign.bck2brwsr.emul; - -import java.lang.reflect.Method; -import java.util.Enumeration; -import org.apidesign.bck2brwsr.core.JavaScriptBody; - -/** Utilities to work on methods. - * - * @author Jaroslav Tulach - */ -public abstract class MethodImpl { - public static MethodImpl INSTANCE; - static { - try { - Class.forName(Method.class.getName()); - } catch (ClassNotFoundException ex) { - throw new IllegalStateException(ex); - } - } - - protected abstract Method create(Class declaringClass, String name, Object data, String sig); - - - // - // bck2brwsr implementation - // - - @JavaScriptBody(args = {"clazz", "prefix"}, - body = "" - + "var c = clazz.cnstr.prototype;" - + "var arr = new Array();\n" - + "for (m in c) {\n" - + " if (m.indexOf(prefix) === 0) {\n" - + " arr.push(m);\n" - + " arr.push(c[m]);\n" - + " }" - + "}\n" - + "return arr;") - private static native Object[] findMethodData( - Class clazz, String prefix); - - public static Method findMethod( - Class clazz, String name, Class... parameterTypes) { - Object[] data = findMethodData(clazz, name + "__"); - BIG: for (int i = 0; i < data.length; i += 2) { - String sig = ((String) data[0]).substring(name.length() + 2); - Method tmp = INSTANCE.create(clazz, name, data[1], sig); - Class[] tmpParms = tmp.getParameterTypes(); - if (parameterTypes.length != tmpParms.length) { - continue; - } - for (int j = 0; j < tmpParms.length; j++) { - if (!parameterTypes[j].equals(tmpParms[j])) { - continue BIG; - } - } - return tmp; - } - return null; - } - - public static Method[] findMethods(Class clazz, int mask) { - Object[] namesAndData = findMethodData(clazz, ""); - int cnt = 0; - for (int i = 0; i < namesAndData.length; i += 2) { - String sig = (String) namesAndData[i]; - Object data = namesAndData[i + 1]; - int middle = sig.indexOf("__"); - if (middle == -1) { - continue; - } - String name = sig.substring(0, middle); - sig = sig.substring(middle + 2); - final Method m = INSTANCE.create(clazz, name, data, sig); - if ((m.getModifiers() & mask) == 0) { - continue; - } - namesAndData[cnt++] = m; - } - Method[] arr = new Method[cnt]; - for (int i = 0; i < cnt; i++) { - arr[i] = (Method) namesAndData[i]; - } - return arr; - } - - public static int signatureElements(String sig) { - Enumeration en = signatureParser(sig); - int cnt = 0; - while (en.hasMoreElements()) { - en.nextElement(); - cnt++; - } - return cnt; - } - - public static Enumeration signatureParser(final String sig) { - class E implements Enumeration { - int pos; - - public boolean hasMoreElements() { - return pos < sig.length(); - } - - public Class nextElement() { - switch (sig.charAt(pos++)) { - case 'I': - return Integer.TYPE; - case 'J': - return Long.TYPE; - case 'D': - return Double.TYPE; - case 'F': - return Float.TYPE; - case 'B': - return Byte.TYPE; - case 'Z': - return Boolean.TYPE; - case 'S': - return Short.TYPE; - case 'V': - return Void.TYPE; - case 'C': - return Character.TYPE; - case 'L': - try { - int up = sig.indexOf("_2"); - String type = sig.substring(1, up); - pos = up + 2; - return Class.forName(type); - } catch (ClassNotFoundException ex) { - // should not happen - } - } - throw new UnsupportedOperationException(sig + " at " + pos); - } - } - return new E(); - } -} diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/AnnotationImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/AnnotationImpl.java Wed Jan 23 20:52:11 2013 +0100 @@ -0,0 +1,81 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.emul.reflect; + +import java.lang.annotation.Annotation; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** + * + * @author Jaroslav Tulach + */ +public final class AnnotationImpl implements Annotation { + public Class annotationType() { + return getClass(); + } + + @JavaScriptBody(args = { "a", "n", "values" }, body = "" + + "function f(v, p) {\n" + + " var val = v;\n" + + " var prop = p;\n" + + " return function() {\n" + + " return val[prop];\n" + + " };\n" + + "}\n" + + "var props = Object.getOwnPropertyNames(values);\n" + + "for (var i = 0; i < props.length; i++) {\n" + + " var p = props[i];\n" + + " a[p] = new f(values, p);\n" + + "}\n" + + "a['$instOf_' + n] = true;\n" + + "return a;" + ) + private static T create(AnnotationImpl a, String n, Object values) { + return null; + } + public static T create(Class annoClass, Object values) { + return create(new AnnotationImpl(), annoClass.getName().replace('.', '_'), values); + } + + public static Annotation[] create(Object anno) { + String[] names = findNames(anno); + Annotation[] ret = new Annotation[names.length]; + for (int i = 0; i < names.length; i++) { + String n = names[i].substring(1, names[i].length() - 1).replace('/', '_'); + ret[i] = create(new AnnotationImpl(), n, findData(anno, names[i])); + } + return ret; + } + @JavaScriptBody(args = "anno", body = + "var arr = new Array();" + + "var props = Object.getOwnPropertyNames(anno);\n" + + "for (var i = 0; i < props.length; i++) {\n" + + " var p = props[i];\n" + + " arr.push(p);" + + "}" + + "return arr;" + ) + private static String[] findNames(Object anno) { + throw new UnsupportedOperationException(); + } + + @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];") + private static Object findData(Object anno, String p) { + throw new UnsupportedOperationException(); + } +} diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/MethodImpl.java Wed Jan 23 20:52:11 2013 +0100 @@ -0,0 +1,164 @@ +/* + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.apidesign.bck2brwsr.emul.reflect; + +import java.lang.reflect.Method; +import java.util.Enumeration; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** Utilities to work on methods. + * + * @author Jaroslav Tulach + */ +public abstract class MethodImpl { + public static MethodImpl INSTANCE; + static { + try { + Class.forName(Method.class.getName()); + } catch (ClassNotFoundException ex) { + throw new IllegalStateException(ex); + } + } + + protected abstract Method create(Class declaringClass, String name, Object data, String sig); + + + // + // bck2brwsr implementation + // + + @JavaScriptBody(args = {"clazz", "prefix"}, + body = "" + + "var c = clazz.cnstr.prototype;" + + "var arr = new Array();\n" + + "for (m in c) {\n" + + " if (m.indexOf(prefix) === 0) {\n" + + " arr.push(m);\n" + + " arr.push(c[m]);\n" + + " }" + + "}\n" + + "return arr;") + private static native Object[] findMethodData( + Class clazz, String prefix); + + public static Method findMethod( + Class clazz, String name, Class... parameterTypes) { + Object[] data = findMethodData(clazz, name + "__"); + BIG: for (int i = 0; i < data.length; i += 2) { + String sig = ((String) data[0]).substring(name.length() + 2); + Method tmp = INSTANCE.create(clazz, name, data[1], sig); + Class[] tmpParms = tmp.getParameterTypes(); + if (parameterTypes.length != tmpParms.length) { + continue; + } + for (int j = 0; j < tmpParms.length; j++) { + if (!parameterTypes[j].equals(tmpParms[j])) { + continue BIG; + } + } + return tmp; + } + return null; + } + + public static Method[] findMethods(Class clazz, int mask) { + Object[] namesAndData = findMethodData(clazz, ""); + int cnt = 0; + for (int i = 0; i < namesAndData.length; i += 2) { + String sig = (String) namesAndData[i]; + Object data = namesAndData[i + 1]; + int middle = sig.indexOf("__"); + if (middle == -1) { + continue; + } + String name = sig.substring(0, middle); + sig = sig.substring(middle + 2); + final Method m = INSTANCE.create(clazz, name, data, sig); + if ((m.getModifiers() & mask) == 0) { + continue; + } + namesAndData[cnt++] = m; + } + Method[] arr = new Method[cnt]; + for (int i = 0; i < cnt; i++) { + arr[i] = (Method) namesAndData[i]; + } + return arr; + } + + public static int signatureElements(String sig) { + Enumeration en = signatureParser(sig); + int cnt = 0; + while (en.hasMoreElements()) { + en.nextElement(); + cnt++; + } + return cnt; + } + + public static Enumeration signatureParser(final String sig) { + class E implements Enumeration { + int pos; + + public boolean hasMoreElements() { + return pos < sig.length(); + } + + public Class nextElement() { + switch (sig.charAt(pos++)) { + case 'I': + return Integer.TYPE; + case 'J': + return Long.TYPE; + case 'D': + return Double.TYPE; + case 'F': + return Float.TYPE; + case 'B': + return Byte.TYPE; + case 'Z': + return Boolean.TYPE; + case 'S': + return Short.TYPE; + case 'V': + return Void.TYPE; + case 'C': + return Character.TYPE; + case 'L': + try { + int up = sig.indexOf("_2"); + String type = sig.substring(1, up); + pos = up + 2; + return Class.forName(type); + } catch (ClassNotFoundException ex) { + // should not happen + } + } + throw new UnsupportedOperationException(sig + " at " + pos); + } + } + return new E(); + } +} diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js Wed Jan 23 20:39:23 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -// empty line needed here -Number.prototype.add32 = function(x) { return (this + x) | 0; }; -Number.prototype.sub32 = function(x) { return (this - x) | 0; }; -Number.prototype.mul32 = function(x) { - return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0; -}; - -Number.prototype.toInt8 = function() { return (this << 24) >> 24; }; -Number.prototype.toInt16 = function() { return (this << 16) >> 16; }; \ No newline at end of file diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js Wed Jan 23 20:39:23 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -// initialize methods on arrays and String constants -vm.java_lang_reflect_Array(false); -vm.java_lang_String(false); - -Array.prototype.at = function(indx, value) { - if (indx < 0 || indx > this.length) { - var e = vm.java_lang_ArrayIndexOutOfBoundsException(true); - e.constructor.cons__VLjava_lang_String_2.call(e, indx.toString()); - throw e; - } - if (arguments.length === 2) { - this[indx] = value; - } - return this[indx]; -}; -Array.prototype.getClass__Ljava_lang_Class_2 = function() { - return vm.java_lang_Class(false).defineArray__Ljava_lang_Class_2Ljava_lang_String_2(this.jvmName); -}; -Array.prototype.clone__Ljava_lang_Object_2 = function() { - var s = this.length; - var ret = new Array(s); - for (var i = 0; i < s; i++) { - ret[i] = this[i]; - } - return ret; -}; diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Wed Jan 23 20:52:11 2013 +0100 @@ -0,0 +1,9 @@ +// empty line needed here +Number.prototype.add32 = function(x) { return (this + x) | 0; }; +Number.prototype.sub32 = function(x) { return (this - x) | 0; }; +Number.prototype.mul32 = function(x) { + return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0; +}; + +Number.prototype.toInt8 = function() { return (this << 24) >> 24; }; +Number.prototype.toInt16 = function() { return (this << 16) >> 16; }; \ No newline at end of file diff -r 05224402145d -r cde0c2d7794e emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_String.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_String.js Wed Jan 23 20:52:11 2013 +0100 @@ -0,0 +1,26 @@ +// initialize methods on arrays and String constants +vm.java_lang_reflect_Array(false); +vm.java_lang_String(false); + +Array.prototype.at = function(indx, value) { + if (indx < 0 || indx > this.length) { + var e = vm.java_lang_ArrayIndexOutOfBoundsException(true); + e.constructor.cons__VLjava_lang_String_2.call(e, indx.toString()); + throw e; + } + if (arguments.length === 2) { + this[indx] = value; + } + return this[indx]; +}; +Array.prototype.getClass__Ljava_lang_Class_2 = function() { + return vm.java_lang_Class(false).defineArray__Ljava_lang_Class_2Ljava_lang_String_2(this.jvmName); +}; +Array.prototype.clone__Ljava_lang_Object_2 = function() { + var s = this.length; + var ret = new Array(s); + for (var i = 0; i < s; i++) { + ret[i] = this[i]; + } + return ret; +};