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