vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 06 Dec 2012 22:02:10 +0100
branchlazyvm
changeset 276 aeb9fe11cd60
parent 274 81f6e7778135
child 277 e4b9eee9be83
permissions -rw-r--r--
The loadClass method accept class name with dots
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 {
jaroslav@274
    47
        out.append("(function VM(global) {");
jaroslav@274
    48
        out.append("\n  var vm = {};");
jtulach@162
    49
        StringArray processed = new StringArray();
jtulach@162
    50
        StringArray initCode = new StringArray();
jtulach@162
    51
        for (String baseClass : names.toArray()) {
jaroslav@272
    52
            references.add(baseClass);
jaroslav@97
    53
            for (;;) {
jaroslav@97
    54
                String name = null;
jaroslav@272
    55
                for (String n : references.toArray()) {
jtulach@162
    56
                    if (processed.contains(n)) {
jaroslav@97
    57
                        continue;
jaroslav@97
    58
                    }
jaroslav@97
    59
                    name = n;
jaroslav@97
    60
                }
jaroslav@97
    61
                if (name == null) {
jaroslav@97
    62
                    break;
jaroslav@97
    63
                }
jaroslav@109
    64
                InputStream is = loadClass(l, name);
jaroslav@97
    65
                if (is == null) {
jaroslav@97
    66
                    throw new IOException("Can't find class " + name); 
jaroslav@97
    67
                }
jaroslav@97
    68
                try {
jaroslav@272
    69
                    String ic = compile(is);
jtulach@162
    70
                    processed.add(name);
jtulach@162
    71
                    initCode.add(ic == null ? "" : ic);
jaroslav@97
    72
                } catch (RuntimeException ex) {
jaroslav@97
    73
                    if (out instanceof CharSequence) {
jaroslav@97
    74
                        CharSequence seq = (CharSequence)out;
jaroslav@97
    75
                        int lastBlock = seq.length();
jaroslav@151
    76
                        while (lastBlock-- > 0) {
jaroslav@97
    77
                            if (seq.charAt(lastBlock) == '{') {
jaroslav@97
    78
                                break;
jaroslav@97
    79
                            }
jaroslav@29
    80
                        }
jaroslav@97
    81
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
    82
                            + seq.subSequence(lastBlock + 1, seq.length()), ex
jaroslav@97
    83
                        );
jaroslav@97
    84
                    } else {
jaroslav@97
    85
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
    86
                            + out, ex
jaroslav@97
    87
                        );
jaroslav@29
    88
                    }
jaroslav@97
    89
                }
jaroslav@29
    90
            }
jaroslav@97
    91
jaroslav@272
    92
            for (String resource : scripts.toArray()) {
jaroslav@250
    93
                while (resource.startsWith("/")) {
jaroslav@250
    94
                    resource = resource.substring(1);
jaroslav@250
    95
                }
jaroslav@250
    96
                InputStream emul = l.getResourceAsStream(resource);
jaroslav@250
    97
                if (emul == null) {
jaroslav@250
    98
                    throw new IOException("Can't find " + resource);
jaroslav@250
    99
                }
jaroslav@250
   100
                readResource(emul, out);
jaroslav@250
   101
            }
jaroslav@272
   102
            scripts = new StringArray();
jaroslav@250
   103
            
jaroslav@272
   104
            StringArray toInit = StringArray.asList(references.toArray());
jtulach@162
   105
            toInit.reverse();
jtulach@129
   106
jtulach@162
   107
            for (String ic : toInit.toArray()) {
jtulach@162
   108
                int indx = processed.indexOf(ic);
jtulach@162
   109
                if (indx >= 0) {
jtulach@162
   110
                    out.append(initCode.toArray()[indx]).append("\n");
jtulach@162
   111
                    initCode.toArray()[indx] = "";
jaroslav@91
   112
                }
jaroslav@91
   113
            }
jaroslav@29
   114
        }
jaroslav@274
   115
        out.append(
jaroslav@274
   116
              "  global.bck2brwsr = function() { return {\n"
jaroslav@276
   117
            + "    loadClass : function(name) { return vm[name.replace__Ljava_lang_String_2CC(name, '.','_')](true); }\n"
jaroslav@274
   118
            + "  };\n};\n");
jaroslav@274
   119
        out.append("}(this));");
jaroslav@29
   120
    }
