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