vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 17 Nov 2012 17:43:15 +0100
branchjavap
changeset 172 9eb74b221cff
parent 168 1d4bf362c3a4
child 219 f395bd3e477c
permissions -rw-r--r--
Pre-fill arrays with nulls
     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             }
   105 
   106             StringArray toInit = StringArray.asList(js.references.toArray());
   107             toInit.reverse();
   108 
   109             for (String ic : toInit.toArray()) {
   110                 int indx = processed.indexOf(ic);
   111                 if (indx >= 0) {
   112                     out.append(initCode.toArray()[indx]).append("\n");
   113                     initCode.toArray()[indx] = "";
   114                 }
   115             }
   116 
   117         }
   118     }
   119     private static void readResource(InputStream emul, Appendable out) throws IOException {
   120         try {
   121             int state = 0;
   122             for (;;) {
   123                 int ch = emul.read();
   124                 if (ch == -1) {
   125                     break;
   126                 }
   127                 if (ch < 0 || ch > 255) {
   128                     throw new IOException("Invalid char in emulation " + ch);
   129                 }
   130                 switch (state) {
   131                     case 0: 
   132                         if (ch == '/') {
   133                             state = 1;
   134                         } else {
   135                             out.append((char)ch);
   136                         }
   137                         break;
   138                     case 1:
   139                         if (ch == '*') {
   140                             state = 2;
   141                         } else {
   142                             out.append('/').append((char)ch);
   143                             state = 0;
   144                         }
   145                         break;
   146                     case 2:
   147                         if (ch == '*') {
   148                             state = 3;
   149                         }
   150                         break;
   151                     case 3:
   152                         if (ch == '/') {
   153                             state = 0;
   154                         } else {
   155                             state = 2;
   156                         }
   157                         break;
   158                 }
   159             }
   160         } finally {
   161             emul.close();
   162         }
   163     }
   164 
   165     private static InputStream loadClass(ClassLoader l, String name) throws IOException {
   166         Enumeration<URL> en = l.getResources(name + ".class");
   167         URL u = null;
   168         while (en.hasMoreElements()) {
   169             u = en.nextElement();
   170         }
   171         if (u == null) {
   172             throw new IOException("Can't find " + name);
   173         }
   174         if (u.toExternalForm().contains("rt.jar!")) {
   175             throw new IOException("No emulation for " + u);
   176         }
   177         return u.openStream();
   178     }
   179 
   180     static String toString(String name) throws IOException {
   181         StringBuilder sb = new StringBuilder();
   182         compile(sb, name);
   183         return sb.toString().toString();
   184     }
   185 
   186     private StringArray scripts = new StringArray();
   187     private StringArray references = new StringArray();
   188     
   189     @Override
   190     protected boolean requireReference(String cn) {
   191         if (references.contains(cn)) {
   192             return false;
   193         }
   194         references.add(cn);
   195         return true;
   196     }
   197 
   198     @Override
   199     protected void requireScript(String resourcePath) {
   200         scripts.add(resourcePath);
   201     }
   202 }