jaroslav@90
   121
    private static void readResource(InputStream emul, Appendable out) throws IOException {
jaroslav@90
   122
        try {
jaroslav@90
   123
            int state = 0;
jaroslav@90
   124
            for (;;) {
jaroslav@90
   125
                int ch = emul.read();
jaroslav@90
   126
                if (ch == -1) {
jaroslav@90
   127
                    break;
jaroslav@90
   128
                }
jaroslav@90
   129
                if (ch < 0 || ch > 255) {
jaroslav@90
   130
                    throw new IOException("Invalid char in emulation " + ch);
jaroslav@90
   131
                }
jaroslav@90
   132
                switch (state) {
jaroslav@90
   133
                    case 0: 
jaroslav@90
   134
                        if (ch == '/') {
jaroslav@90
   135
                            state = 1;
jaroslav@90
   136
                        } else {
jaroslav@90
   137
                            out.append((char)ch);
jaroslav@90
   138
                        }
jaroslav@90
   139
                        break;
jaroslav@90
   140
                    case 1:
jaroslav@90
   141
                        if (ch == '*') {
jaroslav@90
   142
                            state = 2;
jaroslav@90
   143
                        } else {
jaroslav@90
   144
                            out.append('/').append((char)ch);
jaroslav@90
   145
                            state = 0;
jaroslav@90
   146
                        }
jaroslav@90
   147
                        break;
jaroslav@90
   148
                    case 2:
jaroslav@90
   149
                        if (ch == '*') {
jaroslav@90
   150
                            state = 3;
jaroslav@90
   151
                        }
jaroslav@90
   152
                        break;
jaroslav@90
   153
                    case 3:
jaroslav@90
   154
                        if (ch == '/') {
jaroslav@90
   155
                            state = 0;
jaroslav@90
   156
                        } else {
jaroslav@90
   157
                            state = 2;
jaroslav@90
   158
                        }
jaroslav@90
   159
                        break;
jaroslav@90
   160
                }
jaroslav@90
   161
            }
jaroslav@90
   162
        } finally {
jaroslav@90
   163
            emul.close();
jaroslav@90
   164
        }
jaroslav@90
   165
    }
jaroslav@93
   166
jaroslav@109
   167
    private static InputStream loadClass(ClassLoader l, String name) throws IOException {
jaroslav@109
   168
        Enumeration<URL> en = l.getResources(name + ".class");
jaroslav@93
   169
        URL u = null;
jaroslav@93
   170
        while (en.hasMoreElements()) {
jaroslav@93
   171
            u = en.nextElement();
jaroslav@93
   172
        }
jaroslav@93
   173
        if (u == null) {
jaroslav@93
   174
            throw new IOException("Can't find " + name);
jaroslav@93
   175
        }
jtulach@162
   176
        if (u.toExternalForm().contains("rt.jar!")) {
jtulach@162
   177
            throw new IOException("No emulation for " + u);
jtulach@162
   178
        }
jaroslav@93
   179
        return u.openStream();
jaroslav@93
   180
    }
jaroslav@136
   181
jaroslav@136
   182
    static String toString(String name) throws IOException {
jaroslav@136
   183
        StringBuilder sb = new StringBuilder();
jaroslav@136
   184
        compile(sb, name);
jaroslav@136
   185
        return sb.toString().toString();
jaroslav@136
   186
    }
jtulach@162
   187
jtulach@162
   188
    private StringArray scripts = new StringArray();
jtulach@162
   189
    private StringArray references = new StringArray();
jtulach@162
   190
    
jtulach@162
   191
    @Override
jtulach@162
   192
    protected boolean requireReference(String cn) {
jtulach@162
   193
        if (references.contains(cn)) {
jtulach@162
   194
            return false;
jtulach@162
   195
        }
jtulach@162
   196
        references.add(cn);
jtulach@162
   197
        return true;
jtulach@162
   198
    }
jtulach@162
   199
jtulach@162
   200
    @Override
jtulach@162
   201
    protected void requireScript(String resourcePath) {
jtulach@162
   202
        scripts.add(resourcePath);
jtulach@162
   203
    }
jaroslav@274
   204
jaroslav@274
   205
    @Override
jaroslav@274
   206
    String assignClass(String className) {
jaroslav@274
   207
        return "vm." + className + " = ";
jaroslav@274
   208
    }
jaroslav@274
   209
    
jaroslav@274
   210
    @Override
jaroslav@274
   211
    String accessClass(String className) {
jaroslav@274
   212
        return "vm." + className;
jaroslav@274
   213
    }
jaroslav@29
   214
}