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