vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 10 Dec 2012 12:03:22 +0100
changeset 297 a20721a10717
parent 250 42c2ceb1e160
child 274 81f6e7778135
permissions -rw-r--r--
Cache the compiled code per class, not globally
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.net.URL;
    23 import java.util.Enumeration;
    24 
    25 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 class GenJS extends ByteCodeToJavaScript {
    30     public GenJS(Appendable out) {
    31         super(out);
    32     }
    33     
    34     static void compile(Appendable out, String... names) throws IOException {
    35         compile(out, StringArray.asList(names));
    36     }
    37     static void compile(ClassLoader l, Appendable out, String... names) throws IOException {
    38         compile(l, out, StringArray.asList(names));
    39     }
    40     static void compile(Appendable out, StringArray names) throws IOException {
    41         compile(GenJS.class.getClassLoader(), out, names);
    42     }
    43     static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
    44         new GenJS(out).doCompile(l, names);
    45     }
    46     protected void doCompile(ClassLoader l, StringArray names) throws IOException {
    47         StringArray processed = new StringArray();
    48         StringArray initCode = new StringArray();
    49         for (String baseClass : names.toArray()) {
    50             references.add(baseClass);
    51             for (;;) {
    52                 String name = null;
    53                 for (String n : references.toArray()) {
    54                     if (processed.contains(n)) {
    55                         continue;
    56                     }
    57                     name = n;
    58                 }
    59                 if (name == null) {
    60                     break;
    61                 }
    62                 InputStream is = loadClass(l, name);
    63                 if (is == null) {
    64                     throw new IOException("Can't find class " + name); 
    65                 }
    66                 try {
    67                     String ic = compile(is);
    68                     processed.add(name);
    69                     initCode.add(ic == null ? "" : ic);
    70                 } catch (RuntimeException ex) {
    71                     if (out instanceof CharSequence) {
    72                         CharSequence seq = (CharSequence)out;
    73                         int lastBlock = seq.length();
    74                         while (lastBlock-- > 0) {
    75                             if (seq.charAt(lastBlock) == '{') {
    76                                 break;
    77                             }
    78                         }
    79                         throw new IOException("Error while compiling " + name + "\n" 
    80                             + seq.subSequence(lastBlock + 1, seq.length()), ex
    81                         );
    82                     } else {
    83                         throw new IOException("Error while compiling " + name + "\n" 
    84                             + out, ex
    85                         );
    86                     }
    87                 }
    88             }
    89 
    90             for (String resource : scripts.toArray()) {
    91                 while (resource.startsWith("/")) {
    92                     resource = resource.substring(1);
    93                 }
    94                 InputStream emul = l.getResourceAsStream(resource);
    95                 if (emul == null) {
    96                     throw new IOException("Can't find " + resource);
    97                 }
    98                 readResource(emul, out);
    99             }
   100             scripts = new StringArray();
   101             
   102             StringArray toInit = StringArray.asList(references.toArray());
   103             toInit.reverse();
   104 
   105             for (String ic : toInit.toArray()) {
   106                 int indx = processed.indexOf(ic);
   107                 if (indx >= 0) {
   108                     out.append(initCode.toArray()[indx]).append("\n");
   109                     initCode.toArray()[indx] = "";
   110                 }
   111             }
   112 
   113         }
   114     }
   115     private static void readResource(InputStream emul, Appendable out) throws IOException {
   116         try {
   117             int state = 0;
   118             for (;;) {
   119                 int ch = emul.read();
   120                 if (ch == -1) {
   121                     break;
   122                 }
   123                 if (ch < 0 || ch > 255) {
   124                     throw new IOException("Invalid char in emulation " + ch);
   125                 }
   126                 switch (state) {
   127                     case 0: 
   128                         if (ch == '/') {
   129                             state = 1;
   130                         } else {
   131                             out.append((char)ch);
   132                         }
   133                         break;
   134                     case 1:
   135                         if (ch == '*') {
   136                             state = 2;
   137                         } else {
   138                             out.append('/').append((char)ch);
   139                             state = 0;
   140                         }
   141                         break;
   142                     case 2:
   143                         if (ch == '*') {
   144                             state = 3;
   145                         }
   146                         break;
   147                     case 3:
   148                         if (ch == '/') {
   149                             state = 0;
   150                         } else {
   151                             state = 2;
   152                         }
   153                         break;
   154                 }
   155             }
   156         } finally {
   157             emul.close();
   158         }
   159     }
   160 
   161     private static InputStream loadClass(ClassLoader l, String name) throws IOException {
   162         Enumeration<URL> en = l.getResources(name + ".class");
   163         URL u = null;
   164         while (en.hasMoreElements()) {
   165             u = en.nextElement();
   166         }
   167         if (u == null) {
   168             throw new IOException("Can't find " + name);
   169         }
   170         if (u.toExternalForm().contains("rt.jar!")) {
   171             throw new IOException("No emulation for " + u);
   172         }
   173         return u.openStream();
   174     }
   175 
   176     static String toString(String name) throws IOException {
   177         StringBuilder sb = new StringBuilder();
   178         compile(sb, name);
   179         return sb.toString().toString();
   180     }
   181 
   182     private StringArray scripts = new StringArray();
   183     private StringArray references = new StringArray();
   184     
   185     @Override
   186     protected boolean requireReference(String cn) {
   187         if (references.contains(cn)) {
   188             return false;
   189         }
   190         references.add(cn);
   191         return true;
   192     }
   193 
   194     @Override
   195     protected void requireScript(String resourcePath) {
   196         scripts.add(resourcePath);
   197     }
   198 }