vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 12:42:00 +0200
changeset 106 346633cd13d6
parent 104 1376481f15e7
child 109 b19d12a9e6d5
permissions -rw-r--r--
Fixing license in all files
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@29
    18
package org.apidesign.vm4brwsr;
jaroslav@29
    19
jaroslav@29
    20
import java.io.BufferedWriter;
jaroslav@29
    21
import java.io.FileWriter;
jaroslav@29
    22
import java.io.IOException;
jaroslav@29
    23
import java.io.InputStream;
jaroslav@29
    24
import java.io.Writer;
jaroslav@93
    25
import java.net.URL;
jaroslav@29
    26
import java.util.Arrays;
jaroslav@97
    27
import java.util.Collections;
jaroslav@93
    28
import java.util.Enumeration;
jaroslav@97
    29
import java.util.HashMap;
jaroslav@97
    30
import java.util.Iterator;
jaroslav@29
    31
import java.util.LinkedList;
jaroslav@29
    32
import java.util.List;
jaroslav@97
    33
import java.util.Map;
jaroslav@29
    34
jaroslav@29
    35
/** Generator of JavaScript from bytecode of classes on classpath of the VM.
jaroslav@29
    36
 *
jaroslav@29
    37
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@29
    38
 */
jaroslav@29
    39
final class GenJS {
jaroslav@29
    40
    private GenJS() {}
jaroslav@29
    41
    
jaroslav@29
    42
    public static void main(String... args) throws IOException {
jaroslav@29
    43
        if (args.length < 2) {
jaroslav@29
    44
            System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
jaroslav@29
    45
            return;
jaroslav@29
    46
        }
jaroslav@29
    47
        
jaroslav@29
    48
        Writer w = new BufferedWriter(new FileWriter(args[0]));
jaroslav@29
    49
        List<String> classes = Arrays.asList(args).subList(1, args.length);
jaroslav@29
    50
        compile(w, classes);
jaroslav@29
    51
        w.close();
jaroslav@29
    52
    }
jaroslav@29
    53
    
jaroslav@29
    54
    static void compile(Appendable out, String... names) throws IOException {
jaroslav@29
    55
        compile(out, Arrays.asList(names));
jaroslav@29
    56
    }
jaroslav@29
    57
    static void compile(Appendable out, List<String> names) throws IOException {
jaroslav@97
    58
        for (String baseClass : names) {
jaroslav@97
    59
            Map<String,String> processed = new HashMap<String, String>();
jaroslav@97
    60
            LinkedList<String> toProcess = new LinkedList<String>();
jaroslav@97
    61
            toProcess.add(baseClass);
jaroslav@97
    62
            for (;;) {
jaroslav@97
    63
                String name = null;
jaroslav@97
    64
                Iterator<String> it = toProcess.iterator();
jaroslav@97
    65
                while (it.hasNext() && name == null) {
jaroslav@97
    66
                    String n = it.next();
jaroslav@97
    67
                    if (processed.get(n) != null) {
jaroslav@97
    68
                        continue;
jaroslav@97
    69
                    }
jaroslav@97
    70
                    name = n;
jaroslav@97
    71
                }
jaroslav@97
    72
                if (name == null) {
jaroslav@97
    73
                    break;
jaroslav@97
    74
                }
jaroslav@97
    75
                if (name.startsWith("java/")
jaroslav@97
    76
                    && !name.equals("java/lang/Object")
jaroslav@97
    77
                    && !name.equals("java/lang/Class")
jaroslav@104
    78
                    && !name.equals("java/lang/Math")
jaroslav@97
    79
                    && !name.equals("java/lang/Number")
jaroslav@97
    80
                    && !name.equals("java/lang/Integer")
jaroslav@104
    81
                    && !name.equals("java/lang/Float")
jaroslav@104
    82
                    && !name.equals("java/lang/Double")
jaroslav@97
    83
                    && !name.equals("java/lang/Throwable")
jaroslav@97
    84
                    && !name.equals("java/lang/Exception")
jaroslav@97
    85
                    && !name.equals("java/lang/RuntimeException")
jaroslav@97
    86
                    && !name.equals("java/lang/UnsupportedOperationException")
jaroslav@97
    87
                    && !name.equals("java/lang/String")
jaroslav@97
    88
                    && !name.equals("java/lang/String$CaseInsensitiveComparator")
jaroslav@97
    89
                    && !name.equals("java/lang/StringBuilder")
jaroslav@97
    90
                    && !name.equals("java/lang/AbstractStringBuilder")
jaroslav@97
    91
                ) {
jaroslav@97
    92
                    processed.put(name, "");
jaroslav@97
    93
                    continue;
jaroslav@97
    94
                }            
jaroslav@97
    95
                InputStream is = loadClass(name);
jaroslav@97
    96
                if (is == null) {
jaroslav@97
    97
                    throw new IOException("Can't find class " + name); 
jaroslav@97
    98
                }
jaroslav@97
    99
                LinkedList<String> scripts = new LinkedList<String>();
jaroslav@97
   100
                try {
jaroslav@97
   101
                    String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
jaroslav@97
   102
                    processed.put(name, initCode == null ? "" : initCode);
jaroslav@97
   103
                } catch (RuntimeException ex) {
jaroslav@97
   104
                    if (out instanceof CharSequence) {
jaroslav@97
   105
                        CharSequence seq = (CharSequence)out;
jaroslav@97
   106
                        int lastBlock = seq.length();
jaroslav@97
   107
                        while (lastBlock-- >= 0) {
jaroslav@97
   108
                            if (seq.charAt(lastBlock) == '{') {
jaroslav@97
   109
                                break;
jaroslav@97
   110
                            }
jaroslav@29
   111
                        }
jaroslav@97
   112
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
   113
                            + seq.subSequence(lastBlock + 1, seq.length()), ex
jaroslav@97
   114
                        );
jaroslav@97
   115
                    } else {
jaroslav@97
   116
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
   117
                            + out, ex
jaroslav@97
   118
                        );
jaroslav@29
   119
                    }
jaroslav@97
   120
                }
jaroslav@97
   121
                for (String resource : scripts) {
jaroslav@97
   122
                    InputStream emul = GenJS.class.getResourceAsStream(resource);
jaroslav@97
   123
                    if (emul == null) {
jaroslav@97
   124
                        throw new IOException("Can't find " + resource);
jaroslav@97
   125
                    }
jaroslav@97
   126
                    readResource(emul, out);
jaroslav@29
   127
                }
jaroslav@29
   128
            }
jaroslav@97
   129
            
jaroslav@97
   130
            Collections.reverse(toProcess);
jaroslav@97
   131
jaroslav@97
   132
            for (String clazz : toProcess) {
jaroslav@97
   133
                String initCode = processed.remove(clazz);
jaroslav@97
   134
                if (initCode != null) {
jaroslav@97
   135
                    out.append(initCode).append("\n");
jaroslav@91
   136
                }
jaroslav@91
   137
            }
jaroslav@29
   138
        }
