src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java
changeset 12 282828609b86
parent 11 eca88b77b986
child 13 99f832e5765f
     1.1 --- a/src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java	Wed Sep 19 18:53:16 2012 +0200
     1.2 +++ b/src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java	Wed Sep 19 19:29:05 2012 +0200
     1.3 @@ -71,7 +71,7 @@
     1.4          out.append("() {");
     1.5          for (Method m : jc.getMethods()) {
     1.6              if (!m.isStatic()) {
     1.7 -                
     1.8 +                compiler.generateMethodReference(m);
     1.9              }
    1.10          }
    1.11          for (Variable v : jc.getVariables()) {
    1.12 @@ -113,6 +113,13 @@
    1.13          out.append("}");
    1.14      }
    1.15      
    1.16 +    private void generateMethodReference(Method m) throws IOException {
    1.17 +        final String name = findMethodName(m);
    1.18 +        out.append("\n  this.").append(name).append(" = ")
    1.19 +           .append(jc.getName().getExternalName().replace('.', '_'))
    1.20 +           .append('_').append(name).append(";");
    1.21 +    }
    1.22 +    
    1.23      private void generateInstanceMethod(Method m) throws IOException {
    1.24          out.append("\nfunction ").append(
    1.25              jc.getName().getExternalName().replace('.', '_')
    1.26 @@ -361,6 +368,9 @@
    1.27                      i += 2;
    1.28                      break;
    1.29                  }
    1.30 +                case bc_invokevirtual:
    1.31 +                    i = invokeVirtualMethod(byteCodes, i);
    1.32 +                    break;
    1.33                  case bc_invokespecial:
    1.34                      i = invokeStaticMethod(byteCodes, i, false);
    1.35                      break;
    1.36 @@ -561,4 +571,35 @@
    1.37          i += 2;
    1.38          return i;
    1.39      }
    1.40 +    private int invokeVirtualMethod(byte[] byteCodes, int i)
    1.41 +    throws IOException {
    1.42 +        int methodIndex = readIntArg(byteCodes, i);
    1.43 +        CPMethodInfo mi = (CPMethodInfo) jc.getConstantPool().get(methodIndex);
    1.44 +        boolean[] hasReturn = { false };
    1.45 +        int[] cnt = { 0 };
    1.46 +        String mn = findMethodName(mi, cnt, hasReturn);
    1.47 +        out.append("{ ");
    1.48 +        for (int j = cnt[0] - 1; j >= 0; j--) {
    1.49 +            out.append("var v" + j).append(" = stack.pop(); ");
    1.50 +        }
    1.51 +        out.append("var self = stack.pop(); ");
    1.52 +        if (hasReturn[0]) {
    1.53 +            out.append("stack.push(");
    1.54 +        }
    1.55 +        out.append("self.");
    1.56 +        out.append(mn);
    1.57 +        out.append('(');
    1.58 +        out.append("self");
    1.59 +        for (int j = 0; j < cnt[0]; j++) {
    1.60 +            out.append(", ");
    1.61 +            out.append("v" + j);
    1.62 +        }
    1.63 +        out.append(")");
    1.64 +        if (hasReturn[0]) {
    1.65 +            out.append(")");
    1.66 +        }
    1.67 +        out.append("; }");
    1.68 +        i += 2;
    1.69 +        return i;
    1.70 +    }
    1.71  }