Support for invoking virtual methods
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Sep 2012 19:29:05 +0200
changeset 12282828609b86
parent 11 eca88b77b986
child 13 99f832e5765f
Support for invoking virtual methods
nb-configuration.xml
src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java
src/test/java/org/apidesign/java4browser/Instance.java
src/test/java/org/apidesign/java4browser/InstanceTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nb-configuration.xml	Wed Sep 19 19:29:05 2012 +0200
     1.3 @@ -0,0 +1,18 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project-shared-configuration>
     1.6 +    <!--
     1.7 +This file contains additional configuration written by modules in the NetBeans IDE.
     1.8 +The configuration is intended to be shared among all the users of project and
     1.9 +therefore it is assumed to be part of version control checkout.
    1.10 +Without this configuration present, some functionality in the IDE may be limited or fail altogether.
    1.11 +-->
    1.12 +    <properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
    1.13 +        <!--
    1.14 +Properties that influence various parts of the IDE, especially code formatting and the like. 
    1.15 +You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
    1.16 +That way multiple projects can share the same settings (useful for formatting rules for example).
    1.17 +Any value defined here will override the pom.xml file value but is only applicable to the current project.
    1.18 +-->
    1.19 +        <netbeans.compile.on.save>all</netbeans.compile.on.save>
    1.20 +    </properties>
    1.21 +</project-shared-configuration>
     2.1 --- a/src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java	Wed Sep 19 18:53:16 2012 +0200
     2.2 +++ b/src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java	Wed Sep 19 19:29:05 2012 +0200
     2.3 @@ -71,7 +71,7 @@
     2.4          out.append("() {");
     2.5          for (Method m : jc.getMethods()) {
     2.6              if (!m.isStatic()) {
     2.7 -                
     2.8 +                compiler.generateMethodReference(m);
     2.9              }
    2.10          }
    2.11          for (Variable v : jc.getVariables()) {
    2.12 @@ -113,6 +113,13 @@
    2.13          out.append("}");
    2.14      }
    2.15      
    2.16 +    private void generateMethodReference(Method m) throws IOException {
    2.17 +        final String name = findMethodName(m);
    2.18 +        out.append("\n  this.").append(name).append(" = ")
    2.19 +           .append(jc.getName().getExternalName().replace('.', '_'))
    2.20 +           .append('_').append(name).append(";");
    2.21 +    }
    2.22 +    
    2.23      private void generateInstanceMethod(Method m) throws IOException {
    2.24          out.append("\nfunction ").append(
    2.25              jc.getName().getExternalName().replace('.', '_')
    2.26 @@ -361,6 +368,9 @@
    2.27                      i += 2;
    2.28                      break;
    2.29                  }
    2.30 +                case bc_invokevirtual:
    2.31 +                    i = invokeVirtualMethod(byteCodes, i);
    2.32 +                    break;
    2.33                  case bc_invokespecial:
    2.34                      i = invokeStaticMethod(byteCodes, i, false);
    2.35                      break;
    2.36 @@ -561,4 +571,35 @@
    2.37          i += 2;
    2.38          return i;
    2.39      }
    2.40 +    private int invokeVirtualMethod(byte[] byteCodes, int i)
    2.41 +    throws IOException {
    2.42 +        int methodIndex = readIntArg(byteCodes, i);
    2.43 +        CPMethodInfo mi = (CPMethodInfo) jc.getConstantPool().get(methodIndex);
    2.44 +        boolean[] hasReturn = { false };
    2.45 +        int[] cnt = { 0 };
    2.46 +        String mn = findMethodName(mi, cnt, hasReturn);
    2.47 +        out.append("{ ");
    2.48 +        for (int j = cnt[0] - 1; j >= 0; j--) {
    2.49 +            out.append("var v" + j).append(" = stack.pop(); ");
    2.50 +        }
    2.51 +        out.append("var self = stack.pop(); ");
    2.52 +        if (hasReturn[0]) {
    2.53 +            out.append("stack.push(");
    2.54 +        }
    2.55 +        out.append("self.");
    2.56 +        out.append(mn);
    2.57 +        out.append('(');
    2.58 +        out.append("self");
    2.59 +        for (int j = 0; j < cnt[0]; j++) {
    2.60 +            out.append(", ");
    2.61 +            out.append("v" + j);
    2.62 +        }
    2.63 +        out.append(")");
    2.64 +        if (hasReturn[0]) {
    2.65 +            out.append(")");
    2.66 +        }
    2.67 +        out.append("; }");
    2.68 +        i += 2;
    2.69 +        return i;
    2.70 +    }
    2.71  }
     3.1 --- a/src/test/java/org/apidesign/java4browser/Instance.java	Wed Sep 19 18:53:16 2012 +0200
     3.2 +++ b/src/test/java/org/apidesign/java4browser/Instance.java	Wed Sep 19 19:29:05 2012 +0200
     3.3 @@ -42,4 +42,9 @@
     3.4          i.b = (byte)0x09;
     3.5          return (i.i - i.b) * i.d;
     3.6      }
     3.7 +    public static byte virtualBytes() {
     3.8 +        Instance i = new Instance();
     3.9 +        i.setByte((byte)0x0a);
    3.10 +        return i.getByte();
    3.11 +    }
    3.12  }
     4.1 --- a/src/test/java/org/apidesign/java4browser/InstanceTest.java	Wed Sep 19 18:53:16 2012 +0200
     4.2 +++ b/src/test/java/org/apidesign/java4browser/InstanceTest.java	Wed Sep 19 19:29:05 2012 +0200
     4.3 @@ -35,6 +35,13 @@
     4.4              Double.valueOf(3.3)
     4.5          );
     4.6      }
     4.7 +    @Test public void verifyInstanceMethods() throws Exception {
     4.8 +        assertExec(
     4.9 +            "Should be ten",
    4.10 +            "org_apidesign_java4browser_Instance_virtualBytesB",
    4.11 +            Double.valueOf(10)
    4.12 +        );
    4.13 +    }
    4.14      
    4.15      private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    4.16          StringBuilder sb = new StringBuilder();