vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
brancharithmetic
changeset 755 5652acd48509
parent 737 b2731af0357d
parent 747 ae352b763959
child 771 4252bfc396fc
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Fri Feb 15 16:06:12 2013 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Feb 25 19:00:08 2013 +0100
     1.3 @@ -113,7 +113,7 @@
     1.4          final String className = className(jc);
     1.5          out.append("\n\n").append(assignClass(className));
     1.6          out.append("function CLS() {");
     1.7 -        out.append("\n  if (!CLS.prototype.$instOf_").append(className).append(") {");
     1.8 +        out.append("\n  if (!CLS.$class) {");
     1.9          if (proto == null) {
    1.10              String sc = jc.getSuperClassName(); // with _
    1.11              out.append("\n    var pp = ").
    1.12 @@ -175,12 +175,14 @@
    1.13                  out.append("\n    };");
    1.14              }
    1.15              out.append(prefix).append(mn).append(".access = " + m.getAccess()).append(";");
    1.16 +            out.append(prefix).append(mn).append(".cls = CLS;");
    1.17          }
    1.18          out.append("\n    c.constructor = CLS;");
    1.19          out.append("\n    c.$instOf_").append(className).append(" = true;");
    1.20          for (String superInterface : jc.getSuperInterfaces()) {
    1.21              out.append("\n    c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
    1.22          }
    1.23 +        out.append("\n    CLS.$class = 'temp';");
    1.24          out.append("\n    CLS.$class = ");
    1.25          out.append(accessClass("java_lang_Class(true);"));
    1.26          out.append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
    1.27 @@ -1620,7 +1622,7 @@
    1.28          final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
    1.29          class P extends AnnotationParser {
    1.30              public P() {
    1.31 -                super(false);
    1.32 +                super(false, true);
    1.33              }
    1.34              
    1.35              int cnt;
    1.36 @@ -1676,7 +1678,7 @@
    1.37          final String[] values = new String[attrNames.length];
    1.38          final boolean[] found = { false };
    1.39          final String jvmType = "L" + className.replace('.', '/') + ";";
    1.40 -        AnnotationParser ap = new AnnotationParser(false) {
    1.41 +        AnnotationParser ap = new AnnotationParser(false, true) {
    1.42              @Override
    1.43              protected void visitAttr(String type, String attr, String at, String value) {
    1.44                  if (type.equals(jvmType)) {
    1.45 @@ -1713,35 +1715,71 @@
    1.46          return " = null;";
    1.47      }
    1.48  
    1.49 -    private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
    1.50 -        AnnotationParser ap = new AnnotationParser(true) {
    1.51 -            int anno;
    1.52 -            int cnt;
    1.53 +    private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
    1.54 +        AnnotationParser ap = new AnnotationParser(true, false) {
    1.55 +            int[] cnt = new int[32];
    1.56 +            int depth;
    1.57              
    1.58              @Override
    1.59 -            protected void visitAnnotationStart(String type) throws IOException {
    1.60 -                if (anno++ > 0) {
    1.61 +            protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
    1.62 +                final String slashType = attrType.substring(1, attrType.length() - 1);
    1.63 +                requireReference(slashType);
    1.64 +                
    1.65 +                if (cnt[depth]++ > 0) {
    1.66                      out.append(",");
    1.67                  }
    1.68 -                out.append('"').append(type).append("\" : {\n");
    1.69 -                cnt = 0;
    1.70 +                if (top) {
    1.71 +                    out.append('"').append(attrType).append("\" : ");
    1.72 +                }
    1.73 +                out.append("{\n");
    1.74 +                cnt[++depth] = 0;
    1.75              }
    1.76  
    1.77              @Override
    1.78 -            protected void visitAnnotationEnd(String type) throws IOException {
    1.79 +            protected void visitAnnotationEnd(String type, boolean top) throws IOException {
    1.80                  out.append("\n}\n");
    1.81 +                depth--;
    1.82 +            }
    1.83 +
    1.84 +            @Override
    1.85 +            protected void visitValueStart(String attrName, char type) throws IOException {
    1.86 +                if (cnt[depth]++ > 0) {
    1.87 +                    out.append(",\n");
    1.88 +                }
    1.89 +                cnt[++depth] = 0;
    1.90 +                if (attrName != null) {
    1.91 +                    out.append(attrName).append(" : ");
    1.92 +                }
    1.93 +                if (type == '[') {
    1.94 +                    out.append("[");
    1.95 +                }
    1.96 +            }
    1.97 +
    1.98 +            @Override
    1.99 +            protected void visitValueEnd(String attrName, char type) throws IOException {
   1.100 +                if (type == '[') {
   1.101 +                    out.append("]");
   1.102 +                }
   1.103 +                depth--;
   1.104              }
   1.105              
   1.106              @Override
   1.107              protected void visitAttr(String type, String attr, String attrType, String value) 
   1.108              throws IOException {
   1.109 -                if (attr == null) {
   1.110 +                if (attr == null && value == null) {
   1.111                      return;
   1.112                  }
   1.113 -                if (cnt++ > 0) {
   1.114 -                    out.append(",\n");
   1.115 -                }
   1.116 -                out.append(attr).append("__").append(attrType).append(" : ").append(value);
   1.117 +                out.append(value);
   1.118 +            }
   1.119 +
   1.120 +            @Override
   1.121 +            protected void visitEnumAttr(String type, String attr, String attrType, String value) 
   1.122 +            throws IOException {
   1.123 +                final String slashType = attrType.substring(1, attrType.length() - 1);
   1.124 +                requireReference(slashType);
   1.125 +                
   1.126 +                out.append(accessClass(slashType.replace('/', '_')))
   1.127 +                   .append("(false).constructor.").append(value);
   1.128              }
   1.129          };
   1.130          ap.parse(data, cd);