vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
branchlazy
changeset 203 c6a0b5b64133
parent 200 227bafe6ef52
child 204 590a8c98df30
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Nov 25 21:23:06 2012 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Nov 25 23:14:58 2012 +0100
     1.3 @@ -19,7 +19,6 @@
     1.4  
     1.5  import java.io.IOException;
     1.6  import java.io.InputStream;
     1.7 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.8  import org.apidesign.javap.AnnotationParser;
     1.9  import org.apidesign.javap.ClassData;
    1.10  import org.apidesign.javap.FieldData;
    1.11 @@ -74,13 +73,6 @@
    1.12              }
    1.13          }
    1.14          StringArray toInitilize = new StringArray();
    1.15 -        for (MethodData m : jc.getMethods()) {
    1.16 -            if (m.isStatic()) {
    1.17 -                generateStaticMethod(m, toInitilize);
    1.18 -            } else {
    1.19 -                generateInstanceMethod(m);
    1.20 -            }
    1.21 -        }
    1.22          final String className = className(jc);
    1.23          out.append("\nfunction ").append(className);
    1.24          out.append("() {");
    1.25 @@ -90,7 +82,8 @@
    1.26                      append(v.getName()).append(initField(v));
    1.27              }
    1.28          }
    1.29 -        out.append("\n}\n\nfunction ").append(className).append("_proto() {");
    1.30 +        out.append("\n}");
    1.31 +        out.append("\n\nfunction ").append(className).append("_proto() {");
    1.32          out.append("\n  if (").append(className).
    1.33              append(".prototype.$instOf_").append(className).append(") {");
    1.34          out.append("\n    return new ").append(className).append(";");
    1.35 @@ -110,8 +103,10 @@
    1.36              out.append("\n  var p = ").append(className).append(".prototype;");
    1.37          }
    1.38          for (MethodData m : jc.getMethods()) {
    1.39 -            if (!m.getName().contains("<cinit>")) {
    1.40 -                generateMethodReference("\n  p.", m);
    1.41 +            if (m.isStatic()) {
    1.42 +                generateStaticMethod("\n  p.", m, toInitilize);
    1.43 +            } else {
    1.44 +                generateInstanceMethod("\n  p.", m);
    1.45              }
    1.46          }
    1.47          out.append("\n  p.$instOf_").append(className).append(" = true;");
    1.48 @@ -127,17 +122,15 @@
    1.49          }
    1.50          return sb.toString();
    1.51      }
    1.52 -    private void generateStaticMethod(MethodData m, StringArray toInitilize) throws IOException {
    1.53 -        if (javaScriptBody(m, true)) {
    1.54 +    private void generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
    1.55 +        if (javaScriptBody(prefix, m, true)) {
    1.56              return;
    1.57          }
    1.58          StringBuilder argsCnt = new StringBuilder();
    1.59          final String mn = findMethodName(m, argsCnt);
    1.60 -        out.append("\nfunction ").append(
    1.61 -            className(jc)
    1.62 -        ).append('_').append(mn);
    1.63 +        out.append(prefix).append(mn).append(" = function");
    1.64          if (mn.equals("classV")) {
    1.65 -            toInitilize.add(className(jc) + '_' + mn);
    1.66 +            toInitilize.add(className(jc) + "_proto()." + mn);
    1.67          }
    1.68          out.append('(');
    1.69          String space = "";
    1.70 @@ -165,24 +158,16 @@
    1.71          } else {
    1.72              out.append("  /* no code found for ").append(m.getInternalSig()).append(" */\n");
    1.73          }
    1.74 -        out.append("}");
    1.75 +        out.append("};");
    1.76      }
    1.77      
    1.78 -    private void generateMethodReference(String prefix, MethodData m) throws IOException {
    1.79 -        final String name = findMethodName(m, new StringBuilder());
    1.80 -        out.append(prefix).append(name).append(" = ")
    1.81 -           .append(className(jc))
    1.82 -           .append('_').append(name).append(";");
    1.83 -    }
    1.84 -    
    1.85 -    private void generateInstanceMethod(MethodData m) throws IOException {
    1.86 -        if (javaScriptBody(m, false)) {
    1.87 +    private void generateInstanceMethod(String prefix, MethodData m) throws IOException {
    1.88 +        if (javaScriptBody(prefix, m, false)) {
    1.89              return;
    1.90          }
    1.91          StringBuilder argsCnt = new StringBuilder();
    1.92 -        out.append("\nfunction ").append(
    1.93 -            className(jc)
    1.94 -        ).append('_').append(findMethodName(m, argsCnt));
    1.95 +        final String mn = findMethodName(m, argsCnt);
    1.96 +        out.append(prefix).append(mn).append(" = function");
    1.97          out.append("(arg0");
    1.98          String space = ",";
    1.99          for (int index = 1, i = 0; i < argsCnt.length(); i++) {
   1.100 @@ -207,7 +192,7 @@
   1.101          } else {
   1.102              out.append("  /* no code found for ").append(m.getInternalSig()).append(" */\n");
   1.103          }
   1.104 -        out.append("}");
   1.105 +        out.append("};");
   1.106      }
   1.107  
   1.108      private void produceCode(byte[] byteCodes) throws IOException {
   1.109 @@ -980,7 +965,7 @@
   1.110          return d.replace('[', 'A');
   1.111      }
   1.112  
   1.113 -    private boolean javaScriptBody(MethodData m, boolean isStatic) throws IOException {
   1.114 +    private boolean javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
   1.115          byte[] arr = m.findAnnotationData(true);
   1.116          if (arr == null) {
   1.117              return false;
   1.118 @@ -1010,9 +995,8 @@
   1.119              return false;
   1.120          }
   1.121          StringBuilder cnt = new StringBuilder();
   1.122 -        out.append("\nfunction ").append(className(jc)).append('_').
   1.123 -            append(findMethodName(m, cnt));
   1.124 -        out.append("(");
   1.125 +        out.append(prefix).append(findMethodName(m, cnt));
   1.126 +        out.append(" = function(");
   1.127          String space;
   1.128          int index;
   1.129          if (!isStatic) {