vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 31 Oct 2012 15:43:39 +0100
changeset 133 245d9215a97e
parent 129 15df78d24302
child 136 d48a0da4549c
permissions -rw-r--r--
Don't process class files multiple times
     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.BufferedWriter;
    21 import java.io.FileWriter;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.Writer;
    25 import java.net.URL;
    26 import java.util.ArrayList;
    27 import java.util.Arrays;
    28 import java.util.Collections;
    29 import java.util.Enumeration;
    30 import java.util.HashMap;
    31 import java.util.Iterator;
    32 import java.util.LinkedHashSet;
    33 import java.util.LinkedList;
    34 import java.util.List;
    35 import java.util.Map;
    36 
    37 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
    38  *
    39  * @author Jaroslav Tulach <jtulach@netbeans.org>
    40  */
    41 final class GenJS {
    42     private GenJS() {}
    43     
    44     public static void main(String... args) throws IOException {
    45         if (args.length < 2) {
    46             System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    47             return;
    48         }
    49         
    50         Writer w = new BufferedWriter(new FileWriter(args[0]));
    51         List<String> classes = Arrays.asList(args).subList(1, args.length);
    52         compile(w, classes);
    53         w.close();
    54     }
    55     
    56     static void compile(Appendable out, String... names) throws IOException {
    57         compile(out, Arrays.asList(names));
    58     }
    59     static void compile(Appendable out, List<String> names) throws IOException {
    60         compile(GenJS.class.getClassLoader(), out, names);
    61     }
    62     static void compile(ClassLoader l, Appendable out, List<String> names) throws IOException {
    63         final Map<String,String> processed = new HashMap<String, String>();
    64         for (String baseClass : names) {
    65             LinkedHashSet<String> toProcess = new LinkedHashSet<String>() {
    66                 @Override
    67                 public boolean add(String e) {
    68                     if (processed.containsKey(e)) {
    69                         return false;
    70                     }
    71                     return super.add(e);
    72                 }
    73             };
    74             toProcess.add(baseClass);
    75             for (;;) {
    76                 String name = null;
    77                 Iterator<String> it = toProcess.iterator();
    78                 while (it.hasNext() && name == null) {
    79                     String n = it.next();
    80                     if (processed.get(n) != null) {
    81                         continue;
    82                     }
    83                     name = n;
    84                 }
    85                 if (name == null) {
    86                     break;
    87                 }
    88                 if (name.startsWith("sun/")) {
    89                     processed.put(name, "");
    90                     continue;
    91                 }            
    92                 InputStream is = loadClass(l, name);
    93                 if (is == null) {
    94                     throw new IOException("Can't find class " + name); 
    95                 }
    96                 LinkedList<String> scripts = new LinkedList<String>();
    97                 try {
    98                     String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
    99                     processed.put(name, initCode == null ? "" : initCode);
   100                 } catch (RuntimeException ex) {
   101                     if (out instanceof CharSequence) {
   102                         CharSequence seq = (CharSequence)out;
   103                         int lastBlock = seq.length();
   104                         while (lastBlock-- >= 0) {
   105                             if (seq.charAt(lastBlock) == '{') {
   106                                 break;
   107                             }
   108                         }
   109                         throw new IOException("Error while compiling " + name + "\n" 
   110                             + seq.subSequence(lastBlock + 1, seq.length()), ex
   111                         );
   112                     } else {
   113                         throw new IOException("Error while compiling " + name + "\n" 
   114                             + out, ex
   115                         );
   116                     }
   117                 }
   118                 for (String resource : scripts) {
   119                     while (resource.startsWith("/")) {
   120                         resource = resource.substring(1);
   121                     }
   122                     InputStream emul = l.getResourceAsStream(resource);
   123                     if (emul == null) {
   124                         throw new IOException("Can't find " + resource);
   125                     }
   126                     readResource(emul, out);
   127                 }
   128             }
   129 
   130             List<String> toInit = new ArrayList<String>(toProcess);
   131             Collections.reverse(toInit);
   132 
   133             for (String clazz : toInit) {
   134                 String initCode = processed.get(clazz);
   135                 if (initCode != null && !initCode.isEmpty()) {
   136                     out.append(initCode).append("\n");
   137                     processed.put(clazz, "");
   138                 }
   139             }
   140 
   141         }
   142     }
   143     private static void readResource(InputStream emul, Appendable out) throws IOException {
   144         try {
   145             int state = 0;
   146             for (;;) {
   147                 int ch = emul.read();
   148                 if (ch == -1) {
   149                     break;
   150                 }
   151                 if (ch < 0 || ch > 255) {
   152                     throw new IOException("Invalid char in emulation " + ch);
   153                 }
   154                 switch (state) {
   155                     case 0: 
   156                         if (ch == '/') {
   157                             state = 1;
   158                         } else {
   159                             out.append((char)ch);
   160                         }
   161                         break;
   162                     case 1:
   163                         if (ch == '*') {
   164                             state = 2;
   165                         } else {
   166                             out.append('/').append((char)ch);
   167                             state = 0;
   168                         }
   169                         break;
   170                     case 2:
   171                         if (ch == '*') {
   172                             state = 3;
   173                         }
   174                         break;
   175                     case 3:
   176                         if (ch == '/') {
   177                             state = 0;
   178                         } else {
   179                             state = 2;
   180                         }
   181                         break;
   182                 }
   183             }
   184         } finally {
   185             emul.close();
   186         }
   187     }
   188 
   189     private static InputStream loadClass(ClassLoader l, String name) throws IOException {
   190         Enumeration<URL> en = l.getResources(name + ".class");
   191         URL u = null;
   192         while (en.hasMoreElements()) {
   193             u = en.nextElement();
   194         }
   195         if (u == null) {
   196             throw new IOException("Can't find " + name);
   197         }
   198         return u.openStream();
   199     }
   200     
   201 }