rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
changeset 1889 e1953d8b8338
parent 1878 126d266b2da9
child 1898 cf6d5d357696
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sat Jan 30 18:49:26 2016 +0100
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sat Mar 19 10:31:13 2016 +0100
     1.3 @@ -288,7 +288,8 @@
     1.4              byte[] runAnno = m.findAnnotationData(false);
     1.5              if (runAnno != null) {
     1.6                  append("\n    m.anno = {");
     1.7 -                generateAnno(jc, runAnno);
     1.8 +                AnnotationParser ap = new GenerateAnno(true, false);
     1.9 +                ap.parse(runAnno, jc);
    1.10                  append("\n    };");
    1.11              }
    1.12              append("\n    m.access = " + m.getAccess()).append(";");
    1.13 @@ -343,7 +344,8 @@
    1.14          byte[] classAnno = jc.findAnnotationData(false);
    1.15          if (classAnno != null) {
    1.16              append("\n    CLS.$class.anno = {");
    1.17 -            generateAnno(jc, classAnno);
    1.18 +            AnnotationParser ap = new GenerateAnno(true, false);
    1.19 +            ap.parse(classAnno, jc);
    1.20              append("\n    };");
    1.21          }
    1.22          for (String init : toInitilize.toArray()) {
    1.23 @@ -453,10 +455,18 @@
    1.24  
    1.25          final byte[] byteCodes = m.getCode();
    1.26          if (byteCodes == null) {
    1.27 -            if (debug("  throw 'no code found for ")) {
    1.28 -               this
    1.29 -               .append(jc.getClassName()).append('.')
    1.30 -               .append(m.getName()).append("';\n");
    1.31 +            byte[] defaultAttr = m.getDefaultAttribute();
    1.32 +            if (defaultAttr != null) {
    1.33 +                append("  return ");
    1.34 +                AnnotationParser ap = new GenerateAnno(true, false);
    1.35 +                ap.parseDefault(defaultAttr, jc);
    1.36 +                append(";\n");
    1.37 +            } else {
    1.38 +                if (debug("  throw 'no code found for ")) {
    1.39 +                   this
    1.40 +                   .append(jc.getClassName()).append('.')
    1.41 +                   .append(m.getName()).append("';\n");
    1.42 +                }
    1.43              }
    1.44              if (defineProp) {
    1.45                  append("}});");
    1.46 @@ -2156,81 +2166,6 @@
    1.47          return " = null;";
    1.48      }
    1.49  
    1.50 -    private void generateAnno(ClassData cd, byte[] data) throws IOException {
    1.51 -        AnnotationParser ap = new AnnotationParser(true, false) {
    1.52 -            int[] cnt = new int[32];
    1.53 -            int depth;
    1.54 -            
    1.55 -            @Override
    1.56 -            protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
    1.57 -                final String slashType = attrType.substring(1, attrType.length() - 1);
    1.58 -                requireReference(slashType);
    1.59 -                
    1.60 -                if (cnt[depth]++ > 0) {
    1.61 -                    append(",");
    1.62 -                }
    1.63 -                if (top) {
    1.64 -                    append('"').append(attrType).append("\" : ");
    1.65 -                }
    1.66 -                append("{\n");
    1.67 -                cnt[++depth] = 0;
    1.68 -            }
    1.69 -
    1.70 -            @Override
    1.71 -            protected void visitAnnotationEnd(String type, boolean top) throws IOException {
    1.72 -                append("\n}\n");
    1.73 -                depth--;
    1.74 -            }
    1.75 -
    1.76 -            @Override
    1.77 -            protected void visitValueStart(String attrName, char type) throws IOException {
    1.78 -                if (cnt[depth]++ > 0) {
    1.79 -                    append(",\n");
    1.80 -                }
    1.81 -                cnt[++depth] = 0;
    1.82 -                if (attrName != null) {
    1.83 -                    append('"').append(attrName).append("\" : ");
    1.84 -                }
    1.85 -                if (type == '[') {
    1.86 -                    append("[");
    1.87 -                }
    1.88 -            }
    1.89 -
    1.90 -            @Override
    1.91 -            protected void visitValueEnd(String attrName, char type) throws IOException {
    1.92 -                if (type == '[') {
    1.93 -                    append("]");
    1.94 -                }
    1.95 -                depth--;
    1.96 -            }
    1.97 -            
    1.98 -            @Override
    1.99 -            protected void visitAttr(String type, String attr, String attrType, String value) 
   1.100 -            throws IOException {
   1.101 -                if (attr == null && value == null) {
   1.102 -                    return;
   1.103 -                }
   1.104 -                append(value);
   1.105 -            }
   1.106 -
   1.107 -            @Override
   1.108 -            protected void visitEnumAttr(String type, String attr, String attrType, String value) 
   1.109 -            throws IOException {
   1.110 -                final String slashType = attrType.substring(1, attrType.length() - 1);
   1.111 -                requireReference(slashType);
   1.112 -                
   1.113 -                final String cn = mangleClassName(slashType);
   1.114 -                append(accessClassFalse(cn))
   1.115 -                   .append("['valueOf__L").
   1.116 -                    append(cn).
   1.117 -                    append("_2Ljava_lang_String_2']('").
   1.118 -                    append(value).
   1.119 -                    append("')");
   1.120 -            }
   1.121 -        };
   1.122 -        ap.parse(data, cd);
   1.123 -    }
   1.124 -
   1.125      private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
   1.126          final String name = args[indx];
   1.127          if (name == null) {
   1.128 @@ -2505,4 +2440,88 @@
   1.129      private static void println(String msg) {
   1.130          System.err.println(msg);
   1.131      }
   1.132 +
   1.133 +    private class GenerateAnno extends AnnotationParser {
   1.134 +        public GenerateAnno(boolean textual, boolean iterateArray) {
   1.135 +            super(textual, iterateArray);
   1.136 +        }
   1.137 +        int[] cnt = new int[32];
   1.138 +        int depth;
   1.139 +
   1.140 +        @Override
   1.141 +        protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
   1.142 +            final String slashType = attrType.substring(1, attrType.length() - 1);
   1.143 +            requireReference(slashType);
   1.144 +
   1.145 +            if (cnt[depth]++ > 0) {
   1.146 +                append(",");
   1.147 +            }
   1.148 +            if (top) {
   1.149 +                append('"').append(attrType).append("\" : ");
   1.150 +            }
   1.151 +            append("{\n");
   1.152 +            cnt[++depth] = 0;
   1.153 +        }
   1.154 +
   1.155 +        @Override
   1.156 +        protected void visitAnnotationEnd(String type, boolean top) throws IOException {
   1.157 +            append("\n}\n");
   1.158 +            depth--;
   1.159 +        }
   1.160 +
   1.161 +        @Override
   1.162 +        protected void visitValueStart(String attrName, char type) throws IOException {
   1.163 +            if (cnt[depth]++ > 0) {
   1.164 +                append(",\n");
   1.165 +            }
   1.166 +            cnt[++depth] = 0;
   1.167 +            if (attrName != null) {
   1.168 +                append('"').append(attrName).append("\" : ");
   1.169 +            }
   1.170 +            if (type == '[') {
   1.171 +                append("[");
   1.172 +            }
   1.173 +        }
   1.174 +
   1.175 +        @Override
   1.176 +        protected void visitValueEnd(String attrName, char type) throws IOException {
   1.177 +            if (type == '[') {
   1.178 +                append("]");
   1.179 +            }
   1.180 +            depth--;
   1.181 +        }
   1.182 +
   1.183 +        @Override
   1.184 +        protected void visitAttr(String type, String attr, String attrType, String value)
   1.185 +            throws IOException {
   1.186 +            if (attr == null && value == null) {
   1.187 +                return;
   1.188 +            }
   1.189 +            append(value);
   1.190 +        }
   1.191 +
   1.192 +        @Override
   1.193 +        protected void visitEnumAttr(String type, String attr, String attrType, String value)
   1.194 +            throws IOException {
   1.195 +            final String slashType = attrType.substring(1, attrType.length() - 1);
   1.196 +            requireReference(slashType);
   1.197 +
   1.198 +            final String cn = mangleClassName(slashType);
   1.199 +            append(accessClassFalse(cn))
   1.200 +                .append("['valueOf__L").
   1.201 +                append(cn).
   1.202 +                append("_2Ljava_lang_String_2']('").
   1.203 +                append(value).
   1.204 +                append("')");
   1.205 +        }
   1.206 +
   1.207 +        @Override
   1.208 +        protected void visitClassAttr(String annoType, String attr, String className) throws IOException {
   1.209 +            final String slashType = className.substring(1, className.length() - 1);
   1.210 +            requireReference(slashType);
   1.211 +
   1.212 +            final String cn = mangleClassName(slashType);
   1.213 +            append(accessClassFalse(cn)).append(".constructor.$class");
   1.214 +        }
   1.215 +    }
   1.216  }