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