jaroslav@29
   139
    }
jaroslav@90
   140
    private static void readResource(InputStream emul, Appendable out) throws IOException {
jaroslav@90
   141
        try {
jaroslav@90
   142
            int state = 0;
jaroslav@90
   143
            for (;;) {
jaroslav@90
   144
                int ch = emul.read();
jaroslav@90
   145
                if (ch == -1) {
jaroslav@90
   146
                    break;
jaroslav@90
   147
                }
jaroslav@90
   148
                if (ch < 0 || ch > 255) {
jaroslav@90
   149
                    throw new IOException("Invalid char in emulation " + ch);
jaroslav@90
   150
                }
jaroslav@90
   151
                switch (state) {
jaroslav@90
   152
                    case 0: 
jaroslav@90
   153
                        if (ch == '/') {
jaroslav@90
   154
                            state = 1;
jaroslav@90
   155
                        } else {
jaroslav@90
   156
                            out.append((char)ch);
jaroslav@90
   157
                        }
jaroslav@90
   158
                        break;
jaroslav@90
   159
                    case 1:
jaroslav@90
   160
                        if (ch == '*') {
jaroslav@90
   161
                            state = 2;
jaroslav@90
   162
                        } else {
jaroslav@90
   163
                            out.append('/').append((char)ch);
jaroslav@90
   164
                            state = 0;
jaroslav@90
   165
                        }
jaroslav@90
   166
                        break;
jaroslav@90
   167
                    case 2:
jaroslav@90
   168
                        if (ch == '*') {
jaroslav@90
   169
                            state = 3;
jaroslav@90
   170
                        }
jaroslav@90
   171
                        break;
jaroslav@90
   172
                    case 3:
jaroslav@90
   173
                        if (ch == '/') {
jaroslav@90
   174
                            state = 0;
jaroslav@90
   175
                        } else {
jaroslav@90
   176
                            state = 2;
jaroslav@90
   177
                        }
jaroslav@90
   178
                        break;
jaroslav@90
   179
                }
jaroslav@90
   180
            }
jaroslav@90
   181
        } finally {
jaroslav@90
   182
            emul.close();
jaroslav@90
   183
        }
jaroslav@90
   184
    }
jaroslav@93
   185
jaroslav@93
   186
    private static InputStream loadClass(String name) throws IOException {
jaroslav@93
   187
        Enumeration<URL> en = ClassLoader.getSystemClassLoader().getResources(name + ".class");
jaroslav@93
   188
        URL u = null;
jaroslav@93
   189
        while (en.hasMoreElements()) {
jaroslav@93
   190
            u = en.nextElement();
jaroslav@93
   191
        }
jaroslav@93
   192
        if (u == null) {
jaroslav@93
   193
            throw new IOException("Can't find " + name);
jaroslav@93
   194
        }
jaroslav@93
   195
        return u.openStream();
jaroslav@93
   196
    }
jaroslav@29
   197
    
jaroslav@29
   198
}