1.1 --- a/vm/pom.xml Fri Sep 28 07:43:53 2012 +0200
1.2 +++ b/vm/pom.xml Fri Sep 28 13:01:17 2012 +0200
1.3 @@ -50,6 +50,7 @@
1.4 <plugin>
1.5 <groupId>org.apache.maven.plugins</groupId>
1.6 <artifactId>maven-jar-plugin</artifactId>
1.7 + <version>2.4</version>
1.8 <configuration>
1.9 <archive>
1.10 <manifest>
2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri Sep 28 07:43:53 2012 +0200
2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri Sep 28 13:01:17 2012 +0200
2.3 @@ -90,25 +90,25 @@
2.4 final String className = jc.getName().getInternalName().replace('/', '_');
2.5 out.append("\nfunction ").append(className);
2.6 out.append("() {");
2.7 - for (Method m : jc.getMethods()) {
2.8 - if (!m.isStatic()) {
2.9 - compiler.generateMethodReference(m);
2.10 - }
2.11 - }
2.12 for (Variable v : jc.getVariables()) {
2.13 if (!v.isStatic()) {
2.14 out.append("\n this." + v.getName() + " = 0;");
2.15 }
2.16 }
2.17 - out.append("\n this.$instOf_").append(className).append(" = true;");
2.18 - for (ClassName superInterface : jc.getInterfaces()) {
2.19 - out.append("\n this.$instOf_").append(superInterface.getInternalName().replace('/', '_')).append(" = true;");
2.20 - }
2.21 out.append("\n}");
2.22 ClassName sc = jc.getSuperClass();
2.23 if (sc != null) {
2.24 out.append("\n").append(className)
2.25 - .append(".prototype = new ").append(sc.getInternalName().replace('/', '_'));
2.26 + .append(".prototype = new ").append(sc.getInternalName().replace('/', '_')).append(';');
2.27 + }
2.28 + for (Method m : jc.getMethods()) {
2.29 + if (!m.isStatic() && !m.isPrivate() && !m.getName().contains("<init>")) {
2.30 + compiler.generateMethodReference("\n" + className + ".prototype.", m);
2.31 + }
2.32 + }
2.33 + out.append("\n" + className + ".prototype.$instOf_").append(className).append(" = true;");
2.34 + for (ClassName superInterface : jc.getInterfaces()) {
2.35 + out.append("\n" + className + ".prototype.$instOf_").append(superInterface.getInternalName().replace('/', '_')).append(" = true;");
2.36 }
2.37 for (String init : toInitilize) {
2.38 out.append("\n").append(init).append("();");
2.39 @@ -152,9 +152,9 @@
2.40 out.append("}");
2.41 }
2.42
2.43 - private void generateMethodReference(Method m) throws IOException {
2.44 + private void generateMethodReference(String prefix, Method m) throws IOException {
2.45 final String name = findMethodName(m);
2.46 - out.append("\n this.").append(name).append(" = ")
2.47 + out.append(prefix).append(name).append(" = ")
2.48 .append(jc.getName().getInternalName().replace('/', '_'))
2.49 .append('_').append(name).append(";");
2.50 }
2.51 @@ -588,9 +588,13 @@
2.52 case bc_checkcast: {
2.53 int indx = readIntArg(byteCodes, i);
2.54 CPClassInfo ci = jc.getConstantPool().getClass(indx);
2.55 - out.append("if(stack[stack.length - 1].$instOf_")
2.56 - .append(ci.getClassName().getInternalName().replace('/', '_'))
2.57 - .append(" != 1) throw {};"); // XXX proper exception
2.58 + final String type = ci.getClassName().getType();
2.59 + if (!type.startsWith("[")) {
2.60 + // no way to check arrays right now
2.61 + out.append("if(stack[stack.length - 1].$instOf_")
2.62 + .append(type.replace('/', '_'))
2.63 + .append(" != 1) throw {};"); // XXX proper exception
2.64 + }
2.65 i += 2;
2.66 break;
2.67 }
2.68 @@ -605,13 +609,13 @@
2.69 }
2.70
2.71 }
2.72 - out.append(" /*");
2.73 + out.append(" //");
2.74 for (int j = prev; j <= i; j++) {
2.75 out.append(" ");
2.76 final int cc = (byteCodes[j] + 256) % 256;
2.77 out.append(Integer.toString(cc));
2.78 }
2.79 - out.append("*/\n");
2.80 + out.append("\n");
2.81 }
2.82 out.append(" }\n");
2.83 }
2.84 @@ -710,24 +714,24 @@
2.85 }
2.86
2.87 private String findMethodName(Method m) {
2.88 - StringBuilder tmp = new StringBuilder();
2.89 + StringBuilder name = new StringBuilder();
2.90 + String descr = m.getDescriptor();
2.91 if ("<init>".equals(m.getName())) { // NOI18N
2.92 - tmp.append("consV"); // NOI18N
2.93 + name.append("cons"); // NOI18N
2.94 } else if ("<clinit>".equals(m.getName())) { // NOI18N
2.95 - tmp.append("classV"); // NOI18N
2.96 + name.append("class"); // NOI18N
2.97 } else {
2.98 - tmp.append(m.getName());
2.99 - outType(m.getReturnType(), tmp);
2.100 + name.append(m.getName());
2.101 }
2.102 - List<Parameter> args = m.getParameters();
2.103 - for (Parameter t : args) {
2.104 - outType(t.getDescriptor(), tmp);
2.105 - }
2.106 - return tmp.toString();
2.107 +
2.108 + boolean hasReturn[] = { false };
2.109 + countArgs(findDescriptor(m.getDescriptor()), hasReturn, name);
2.110 + return name.toString();
2.111 }
2.112
2.113 private String findMethodName(CPMethodInfo mi, int[] cnt, boolean[] hasReturn) {
2.114 StringBuilder name = new StringBuilder();
2.115 + String descr = mi.getDescriptor();
2.116 if ("<init>".equals(mi.getName())) { // NOI18N
2.117 name.append("cons"); // NOI18N
2.118 } else {