1.1 --- a/emul/src/main/java/java/lang/AbstractStringBuilder.java Sun Nov 25 21:31:51 2012 +0100
1.2 +++ b/emul/src/main/java/java/lang/AbstractStringBuilder.java Sun Nov 25 23:14:58 2012 +0100
1.3 @@ -602,7 +602,7 @@
1.4 */
1.5 @JavaScriptBody(
1.6 args={ "self", "i" },
1.7 - body="return java_lang_AbstractStringBuilder_appendLjava_lang_AbstractStringBuilderLjava_lang_String(self,i.toString());"
1.8 + body="return java_lang_AbstractStringBuilder_proto().appendLjava_lang_AbstractStringBuilderLjava_lang_String(self,i.toString());"
1.9 )
1.10 public AbstractStringBuilder append(int i) {
1.11 if (i == Integer.MIN_VALUE) {
2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Nov 25 21:31:51 2012 +0100
2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Nov 25 23:14:58 2012 +0100
2.3 @@ -19,7 +19,6 @@
2.4
2.5 import java.io.IOException;
2.6 import java.io.InputStream;
2.7 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
2.8 import org.apidesign.javap.AnnotationParser;
2.9 import org.apidesign.javap.ClassData;
2.10 import org.apidesign.javap.FieldData;
2.11 @@ -74,13 +73,6 @@
2.12 }
2.13 }
2.14 StringArray toInitilize = new StringArray();
2.15 - for (MethodData m : jc.getMethods()) {
2.16 - if (m.isStatic()) {
2.17 - generateStaticMethod(m, toInitilize);
2.18 - } else {
2.19 - generateInstanceMethod(m);
2.20 - }
2.21 - }
2.22 final String className = className(jc);
2.23 out.append("\nfunction ").append(className);
2.24 out.append("() {");
2.25 @@ -90,7 +82,8 @@
2.26 append(v.getName()).append(initField(v));
2.27 }
2.28 }
2.29 - out.append("\n}\n\nfunction ").append(className).append("_proto() {");
2.30 + out.append("\n}");
2.31 + out.append("\n\nfunction ").append(className).append("_proto() {");
2.32 out.append("\n if (").append(className).
2.33 append(".prototype.$instOf_").append(className).append(") {");
2.34 out.append("\n return new ").append(className).append(";");
2.35 @@ -110,8 +103,10 @@
2.36 out.append("\n var p = ").append(className).append(".prototype;");
2.37 }
2.38 for (MethodData m : jc.getMethods()) {
2.39 - if (!m.getName().contains("<cinit>")) {
2.40 - generateMethodReference("\n p.", m);
2.41 + if (m.isStatic()) {
2.42 + generateStaticMethod("\n p.", m, toInitilize);
2.43 + } else {
2.44 + generateInstanceMethod("\n p.", m);
2.45 }
2.46 }
2.47 out.append("\n p.$instOf_").append(className).append(" = true;");
2.48 @@ -127,17 +122,15 @@
2.49 }
2.50 return sb.toString();
2.51 }
2.52 - private void generateStaticMethod(MethodData m, StringArray toInitilize) throws IOException {
2.53 - if (javaScriptBody(m, true)) {
2.54 + private void generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
2.55 + if (javaScriptBody(prefix, m, true)) {
2.56 return;
2.57 }
2.58 StringBuilder argsCnt = new StringBuilder();
2.59 final String mn = findMethodName(m, argsCnt);
2.60 - out.append("\nfunction ").append(
2.61 - className(jc)
2.62 - ).append('_').append(mn);
2.63 + out.append(prefix).append(mn).append(" = function");
2.64 if (mn.equals("classV")) {
2.65 - toInitilize.add(className(jc) + '_' + mn);
2.66 + toInitilize.add(className(jc) + "_proto()." + mn);
2.67 }
2.68 out.append('(');
2.69 String space = "";
2.70 @@ -165,24 +158,16 @@
2.71 } else {
2.72 out.append(" /* no code found for ").append(m.getInternalSig()).append(" */\n");
2.73 }
2.74 - out.append("}");
2.75 + out.append("};");
2.76 }
2.77
2.78 - private void generateMethodReference(String prefix, MethodData m) throws IOException {
2.79 - final String name = findMethodName(m, new StringBuilder());
2.80 - out.append(prefix).append(name).append(" = ")
2.81 - .append(className(jc))
2.82 - .append('_').append(name).append(";");
2.83 - }
2.84 -
2.85 - private void generateInstanceMethod(MethodData m) throws IOException {
2.86 - if (javaScriptBody(m, false)) {
2.87 + private void generateInstanceMethod(String prefix, MethodData m) throws IOException {
2.88 + if (javaScriptBody(prefix, m, false)) {
2.89 return;
2.90 }
2.91 StringBuilder argsCnt = new StringBuilder();
2.92 - out.append("\nfunction ").append(
2.93 - className(jc)
2.94 - ).append('_').append(findMethodName(m, argsCnt));
2.95 + final String mn = findMethodName(m, argsCnt);
2.96 + out.append(prefix).append(mn).append(" = function");
2.97 out.append("(arg0");
2.98 String space = ",";
2.99 for (int index = 1, i = 0; i < argsCnt.length(); i++) {
2.100 @@ -207,7 +192,7 @@
2.101 } else {
2.102 out.append(" /* no code found for ").append(m.getInternalSig()).append(" */\n");
2.103 }
2.104 - out.append("}");
2.105 + out.append("};");
2.106 }
2.107
2.108 private void produceCode(byte[] byteCodes) throws IOException {
2.109 @@ -980,7 +965,7 @@
2.110 return d.replace('[', 'A');
2.111 }
2.112
2.113 - private boolean javaScriptBody(MethodData m, boolean isStatic) throws IOException {
2.114 + private boolean javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
2.115 byte[] arr = m.findAnnotationData(true);
2.116 if (arr == null) {
2.117 return false;
2.118 @@ -1010,9 +995,8 @@
2.119 return false;
2.120 }
2.121 StringBuilder cnt = new StringBuilder();
2.122 - out.append("\nfunction ").append(className(jc)).append('_').
2.123 - append(findMethodName(m, cnt));
2.124 - out.append("(");
2.125 + out.append(prefix).append(findMethodName(m, cnt));
2.126 + out.append(" = function(");
2.127 String space;
2.128 int index;
2.129 if (!isStatic) {
3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java Sun Nov 25 21:31:51 2012 +0100
3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java Sun Nov 25 23:14:58 2012 +0100
3.3 @@ -29,7 +29,7 @@
3.4 */
3.5 public class ArrayTest {
3.6 @Test public void verifySimpleIntOperation() throws Exception {
3.7 - assertExec("CheckTheSum", "org_apidesign_vm4brwsr_Array_simpleI",
3.8 + assertExec("CheckTheSum", Array.class, "simpleI",
3.9 Double.valueOf(15)
3.10 );
3.11 }
3.12 @@ -39,13 +39,13 @@
3.13 }
3.14
3.15 @Test public void verifyOperationsOnArrays() throws Exception {
3.16 - assertExec("The sum is 105", "org_apidesign_vm4brwsr_Array_sumD",
3.17 + assertExec("The sum is 105", Array.class, "sumD",
3.18 Double.valueOf(105)
3.19 );
3.20 }
3.21
3.22 @Test public void doesCopyArrayWork() throws Exception {
3.23 - assertExec("Returns 'a'", "org_apidesign_vm4brwsr_Array_copyArrayC", Double.valueOf('a'));
3.24 + assertExec("Returns 'a'", Array.class, "copyArrayC", Double.valueOf('a'));
3.25 }
3.26
3.27 private static CharSequence codeSeq;
3.28 @@ -59,21 +59,7 @@
3.29 );
3.30 codeSeq = sb;
3.31 }
3.32 - private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
3.33 - Object ret = null;
3.34 - try {
3.35 - ret = code.invokeFunction(methodName, args);
3.36 - } catch (ScriptException ex) {
3.37 - fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
3.38 - } catch (NoSuchMethodException ex) {
3.39 - fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
3.40 - }
3.41 - if (ret == null && expRes == null) {
3.42 - return;
3.43 - }
3.44 - if (expRes.equals(ret)) {
3.45 - return;
3.46 - }
3.47 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
3.48 + private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
3.49 + StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
3.50 }
3.51 }
4.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java Sun Nov 25 21:31:51 2012 +0100
4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java Sun Nov 25 23:14:58 2012 +0100
4.3 @@ -31,42 +31,42 @@
4.4 @Test public void verifyDefaultDoubleValue() throws Exception {
4.5 assertExec(
4.6 "Will be zero",
4.7 - "org_apidesign_vm4brwsr_Instance_defaultDblValueD",
4.8 + Instance.class, "defaultDblValueD",
4.9 Double.valueOf(0)
4.10 );
4.11 }
4.12 @Test public void verifyStaticMethodCall() throws Exception {
4.13 assertExec(
4.14 "Will be zero",
4.15 - "org_apidesign_vm4brwsr_InstanceSub_recallDblD",
4.16 + InstanceSub.class, "recallDblD",
4.17 Double.valueOf(0)
4.18 );
4.19 }
4.20 @Test public void verifyAssignedByteValue() throws Exception {
4.21 assertExec(
4.22 "Will one thirty one",
4.23 - "org_apidesign_vm4brwsr_Instance_assignedByteValueB",
4.24 + Instance.class, "assignedByteValueB",
4.25 Double.valueOf(31)
4.26 );
4.27 }
4.28 @Test public void verifyMagicOne() throws Exception {
4.29 assertExec(
4.30 "Should be three and something",
4.31 - "org_apidesign_vm4brwsr_Instance_magicOneD",
4.32 + Instance.class, "magicOneD",
4.33 Double.valueOf(3.3)
4.34 );
4.35 }
4.36 @Test public void verifyInstanceMethods() throws Exception {
4.37 assertExec(
4.38 "Should be eleven as we invoke overwritten method, plus 44",
4.39 - "org_apidesign_vm4brwsr_Instance_virtualBytesI",
4.40 + Instance.class, "virtualBytesI",
4.41 Double.valueOf(55)
4.42 );
4.43 }
4.44 @Test public void verifyInterfaceMethods() throws Exception {
4.45 assertExec(
4.46 "Retruns default value",
4.47 - "org_apidesign_vm4brwsr_Instance_interfaceBytesF",
4.48 + Instance.class, "interfaceBytesF",
4.49 Double.valueOf(31)
4.50 );
4.51 }
4.52 @@ -74,7 +74,7 @@
4.53 @Test public void isNull() throws Exception {
4.54 assertExec(
4.55 "Yes, we are instance",
4.56 - "org_apidesign_vm4brwsr_Instance_isNullZ",
4.57 + Instance.class, "isNullZ",
4.58 Double.valueOf(0.0)
4.59 );
4.60 }
4.61 @@ -82,7 +82,7 @@
4.62 @Test public void isInstanceOf() throws Exception {
4.63 assertExec(
4.64 "Yes, we are instance",
4.65 - "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
4.66 + Instance.class, "instanceOfZZ",
4.67 Double.valueOf(1.0), true
4.68 );
4.69 }
4.70 @@ -90,7 +90,7 @@
4.71 @Test public void notInstanceOf() throws Exception {
4.72 assertExec(
4.73 "No, we are not an instance",
4.74 - "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
4.75 + Instance.class, "instanceOfZZ",
4.76 Double.valueOf(0.0), false
4.77 );
4.78 }
4.79 @@ -98,14 +98,14 @@
4.80 @Test public void verifyCastToClass() throws Exception {
4.81 assertExec(
4.82 "Five signals all is good",
4.83 - "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
4.84 + Instance.class, "castsWorkIZ",
4.85 Double.valueOf(5.0), false
4.86 );
4.87 }
4.88 @Test public void verifyCastToInterface() throws Exception {
4.89 assertExec(
4.90 "Five signals all is good",
4.91 - "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
4.92 + Instance.class, "castsWorkIZ",
4.93 Double.valueOf(5.0), true
4.94 );
4.95 }
4.96 @@ -127,25 +127,9 @@
4.97 }
4.98
4.99 private void assertExec(
4.100 - String msg, String methodName, Object expRes, Object... args
4.101 + String msg, Class clazz, String method, Object expRes, Object... args
4.102 ) throws Exception {
4.103 -
4.104 - Object ret = null;
4.105 - try {
4.106 - ret = code.invokeFunction(methodName, args);
4.107 - } catch (ScriptException ex) {
4.108 - fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
4.109 - } catch (NoSuchMethodException ex) {
4.110 - fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
4.111 - }
4.112 - if (ret == null && expRes == null) {
4.113 - return;
4.114 - }
4.115 - if (expRes.equals(ret)) {
4.116 - return;
4.117 - }
4.118 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
4.119 -
4.120 + StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
4.121 }
4.122
4.123 }
5.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Sun Nov 25 21:31:51 2012 +0100
5.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Sun Nov 25 23:14:58 2012 +0100
5.3 @@ -17,11 +17,6 @@
5.4 */
5.5 package org.apidesign.vm4brwsr;
5.6
5.7 -import java.io.ByteArrayInputStream;
5.8 -import java.io.ByteArrayOutputStream;
5.9 -import java.io.DataInputStream;
5.10 -import java.io.DataOutputStream;
5.11 -import java.io.IOException;
5.12 import javax.script.Invocable;
5.13 import javax.script.ScriptException;
5.14 import static org.testng.Assert.*;
5.15 @@ -34,19 +29,19 @@
5.16 */
5.17 public class NumberTest {
5.18 @Test public void integerFromString() throws Exception {
5.19 - assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
5.20 + assertExec("Can convert string to integer", Integer.class, "parseIntILjava_lang_String",
5.21 Double.valueOf(333), "333"
5.22 );
5.23 }
5.24
5.25 @Test public void doubleFromString() throws Exception {
5.26 - assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
5.27 + assertExec("Can convert string to double", Double.class, "parseDoubleDLjava_lang_String",
5.28 Double.valueOf(33.3), "33.3"
5.29 );
5.30 }
5.31
5.32 @Test public void autoboxDouble() throws Exception {
5.33 - assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
5.34 + assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToStringLjava_lang_String",
5.35 "3.3"
5.36 );
5.37 }
5.38 @@ -56,7 +51,7 @@
5.39 }
5.40
5.41 @Test public void jslog1000() throws Exception {
5.42 - assertExec("log_10(1000) == 3", "java_lang_Math_log10DD",
5.43 + assertExec("log_10(1000) == 3", Math.class, "log10DD",
5.44 Double.valueOf(3.0), 1000.0
5.45 );
5.46 }
5.47 @@ -65,20 +60,20 @@
5.48 assertEquals(3, Numbers.rem(303, 10));
5.49 }
5.50 @Test public void jsRem() throws Exception {
5.51 - assertExec("Should be three", "org_apidesign_vm4brwsr_Numbers_remIII",
5.52 + assertExec("Should be three", Numbers.class, "remIII",
5.53 Double.valueOf(3.0), 303, 10
5.54 );
5.55 }
5.56
5.57 @Test public void deserializeInt() throws Exception {
5.58 int exp = Numbers.deserInt();
5.59 - assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserIntI",
5.60 + assertExec("Should be the same", Numbers.class, "deserIntI",
5.61 Double.valueOf(exp)
5.62 );
5.63 }
5.64
5.65 @Test public void deserializeSimpleLong() throws Exception {
5.66 - assertExec("Should be 3454", "org_apidesign_vm4brwsr_Numbers_deserLongJAB",
5.67 + assertExec("Should be 3454", Numbers.class, "deserLongJAB",
5.68 Double.valueOf(3454),
5.69 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
5.70 );
5.71 @@ -102,7 +97,7 @@
5.72
5.73 @Test public void deserializeFloatInJS() throws Exception {
5.74 float f = 54324.32423f;
5.75 - assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserFloatF",
5.76 + assertExec("Should be the same", Numbers.class, "deserFloatF",
5.77 Double.valueOf(f)
5.78 );
5.79 }
5.80 @@ -115,7 +110,7 @@
5.81
5.82 @Test public void deserializeDoubleInJS() throws Exception {
5.83 double f = 3.0;
5.84 - assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserDoubleD", f);
5.85 + assertExec("Should be the same", Numbers.class, "deserDoubleD", f);
5.86 }
5.87 /*
5.88 @Test public void serDouble() throws IOException {
5.89 @@ -136,7 +131,7 @@
5.90 @Test public void fiveInStringJS() throws Exception {
5.91 String s = Numbers.intToString();
5.92 assertExec("Should be the same: " + s,
5.93 - "org_apidesign_vm4brwsr_Numbers_intToStringLjava_lang_String",
5.94 + Numbers.class, "intToStringLjava_lang_String",
5.95 s
5.96 );
5.97 }
5.98 @@ -144,7 +139,7 @@
5.99 @Test public void sevenInStringJS() throws Exception {
5.100 String s = Numbers.floatToString();
5.101 assertExec("Should be the same: " + s,
5.102 - "org_apidesign_vm4brwsr_Numbers_floatToStringLjava_lang_String",
5.103 + Numbers.class, "floatToStringLjava_lang_String",
5.104 s
5.105 );
5.106 }
5.107 @@ -162,15 +157,16 @@
5.108 }
5.109
5.110 private static void assertExec(
5.111 - String msg, String methodName, Object expRes, Object... args) throws Exception {
5.112 + String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception {
5.113
5.114 Object ret = null;
5.115 try {
5.116 - ret = code.invokeFunction(methodName, args);
5.117 + ret = code.invokeFunction(clazz.getName().replace('.', '_') + "_proto");
5.118 + ret = code.invokeMethod(ret, method, args);
5.119 } catch (ScriptException ex) {
5.120 - fail("Execution failed in\n" + codeSeq, ex);
5.121 + fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
5.122 } catch (NoSuchMethodException ex) {
5.123 - fail("Cannot find method in\n" + codeSeq, ex);
5.124 + fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
5.125 }
5.126 if (ret == null && expRes == null) {
5.127 return;
5.128 @@ -181,10 +177,10 @@
5.129 if (expRes instanceof Double && ret instanceof Double) {
5.130 double expD = ((Double)expRes).doubleValue();
5.131 double retD = ((Double)ret).doubleValue();
5.132 - assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + codeSeq);
5.133 + assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
5.134 return;
5.135 }
5.136 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
5.137 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
5.138 }
5.139
5.140 }
6.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Sun Nov 25 21:31:51 2012 +0100
6.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Sun Nov 25 23:14:58 2012 +0100
6.3 @@ -36,7 +36,7 @@
6.4 @Test public void threePlusFour() throws Exception {
6.5 assertExec(
6.6 "Should be seven",
6.7 - "org_apidesign_vm4brwsr_StaticMethod_sumIII",
6.8 + StaticMethod.class, "sumIII",
6.9 Double.valueOf(7),
6.10 3, 4
6.11 );
6.12 @@ -45,7 +45,7 @@
6.13 @Test public void checkReallyInitializedValues() throws Exception {
6.14 assertExec(
6.15 "Return true",
6.16 - "org_apidesign_vm4brwsr_StaticMethod_isNullZ",
6.17 + StaticMethod.class, "isNullZ",
6.18 Double.valueOf(1)
6.19 );
6.20 }
6.21 @@ -53,7 +53,7 @@
6.22 @Test public void powerOfThree() throws Exception {
6.23 assertExec(
6.24 "Should be nine",
6.25 - "org_apidesign_vm4brwsr_StaticMethod_powerFF",
6.26 + StaticMethod.class, "powerFF",
6.27 Double.valueOf(9),
6.28 3.0f
6.29 );
6.30 @@ -62,7 +62,7 @@
6.31 @Test public void minusOne() throws Exception {
6.32 assertExec(
6.33 "Should be minus one",
6.34 - "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
6.35 + StaticMethod.class, "minusOneI",
6.36 Double.valueOf(-1)
6.37 );
6.38 }
6.39 @@ -70,7 +70,7 @@
6.40 @Test public void doubleWithoutLong() throws Exception {
6.41 assertExec(
6.42 "Should be two",
6.43 - "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
6.44 + StaticMethod.class, "minusDDJ",
6.45 Double.valueOf(2),
6.46 3.0d, 1l
6.47 );
6.48 @@ -79,7 +79,7 @@
6.49 @Test public void divAndRound() throws Exception {
6.50 assertExec(
6.51 "Should be rounded to one",
6.52 - "org_apidesign_vm4brwsr_StaticMethod_divIBD",
6.53 + StaticMethod.class, "divIBD",
6.54 Double.valueOf(1),
6.55 3, 3.75
6.56 );
6.57 @@ -87,7 +87,7 @@
6.58 @Test public void mixedMethodFourParams() throws Exception {
6.59 assertExec(
6.60 "Should be two",
6.61 - "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
6.62 + StaticMethod.class, "mixIIJBD",
6.63 Double.valueOf(20),
6.64 2, 10l, 5, 2.0
6.65 );
6.66 @@ -95,7 +95,7 @@
6.67 @Test public void factRec() throws Exception {
6.68 assertExec(
6.69 "Factorial of 5 is 120",
6.70 - "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
6.71 + StaticMethod.class, "factRecJI",
6.72 Double.valueOf(120),
6.73 5
6.74 );
6.75 @@ -103,7 +103,7 @@
6.76 @Test public void factIter() throws Exception {
6.77 assertExec(
6.78 "Factorial of 5 is 120",
6.79 - "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
6.80 + StaticMethod.class, "factIterJI",
6.81 Double.valueOf(120),
6.82 5
6.83 );
6.84 @@ -112,7 +112,7 @@
6.85 @Test public void xor() throws Exception {
6.86 assertExec(
6.87 "Xor is 4",
6.88 - "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
6.89 + StaticMethod.class, "xorJIJ",
6.90 Double.valueOf(4),
6.91 7,
6.92 3
6.93 @@ -122,7 +122,7 @@
6.94 @Test public void or() throws Exception {
6.95 assertExec(
6.96 "Or will be 7",
6.97 - "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
6.98 + StaticMethod.class, "orOrAndJZII",
6.99 Double.valueOf(7),
6.100 true,
6.101 4,
6.102 @@ -132,14 +132,14 @@
6.103 @Test public void nullCheck() throws Exception {
6.104 assertExec(
6.105 "Returns nothing",
6.106 - "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
6.107 + StaticMethod.class, "noneLjava_lang_ObjectII",
6.108 null, 1, 3
6.109 );
6.110 }
6.111 @Test public void and() throws Exception {
6.112 assertExec(
6.113 "And will be 3",
6.114 - "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
6.115 + StaticMethod.class, "orOrAndJZII",
6.116 Double.valueOf(3),
6.117 false,
6.118 7,
6.119 @@ -149,7 +149,7 @@
6.120 @Test public void inc4() throws Exception {
6.121 assertExec(
6.122 "It will be 4",
6.123 - "org_apidesign_vm4brwsr_StaticMethod_inc4I",
6.124 + StaticMethod.class, "inc4I",
6.125 Double.valueOf(4)
6.126 );
6.127 }
6.128 @@ -162,7 +162,7 @@
6.129 @Test public void shiftLeftInJS() throws Exception {
6.130 assertExec(
6.131 "Setting 9th bit",
6.132 - "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
6.133 + StaticMethod.class, "shiftLeftIII",
6.134 Double.valueOf(256),
6.135 1, 8
6.136 );
6.137 @@ -176,7 +176,7 @@
6.138 @Test public void shiftRightInJS() throws Exception {
6.139 assertExec(
6.140 "Get -1",
6.141 - "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
6.142 + StaticMethod.class, "shiftArithmRightIIIZ",
6.143 Double.valueOf(-1),
6.144 -8, 3, true
6.145 );
6.146 @@ -189,7 +189,7 @@
6.147 @Test public void unsignedShiftRightInJS() throws Exception {
6.148 assertExec(
6.149 "Get -1",
6.150 - "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
6.151 + StaticMethod.class, "shiftArithmRightIIIZ",
6.152 Double.valueOf(1),
6.153 8, 3, false
6.154 );
6.155 @@ -198,7 +198,7 @@
6.156 @Test public void javaScriptBody() throws Exception {
6.157 assertExec(
6.158 "JavaScript string",
6.159 - "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
6.160 + StaticMethod.class, "i2sLjava_lang_StringII",
6.161 "333",
6.162 330, 3
6.163 );
6.164 @@ -207,7 +207,7 @@
6.165 @Test public void switchJarda() throws Exception {
6.166 assertExec(
6.167 "The expected value",
6.168 - "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
6.169 + StaticMethod.class, "swtchLjava_lang_StringI",
6.170 "Jarda",
6.171 0
6.172 );
6.173 @@ -216,7 +216,7 @@
6.174 @Test public void switchDarda() throws Exception {
6.175 assertExec(
6.176 "The expected value",
6.177 - "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
6.178 + StaticMethod.class, "swtchLjava_lang_StringI",
6.179 "Darda",
6.180 1
6.181 );
6.182 @@ -224,7 +224,7 @@
6.183 @Test public void switchParda() throws Exception {
6.184 assertExec(
6.185 "The expected value",
6.186 - "org_apidesign_vm4brwsr_StaticMethod_swtch2Ljava_lang_StringI",
6.187 + StaticMethod.class, "swtch2Ljava_lang_StringI",
6.188 "Parda",
6.189 22
6.190 );
6.191 @@ -232,7 +232,7 @@
6.192 @Test public void switchMarda() throws Exception {
6.193 assertExec(
6.194 "The expected value",
6.195 - "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
6.196 + StaticMethod.class, "swtchLjava_lang_StringI",
6.197 "Marda",
6.198 -433
6.199 );
6.200 @@ -249,16 +249,25 @@
6.201 }
6.202
6.203
6.204 - private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
6.205 - StringBuilder sb = new StringBuilder();
6.206 -
6.207 + private static void assertExec(
6.208 + String msg, Class clazz, String method,
6.209 + Object expRes, Object... args
6.210 + ) throws Exception {
6.211 + assertExec(code, codeSeq, msg, clazz, method, expRes, args);
6.212 + }
6.213 + static void assertExec(
6.214 + Invocable toRun, CharSequence theCode,
6.215 + String msg, Class clazz, String method,
6.216 + Object expRes, Object... args
6.217 + ) throws Exception {
6.218 Object ret = null;
6.219 try {
6.220 - ret = code.invokeFunction(methodName, args);
6.221 + ret = toRun.invokeFunction(clazz.getName().replace('.', '_') + "_proto");
6.222 + ret = toRun.invokeMethod(ret, method, args);
6.223 } catch (ScriptException ex) {
6.224 - fail("Execution failed in\n" + codeSeq, ex);
6.225 + fail("Execution failed in\n" + dumpJS(theCode), ex);
6.226 } catch (NoSuchMethodException ex) {
6.227 - fail("Cannot find method in\n" + codeSeq, ex);
6.228 + fail("Cannot find method in\n" + dumpJS(theCode), ex);
6.229 }
6.230 if (ret == null && expRes == null) {
6.231 return;
6.232 @@ -266,7 +275,7 @@
6.233 if (expRes != null && expRes.equals(ret)) {
6.234 return;
6.235 }
6.236 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
6.237 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
6.238
6.239 }
6.240
6.241 @@ -293,7 +302,7 @@
6.242 if (sb.length() > 2000) {
6.243 sb = dumpJS(sb);
6.244 }
6.245 - fail("Could not compile:\n" + sb, ex);
6.246 + fail("Could not evaluate:\n" + sb, ex);
6.247 return null;
6.248 }
6.249 }
7.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java Sun Nov 25 21:31:51 2012 +0100
7.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java Sun Nov 25 23:14:58 2012 +0100
7.3 @@ -31,7 +31,7 @@
7.4 @Test public void firstChar() throws Exception {
7.5 assertExec(
7.6 "First char in Hello is H",
7.7 - "org_apidesign_vm4brwsr_StringSample_sayHelloCI",
7.8 + StringSample.class, "sayHelloCI",
7.9 72, 0
7.10 );
7.11 }
7.12 @@ -39,7 +39,7 @@
7.13 @Test public void fromChars() throws Exception {
7.14 assertExec(
7.15 "First char in Hello is ABC",
7.16 - "org_apidesign_vm4brwsr_StringSample_fromCharsLjava_lang_StringCCC",
7.17 + StringSample.class, "fromCharsLjava_lang_StringCCC",
7.18 "ABC", 'A', 'B', 'C'
7.19 );
7.20 }
7.21 @@ -47,7 +47,7 @@
7.22 @Test public void concatChars() throws Exception {
7.23 assertExec(
7.24 "Composing yields ABC",
7.25 - "org_apidesign_vm4brwsr_StringSample_charsLjava_lang_StringCCC",
7.26 + StringSample.class, "charsLjava_lang_StringCCC",
7.27 "ABC", 'A', 'B', 'C'
7.28 );
7.29 }
7.30 @@ -55,7 +55,7 @@
7.31 @Test public void concatCharsFromInts() throws Exception {
7.32 assertExec(
7.33 "Composing yields ABC",
7.34 - "org_apidesign_vm4brwsr_StringSample_charsFromNumbersLjava_lang_String",
7.35 + StringSample.class, "charsFromNumbersLjava_lang_String",
7.36 "ABC"
7.37 );
7.38 }
7.39 @@ -63,7 +63,7 @@
7.40 @Test public void concatCharsFromChars() throws Exception {
7.41 assertExec(
7.42 "Composing yields ABC",
7.43 - "org_apidesign_vm4brwsr_StringSample_charsFromCharsLjava_lang_String",
7.44 + StringSample.class, "charsFromCharsLjava_lang_String",
7.45 "ABC"
7.46 );
7.47 }
7.48 @@ -71,7 +71,7 @@
7.49 @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
7.50 assertExec(
7.51 "Five executions should generate 5Hello World!",
7.52 - "org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI",
7.53 + StringSample.class, "toStringTestLjava_lang_StringI",
7.54 "Hello World!5", 5
7.55 );
7.56 }
7.57 @@ -82,7 +82,7 @@
7.58 @Test(timeOut=10000) public void stringStringConcat() throws Exception {
7.59 assertExec(
7.60 "Composes strings OK",
7.61 - "org_apidesign_vm4brwsr_StringSample_concatStringsLjava_lang_String",
7.62 + StringSample.class, "concatStringsLjava_lang_String",
7.63 "Hello World!1" + "\\\n\r\t"
7.64 );
7.65 }
7.66 @@ -90,21 +90,21 @@
7.67 @Test public void equalsAndSubstring() throws Exception {
7.68 assertExec(
7.69 "Composes are OK",
7.70 - "org_apidesign_vm4brwsr_StringSample_equalToHelloZII",
7.71 + StringSample.class, "equalToHelloZII",
7.72 true, 0, 5
7.73 );
7.74 }
7.75 @Test public void replaceChars() throws Exception {
7.76 assertExec(
7.77 "Can replace slashes by underscores",
7.78 - "org_apidesign_vm4brwsr_StringSample_replaceLjava_lang_StringLjava_lang_StringCC",
7.79 + StringSample.class, "replaceLjava_lang_StringLjava_lang_StringCC",
7.80 "x_y_z", "x/y/z", '/', '_'
7.81 );
7.82 }
7.83 @Test public void replaceIntChars() throws Exception {
7.84 assertExec(
7.85 "Can replace slashes by underscores",
7.86 - "org_apidesign_vm4brwsr_StringSample_replaceLjava_lang_StringLjava_lang_StringCC",
7.87 + StringSample.class, "replaceLjava_lang_StringLjava_lang_StringCC",
7.88 "x_y_z", "x/y/z", (int)'/', (int)'_'
7.89 );
7.90 }
7.91 @@ -112,7 +112,7 @@
7.92 @Test public void insertBuilder() throws Exception {
7.93 assertExec(
7.94 "Can insert something into a buffer?",
7.95 - "org_apidesign_vm4brwsr_StringSample_insertBufferLjava_lang_String",
7.96 + StringSample.class, "insertBufferLjava_lang_String",
7.97 "Ahojdo!"
7.98 );
7.99 }
7.100 @@ -122,7 +122,7 @@
7.101 int jh = StringSample.hashCode(j);
7.102 assertExec(
7.103 "Hashcode is the same " +jh,
7.104 - "org_apidesign_vm4brwsr_StringSample_hashCodeILjava_lang_String",
7.105 + StringSample.class, "hashCodeILjava_lang_String",
7.106 Double.valueOf(jh), j
7.107 );
7.108 }
7.109 @@ -131,28 +131,28 @@
7.110 int jh = StringSample.hashCode(j);
7.111 assertExec(
7.112 "Hashcode is the same " + jh,
7.113 - "org_apidesign_vm4brwsr_StringSample_hashCodeILjava_lang_String",
7.114 + StringSample.class, "hashCodeILjava_lang_String",
7.115 Double.valueOf(jh), j
7.116 );
7.117 }
7.118 @Test public void stringSwitch1() throws Exception {
7.119 assertExec(
7.120 "Get one",
7.121 - "org_apidesign_vm4brwsr_StringSample_stringSwitchILjava_lang_String",
7.122 + StringSample.class, "stringSwitchILjava_lang_String",
7.123 Double.valueOf(1), "jedna"
7.124 );
7.125 }
7.126 @Test public void stringSwitch2() throws Exception {
7.127 assertExec(
7.128 "Get two",
7.129 - "org_apidesign_vm4brwsr_StringSample_stringSwitchILjava_lang_String",
7.130 + StringSample.class, "stringSwitchILjava_lang_String",
7.131 Double.valueOf(2), "dve"
7.132 );
7.133 }
7.134 @Test public void stringSwitchDefault() throws Exception {
7.135 assertExec(
7.136 "Get -1",
7.137 - "org_apidesign_vm4brwsr_StringSample_stringSwitchILjava_lang_String",
7.138 + StringSample.class, "stringSwitchILjava_lang_String",
7.139 Double.valueOf(-1), "none"
7.140 );
7.141 }
7.142 @@ -161,7 +161,7 @@
7.143 assertEquals(StringSample.countAB("Ahoj Bedo!"), 3, "Verify Java code is sane");
7.144 assertExec(
7.145 "One A and one B adds to 3",
7.146 - "org_apidesign_vm4brwsr_StringSample_countABILjava_lang_String",
7.147 + StringSample.class, "countABILjava_lang_String",
7.148 Double.valueOf(3), "Ahoj Bedo!"
7.149 );
7.150
7.151 @@ -180,23 +180,10 @@
7.152 codeSeq = sb;
7.153 }
7.154
7.155 - private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
7.156 - Object ret = null;
7.157 - try {
7.158 - ret = code.invokeFunction(methodName, args);
7.159 - } catch (ScriptException ex) {
7.160 - fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
7.161 - } catch (NoSuchMethodException ex) {
7.162 - fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
7.163 - }
7.164 - if (ret == null && expRes == null) {
7.165 - return;
7.166 - }
7.167 - if (expRes.equals(ret)) {
7.168 - return;
7.169 - }
7.170 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
7.171 -
7.172 + private static void assertExec(String msg,
7.173 + Class<?> clazz, String method, Object expRes, Object... args
7.174 + ) throws Exception {
7.175 + StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
7.176 }
7.177
7.178 }
8.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Sun Nov 25 21:31:51 2012 +0100
8.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Sun Nov 25 23:14:58 2012 +0100
8.3 @@ -41,10 +41,8 @@
8.4
8.5 Object ret;
8.6 try {
8.7 - ret = code.invokeFunction(
8.8 - "org_apidesign_vm4brwsr_VMinVM_toJavaScriptLjava_lang_StringAB",
8.9 - arr
8.10 - );
8.11 + ret = code.invokeFunction(VMinVM.class.getName().replace('.', '_') + "_proto");
8.12 + ret = code.invokeMethod(ret, "toJavaScriptLjava_lang_StringAB", arr);
8.13 } catch (Exception ex) {
8.14 File f = File.createTempFile("execution", ".js");
8.15 FileWriter w = new FileWriter(f);