vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 16 Nov 2012 07:36:32 +0100
branchjavap
changeset 162 70e7710a65dc
parent 151 40f95fe90cdc
child 168 1d4bf362c3a4
permissions -rw-r--r--
Eliminating references to java.util classes that have too huge transitive closure.
     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 final 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(Appendable out, StringArray names) throws IOException {
    38         compile(GenJS.class.getClassLoader(), out, names);
    39     }
    40     static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
    41         StringArray processed = new StringArray();
    42         StringArray initCode = new StringArray();
    43         for (String baseClass : names.toArray()) {
    44             GenJS js = new GenJS(out);
    45             js.references.add(baseClass);
    46             for (;;) {
    47                 String name = null;
    48                 for (String n : js.references.toArray()) {
    49                     if (processed.contains(n)) {
    50                         continue;
    51                     }
    52                     name = n;
    53                 }
    54                 if (name == null) {
    55                     break;
    56                 }
    57                 InputStream is = loadClass(l, name);
    58                 if (is == null) {
    59                     throw new IOException("Can't find class " + name); 
    60                 }
    61                 try {
    62                     String ic = js.compile(is);
    63                     processed.add(name);
    64                     initCode.add(ic == null ? "" : ic);
    65                 } catch (RuntimeException ex) {
    66                     if (out instanceof CharSequence) {
    67                         CharSequence seq = (CharSequence)out;
    68                         int lastBlock = seq.length();
    69                         while (lastBlock-- > 0) {
    70                             if (seq.charAt(lastBlock) == '{') {
    71                                 break;
    72                             }
    73                         }
    74                         throw new IOException("Error while compiling " + name + "\n" 
    75                             + seq.subSequence(lastBlock + 1, seq.length()), ex
    76                         );
    77                     } else {
    78                         throw new IOException("Error while compiling " + name + "\n" 
    79                             + out, ex
    80                         );
    81                     }
    82                 }
    83                 for (String resource : js.scripts.toArray()) {
    84                     while (resource.startsWith("/")) {
    85                         resource = resource.substring(1);
    86                     }
    87                     InputStream emul = l.getResourceAsStream(resource);
    88                     if (emul == null) {
    89                         throw new IOException("Can't find " + resource);
    90                     }
    91                     readResource(emul, out);
    92                 }
    93             }
    94 
    95             StringArray toInit = StringArray.asList(js.references.toArray());
    96             toInit.reverse();
    97 
    98             for (String ic : toInit.toArray()) {
    99                 int indx = processed.indexOf(ic);
   100                 if (indx >= 0) {
   101                     out.append(initCode.toArray()[indx]).append("\n");
   102                     initCode.toArray()[indx] = "";
   103                 }
   104             }
   105 
   106         }
   107     }
   108     private static void readResource(InputStream emul, Appendable out) throws IOException {
   109         try {
   110             int state = 0;
   111             for (;;) {
   112                 int ch = emul.read();
   113                 if (ch == -1) {
   114                     break;
   115                 }
   116                 if (ch < 0 || ch > 255) {
   117                     throw new IOException("Invalid char in emulation " + ch);
   118                 }
   119                 switch (state) {
   120                     case 0: 
   121                         if (ch == '/') {
   122                             state = 1;
   123                         } else {
   124                             out.append((char)ch);
   125                         }
   126                         break;
   127                     case 1:
   128                         if (ch == '*') {
   129                             state = 2;
   130                         } else {
   131                             out.append('/').append((char)ch);
   132                             state = 0;
   133                         }
   134                         break;
   135                     case 2:
   136                         if (ch == '*') {
   137                             state = 3;
   138                         }
   139                         break;
   140                     case 3:
   141                         if (ch == '/') {
   142                             state = 0;
   143                         } else {
   144                             state = 2;
   145                         }
   146                         break;
   147                 }
   148             }
   149         } finally {
   150             emul.close();
   151         }
   152     }
   153 
   154     private static InputStream loadClass(ClassLoader l, String name) throws IOException {
   155         Enumeration<URL> en = l.getResources(name + ".class");
   156         URL u = null;
   157         while (en.hasMoreElements()) {
   158             u = en.nextElement();
   159         }
   160         if (u == null) {
   161             throw new IOException("Can't find " + name);
   162         }
   163         if (u.toExternalForm().contains("rt.jar!")) {
   164             throw new IOException("No emulation for " + u);
   165         }
   166         return u.openStream();
   167     }
   168 
   169     static String toString(String name) throws IOException {
   170         StringBuilder sb = new StringBuilder();
   171         compile(sb, name);
   172         return sb.toString().toString();
   173     }
   174 
   175     private StringArray scripts = new StringArray();
   176     private StringArray references = new StringArray();
   177     
   178     @Override
   179     protected boolean requireReference(String cn) {
   180         if (references.contains(cn)) {
   181             return false;
   182         }
   183         references.add(cn);
   184         return true;
   185     }
   186 
   187     @Override
   188     protected void requireScript(String resourcePath) {
   189         scripts.add(resourcePath);
   190     }
   191 }