vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 10 Dec 2012 12:03:22 +0100
changeset 297 a20721a10717
parent 250 42c2ceb1e160
child 274 81f6e7778135
permissions -rw-r--r--
Cache the compiled code per class, not globally
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.IOException;
jaroslav@29
    21
import java.io.InputStream;
jaroslav@93
    22
import java.net.URL;
jaroslav@93
    23
import java.util.Enumeration;
jaroslav@29
    24
jaroslav@29
    25
/** Generator of JavaScript from bytecode of classes on classpath of the VM.
jaroslav@29
    26
 *
jaroslav@29
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@29
    28
 */
jaroslav@272
    29
class GenJS extends ByteCodeToJavaScript {
jtulach@162
    30
    public GenJS(Appendable out) {
jtulach@162
    31
        super(out);
jtulach@162
    32
    }
jaroslav@29
    33
    
jaroslav@29
    34
    static void compile(Appendable out, String... names) throws IOException {
jtulach@162
    35
        compile(out, StringArray.asList(names));
jaroslav@29
    36
    }
jtulach@168
    37
    static void compile(ClassLoader l, Appendable out, String... names) throws IOException {
jtulach@168
    38
        compile(l, out, StringArray.asList(names));
jtulach@168
    39
    }
jtulach@162
    40
    static void compile(Appendable out, StringArray names) throws IOException {
jaroslav@109
    41
        compile(GenJS.class.getClassLoader(), out, names);
jaroslav@109
    42
    }
jtulach@162
    43
    static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
jaroslav@272
    44
        new GenJS(out).doCompile(l, names);
jaroslav@272
    45
    }
jaroslav@272
    46
    protected void doCompile(ClassLoader l, StringArray names) throws IOException {
jtulach@162
    47
        StringArray processed = new StringArray();
jtulach@162
    48
        StringArray initCode = new StringArray();
jtulach@162
    49
        for (String baseClass : names.toArray()) {
jaroslav@272
    50
            references.add(baseClass);
jaroslav@97
    51
            for (;;) {
jaroslav@97
    52
                String name = null;
jaroslav@272
    53
                for (String n : references.toArray()) {
jtulach@162
    54
                    if (processed.contains(n)) {
jaroslav@97
    55
                        continue;
jaroslav@97
    56
                    }
jaroslav@97
    57
                    name = n;
jaroslav@97
    58
                }
jaroslav@97
    59
                if (name == null) {
jaroslav@97
    60
                    break;
jaroslav@97
    61
                }
jaroslav@109
    62
                InputStream is = loadClass(l, name);
jaroslav@97
    63
                if (is == null) {
jaroslav@97
    64
                    throw new IOException("Can't find class " + name); 
jaroslav@97
    65
                }
jaroslav@97
    66
                try {
jaroslav@272
    67
                    String ic = compile(is);
jtulach@162
    68
                    processed.add(name);
jtulach@162
    69
                    initCode.add(ic == null ? "" : ic);
jaroslav@97
    70
                } catch (RuntimeException ex) {
jaroslav@97
    71
                    if (out instanceof CharSequence) {
jaroslav@97
    72
                        CharSequence seq = (CharSequence)out;
jaroslav@97
    73
                        int lastBlock = seq.length();
jaroslav@151
    74
                        while (lastBlock-- > 0) {
jaroslav@97
    75
                            if (seq.charAt(lastBlock) == '{') {
jaroslav@97
    76
                                break;
jaroslav@97
    77
                            }
jaroslav@29
    78
                        }
jaroslav@97
    79
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
    80
                            + seq.subSequence(lastBlock + 1, seq.length()), ex
jaroslav@97
    81
                        );
jaroslav@97
    82
                    } else {
jaroslav@97
    83
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
    84
                            + out, ex
jaroslav@97
    85
                        );
jaroslav@29
    86
                    }
jaroslav@97
    87
                }
jaroslav@29
    88
            }
jaroslav@97
    89
jaroslav@272
    90
            for (String resource : scripts.toArray()) {
jaroslav@250
    91
                while (resource.startsWith("/")) {
jaroslav@250
    92
                    resource = resource.substring(1);
jaroslav@250
    93
                }
jaroslav@250
    94
                InputStream emul = l.getResourceAsStream(resource);
jaroslav@250
    95
                if (emul == null) {
jaroslav@250
    96
                    throw new IOException("Can't find " + resource);
jaroslav@250
    97
                }
jaroslav@250
    98
                readResource(emul, out);
jaroslav@250
    99
            }
jaroslav@272
   100
            scripts = new StringArray();
jaroslav@250
   101
            
jaroslav@272
   102
            StringArray toInit = StringArray.asList(references.toArray());
