1.1 --- a/emul/src/main/java/java/lang/Class.java Thu Jan 24 00:11:04 2013 +0100
1.2 +++ b/emul/src/main/java/java/lang/Class.java Thu Jan 24 17:08:02 2013 +0100
1.3 @@ -278,6 +278,10 @@
1.4 * @since JDK1.1
1.5 */
1.6 public boolean isInstance(Object obj) {
1.7 + if (isArray()) {
1.8 + return isAssignableFrom(obj.getClass());
1.9 + }
1.10 +
1.11 String prop = "$instOf_" + getName().replace('.', '_');
1.12 return hasProperty(obj, prop);
1.13 }
1.14 @@ -312,7 +316,21 @@
1.15 * null.
1.16 * @since JDK1.1
1.17 */
1.18 - public native boolean isAssignableFrom(Class<?> cls);
1.19 + public boolean isAssignableFrom(Class<?> cls) {
1.20 + if (this == cls) {
1.21 + return true;
1.22 + }
1.23 +
1.24 + if (isArray()) {
1.25 + final Class<?> cmpType = cls.getComponentType();
1.26 + if (isPrimitive()) {
1.27 + return this == cmpType;
1.28 + }
1.29 + return cmpType != null && getComponentType().isAssignableFrom(cmpType);
1.30 + }
1.31 + String prop = "$instOf_" + getName().replace('.', '_');
1.32 + return hasProperty(cls, prop);
1.33 + }
1.34
1.35
1.36 /**
2.1 --- a/emul/src/main/java/java/lang/reflect/Array.java Thu Jan 24 00:11:04 2013 +0100
2.2 +++ b/emul/src/main/java/java/lang/reflect/Array.java Thu Jan 24 17:08:02 2013 +0100
2.3 @@ -190,7 +190,7 @@
2.4 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
2.5 final Class<?> t = array.getClass().getComponentType();
2.6 if (t.isPrimitive()) {
2.7 - return Array.fromPrimitive(t, array, index);
2.8 + return fromPrimitive(t, array, index);
2.9 } else {
2.10 return ((Object[])array)[index];
2.11 }
2.12 @@ -512,7 +512,7 @@
2.13 }
2.14
2.15 }
2.16 -
2.17 +
2.18 /**
2.19 * Sets the value of the indexed component of the specified array
2.20 * object to the specified {@code int} value.
2.21 @@ -534,7 +534,7 @@
2.22 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
2.23 Class<?> t = array.getClass().getComponentType();
2.24 if (t == Integer.TYPE) {
2.25 - long[] arr = (long[]) array;
2.26 + int[] arr = (int[]) array;
2.27 arr[index] = i;
2.28 } else {
2.29 setLong(array, index, i);
2.30 @@ -643,10 +643,11 @@
2.31 if (dims.length == index + 1) {
2.32 return newArray(sig.length() == 2, sig, dims[index]);
2.33 }
2.34 - Object[] arr = (Object[]) newArray(false, sig, dims[index]);
2.35 + Object arr = newArray(false, sig, dims[index]);
2.36 String compsig = sig.substring(1);
2.37 - for (int i = 0; i < arr.length; i++) {
2.38 - arr[i] = multiNewArray(compsig, dims, index + 1);
2.39 + int len = getLength(arr);
2.40 + for (int i = 0; i < len; i++) {
2.41 + setArray(arr, i, multiNewArray(compsig, dims, index + 1));
2.42 }
2.43 return arr;
2.44 }
2.45 @@ -654,6 +655,9 @@
2.46 return Method.fromPrimitive(t, atArray(array, index));
2.47 }
2.48
2.49 - @JavaScriptBody(args = { "array", "index" }, body = "return array[index]")
2.50 + @JavaScriptBody(args = { "array", "index" }, body = "return array[index];")
2.51 private static native Object atArray(Object array, int index);
2.52 +
2.53 + @JavaScriptBody(args = { "array", "index", "v" }, body = "array[index] = v;")
2.54 + private static native Object setArray(Object array, int index, Object v);
2.55 }
3.1 --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js Thu Jan 24 00:11:04 2013 +0100
3.2 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js Thu Jan 24 17:08:02 2013 +0100
3.3 @@ -22,5 +22,6 @@
3.4 for (var i = 0; i < s; i++) {
3.5 ret[i] = this[i];
3.6 }
3.7 + ret.jvmName = this.jvmName;
3.8 return ret;
3.9 };
4.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Jan 24 00:11:04 2013 +0100
4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Jan 24 17:08:02 2013 +0100
4.3 @@ -1106,11 +1106,13 @@
4.4 int indx = readIntArg(byteCodes, i);
4.5 final String type = jc.getClassName(indx);
4.6 if (!type.startsWith("[")) {
4.7 - // no way to check arrays right now
4.8 - // XXX proper exception
4.9 emit(out,
4.10 "if (@1 !== null && !@1.$instOf_@2) throw {};",
4.11 smapper.getA(0), type.replace('/', '_'));
4.12 + } else {
4.13 + emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
4.14 + smapper.getA(0), type
4.15 + );
4.16 }
4.17 i += 2;
4.18 break;
4.19 @@ -1118,9 +1120,16 @@
4.20 case opc_instanceof: {
4.21 int indx = readIntArg(byteCodes, i);
4.22 final String type = jc.getClassName(indx);
4.23 - emit(out, "var @2 = @1.$instOf_@3 ? 1 : 0;",
4.24 - smapper.popA(), smapper.pushI(),
4.25 - type.replace('/', '_'));
4.26 + if (!type.startsWith("[")) {
4.27 + emit(out, "var @2 = @1.$instOf_@3 ? 1 : 0;",
4.28 + smapper.popA(), smapper.pushI(),
4.29 + type.replace('/', '_'));
4.30 + } else {
4.31 + emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
4.32 + smapper.popA(), smapper.pushI(),
4.33 + type
4.34 + );
4.35 + }
4.36 i += 2;
4.37 break;
4.38 }
5.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Jan 24 00:11:04 2013 +0100
5.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Jan 24 17:08:02 2013 +0100
5.3 @@ -98,7 +98,7 @@
5.4 "try {\n" +
5.5 " new Function(script)(loader, name);\n" +
5.6 "} catch (ex) {\n" +
5.7 - " throw 'Cannot compile ' + ex + ' line: ' + ex.lineNumber + ' script:\\n' + script;\n" +
5.8 + " throw 'Cannot compile ' + name + ' ' + ex + ' line: ' + ex.lineNumber + ' script:\\n' + script;\n" +
5.9 "}\n" +
5.10 "return name != null ? vm[name](instance) : null;\n"
5.11 )
6.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Array.java Thu Jan 24 00:11:04 2013 +0100
6.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Array.java Thu Jan 24 17:08:02 2013 +0100
6.3 @@ -112,6 +112,10 @@
6.4 return Object[].class.getName();
6.5 }
6.6
6.7 + public static boolean instanceOfArray(Object obj) {
6.8 + return obj instanceof Object[];
6.9 + }
6.10 +
6.11 public static int sum(int size) {
6.12 int[] arr = new int[size];
6.13 return arr[0] + arr[1];
7.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java Thu Jan 24 00:11:04 2013 +0100
7.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java Thu Jan 24 17:08:02 2013 +0100
7.3 @@ -72,6 +72,9 @@
7.4 @Test public void verifyObjectArrayClass() throws Exception {
7.5 assertExec("Returns 'Object[]'", Array.class, "objectArrayClass__Ljava_lang_String_2", Array.objectArrayClass());
7.6 }
7.7 + @Test public void verifyInstanceOfArray() throws Exception {
7.8 + assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(0), "non-array");
7.9 + }
7.10
7.11 private static CharSequence codeSeq;
7.12 private static Invocable code;