vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 30 Oct 2012 22:35:32 +0100
changeset 129 15df78d24302
parent 114 a0505844750a
child 133 245d9215a97e
permissions -rw-r--r--
Print the information about "needs" only once per each type
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;
jtulach@129
    26
import java.util.ArrayList;
jaroslav@29
    27
import java.util.Arrays;
jaroslav@97
    28
import java.util.Collections;
jaroslav@93
    29
import java.util.Enumeration;
jaroslav@97
    30
import java.util.HashMap;
jaroslav@97
    31
import java.util.Iterator;
jtulach@129
    32
import java.util.LinkedHashSet;
jaroslav@29
    33
import java.util.LinkedList;
jaroslav@29
    34
import java.util.List;
jaroslav@97
    35
import java.util.Map;
jaroslav@29
    36
jaroslav@29
    37
/** Generator of JavaScript from bytecode of classes on classpath of the VM.
jaroslav@29
    38
 *
jaroslav@29
    39
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@29
    40
 */
jaroslav@29
    41
final class GenJS {
jaroslav@29
    42
    private GenJS() {}
jaroslav@29
    43
    
jaroslav@29
    44
    public static void main(String... args) throws IOException {
jaroslav@29
    45
        if (args.length < 2) {
jaroslav@29
    46
            System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
jaroslav@29
    47
            return;
jaroslav@29
    48
        }
jaroslav@29
    49
        
jaroslav@29
    50
        Writer w = new BufferedWriter(new FileWriter(args[0]));
jaroslav@29
    51
        List<String> classes = Arrays.asList(args).subList(1, args.length);
jaroslav@29
    52
        compile(w, classes);
jaroslav@29
    53
        w.close();
jaroslav@29
    54
    }
jaroslav@29
    55
    
jaroslav@29
    56
    static void compile(Appendable out, String... names) throws IOException {
jaroslav@29
    57
        compile(out, Arrays.asList(names));
jaroslav@29
    58
    }
jaroslav@29
    59
    static void compile(Appendable out, List<String> names) throws IOException {
jaroslav@109
    60
        compile(GenJS.class.getClassLoader(), out, names);
jaroslav@109
    61
    }
jaroslav@109
    62
    static void compile(ClassLoader l, Appendable out, List<String> names) throws IOException {
jaroslav@97
    63
        for (String baseClass : names) {
jtulach@129
    64
            final Map<String,String> processed = new HashMap<String, String>();
jtulach@129
    65
            LinkedHashSet<String> toProcess = new LinkedHashSet<String>() {
jtulach@129
    66
                @Override
jtulach@129
    67
                public boolean add(String e) {
jtulach@129
    68
                    if (processed.containsKey(e)) {
jtulach@129
    69
                        return false;
jtulach@129
    70
                    }
jtulach@129
    71
                    return super.add(e);
jtulach@129
    72
                }
jtulach@129
    73
            };
jaroslav@97
    74
            toProcess.add(baseClass);
jaroslav@97
    75
            for (;;) {
jaroslav@97
    76
                String name = null;
jaroslav@97
    77
                Iterator<String> it = toProcess.iterator();
jaroslav@97
    78
                while (it.hasNext() && name == null) {
jaroslav@97
    79
                    String n = it.next();
jaroslav@97
    80
                    if (processed.get(n) != null) {
jaroslav@97
    81
                        continue;
jaroslav@97
    82
                    }
jaroslav@97
    83
                    name = n;
jaroslav@97
    84
                }
jaroslav@97
    85
                if (name == null) {
jaroslav@97
    86
                    break;
jaroslav@97
    87
                }
jtulach@129
    88
                if (name.startsWith("sun/")) {
jaroslav@97
    89
                    processed.put(name, "");
jaroslav@97
    90
                    continue;
jaroslav@97
    91
                }            
jaroslav@109
    92
                InputStream is = loadClass(l, name);
jaroslav@97
    93
                if (is == null) {
jaroslav@97
    94
                    throw new IOException("Can't find class " + name); 
jaroslav@97
    95
                }
jaroslav@97
    96
                LinkedList<String> scripts = new LinkedList<String>();
jaroslav@97
    97
                try {
jaroslav@97
    98
                    String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
jaroslav@97
    99
                    processed.put(name, initCode == null ? "" : initCode);
jaroslav@97
   100
                } catch (RuntimeException ex) {
jaroslav@97
   101
                    if (out instanceof CharSequence) {
jaroslav@97
   102
                        CharSequence seq = (CharSequence)out;
jaroslav@97
   103
                        int lastBlock = seq.length();
jaroslav@97
   104
                        while (lastBlock-- >= 0) {
jaroslav@97
   105
                            if (seq.charAt(lastBlock) == '{') {
jaroslav@97
   106
                                break;
jaroslav@97
   107
                            }
jaroslav@29
   108
                        }
jaroslav@97
   109
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
   110
                            + seq.subSequence(lastBlock + 1, seq.length()), ex
jaroslav@97
   111
                        );
jaroslav@97
   112
                    } else {
jaroslav@97
   113
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
   114
                            + out, ex
jaroslav@97
   115
                        );
jaroslav@29
   116
                    }
jaroslav@97
   117
                }
jaroslav@97
   118
                for (String resource : scripts) {
jaroslav@109
   119
                    while (resource.startsWith("/")) {
jaroslav@109
   120
                        resource = resource.substring(1);
jaroslav@109
   121
                    }
jaroslav@109
   122
                    InputStream emul = l.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
jtulach@129
   130
            List<String> toInit = new ArrayList<String>(toProcess);
jtulach@129
   131
            Collections.reverse(toInit);
jtulach@129
   132
jtulach@129
   133
            for (String clazz : toInit) {
jaroslav@97
   134
                String initCode = processed.remove(clazz);
jaroslav@97
   135
                if (initCode != null) {
jaroslav@97
   136
                    out.append(initCode).append("\n");
jaroslav@91
   137
                }
jaroslav@91
   138
            }
jaroslav@29
   139
        }
jaroslav@29
   140
    }
jaroslav@90
   141
    private static void readResource(InputStream emul, Appendable out) throws IOException {
jaroslav@90
   142
        try {
jaroslav@90
   143
            int state = 0;
jaroslav@90
   144
            for (;;) {
jaroslav@90
   145
                int ch = emul.read();
jaroslav@90
   146
                if (ch == -1) {
jaroslav@90
   147
                    break;
jaroslav@90
   148
                }
jaroslav@90
   149
                if (ch < 0 || ch > 255) {
jaroslav@90
   150
                    throw new IOException("Invalid char in emulation " + ch);
jaroslav@90
   151
                }
jaroslav@90
   152
                switch (state) {
jaroslav@90
   153
                    case 0: 
jaroslav@90
   154
                        if (ch == '/') {
jaroslav@90
   155
                            state = 1;
jaroslav@90
   156
                        } else {
jaroslav@90
   157
                            out.append((char)ch);
jaroslav@90
   158
                        }
jaroslav@90
   159
                        break;
jaroslav@90
   160
                    case 1:
jaroslav@90
   161
                        if (ch == '*') {
jaroslav@90
   162
                            state = 2;
jaroslav@90
   163
                        } else {
jaroslav@90
   164
                            out.append('/').append((char)ch);
jaroslav@90
   165
                            state = 0;
jaroslav@90
   166
                        }
jaroslav@90
   167
                        break;
jaroslav@90
   168
                    case 2:
jaroslav@90
   169
                        if (ch == '*') {
jaroslav@90
   170
                            state = 3;
jaroslav@90
   171
                        }
jaroslav@90
   172
                        break;
jaroslav@90
   173
                    case 3:
jaroslav@90
   174
                        if (ch == '/') {
jaroslav@90
   175
                            state = 0;
jaroslav@90
   176
                        } else {
jaroslav@90
   177
                            state = 2;
jaroslav@90
   178
                        }
jaroslav@90
   179
                        break;
jaroslav@90
   180
                }
jaroslav@90
   181
            }
jaroslav@90
   182
        } finally {
jaroslav@90
   183
            emul.close();
jaroslav@90
   184
        }
jaroslav@90
   185
    }
jaroslav@93
   186
jaroslav@109
   187
    private static InputStream loadClass(ClassLoader l, String name) throws IOException {
jaroslav@109
   188
        Enumeration<URL> en = l.getResources(name + ".class");
jaroslav@93
   189
        URL u = null;
jaroslav@93
   190
        while (en.hasMoreElements()) {
jaroslav@93
   191
            u = en.nextElement();
jaroslav@93
   192
        }
jaroslav@93
   193
        if (u == null) {
jaroslav@93
   194
            throw new IOException("Can't find " + name);
jaroslav@93
   195
        }
jaroslav@93
   196
        return u.openStream();
jaroslav@93
   197
    }
jaroslav@29
   198
    
jaroslav@29
   199
}