vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 11:55:56 +0200
changeset 104 1376481f15e7
parent 97 437df2a719e7
child 106 346633cd13d6
permissions -rw-r--r--
Concatenation of strings works
     1 package org.apidesign.vm4brwsr;
     2 
     3 import java.io.BufferedWriter;
     4 import java.io.FileWriter;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.Writer;
     8 import java.net.URL;
     9 import java.util.Arrays;
    10 import java.util.Collections;
    11 import java.util.Enumeration;
    12 import java.util.HashMap;
    13 import java.util.Iterator;
    14 import java.util.LinkedList;
    15 import java.util.List;
    16 import java.util.Map;
    17 
    18 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
    19  *
    20  * @author Jaroslav Tulach <jtulach@netbeans.org>
    21  */
    22 final class GenJS {
    23     private GenJS() {}
    24     
    25     public static void main(String... args) throws IOException {
    26         if (args.length < 2) {
    27             System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    28             return;
    29         }
    30         
    31         Writer w = new BufferedWriter(new FileWriter(args[0]));
    32         List<String> classes = Arrays.asList(args).subList(1, args.length);
    33         compile(w, classes);
    34         w.close();
    35     }
    36     
    37     static void compile(Appendable out, String... names) throws IOException {
    38         compile(out, Arrays.asList(names));
    39     }
    40     static void compile(Appendable out, List<String> names) throws IOException {
    41         for (String baseClass : names) {
    42             Map<String,String> processed = new HashMap<String, String>();
    43             LinkedList<String> toProcess = new LinkedList<String>();
    44             toProcess.add(baseClass);
    45             for (;;) {
    46                 String name = null;
    47                 Iterator<String> it = toProcess.iterator();
    48                 while (it.hasNext() && name == null) {
    49                     String n = it.next();
    50                     if (processed.get(n) != null) {
    51                         continue;
    52                     }
    53                     name = n;
    54                 }
    55                 if (name == null) {
    56                     break;
    57                 }
    58                 if (name.startsWith("java/")
    59                     && !name.equals("java/lang/Object")
    60                     && !name.equals("java/lang/Class")
    61                     && !name.equals("java/lang/Math")
    62                     && !name.equals("java/lang/Number")
    63                     && !name.equals("java/lang/Integer")
    64                     && !name.equals("java/lang/Float")
    65                     && !name.equals("java/lang/Double")
    66                     && !name.equals("java/lang/Throwable")
    67                     && !name.equals("java/lang/Exception")
    68                     && !name.equals("java/lang/RuntimeException")
    69                     && !name.equals("java/lang/UnsupportedOperationException")
    70                     && !name.equals("java/lang/String")
    71                     && !name.equals("java/lang/String$CaseInsensitiveComparator")
    72                     && !name.equals("java/lang/StringBuilder")
    73                     && !name.equals("java/lang/AbstractStringBuilder")
    74                 ) {
    75                     processed.put(name, "");
    76                     continue;
    77                 }            
    78                 InputStream is = loadClass(name);
    79                 if (is == null) {
    80                     throw new IOException("Can't find class " + name); 
    81                 }
    82                 LinkedList<String> scripts = new LinkedList<String>();
    83                 try {
    84                     String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
    85                     processed.put(name, initCode == null ? "" : initCode);
    86                 } catch (RuntimeException ex) {
    87                     if (out instanceof CharSequence) {
    88                         CharSequence seq = (CharSequence)out;
    89                         int lastBlock = seq.length();
    90                         while (lastBlock-- >= 0) {
    91                             if (seq.charAt(lastBlock) == '{') {
    92                                 break;
    93                             }
    94                         }
    95                         throw new IOException("Error while compiling " + name + "\n" 
    96                             + seq.subSequence(lastBlock + 1, seq.length()), ex
    97                         );
    98                     } else {
    99                         throw new IOException("Error while compiling " + name + "\n" 
   100                             + out, ex
   101                         );
   102                     }
   103                 }
   104                 for (String resource : scripts) {
   105                     InputStream emul = GenJS.class.getResourceAsStream(resource);
   106                     if (emul == null) {
   107                         throw new IOException("Can't find " + resource);
   108                     }
   109                     readResource(emul, out);
   110                 }
   111             }
   112             
   113             Collections.reverse(toProcess);
   114 
   115             for (String clazz : toProcess) {
   116                 String initCode = processed.remove(clazz);
   117                 if (initCode != null) {
   118                     out.append(initCode).append("\n");
   119                 }
   120             }
   121         }
   122     }
   123     private static void readResource(InputStream emul, Appendable out) throws IOException {
   124         try {
   125             int state = 0;
   126             for (;;) {
   127                 int ch = emul.read();
   128                 if (ch == -1) {
   129                     break;
   130                 }
   131                 if (ch < 0 || ch > 255) {
   132                     throw new IOException("Invalid char in emulation " + ch);
   133                 }
   134                 switch (state) {
   135                     case 0: 
   136                         if (ch == '/') {
   137                             state = 1;
   138                         } else {
   139                             out.append((char)ch);
   140                         }
   141                         break;
   142                     case 1:
   143                         if (ch == '*') {
   144                             state = 2;
   145                         } else {
   146                             out.append('/').append((char)ch);
   147                             state = 0;
   148                         }
   149                         break;
   150                     case 2:
   151                         if (ch == '*') {
   152                             state = 3;
   153                         }
   154                         break;
   155                     case 3:
   156                         if (ch == '/') {
   157                             state = 0;
   158                         } else {
   159                             state = 2;
   160                         }
   161                         break;
   162                 }
   163             }
   164         } finally {
   165             emul.close();
   166         }
   167     }
   168 
   169     private static InputStream loadClass(String name) throws IOException {
   170         Enumeration<URL> en = ClassLoader.getSystemClassLoader().getResources(name + ".class");
   171         URL u = null;
   172         while (en.hasMoreElements()) {
   173             u = en.nextElement();
   174         }
   175         if (u == null) {
   176             throw new IOException("Can't find " + name);
   177         }
   178         return u.openStream();
   179     }
   180     
   181 }