jtulach@162
   103
            toInit.reverse();
jtulach@129
   104
jtulach@162
   105
            for (String ic : toInit.toArray()) {
jtulach@162
   106
                int indx = processed.indexOf(ic);
jtulach@162
   107
                if (indx >= 0) {
jtulach@162
   108
                    out.append(initCode.toArray()[indx]).append("\n");
jtulach@162
   109
                    initCode.toArray()[indx] = "";
jaroslav@91
   110
                }
jaroslav@91
   111
            }
jaroslav@133
   112
jaroslav@29
   113
        }
jaroslav@29
   114
    }
jaroslav@90
   115
    private static void readResource(InputStream emul, Appendable out) throws IOException {
jaroslav@90
   116
        try {
jaroslav@90
   117
            int state = 0;
jaroslav@90
   118
            for (;;) {
jaroslav@90
   119
                int ch = emul.read();
jaroslav@90
   120
                if (ch == -1) {
jaroslav@90
   121
                    break;
jaroslav@90
   122
                }
jaroslav@90
   123
                if (ch < 0 || ch > 255) {
jaroslav@90
   124
                    throw new IOException("Invalid char in emulation " + ch);
jaroslav@90
   125
                }
jaroslav@90
   126
                switch (state) {
jaroslav@90
   127
                    case 0: 
jaroslav@90
   128
                        if (ch == '/') {
jaroslav@90
   129
                            state = 1;
jaroslav@90
   130
                        } else {
jaroslav@90
   131
                            out.append((char)ch);
jaroslav@90
   132
                        }
jaroslav@90
   133
                        break;
jaroslav@90
   134
                    case 1:
jaroslav@90
   135
                        if (ch == '*') {
jaroslav@90
   136
                            state = 2;
jaroslav@90
   137
                        } else {
jaroslav@90
   138
                            out.append('/').append((char)ch);
jaroslav@90
   139
                            state = 0;
jaroslav@90
   140
                        }
jaroslav@90
   141
                        break;
jaroslav@90
   142
                    case 2:
jaroslav@90
   143
                        if (ch == '*') {
jaroslav@90
   144
                            state = 3;
jaroslav@90
   145
                        }
jaroslav@90
   146
                        break;
jaroslav@90
   147
                    case 3:
jaroslav@90
   148
                        if (ch == '/') {
jaroslav@90
   149
                            state = 0;
jaroslav@90
   150
                        } else {
jaroslav@90
   151
                            state = 2;
jaroslav@90
   152
                        }
jaroslav@90
   153
                        break;
jaroslav@90
   154
                }
jaroslav@90
   155
            }
jaroslav@90
   156
        } finally {
jaroslav@90
   157
            emul.close();
jaroslav@90
   158
        }
jaroslav@90
   159
    }
jaroslav@93
   160
jaroslav@109
   161
    private static InputStream loadClass(ClassLoader l, String name) throws IOException {
jaroslav@109
   162
        Enumeration<URL> en = l.getResources(name + ".class");
jaroslav@93
   163
        URL u = null;
jaroslav@93
   164
        while (en.hasMoreElements()) {
jaroslav@93
   165
            u = en.nextElement();
jaroslav@93
   166
        }
jaroslav@93
   167
        if (u == null) {
jaroslav@93
   168
            throw new IOException("Can't find " + name);
jaroslav@93
   169
        }
jtulach@162
   170
        if (u.toExternalForm().contains("rt.jar!")) {
jtulach@162
   171
            throw new IOException("No emulation for " + u);
jtulach@162
   172
        }
jaroslav@93
   173
        return u.openStream();
jaroslav@93
   174
    }
jaroslav@136
   175
jaroslav@136
   176
    static String toString(String name) throws IOException {
jaroslav@136
   177
        StringBuilder sb = new StringBuilder();
jaroslav@136
   178
        compile(sb, name);
jaroslav@136
   179
        return sb.toString().toString();
jaroslav@136
   180
    }
jtulach@162
   181
jtulach@162
   182
    private StringArray scripts = new StringArray();
jtulach@162
   183
    private StringArray references = new StringArray();
jtulach@162
   184
    
jtulach@162
   185
    @Override
jtulach@162
   186
    protected boolean requireReference(String cn) {
jtulach@162
   187
        if (references.contains(cn)) {
jtulach@162
   188
            return false;
jtulach@162
   189
        }
jtulach@162
   190
        references.add(cn);
jtulach@162
   191
        return true;
jtulach@162
   192
    }
jtulach@162
   193
jtulach@162
   194
    @Override
jtulach@162
   195
    protected void requireScript(String resourcePath) {
jtulach@162
   196
        scripts.add(resourcePath);
jtulach@162
   197
    }
jaroslav@29
   198
}